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 ++++++++++++++++++++++++++++------------------ src/Chunk.cpp | 2 +- src/Chunk.h | 2 +- src/ChunkData.cpp | 2 +- src/ChunkData.h | 4 ++-- src/ChunkMap.cpp | 12 +++++++++--- src/ChunkMap.h | 8 ++++---- 7 files changed, 46 insertions(+), 30 deletions(-) (limited to 'src') 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; }; diff --git a/src/Chunk.cpp b/src/Chunk.cpp index a0a397a08..4703e4536 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -65,7 +65,7 @@ cChunk::cChunk( int a_ChunkX, int a_ChunkY, int a_ChunkZ, cChunkMap * a_ChunkMap, cWorld * a_World, cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, - cAllocationPool & a_Pool + cAllocationPool & a_Pool ) : m_IsValid(false), m_IsLightValid(false), diff --git a/src/Chunk.h b/src/Chunk.h index 07f597d59..7664a7afd 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -66,7 +66,7 @@ public: int a_ChunkX, int a_ChunkY, int a_ChunkZ, // Chunk coords cChunkMap * a_ChunkMap, cWorld * a_World, // Parent objects cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, // Neighbor chunks - cAllocationPool & a_Pool + cAllocationPool & a_Pool ); cChunk(cChunk & other); ~cChunk(); diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index f9c263d88..03b0224a6 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -27,7 +27,7 @@ template inline bool IsAllValue(const T * a_Array, size_t a_NumElem -cChunkData::cChunkData(cAllocationPool & a_Pool) : +cChunkData::cChunkData(cAllocationPool & a_Pool) : #if __cplusplus < 201103L // auto_ptr style interface for memory management m_IsOwner(true), diff --git a/src/ChunkData.h b/src/ChunkData.h index 9dee78ad0..e3b36c581 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -37,7 +37,7 @@ public: struct sChunkSection; - cChunkData(cAllocationPool & a_Pool); + cChunkData(cAllocationPool & a_Pool); ~cChunkData(); #if __cplusplus < 201103L @@ -122,7 +122,7 @@ private: /** Sets the data in the specified section to their default values. */ void ZeroSection(sChunkSection * a_Section) const; - cAllocationPool & m_Pool; + cAllocationPool & m_Pool; }; diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 704c4f823..3b946f9ec 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -35,8 +35,14 @@ cChunkMap::cChunkMap(cWorld * a_World ) : m_World( a_World ), - m_Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())) + m_Pool( + new cListAllocationPool( + std::auto_ptr::cStarvationCallbacks>( + new cStarvationCallbacks()) + ) + ) { + } @@ -79,7 +85,7 @@ cChunkMap::cChunkLayer * cChunkMap::GetLayer(int a_LayerX, int a_LayerZ) } // Not found, create new: - cChunkLayer * Layer = new cChunkLayer(a_LayerX, a_LayerZ, this, m_Pool); + cChunkLayer * Layer = new cChunkLayer(a_LayerX, a_LayerZ, this, *m_Pool); if (Layer == NULL) { LOGERROR("cChunkMap: Cannot create new layer, server out of memory?"); @@ -2672,7 +2678,7 @@ void cChunkMap::QueueTickBlock(int a_BlockX, int a_BlockY, int a_BlockZ) // cChunkMap::cChunkLayer: cChunkMap::cChunkLayer::cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent, - cAllocationPool & a_Pool) + cAllocationPool & a_Pool) : m_LayerX( a_LayerX ) , m_LayerZ( a_LayerZ ) , m_Parent( a_Parent ) diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 08156e7e6..c1dc743e5 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -352,7 +352,7 @@ private: { public: cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent, - cAllocationPool & a_Pool); + cAllocationPool & a_Pool); ~cChunkLayer(); /** Always returns an assigned chunkptr, but the chunk needn't be valid (loaded / generated) - callers must check */ @@ -397,11 +397,11 @@ private: cChunkMap * m_Parent; int m_NumChunksLoaded; - cAllocationPool & m_Pool; + cAllocationPool & m_Pool; }; class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() { @@ -447,7 +447,7 @@ private: /** The cChunkStay descendants that are currently enabled in this chunkmap */ cChunkStays m_ChunkStays; - cAllocationPool m_Pool; + std::auto_ptr> m_Pool; cChunkPtr GetChunk (int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Also queues the chunk for loading / generating if not valid cChunkPtr GetChunkNoGen (int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Also queues the chunk for loading if not valid; doesn't generate -- cgit v1.2.3