Improved MemoryPool class

It's size is now dynamic (defaulted to 1024)
Added MemoryPool::GetFreeBlocks()
Added MemoryPool::GetSize()
Tried to make it thread-safe
It supports dynamics allocations (in case where it can't allocate memory
from the pool for some reasons)


Former-commit-id: 1cc6cb2bc118556363a5c8ba1d18b3b1ce734862
This commit is contained in:
Lynix 2014-06-15 01:17:57 +02:00
parent 13e1cadcdd
commit f55b151c8e
2 changed files with 52 additions and 37 deletions

View File

@ -8,27 +8,33 @@
#define NAZARA_MEMORYPOOL_HPP #define NAZARA_MEMORYPOOL_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <atomic>
#include <memory> #include <memory>
template<unsigned int typeSize, unsigned int count = 1000, bool canGrow = true> template<unsigned int blockSize, bool canGrow = true>
class NzMemoryPool class NzMemoryPool
{ {
public: public:
NzMemoryPool(); NzMemoryPool(unsigned int size = 1024);
~NzMemoryPool() = default; ~NzMemoryPool() = default;
template<typename T> T* Allocate(); template<typename T> T* Allocate();
void* Allocate(unsigned int size); void* Allocate(unsigned int size);
void Free(void* ptr); void Free(void* ptr);
unsigned int GetFreeBlocks() const;
unsigned int GetSize() const;
private: private:
NzMemoryPool(NzMemoryPool* pool); NzMemoryPool(NzMemoryPool* pool);
std::unique_ptr<void*[]> m_freeList; std::unique_ptr<void*[]> m_freeList;
std::unique_ptr<nzUInt8[]> m_pool; std::unique_ptr<nzUInt8[]> m_pool;
std::unique_ptr<NzMemoryPool> m_next; std::unique_ptr<NzMemoryPool> m_next;
std::atomic_uint m_freeCount;
NzMemoryPool* m_previous; NzMemoryPool* m_previous;
unsigned int m_freeCount; unsigned int m_size;
}; };
#include <Nazara/Core/MemoryPool.inl> #include <Nazara/Core/MemoryPool.inl>

View File

@ -5,70 +5,67 @@
#include <new> #include <new>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
template<unsigned int typeSize, unsigned int count, bool canGrow> template<unsigned int blockSize, bool canGrow>
NzMemoryPool<typeSize, count, canGrow>::NzMemoryPool() : NzMemoryPool<blockSize, canGrow>::NzMemoryPool(unsigned int count) :
m_freeCount(count),
m_previous(nullptr), m_previous(nullptr),
m_freeCount(count) m_size(count)
{ {
m_pool.reset(new nzUInt8[typeSize * count]); m_pool.reset(new nzUInt8[blockSize * count]);
m_freeList.reset(new void*[count]); m_freeList.reset(new void*[count]);
// Remplissage de la free list // Remplissage de la free list
for (unsigned int i = 0; i < count; ++i) for (unsigned int i = 0; i < count; ++i)
m_freeList[i] = &m_pool[typeSize * (count-i-1)]; m_freeList[i] = &m_pool[blockSize * (count-i-1)];
} }
template<unsigned int typeSize, unsigned int count, bool canGrow> template<unsigned int blockSize, bool canGrow>
NzMemoryPool<typeSize, count, canGrow>::NzMemoryPool(NzMemoryPool* pool) : NzMemoryPool<blockSize, canGrow>::NzMemoryPool(NzMemoryPool* pool) :
NzMemoryPool() NzMemoryPool(pool->m_size)
{ {
m_previous = pool; m_previous = pool;
} }
template<unsigned int typeSize, unsigned int count, bool canGrow> template<unsigned int blockSize, bool canGrow>
template<typename T> template<typename T>
T* NzMemoryPool<typeSize, count, canGrow>::Allocate() T* NzMemoryPool<blockSize, canGrow>::Allocate()
{ {
static_assert(sizeof(T) <= typeSize, "This type is too large for this memory pool"); static_assert(sizeof(T) <= blockSize, "This type is too large for this memory pool");
return static_cast<T*>(Allocate(sizeof(T))); return static_cast<T*>(Allocate(sizeof(T)));
} }
template<unsigned int typeSize, unsigned int count, bool canGrow> template<unsigned int blockSize, bool canGrow>
void* NzMemoryPool<typeSize, count, canGrow>::Allocate(unsigned int size) void* NzMemoryPool<blockSize, canGrow>::Allocate(unsigned int size)
{ {
if (size > typeSize) if (size <= blockSize)
{ {
throw std::bad_alloc(); if (m_freeCount > 0)
return nullptr; return m_freeList[--m_freeCount];
else if (canGrow)
{
if (!m_next)
m_next.reset(new NzMemoryPool(this));
return m_next->Allocate(size);
}
} }
if (m_freeCount > 0) return operator new(size);
return m_freeList[--m_freeCount];
else if (canGrow)
{
if (!m_next)
m_next.reset(new NzMemoryPool(this));
return m_next->Allocate(size);
}
throw std::bad_alloc();
return nullptr;
} }
template<unsigned int typeSize, unsigned int count, bool canGrow> template<unsigned int blockSize, bool canGrow>
void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr) void NzMemoryPool<blockSize, canGrow>::Free(void* ptr)
{ {
if (ptr) if (ptr)
{ {
// Le pointer nous appartient-il ? // Le pointer nous appartient-il ?
nzUInt8* freePtr = static_cast<nzUInt8*>(ptr); nzUInt8* freePtr = static_cast<nzUInt8*>(ptr);
nzUInt8* poolPtr = m_pool.get(); nzUInt8* poolPtr = m_pool.get();
if (freePtr >= poolPtr && freePtr < poolPtr + typeSize*count) if (freePtr >= poolPtr && freePtr < poolPtr + blockSize*m_size)
{ {
#if NAZARA_CORE_SAFE #if NAZARA_CORE_SAFE
if ((freePtr - poolPtr) % typeSize != 0) if ((freePtr - poolPtr) % blockSize != 0)
{ {
throw std::runtime_error("Pointer does not belong to memory pool"); throw std::runtime_error("Pointer does not belong to memory pool");
return; return;
@ -78,7 +75,7 @@ void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr)
m_freeList[m_freeCount++] = ptr; m_freeList[m_freeCount++] = ptr;
// Si nous sommes vide et l'extension d'un autre pool, on se suicide // Si nous sommes vide et l'extension d'un autre pool, on se suicide
if (m_freeCount == count && m_previous && !m_next) if (m_freeCount == m_size && m_previous && !m_next)
{ {
m_previous->m_next.release(); m_previous->m_next.release();
delete this; // Suicide delete this; // Suicide
@ -89,9 +86,21 @@ void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr)
if (m_next) if (m_next)
m_next->Free(ptr); m_next->Free(ptr);
else else
throw std::runtime_error("Pointer does not belong to memory pool"); operator delete(ptr);
} }
} }
} }
template<unsigned int blockSize, bool canGrow>
unsigned int NzMemoryPool<blockSize, canGrow>::GetFreeBlocks() const
{
return m_freeCount;
}
template<unsigned int blockSize, bool canGrow>
unsigned int NzMemoryPool<blockSize, canGrow>::GetSize() const
{
return m_size;
}
#include <Nazara/Core/DebugOff.hpp> #include <Nazara/Core/DebugOff.hpp>