PREV UP next StackThreads/MP: version 0.77 User's Guide

4.4: Alloca is not supported (yet)

alloca is sometimes used to optimize memory allocation. It behaves like malloc, but automatically frees the allocated storage when the procedure that called it returns. It is normally implemented by allocating a block of memory on top of the stack, effectively extending the currently running frame.

I have to tell you that alloca is currently not supported with StackThreads/MP, as the standard implementation of alloca fundamentally conflicts with the execution scheme of StackThreads/MP. Please use above alternatives (malloc, SGC, or region-based allocator). Region-based allocator will be particularly useful when a single procedure calls alloca many times, as blocks allocated in a single procedure die together.

stgcc currently redefines alloca as the name of an undefined function, so that programs that use alloca fail to link. If you want to optimize allocation on thin ice, you can call the builtin alloca function (__builtin_alloca), as long as you know it is safe. Calling __builtin_alloca from a procedure is safe, as long as the procedure does not call any procedure compiled by stgcc. In other words, if you place all calls to __builtin_alloca to the procedure entry, you are safe.

We are planning to support alloca in future, although it will not allocate memory from stack but from heap. Like standard alloca, it automatically frees memory allocated by a procedure after the procedure exits. However, it will not be as efficient as __builtin_alloca; its overhead is likely to be similar to that of the region-based allocator.