Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ memory pool for a class and its derived classes

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

like image 550
lezebulon Avatar asked Nov 13 '22 17:11

lezebulon


1 Answers

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.

like image 121
Josh Heitzman Avatar answered Nov 15 '22 07:11

Josh Heitzman