is there a common algorithm or implementation to have a memory pool that works both on a class A
and its derived classes?
It is fairly easy to create a memory pool that works in O(1) only for a specific class A . For instance : allocate a big chunk of data, that is 10*sizeof(A)
and then give out 1à blocks of size sizeof(A)
each time a allocation is needed.
Is there such a simple implementation when we can also consider the derived classes of A, that have a bigger size? thanks
It really depends on what your definition of simple is. As Jack said, you can use the size of largest of the derived classes as the element size of the array that is the memory pool. That is definitely a simple implementation.
If some types were half the size of the largest type or smaller you could modify the implementation to allow a second instance to occupy a slot that is occupied a compatible instance. That could be extended to quarter size types as well if applicable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With