AlexKindel
Would any language's built-in RAII mechanism give me a close equivalent of what I'm doing here? If I were to try to leverage C++ constructors and destructors I guess it would mean doing something with placement new.
To clarify the question more: suppose that, instead of having the function that does stack allocations take a stack_cursor, I needed to have it take a large Stack struct that
contains a stack_cursor. If I wanted a function's internal allocations to get wiped out without any extra work, as is happening now, I would have to pass the Stack by value, but if it was a large enough struct, I wouldn't want to. At that point I would again start considering always passing by pointer, saving the cursor position at the start of the function, and manually resetting it to the saved position before returning, even if I don't care about decommitting memory pages. Would any language's built-in RAII mechanisms help with
that?
I have in fact replaced stack_cursors with
small structs - a cursor together with a pointer to the end of the reserved block so the allocation function can check whether there is room for the allocation, and to enable splitting off the upper half of the free portion of the reserved block into a second stack in cases where I want to start a new thread. It's still small enough that switching to manual rewinding in order to be able to always pass by pointer probably wouldn't reduce overhead, much less enough to justify the extra code, but it might get bigger. I don't know yet. I suppose it would always be possible to split the stack_cursors back out of the structs and pass them separately, the structs always by pointer and the cursors by pointer or by value as necessary, but I wouldn't look forward to the verbosity of that.