A very fast cross-platform memory pool mechanism for C++ built using a data-oriented approach (3 to 24 times faster than regular new or delete, depending on operating system & compiler)
new
and delete
operators completely to use a global MemoryPool of the application. Then, I noticed that the pool just keeps filling up since memory is allocated and de-allocated almost randomly by libraries and the system, thus is not sequential as the pool likes it. Using the memory pool scope feature won't help me here - so I decided to add some garbage collection mechanism that keeps a linked chain of all free area in the pool that can be used for further allocation - this seemed to work great so I decided to add it to the library later.new
and delete
operators to speed things up a little. This also required me to add changes to the memory pool, once it will be complete I will also add it to a dedicate release :)Discussion is open about the algorithms used in the MemoryPool that can be affective and speed up the library even more. Personally, I have a couple of different ideas regarding the garbage collection of the pool that can come in different forms and performances and would love to hear your ideas too! :)
When freeing a block in the middle of the block chain of the pool, the next block didn't connect to the previous block correctly, which might cause a memory access violation if more that 2 block are freed in a chain in a certain order.
CPPShift::Memory::MemoryPoolManager no longer present. Right now the MemoryPool object holds all the functions to handle the memory pool data structure.
auto mp = new CPPShift::Memory::MemoryPool(size);
or auto mp = CPPShift::Memory::MemoryPool(size);
new (mp) Type[size];
, mp->reallocate<Type>(pointer, size);
and mp->free(pointer)
respectively.mp->allocate<Type>(size);
or (Type*) mp->allocate(size * sizeof(Type))
, and mp->reallocate<Type>(pinter, size)
or (Type*) mp->reallocate(pinter, size * sizeof(Type))
. These 2 approaches to each function are actually the same.mp->startScope()
& mp->endScope()
The header & code files for adding the memory pool to your project.
Features:
CPPShift::Memory::MemoryPool * mp = CPPShift::Memory::MemoryPoolManager::create(size);
Type* my_type = new (mp) Type[size];
CPPShift::Memory::MemoryPoolManager::free(my_type)
& CPPShift::Memory::MemoryPoolManager::reallocate(my_type, new_size)
respectively.CPPShift::Memory::MemoryPoolManager::startScope(mp)
& CPPShift::Memory::MemoryPoolManager::endScope(mp)