Tuesday, June 10, 2008

The alloca routine: Stack allocation

The alloca routine allows a C program to allocate memory on the stack instead of the heap. The advantage is that this memory is cleaned up once the allocating routine exits and does not need to explicitly do a ' free'. So probably you would not need to worry about memory leaks (but you are obviously restricting the life time of the object to the method boundary -- an escape analysis could use this method to convert all malloc calls to alloca calls for captured objects).

How could this be implemented ?

It is just a manipulation of the stack pointer (esp) using the size passed as an argument to the alloca routine (in a register). Since the base pointer (ebp) holds the base of the current function's stack frame, when the function returns, the stack is unwound using ebp and the de-allocation is automatically taken care of.

What could be the potential disadvantages ?

Size is obviously a restriction as the stack space grows linearly and upto a limit. There are other disadvantages mentioned in this Wine mailing list thread.

A long list of pros and cons are discussed in the gdb developer's mailing list. See here

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home