From 94c48febd2f596648fc2616a8a577316a219b581 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 19:46:34 +0100 Subject: Added generic Allocation Pool Interface --- src/AllocationPool.h | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'src/AllocationPool.h') 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 +template +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 cAllocationPool +class cListAllocationPool : public 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) : + cListAllocationPool(std::auto_ptr::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 m_FreeList; - std::auto_ptr m_Callbacks; + std::auto_ptr::cStarvationCallbacks> m_Callbacks; }; -- cgit v1.2.3