From 8be3a8f7dc10dbc49dfcdeca572677ef1e00f714 Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 23 May 2014 17:18:11 +0100 Subject: Implemented Allocation Pool use by cChunkData --- src/AllocationPool.h | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/AllocationPool.h') 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 AllocationPool { +#include + +template +class cAllocationPool { public: + + class cStarvationCallbacks + { + public: + virtual ~cStarvationCallbacks() {} + virtual void OnStartingUsingBuffer() = 0; + virtual void OnStopUsingBuffer() = 0; + virtual void OnBufferEmpty() = 0; + }; + + cAllocationPool(std::auto_ptr 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 m_FreeList; -} + std::auto_ptr m_Callbacks; +}; -- cgit v1.2.3