summaryrefslogtreecommitdiffstats
path: root/src/AllocationPool.h
diff options
context:
space:
mode:
authorTycho <work.tycho+git@gmail.com>2014-05-23 18:18:11 +0200
committerTycho <work.tycho+git@gmail.com>2014-05-23 18:18:11 +0200
commit8be3a8f7dc10dbc49dfcdeca572677ef1e00f714 (patch)
tree204743272c8948237a8322027510f5240bfdb71e /src/AllocationPool.h
parentUse placement new to initalise objects (diff)
downloadcuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar.gz
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar.bz2
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar.lz
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar.xz
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.tar.zst
cuberite-8be3a8f7dc10dbc49dfcdeca572677ef1e00f714.zip
Diffstat (limited to '')
-rw-r--r--src/AllocationPool.h39
1 files changed, 28 insertions, 11 deletions
diff --git a/src/AllocationPool.h b/src/AllocationPool.h
index d14d56f7a..b3818e8b1 100644
--- a/src/AllocationPool.h
+++ b/src/AllocationPool.h
@@ -1,36 +1,52 @@
#pragma once
-template<class T, size_t BufferSize, class StarvationCallbacks>
-class AllocationPool {
+#include <memory>
+
+template<class T, size_t BufferSize>
+class cAllocationPool {
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) :
+ m_Callbacks(a_Callbacks)
+ {
+ }
- ~AllocationPool()
+ ~cAllocationPool()
{
while (!m_FreeList.empty())
{
- delete m_FreeList.front();
+ free (m_FreeList.front());
m_FreeList.pop_front();
}
}
T* Allocate()
{
- if (m_FreeList.Size() <= BufferSize)
+ if (m_FreeList.size() <= BufferSize)
{
try
{
- return new T;
+ return new(malloc(sizeof(T))) T;
}
- catch (std::bad_alloc& ex)
+ catch (std::bad_alloc&)
{
if (m_FreeList.size() == BufferSize)
{
- StarvationCallbacks.OnStartingUsingBuffer();
+ m_Callbacks->OnStartingUsingBuffer();
}
else if (m_FreeList.empty())
{
- StarvationCallbacks.OnBufferEmpty();
+ m_Callbacks->OnBufferEmpty();
// Try again until the memory is avalable
return Allocate();
}
@@ -48,10 +64,11 @@ class AllocationPool {
m_FreeList.push_front(ptr);
if (m_FreeList.size() == BufferSize)
{
- StarvationCallbacks.OnStopUsingBuffer();
+ m_Callbacks->OnStopUsingBuffer();
}
}
private:
std::list<void *> m_FreeList;
-}
+ std::auto_ptr<cStarvationCallbacks> m_Callbacks;
+};