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
#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>