Shared memory pool - How? - Design problem

I create my own memory pool for small and very often used objects. I am well versed in distribution and d-distribution.

Here is the layout of my pool

class CPool { unsigned int m_uiBlocks; unsigned int m_uiSizeOfBlock; unsigned int m_uiFreeBlocks; unsigned int m_uiInitialized; unsigned char *m_pMemStart; unsigned char *m_pNext; public: CPool(); ~CPool(); void CreatePool(size_t sizeOfEachBlock, unsigned int numOfBlocks); void DestroyPool(); unsigned char* AddrFromIndex(unsigned int i) const; unsigned int IndexFromAddr(const unsigned char* p) const; void* Allocate(); void DeAllocate(void* p); }; 

I would like each class to have its own pool. Now, if some class should use this pool, it is necessary that

  • They call CreatePool() with size and no_of_objects
  • They either call the parameterized new and delete , or they overload the operators and call the Allocate and DeAllocate .
  • calling 'DestroyPool ()'

I’m more worried about calls like Derived *derObj = new (poolObj)Derived(); . Here the user can forget poolObj , and this object will not be on my heap at all. Of course, for this I have a global function like

 inline void* operator new(size_t size, CPool& objPool) { return objPool.Allocate(); } 

Therefore, I would like to ask specific questions:

  • How to reverse engineer my pool class so that when calling the client Derived *derObj = new Derived(); I have the opportunity to allocate memory from my pool. Is it possible?

  • Is there a way to recognize the type object? So, can CreatePool and DestroyPool also be removed from client code? But I need to be very careful that there is only one pool per type.

I am also ready to use template code, but I'm not sure what to do template. Please suggest.

+2
source share

Source: https://habr.com/ru/post/1208676/


All Articles