[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: alloca and GC

From: Jim Blandy
Subject: Re: alloca and GC
Date: 26 Jan 2001 16:25:03 -0500

Keisuke Nishida <address@hidden> writes:
> I am wondering if SCM objects stored in alloca'ed memories,
> as shown below, are garbage-collected or not.  A preliminary
> test showed they aren't:
>   scm_bits_t *sp = alloca (100 * sizeof (scm_bits_t));
>   sp[0] = SCM_UNPACK (scm_cons (SCM_BOOL_F, SCM_BOOL_F));
>   /* sp[0] won't be collected until the end of this function */
> Is this true?  Is it safe to store other data than SCM objects
> in the allocated memory?  If so, it's convenient for VM stacks.
> (BTW, how this works?)

Some implementations of alloca allocate space from the function's
stack frame itself.  Thus, when Guile's conservative GC scans the C
stack, it will find the SCM object references, and mark the objects.
GCC provides an alloca which behaves this way.

However, the original implementation of alloca, before GCC existed,
was kind of gross and non-portable; its implementation depended on
obscure details of the processor's calling conventions.  On the VAX,
it just bumped the stack pointer by an appropriate amount and returned
a pointer to the newly reserved stack space.  It "knew" that functions
always accessed their local variables via the frame pointer, so
tweaking the SP was harmless.  But any alloca that allocates space on
the stack must dependend on knowledge of the stack frame layout; this
is why alloca should be supported directly in the compiler, as GCC's
implementation is.

A lot of people felt that this made alloca gross, but Stallman felt
that it was a perfectly well-defined and very handy facility; GCC is
the only C compiler (as far as I know) that goes out of its way to
give alloca the support it needs.  Stallman encouraged the use of
alloca in GNU code.

Eventually, someone came up with a malloc-based implementation of
alloca, which takes the address of one of its own automatic variables
and uses that to guess which old blocks need to be freed.  It usually
works okay, I guess.

But to get back to your original question: since the malloc-based
alloca returns blocks allocated from the heap, which Guile's GC won't
scan, your example above won't work with the malloc-based alloca.
Since you can't tell which alloca you're using, I'd recommend against

reply via email to

[Prev in Thread] Current Thread [Next in Thread]