How can C++ 𝐢𝐦𝐩𝐫𝐨𝐯𝐞the typical C style 𝐚𝐫𝐞𝐧𝐚 𝐚𝐥𝐥𝐨𝐜𝐚𝐭𝐨𝐫𝐬 usage. 𝐒𝐡𝐚𝐫𝐞𝐝 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 comes to the rescue, especially with fragmented arenas, we can use the "𝐢𝐧𝐟𝐚𝐦𝐨𝐮𝐬" custom destructor functionality. This has different benefits, more than just avoiding to store the parent arena pointer/index/id or avoid costly arena lookups. Of course it doesn't have to be a shared ptr at all, only just a lambda is required. Why this pattern works well (especially in 𝐄𝐂𝐒) ✔ Correct arena used even with shared ownership. ✔ No global allocator branching. ✔ No RTTI or virtual allocator required. ✔ Works cleanly with multiple arenas. ✔ Compatible with ECS with memory pools allocations. #c++ #programming #coding
Yes, but since you're calling the constructor of the shared_ptr the control block is still doing a heap allocation, thus the problem of allocation still persits. For this to work the shared_pointer has to be fully re implemented or use the std::allocate_shared under the polymorphic allocators
How do you return memory to the arena?
Alignment?
Shared ptrs are slow. I believe they have a hidden mutex in them too.
No thanks, I'll stick to my C compiler.
Don't shared pointers dynamically allocate the ref counter? And as far as I know you can t just return memory to an arena, that would defeat the whole purpose of it. An arena allocator is literally a stack, you can't just delete from the middle.
Why not just use std::allocate_shared? It would allow you to use the standard allocator interface of the STL.
You may provide to std::shared_ptr as a last argument your allocator that will allocate a memory in order to create the std::shared_ptr inside the your memory pool.
I guess you can and should whenever possible. I didn't know about std::allocate_shared