1
0

Implemented Allocation Pool

This commit is contained in:
Tycho 2014-05-21 20:59:04 +01:00
parent cdd3d11496
commit 485752de82

50
src/AllocationPool.h Normal file
View File

@ -0,0 +1,50 @@
#pragma once
template<class T, size_t BufferSize, class StarvationCallbacks>
class AllocationPool {
public:
~AllocationPool()
{
while (!m_FreeList.empty())
{
delete m_FreeList.front();
m_FreeList.pop_front();
}
}
T* Allocate()
{
if (m_FreeList.Size() <= BufferSize)
{
try
{
return new T;
}
catch (std::bad_alloc& ex)
{
if (m_FreeList.size() == BufferSize)
{
StarvationCallbacks.OnStartingUsingBuffer();
}
else if (m_FreeList.empty())
{
StarvationCallbacks.OnBufferEmpty();
// Try again until the memory is avalable
return Allocate();
}
}
}
T* ret = m_FreeList.front();
m_FreeList.pop_front();
return ret;
}
void Free(T* ptr)
{
m_FreeList.push_front(ptr);
}
private:
std::list<T*> m_FreeList;
}