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: d42804a47eaafbbc1c0b151f8fcaeb5ddb086ff5
This commit is contained in:
parent
3f81ab4742
commit
55c205a5e5
|
|
@ -8,27 +8,33 @@
|
|||
#define NAZARA_MEMORYPOOL_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
template<unsigned int typeSize, unsigned int count = 1000, bool canGrow = true>
|
||||
template<unsigned int blockSize, bool canGrow = true>
|
||||
class NzMemoryPool
|
||||
{
|
||||
public:
|
||||
NzMemoryPool();
|
||||
NzMemoryPool(unsigned int size = 1024);
|
||||
~NzMemoryPool() = default;
|
||||
|
||||
template<typename T> T* Allocate();
|
||||
void* Allocate(unsigned int size);
|
||||
|
||||
void Free(void* ptr);
|
||||
|
||||
unsigned int GetFreeBlocks() const;
|
||||
unsigned int GetSize() const;
|
||||
|
||||
private:
|
||||
NzMemoryPool(NzMemoryPool* pool);
|
||||
|
||||
std::unique_ptr<void*[]> m_freeList;
|
||||
std::unique_ptr<nzUInt8[]> m_pool;
|
||||
std::unique_ptr<NzMemoryPool> m_next;
|
||||
std::atomic_uint m_freeCount;
|
||||
NzMemoryPool* m_previous;
|
||||
unsigned int m_freeCount;
|
||||
unsigned int m_size;
|
||||
};
|
||||
|
||||
#include <Nazara/Core/MemoryPool.inl>
|
||||
|
|
|
|||
|
|
@ -5,70 +5,67 @@
|
|||
#include <new>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
template<unsigned int typeSize, unsigned int count, bool canGrow>
|
||||
NzMemoryPool<typeSize, count, canGrow>::NzMemoryPool() :
|
||||
template<unsigned int blockSize, bool canGrow>
|
||||
NzMemoryPool<blockSize, canGrow>::NzMemoryPool(unsigned int count) :
|
||||
m_freeCount(count),
|
||||
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]);
|
||||
|
||||
// Remplissage de la free list
|
||||
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>
|
||||
NzMemoryPool<typeSize, count, canGrow>::NzMemoryPool(NzMemoryPool* pool) :
|
||||
NzMemoryPool()
|
||||
template<unsigned int blockSize, bool canGrow>
|
||||
NzMemoryPool<blockSize, canGrow>::NzMemoryPool(NzMemoryPool* pool) :
|
||||
NzMemoryPool(pool->m_size)
|
||||
{
|
||||
m_previous = pool;
|
||||
}
|
||||
|
||||
template<unsigned int typeSize, unsigned int count, bool canGrow>
|
||||
template<unsigned int blockSize, bool canGrow>
|
||||
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)));
|
||||
}
|
||||
|
||||
template<unsigned int typeSize, unsigned int count, bool canGrow>
|
||||
void* NzMemoryPool<typeSize, count, canGrow>::Allocate(unsigned int size)
|
||||
template<unsigned int blockSize, bool canGrow>
|
||||
void* NzMemoryPool<blockSize, canGrow>::Allocate(unsigned int size)
|
||||
{
|
||||
if (size > typeSize)
|
||||
if (size <= blockSize)
|
||||
{
|
||||
throw std::bad_alloc();
|
||||
return nullptr;
|
||||
if (m_freeCount > 0)
|
||||
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 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;
|
||||
return operator new(size);
|
||||
}
|
||||
|
||||
template<unsigned int typeSize, unsigned int count, bool canGrow>
|
||||
void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr)
|
||||
template<unsigned int blockSize, bool canGrow>
|
||||
void NzMemoryPool<blockSize, canGrow>::Free(void* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
// Le pointer nous appartient-il ?
|
||||
nzUInt8* freePtr = static_cast<nzUInt8*>(ptr);
|
||||
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 ((freePtr - poolPtr) % typeSize != 0)
|
||||
if ((freePtr - poolPtr) % blockSize != 0)
|
||||
{
|
||||
throw std::runtime_error("Pointer does not belong to memory pool");
|
||||
return;
|
||||
|
|
@ -78,7 +75,7 @@ void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr)
|
|||
m_freeList[m_freeCount++] = ptr;
|
||||
|
||||
// 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();
|
||||
delete this; // Suicide
|
||||
|
|
@ -89,9 +86,21 @@ void NzMemoryPool<typeSize, count, canGrow>::Free(void* ptr)
|
|||
if (m_next)
|
||||
m_next->Free(ptr);
|
||||
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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue