Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack allocation in function wrapper / alloca in function

Tags:

c++

gcc

alloca

I'm looking for a way to wrap stack allocations in abstract data types. For example, I'd like to have a vector which can work strictly via allocations on the stack. My biggest hurdle of course is that alloca works only within the current stack frame -- thus I don't see an easy way to wrap this into a function.

So far the only way I see to do this is by using macro-like functions which are guaranteed to be compiled into a given stack frame. I don't like this approach since it isn't as type friendly as one would hope, and requires more verbose naming than desired.

Is there anyway I can get a function to allocate on its caller stack? I understand this would normally destroy the immediately calling stack, thus likely the function would also have to be forced inline somehow. I'm not clear on what options I have, so I'm looking for some ideas, or pointers towards possible options.


Notes:

The ultimate goal is something like a std::vector which works strictly on the immediate functions stack. Obviously it would only be passed as a const object to callees, and its life ends with the function.

C approach is fine so long as it is better than my macro based approach. Though some support macros are also acceptable.

I understand this is a fairly specific optimization, and optimally I'd like to be able to (with a flag) turn it on/off (using just an normal std::vector for debugging). It would give a minor speed boost to significant parts of our code, but probably not enough to justify making it unreadable via too many odd constructs.

Answer: Is most likely that it isn't possible and that only the macro approach would work.

like image 666
edA-qa mort-ora-y Avatar asked Dec 27 '11 14:12

edA-qa mort-ora-y


People also ask

What is alloca function?

_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope).

What is Alloca C++?

The function alloca() is used to allocate memory for a stack frame. It's just an unconventional method of dynamically allocating memory.

How do you allocate a stack?

To delete a stack On the Stacks page in the CloudFormation console, select the stack that you want to delete. The stack must be currently running. In the stack details pane, choose Delete. Select Delete stack when prompted.

Can you allocate memory on the stack?

Stack memory allocation takes place on contiguous blocks of memory. As this allocation occurs in the function called stack, it is commonly referred to it as a stack memory allocation. The compiler calculates how much memory to allocate for each type of variable specified in the program.


2 Answers

You can't.
When a function returns, its stack is unwound, and the stack pointer goes back where it was before. It has to, if you don't want a real mess. All alloca does is move the stack pointer, so a function return undoes this allocation.
Macros would work, because they just add code to the same function. But it would be ugly, with no real hope of improvement.

like image 52
ugoren Avatar answered Sep 18 '22 13:09

ugoren


The main benefit of using stack allocation is probably the by-pass of the standard library malloc/new allocator. In this light, using the stack is not the only option.

One alternative to stack allocation is using a custom memory allocator based on mmap() system call. The memory allocated by mmap() can be used as storage for the custom allocator instead of the stack. To avoid calling mmap() often the memory region allocated by mmap() should be cached, in a global thread-specific variable, for example.

like image 38
Maxim Egorushkin Avatar answered Sep 20 '22 13:09

Maxim Egorushkin