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.