3 ms·
> From that point on, every "allocation" is equivalent to bumping a pointer in that arena. I'm not a C++ developer, so I have a hard time imagining how would t
by klibertp 6y ago
> From that point on, every "allocation" is equivalent to bumping a pointer in that arena.
I'm not a C++ developer, so I have a hard time imagining how would this be implemented in real code. I know how to allocate a blob of memory, but how do I redirect all the later allocations (that use `new` or that are hidden in std::string and other containers) to use parts of that blob? How do I know when the blob is going to overflow so that I need to allocate another one? Is it possible to give 5-10 lines example showing the basics of the technique?
- HexDecOctBin 6y agoSure, here is a snippet: https://pastebin.com/XGUeKdyk https://pastebin.com/XGUeKdyk Disclaimer: Quickly typed it on phone, so might make your computer explode.
- kaba0 6y agoIt’s not an either or thing, there is probably no need to allocate strings in the arena. Some cpp structures do allow for custom allocators, but you are more likely to have an arena instance and call some specific function on it and it will do the allocation for you. It usually operates on only a few types, not meant for arbitrary allocations.
- klibertp 6y ago> Some cpp structures do allow for custom allocators, Yeah, that's what I was thinking about, but my only knowledge of allocators comes from gcc error messages which include all the template parameters - I have no idea how they work or how to switch to another one :( I get the idea, though, it's basically what Erlang does for its processes - each one has a "private heap" just for it. If the process exits, the whole chunk of memory can be reclaimed and there's no need to run GC on it anymore (there's no sharing of memory between processes in Erlang, at all, so it's safe).
- clddd 6y agoYou either pass the allocator around to every stl container, and everything that uses an stl container. Or you override new/delete and have a sidechannel that defines what arena to allocate in.
- klibertp 6y agoThe second option seems much more convenient - I didn't know you can do that!