is there a way to design a C++ memory pool (for a specific class only) that can allocate and free memory in O(1)?
let’s say I have class T, I was thinking of allocating only chunks of 100*sizeof(T) when needed. But then how can I deal with the fragmentation that occurs when a specific object is deleted in the chunk? I can have a boolean value for each slot to tell if the slot is occupied or not, but then I need an algorithm to give me the next free slot.
Is there any standard way to implement this in O(1)? I guess this is a fairly common thing
edit :
image to see what I mean

This solution is using additional memory (which may or may not be what you want), also you’ll have problems if you try to free a chunk twice in a row.
Preallocate enough memory. Divide it into chunks, one per object. Keep a list of free chunks. When you allocate a new object, pick a chunk from the top of the free chunks list. When you free an object, append its chunk to the list of free chunks. Both of these operations are O(1).
It would look something like this:
Init:
Getting a new chunk for allocation:
Returning a chunk after deallocation: