summaryrefslogtreecommitdiffstats
path: root/src/AllocationPool.h
diff options
context:
space:
mode:
authorTycho <work.tycho+git@gmail.com>2014-06-14 20:46:34 +0200
committerTycho <work.tycho+git@gmail.com>2014-06-14 20:46:34 +0200
commit94c48febd2f596648fc2616a8a577316a219b581 (patch)
tree3275d163808851c7413cd9abb5f3019788823fda /src/AllocationPool.h
parentreverted accedental android changes (diff)
downloadcuberite-94c48febd2f596648fc2616a8a577316a219b581.tar
cuberite-94c48febd2f596648fc2616a8a577316a219b581.tar.gz
cuberite-94c48febd2f596648fc2616a8a577316a219b581.tar.bz2
cuberite-94c48febd2f596648fc2616a8a577316a219b581.tar.lz
cuberite-94c48febd2f596648fc2616a8a577316a219b581.tar.xz
cuberite-94c48febd2f596648fc2616a8a577316a219b581.tar.zst
cuberite-94c48febd2f596648fc2616a8a577316a219b581.zip
Diffstat (limited to 'src/AllocationPool.h')
-rw-r--r--src/AllocationPool.h46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/AllocationPool.h b/src/AllocationPool.h
index 9bb44ff1f..88bc132e9 100644
--- a/src/AllocationPool.h
+++ b/src/AllocationPool.h
@@ -3,21 +3,31 @@
#include <memory>
+template<class T>
+class cAllocationPool
+{
+public:
+ class cStarvationCallbacks
+ {
+ public:
+ virtual ~cStarvationCallbacks() {}
+ virtual void OnStartingUsingBuffer() = 0;
+ virtual void OnStopUsingBuffer() = 0;
+ virtual void OnBufferEmpty() = 0;
+ };
+
+ virtual ~cAllocationPool() {}
+
+ virtual T * Allocate() = 0;
+ virtual void Free(T * a_ptr) = 0;
+};
+
template<class T, size_t NumElementsInReserve>
-class cAllocationPool
+class cListAllocationPool : public cAllocationPool<T>
{
public:
-
- class cStarvationCallbacks
- {
- public:
- virtual ~cStarvationCallbacks() {}
- virtual void OnStartingUsingBuffer() = 0;
- virtual void OnStopUsingBuffer() = 0;
- virtual void OnBufferEmpty() = 0;
- };
- cAllocationPool(std::auto_ptr<cStarvationCallbacks> a_Callbacks) :
+ cListAllocationPool(std::auto_ptr<typename cAllocationPool<T>::cStarvationCallbacks> a_Callbacks) :
m_Callbacks(a_Callbacks)
{
for (size_t i = 0; i < NumElementsInReserve; i++)
@@ -32,7 +42,7 @@ class cAllocationPool
}
}
- ~cAllocationPool()
+ virtual ~cListAllocationPool()
{
while (!m_FreeList.empty())
{
@@ -41,7 +51,7 @@ class cAllocationPool
}
}
- T * Allocate()
+ virtual T * Allocate() override
{
if (m_FreeList.size() <= NumElementsInReserve)
{
@@ -66,15 +76,15 @@ class cAllocationPool
m_FreeList.pop_front();
return ret;
}
- void Free(T * ptr)
+ virtual void Free(T * a_ptr) override
{
- if (ptr == NULL)
+ if (a_ptr == NULL)
{
return;
}
// placement destruct.
- ptr->~T();
- m_FreeList.push_front(ptr);
+ a_ptr->~T();
+ m_FreeList.push_front(a_ptr);
if (m_FreeList.size() == NumElementsInReserve)
{
m_Callbacks->OnStopUsingBuffer();
@@ -83,5 +93,5 @@ class cAllocationPool
private:
std::list<void *> m_FreeList;
- std::auto_ptr<cStarvationCallbacks> m_Callbacks;
+ std::auto_ptr<typename cAllocationPool<T>::cStarvationCallbacks> m_Callbacks;
};