On 6/29/05, Daniel Jacobowitz <[EMAIL PROTECTED]> wrote:
> On Wed, Jun 29, 2005 at 10:34:20AM -0700, Shaun Jackman wrote:
> > the statement expression macro? My best idea was to use alloca, but it
> > wouldn't look pretty. Can someone confirm that memory allocated with
> > alloca would last the lifetime of the function call, and not the
> > lifetime of the statement expression?
> 
> Yes, that's correct (and the only way to do this).

Great! I've rewritten the vfork macro to use alloca. Here's the big
catch, the pointer returned by alloca is stored on the stack, and it
gets trashed upon leaving the statement expression. Where can I store
the pointer returned by alloca? Please don't say allocate some memory
for the pointer using alloca. <evil grin> My best idea so far was to
make the pointer returned by alloca (struct context *c, see below)
static, so that each time the macro is invoked, a little static memory
is put aside for it. I think this would work, but it doesn't seem to
me to be the best solution.

Here's the revised vfork macro that uses alloca:

jmp_buf *vfork_jmp_buf;

struct context {
        jmp_buf *prev_jmp_buf;
        jmp_buf new_jmp_buf;
};

# define vfork() ({ \
        int setjmp_ret; \
        struct context *c = alloca(sizeof *c); \
        c->prev_jmp_buf = vfork_jmp_buf; \
        vfork_jmp_buf = &c->new_jmp_buf; \
        if( (setjmp_ret = setjmp(*vfork_jmp_buf)) != 0 ) \
                vfork_jmp_buf = c->prev_jmp_buf; \
        setjmp_ret; \
})

Cheers,
Shaun

Reply via email to