From 485752de82e6058473dfe8dccdf64ac04e983ae9 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 20:59:04 +0100 Subject: Implemented Allocation Pool --- src/AllocationPool.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/AllocationPool.h diff --git a/src/AllocationPool.h b/src/AllocationPool.h new file mode 100644 index 000000000..f03897f98 --- /dev/null +++ b/src/AllocationPool.h @@ -0,0 +1,50 @@ + +#pragma once + +template +class AllocationPool { + public: + + ~AllocationPool() + { + while (!m_FreeList.empty()) + { + delete m_FreeList.front(); + m_FreeList.pop_front(); + } + } + + T* Allocate() + { + if (m_FreeList.Size() <= BufferSize) + { + try + { + return new T; + } + catch (std::bad_alloc& ex) + { + if (m_FreeList.size() == BufferSize) + { + StarvationCallbacks.OnStartingUsingBuffer(); + } + else if (m_FreeList.empty()) + { + StarvationCallbacks.OnBufferEmpty(); + // Try again until the memory is avalable + return Allocate(); + } + } + } + T* ret = m_FreeList.front(); + m_FreeList.pop_front(); + return ret; + } + void Free(T* ptr) + { + m_FreeList.push_front(ptr); + } + + private: + std::list m_FreeList; +} -- cgit v1.2.3 From 4124eac15d6b545b685e58f9f5f3a0266c1d8df0 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 21:30:08 +0100 Subject: Added callback for stopping starvation mode --- src/AllocationPool.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index f03897f98..f1e324953 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -43,6 +43,10 @@ class AllocationPool { void Free(T* ptr) { m_FreeList.push_front(ptr); + if (m_FreeList.size() == BufferSize) + { + StarvationCallbacks.OnStopUsingBuffer(); + } } private: -- cgit v1.2.3 From 6991c2dd84060f8e7375966d4e488a359b42d43a Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 23 May 2014 15:48:09 +0100 Subject: Use placement new to initalise objects --- src/AllocationPool.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index f1e324953..d14d56f7a 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -36,12 +36,15 @@ class AllocationPool { } } } - T* ret = m_FreeList.front(); + // placement new, used to initalize the object + T* ret = new (m_FreeList.front()) T; m_FreeList.pop_front(); return ret; } void Free(T* ptr) { + // placement destruct. + ptr->~T(); m_FreeList.push_front(ptr); if (m_FreeList.size() == BufferSize) { @@ -50,5 +53,5 @@ class AllocationPool { } private: - std::list m_FreeList; + std::list m_FreeList; } -- cgit v1.2.3 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 ++++++++++++++++++++++++++++----------- src/Chunk.cpp | 4 +++- src/Chunk.h | 3 ++- src/ChunkData.cpp | 6 +++--- src/ChunkData.h | 33 ++++++++++++++++++++++----------- src/ChunkMap.cpp | 11 +++++++---- src/ChunkMap.h | 15 ++++++++++++++- tests/ChunkData/ArraytoCoord.cpp | 15 ++++++++++++--- tests/ChunkData/Coordinates.cpp | 16 ++++++++++++---- tests/ChunkData/Copies.cpp | 18 +++++++++++++----- tests/ChunkData/creatable.cpp | 10 +++++++++- 11 files changed, 125 insertions(+), 45 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 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; +}; diff --git a/src/Chunk.cpp b/src/Chunk.cpp index d85b44607..cdec0774f 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -64,7 +64,8 @@ sSetBlock::sSetBlock( int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_Bloc 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 + cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, + cAllocationPool& a_Pool ) : m_IsValid(false), m_IsLightValid(false), @@ -77,6 +78,7 @@ cChunk::cChunk( m_PosZ(a_ChunkZ), m_World(a_World), m_ChunkMap(a_ChunkMap), + m_ChunkData(a_Pool), m_BlockTickX(0), m_BlockTickY(0), m_BlockTickZ(0), diff --git a/src/Chunk.h b/src/Chunk.h index 2de45919e..e5b55ed2a 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -65,7 +65,8 @@ public: cChunk( 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 + cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP, // Neighbor chunks + cAllocationPool& a_Pool ); cChunk(cChunk& other); ~cChunk(); diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 86b0c431c..31de364b9 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -4,7 +4,7 @@ cChunkData cChunkData::Copy() const { - cChunkData copy; + cChunkData copy(m_Pool); for (int i = 0; i < CHUNK_SECTION_NUM; i++) { if (m_Sections[i] != NULL) @@ -360,14 +360,14 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) cChunkData::sChunkSection * cChunkData::Allocate() const { // TODO: use a allocation pool - return new cChunkData::sChunkSection; + return m_Pool.Allocate(); } void cChunkData::Free(cChunkData::sChunkSection * ptr) const { - delete ptr; + m_Pool.Free(ptr); } diff --git a/src/ChunkData.h b/src/ChunkData.h index 9c852ee24..527c5b2ae 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -7,6 +7,8 @@ #include "ChunkDef.h" +#include "AllocationPool.h" + #define CHUNK_SECTION_HEIGHT 16 #define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT) @@ -21,11 +23,14 @@ class cChunkData { public: - cChunkData() + struct sChunkSection; + + cChunkData(cAllocationPool& a_Pool) : #if __cplusplus < 201103L // auto_ptr style interface for memory management - : IsOwner(true) + IsOwner(true), #endif + m_Pool(a_Pool) { memset(m_Sections, 0, sizeof(m_Sections)); } @@ -44,7 +49,8 @@ public: #if __cplusplus < 201103L // auto_ptr style interface for memory management cChunkData(const cChunkData& other) : - IsOwner(true) + IsOwner(true), + m_Pool(other.m_Pool) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -70,13 +76,15 @@ public: m_Sections[i] = other.m_Sections[i]; } other.IsOwner = false; + ASSERT(&m_Pool == &other.m_Pool); } return *this; } #else // unique_ptr style interface for memory management - cChunkData(cChunkData&& other) + cChunkData(cChunkData&& other) : + m_Pool(other.m_Pool) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -95,6 +103,7 @@ public: m_Sections[i] = other.m_Sections[i]; other.m_Sections[i] = 0; } + ASSERT(&m_Pool == &other.m_Pool); } return *this; } @@ -252,13 +261,6 @@ public: void SetLight (const NIBBLETYPE * a_src); void SetSkyLight (const NIBBLETYPE * a_src); -private: - - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - mutable bool IsOwner; - #endif - struct sChunkSection { BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16] ; NIBBLETYPE m_BlockMeta [CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; @@ -266,12 +268,21 @@ private: NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; }; +private: + + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + mutable bool IsOwner; + #endif + sChunkSection *m_Sections[CHUNK_SECTION_NUM]; sChunkSection * Allocate() const; void Free(sChunkSection * ptr) const; void ZeroSection(sChunkSection * ptr) const; + + cAllocationPool& m_Pool; }; diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index d3f44bef8..bf2b09342 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -34,7 +34,8 @@ // cChunkMap: cChunkMap::cChunkMap(cWorld * a_World ) - : m_World( a_World ) + : m_World( a_World ), + m_Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())) { } @@ -78,7 +79,7 @@ cChunkMap::cChunkLayer * cChunkMap::GetLayer(int a_LayerX, int a_LayerZ) } // Not found, create new: - cChunkLayer * Layer = new cChunkLayer(a_LayerX, a_LayerZ, this); + cChunkLayer * Layer = new cChunkLayer(a_LayerX, a_LayerZ, this, m_Pool); if (Layer == NULL) { LOGERROR("cChunkMap: Cannot create new layer, server out of memory?"); @@ -2646,11 +2647,13 @@ 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) +cChunkMap::cChunkLayer::cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent, + cAllocationPool& a_Pool) : m_LayerX( a_LayerX ) , m_LayerZ( a_LayerZ ) , m_Parent( a_Parent ) , m_NumChunksLoaded( 0 ) + , m_Pool(a_Pool) { memset(m_Chunks, 0, sizeof(m_Chunks)); } @@ -2692,7 +2695,7 @@ cChunkPtr cChunkMap::cChunkLayer::GetChunk( int a_ChunkX, int a_ChunkY, int a_Ch cChunk * neixp = (LocalX < LAYER_SIZE - 1) ? m_Chunks[Index + 1] : m_Parent->FindChunk(a_ChunkX + 1, a_ChunkZ); cChunk * neizm = (LocalZ > 0) ? m_Chunks[Index - LAYER_SIZE] : m_Parent->FindChunk(a_ChunkX , a_ChunkZ - 1); cChunk * neizp = (LocalZ < LAYER_SIZE - 1) ? m_Chunks[Index + LAYER_SIZE] : m_Parent->FindChunk(a_ChunkX , a_ChunkZ + 1); - m_Chunks[Index] = new cChunk(a_ChunkX, 0, a_ChunkZ, m_Parent, m_Parent->GetWorld(), neixm, neixp, neizm, neizp); + m_Chunks[Index] = new cChunk(a_ChunkX, 0, a_ChunkZ, m_Parent, m_Parent->GetWorld(), neixm, neixp, neizm, neizp, m_Pool); } return m_Chunks[Index]; } diff --git a/src/ChunkMap.h b/src/ChunkMap.h index c3deda088..1744c09d0 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -347,7 +347,8 @@ private: class cChunkLayer { public: - cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent); + cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent, + cAllocationPool& a_Pool); ~cChunkLayer(); /** Always returns an assigned chunkptr, but the chunk needn't be valid (loaded / generated) - callers must check */ @@ -391,6 +392,16 @@ private: int m_LayerZ; cChunkMap * m_Parent; int m_NumChunksLoaded; + + cAllocationPool & m_Pool; + }; + + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks + { + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} }; typedef std::list cChunkLayerList; @@ -423,6 +434,8 @@ private: /** The cChunkStay descendants that are currently enabled in this chunkmap */ cChunkStays m_ChunkStays; + cAllocationPool 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 cChunkPtr GetChunkNoLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ); // Doesn't load, doesn't generate diff --git a/tests/ChunkData/ArraytoCoord.cpp b/tests/ChunkData/ArraytoCoord.cpp index fe82a3a7b..04e6fbc5a 100644 --- a/tests/ChunkData/ArraytoCoord.cpp +++ b/tests/ChunkData/ArraytoCoord.cpp @@ -6,9 +6,18 @@ int main(int argc, char** argv) { + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + { + // Test first segment - cChunkData buffer; + cChunkData buffer(Pool); BLOCKTYPE* SrcBlockBuffer = new BLOCKTYPE[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); @@ -45,7 +54,7 @@ int main(int argc, char** argv) { // test following segment - cChunkData buffer; + cChunkData buffer(Pool); BLOCKTYPE* SrcBlockBuffer = new BLOCKTYPE[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); @@ -82,7 +91,7 @@ int main(int argc, char** argv) { // test zeros - cChunkData buffer; + cChunkData buffer(Pool); BLOCKTYPE* SrcBlockBuffer = new BLOCKTYPE[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index c0c46000e..0a7d5e3f1 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -6,8 +6,16 @@ int main(int argc, char** argv) { + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { - cChunkData buffer; + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + { + cChunkData buffer(Pool); // Empty chunks buffer.SetBlock(0,0,0, 0xAB); @@ -105,7 +113,7 @@ int main(int argc, char** argv) } { - cChunkData buffer; + cChunkData buffer(Pool); // Zero's buffer.SetBlock(0,0,0, 0x0); @@ -122,9 +130,9 @@ int main(int argc, char** argv) { // Operator = - cChunkData buffer; + cChunkData buffer(Pool); buffer.SetBlock(0,0,0,0x42); - cChunkData copy; + cChunkData copy(Pool); #if __cplusplus < 201103L copy = buffer; #else diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp index 145ffd8e0..1ccda9d9c 100644 --- a/tests/ChunkData/Copies.cpp +++ b/tests/ChunkData/Copies.cpp @@ -6,8 +6,16 @@ int main(int argc, char** argv) { + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { - cChunkData buffer; + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + { + cChunkData buffer(Pool); buffer.SetBlock(3,1,4,0xDE); buffer.SetMeta(3,1,4,0xA); @@ -47,7 +55,7 @@ int main(int argc, char** argv) } { - cChunkData buffer; + cChunkData buffer(Pool); NIBBLETYPE * SrcNibbleBuffer = new NIBBLETYPE[16 * 16 * 256/2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) @@ -80,7 +88,7 @@ int main(int argc, char** argv) } { - cChunkData buffer; + cChunkData buffer(Pool); NIBBLETYPE * SrcNibbleBuffer = new NIBBLETYPE[16 * 16 * 256/2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) @@ -114,7 +122,7 @@ int main(int argc, char** argv) } { - cChunkData buffer; + cChunkData buffer(Pool); NIBBLETYPE * SrcNibbleBuffer = new NIBBLETYPE[16 * 16 * 256/2]; for (int i = 0; i < 16 * 16 * 256 / 2; i += 4) @@ -148,7 +156,7 @@ int main(int argc, char** argv) } { - cChunkData buffer; + cChunkData buffer(Pool); BLOCKTYPE * SrcBlockBuffer = new BLOCKTYPE[16 * 16 * 256]; memset(SrcBlockBuffer, 0x00, 16 * 16 * 256); diff --git a/tests/ChunkData/creatable.cpp b/tests/ChunkData/creatable.cpp index 74025cb14..78b1a82d1 100644 --- a/tests/ChunkData/creatable.cpp +++ b/tests/ChunkData/creatable.cpp @@ -4,6 +4,14 @@ int main(int argc, char** argv) { - cChunkData buffer; + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks + { + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cChunkData buffer(Pool); return 0; } -- cgit v1.2.3 From 25910873852252fb388059e78d88a293ccd9d797 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 25 May 2014 17:48:40 +0100 Subject: Fixed bug in freeing NULL pointers --- src/AllocationPool.h | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index b3818e8b1..643b44a6d 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -34,22 +34,20 @@ class cAllocationPool { { if (m_FreeList.size() <= BufferSize) { - try + void * space = malloc(sizeof(T)); + if (space != NULL) { - return new(malloc(sizeof(T))) T; + return new(space) T; } - catch (std::bad_alloc&) + else if (m_FreeList.size() == BufferSize) { - if (m_FreeList.size() == BufferSize) - { - m_Callbacks->OnStartingUsingBuffer(); - } - else if (m_FreeList.empty()) - { - m_Callbacks->OnBufferEmpty(); - // Try again until the memory is avalable - return Allocate(); - } + m_Callbacks->OnStartingUsingBuffer(); + } + else if (m_FreeList.empty()) + { + m_Callbacks->OnBufferEmpty(); + // Try again until the memory is avalable + return Allocate(); } } // placement new, used to initalize the object @@ -59,6 +57,10 @@ class cAllocationPool { } void Free(T* ptr) { + if (ptr == NULL) + { + return; + } // placement destruct. ptr->~T(); m_FreeList.push_front(ptr); -- cgit v1.2.3 From 2a7d199df68d11f6fd1fa2b4186faa00d0e81676 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 25 May 2014 18:26:10 +0100 Subject: Fixed bad merge --- src/ChunkData.cpp | 10 +++++----- src/ChunkData.h | 9 +++++---- tests/ChunkData/Coordinates.cpp | 8 +++++++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 1bc3072c2..99421bc33 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -242,7 +242,7 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const cChunkData cChunkData::Copy() const { cChunkData copy(m_Pool); - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { if (m_Sections[i] != NULL) { @@ -432,7 +432,7 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) void cChunkData::SetMeta(const NIBBLETYPE * a_src) { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -484,10 +484,10 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) -void cChunkData::SetLight(const NIBBLETYPE * a_src) +void cChunkData::SetBlockLight(const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -542,7 +542,7 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) diff --git a/src/ChunkData.h b/src/ChunkData.h index 08f1603bb..637771741 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -19,6 +19,11 @@ class cChunkData { +private: + + static const size_t CHUNK_SECTION_HEIGHT = 16; + static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT); + public: struct sChunkSection; @@ -65,10 +70,6 @@ public: }; private: - - static const size_t CHUNK_SECTION_HEIGHT = 16; - static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT); - #if __cplusplus < 201103L // auto_ptr style interface for memory management mutable bool IsOwner; diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index f94532183..3a4477fd6 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -8,8 +8,14 @@ int main(int argc, char** argv) { class cStarvationCallbacks : public cAllocationPool::cStarvationCallbacks + { + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); { - cChunkData buffer; + cChunkData buffer(Pool); // Empty chunks buffer.SetBlock(0, 0, 0, 0xAB); -- cgit v1.2.3 From 061102c77860e7a31b7504ca52bb99e419393fb2 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 17:23:57 +0100 Subject: Added logging --- src/ChunkMap.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 1a3258f15..efe489854 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -403,9 +403,18 @@ private: class cStarvationCallbacks : public cAllocationPool::cStarvationCallbacks { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} + virtual void OnStartingUsingBuffer() + { + LOG("Using backup memory buffer"); + } + virtual void OnStopUsingBuffer() + { + LOG("Stoped using backup memory buffer"); + } + virtual void OnBufferEmpty() + { + LOG("Out of Memory"); + } }; typedef std::list cChunkLayerList; -- cgit v1.2.3 From 8bf37f3dd7ff017eedaac427b9291a9335d61efc Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 17:57:21 +0100 Subject: Fixed comments --- src/AllocationPool.h | 8 ++++---- src/ChunkData.cpp | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index 643b44a6d..b8862e7df 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -3,7 +3,7 @@ #include -template +template class cAllocationPool { public: @@ -32,14 +32,14 @@ class cAllocationPool { T* Allocate() { - if (m_FreeList.size() <= BufferSize) + if (m_FreeList.size() <= NumElementsInReserve) { void * space = malloc(sizeof(T)); if (space != NULL) { return new(space) T; } - else if (m_FreeList.size() == BufferSize) + else if (m_FreeList.size() == NumElementsInReserve) { m_Callbacks->OnStartingUsingBuffer(); } @@ -64,7 +64,7 @@ class cAllocationPool { // placement destruct. ptr->~T(); m_FreeList.push_front(ptr); - if (m_FreeList.size() == BufferSize) + if (m_FreeList.size() == NumElementsInReserve) { m_Callbacks->OnStopUsingBuffer(); } diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 24a2c3e14..a80b1de0f 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -30,9 +30,9 @@ template inline bool IsAllValue(const T * a_Array, size_t a_NumElem cChunkData::cChunkData(cAllocationPool& a_Pool) : #if __cplusplus < 201103L // auto_ptr style interface for memory management - m_IsOwner(true) + m_IsOwner(true), #endif -m_Pool(a_Pool) + m_Pool(a_Pool) { for (size_t i = 0; i < NumSections; i++) { -- cgit v1.2.3 From bff76f201ffec0cc4c4df2be6ac125efa985dce7 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 17:59:47 +0100 Subject: Fill with buffer on startup --- src/AllocationPool.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index b8862e7df..e4f1427f6 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -19,6 +19,16 @@ class cAllocationPool { cAllocationPool(std::auto_ptr a_Callbacks) : m_Callbacks(a_Callbacks) { + for(int i = 0; i < NumElementsInReserve; i++) + { + void * space = malloc(sizeof(T)); + if (space == NULL) + { + m_Callbacks->OnStartingUsingBuffer(); + break; + } + m_FreeList.push_front(space); + } } ~cAllocationPool() -- cgit v1.2.3 From f6af584efa60d23c06abdc86dbfdfc85da23a3dc Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 18:11:43 +0100 Subject: fixed const issue --- src/ChunkData.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index a80b1de0f..d2903cff1 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -68,7 +68,7 @@ cChunkData::~cChunkData() // auto_ptr style interface for memory management cChunkData::cChunkData(const cChunkData & a_Other) : m_IsOwner(true), - m_Pool(other.m_Pool) + m_Pool(a_Other.m_Pool) { // Move contents and ownership from a_Other to this, pointer-wise: for (size_t i = 0; i < NumSections; i++) @@ -107,7 +107,7 @@ cChunkData::~cChunkData() m_Sections[i] = a_Other.m_Sections[i]; } a_Other.m_IsOwner = false; - ASSERT(&m_Pool == &other.m_Pool); + ASSERT(&m_Pool == &a_Other.m_Pool); return *this; } @@ -327,7 +327,7 @@ cChunkData cChunkData::Copy(void) const { if (m_Sections[i] != NULL) { - copy.m_Sections[i] = Allocate(); + copy.m_Sections[i] = copy.Allocate(); *copy.m_Sections[i] = *m_Sections[i]; } } -- cgit v1.2.3 From a2df68b80b3ccb4395922c4351d4bd2f366003db Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 18:19:34 +0100 Subject: fixed compile --- src/AllocationPool.h | 2 +- tests/ChunkData/CopyBlocks.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index e4f1427f6..f73b32601 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -19,7 +19,7 @@ class cAllocationPool { cAllocationPool(std::auto_ptr a_Callbacks) : m_Callbacks(a_Callbacks) { - for(int i = 0; i < NumElementsInReserve; i++) + for(size_t i = 0; i < NumElementsInReserve; i++) { void * space = malloc(sizeof(T)); if (space == NULL) diff --git a/tests/ChunkData/CopyBlocks.cpp b/tests/ChunkData/CopyBlocks.cpp index be8cab234..7f8f66c4d 100644 --- a/tests/ChunkData/CopyBlocks.cpp +++ b/tests/ChunkData/CopyBlocks.cpp @@ -17,7 +17,15 @@ int main(int argc, char ** argv) { // Set up a cChunkData with known contents - all blocks 0x01, all metas 0x02: - cChunkData Data; + class cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks + { + virtual void OnStartingUsingBuffer() {} + virtual void OnStopUsingBuffer() {} + virtual void OnBufferEmpty() {} + }; + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cChunkData Data(Pool); cChunkDef::BlockTypes BlockTypes; cChunkDef::BlockNibbles BlockMetas; memset(BlockTypes, 0x01, sizeof(BlockTypes)); -- cgit v1.2.3 From 0310a50192fcbd4a9e5ec03c98a0d7f33457df54 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 18:43:36 +0100 Subject: fixed spaces --- src/AllocationPool.h | 2 +- src/ChunkMap.cpp | 2 +- src/ChunkMap.h | 8 ++++---- tests/ChunkData/ArraytoCoord.cpp | 4 ++-- tests/ChunkData/Coordinates.cpp | 4 ++-- tests/ChunkData/Copies.cpp | 4 ++-- tests/ChunkData/CopyBlocks.cpp | 4 ++-- tests/ChunkData/creatable.cpp | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index f73b32601..9144c2eac 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -19,7 +19,7 @@ class cAllocationPool { cAllocationPool(std::auto_ptr a_Callbacks) : m_Callbacks(a_Callbacks) { - for(size_t i = 0; i < NumElementsInReserve; i++) + for (size_t i = 0; i < NumElementsInReserve; i++) { void * space = malloc(sizeof(T)); if (space == NULL) diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 586d7a65e..862d0b7ec 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -35,7 +35,7 @@ cChunkMap::cChunkMap(cWorld * a_World ) : m_World( a_World ), - m_Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())) + m_Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())) { } diff --git a/src/ChunkMap.h b/src/ChunkMap.h index efe489854..071921f92 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; + cAllocationPool 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 diff --git a/tests/ChunkData/ArraytoCoord.cpp b/tests/ChunkData/ArraytoCoord.cpp index 80bcc5283..3f22d239a 100644 --- a/tests/ChunkData/ArraytoCoord.cpp +++ b/tests/ChunkData/ArraytoCoord.cpp @@ -7,13 +7,13 @@ int main(int argc, char** argv) { class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() {} virtual void OnStopUsingBuffer() {} virtual void OnBufferEmpty() {} }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); { // Test first segment diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index d5abbb143..66fab46ab 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -7,13 +7,13 @@ int main(int argc, char** argv) { class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() {} virtual void OnStopUsingBuffer() {} virtual void OnBufferEmpty() {} }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); { cChunkData buffer(Pool); diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp index 6f5d40792..4a672380f 100644 --- a/tests/ChunkData/Copies.cpp +++ b/tests/ChunkData/Copies.cpp @@ -7,13 +7,13 @@ int main(int argc, char** argv) { class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() {} virtual void OnStopUsingBuffer() {} virtual void OnBufferEmpty() {} }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); { cChunkData buffer(Pool); diff --git a/tests/ChunkData/CopyBlocks.cpp b/tests/ChunkData/CopyBlocks.cpp index 7f8f66c4d..db3d391c3 100644 --- a/tests/ChunkData/CopyBlocks.cpp +++ b/tests/ChunkData/CopyBlocks.cpp @@ -18,13 +18,13 @@ int main(int argc, char ** argv) { // Set up a cChunkData with known contents - all blocks 0x01, all metas 0x02: class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() {} virtual void OnStopUsingBuffer() {} virtual void OnBufferEmpty() {} }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); cChunkData Data(Pool); cChunkDef::BlockTypes BlockTypes; cChunkDef::BlockNibbles BlockMetas; diff --git a/tests/ChunkData/creatable.cpp b/tests/ChunkData/creatable.cpp index 0dde8cf3b..2bb61b7ce 100644 --- a/tests/ChunkData/creatable.cpp +++ b/tests/ChunkData/creatable.cpp @@ -5,13 +5,13 @@ int main(int argc, char** argv) { class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + : public cAllocationPool::cStarvationCallbacks { virtual void OnStartingUsingBuffer() {} virtual void OnStopUsingBuffer() {} virtual void OnBufferEmpty() {} }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); cChunkData buffer(Pool); return 0; } -- cgit v1.2.3 From 6b99e556462fdbdf6a265c014fb03f070b0f1b25 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 19:05:02 +0100 Subject: fixed spaces --- src/AllocationPool.h | 11 ++++++----- src/Chunk.cpp | 2 +- src/Chunk.h | 2 +- src/ChunkData.cpp | 2 +- src/ChunkData.h | 7 ++++--- src/ChunkMap.cpp | 2 +- src/ChunkMap.h | 2 +- tests/ChunkData/Coordinates.cpp | 2 +- 8 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index 9144c2eac..9bb44ff1f 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -4,7 +4,8 @@ #include template -class cAllocationPool { +class cAllocationPool +{ public: class cStarvationCallbacks @@ -17,7 +18,7 @@ class cAllocationPool { }; cAllocationPool(std::auto_ptr a_Callbacks) : - m_Callbacks(a_Callbacks) + m_Callbacks(a_Callbacks) { for (size_t i = 0; i < NumElementsInReserve; i++) { @@ -40,7 +41,7 @@ class cAllocationPool { } } - T* Allocate() + T * Allocate() { if (m_FreeList.size() <= NumElementsInReserve) { @@ -61,11 +62,11 @@ class cAllocationPool { } } // placement new, used to initalize the object - T* ret = new (m_FreeList.front()) T; + T * ret = new (m_FreeList.front()) T; m_FreeList.pop_front(); return ret; } - void Free(T* ptr) + void Free(T * ptr) { if (ptr == NULL) { diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 64a5660d8..a0a397a08 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 7309fd022..07f597d59 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 d2903cff1..f9c263d88 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 aaefc4575..9dee78ad0 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 @@ -96,7 +96,8 @@ public: Allows a_Src to be NULL, in which case it doesn't do anything. */ void SetSkyLight(const NIBBLETYPE * a_Src); - struct sChunkSection { + struct sChunkSection + { BLOCKTYPE m_BlockTypes [SectionHeight * 16 * 16] ; NIBBLETYPE m_BlockMetas [SectionHeight * 16 * 16 / 2]; NIBBLETYPE m_BlockLight [SectionHeight * 16 * 16 / 2]; @@ -121,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 862d0b7ec..704c4f823 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -2672,7 +2672,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 071921f92..08156e7e6 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 */ diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index 66fab46ab..1ac600f82 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -144,7 +144,7 @@ int main(int argc, char** argv) #else copy = std::move(copy); #endif - testassert(copy.GetBlock(0,0,0) == 0x42); + testassert(copy.GetBlock(0, 0, 0) == 0x42); } return 0; -- cgit v1.2.3 From a1520c7a406b74d20da674426444d538fff56fac Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 19:06:54 +0100 Subject: reverted accedental android changes --- Android/.classpath | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Android/.classpath b/Android/.classpath index 7bc01d9a9..a4763d1ee 100644 --- a/Android/.classpath +++ b/Android/.classpath @@ -3,7 +3,6 @@ - - + -- cgit v1.2.3 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 +++---- tests/ChunkData/ArraytoCoord.cpp | 21 +++++++++++------- tests/ChunkData/Coordinates.cpp | 19 +++++++++++------ tests/ChunkData/Copies.cpp | 21 +++++++++++------- tests/ChunkData/CopyBlocks.cpp | 21 +++++++++++------- tests/ChunkData/creatable.cpp | 21 +++++++++++------- 12 files changed, 110 insertions(+), 69 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 +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 diff --git a/tests/ChunkData/ArraytoCoord.cpp b/tests/ChunkData/ArraytoCoord.cpp index 3f22d239a..9d0ca6c8c 100644 --- a/tests/ChunkData/ArraytoCoord.cpp +++ b/tests/ChunkData/ArraytoCoord.cpp @@ -6,14 +6,19 @@ int main(int argc, char** argv) { - class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks - { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} - }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + class cMockAllocationPool + : public cAllocationPool + { + virtual cChunkData::sChunkSection * Allocate() + { + return new cChunkData::sChunkSection(); + } + + virtual void Free(cChunkData::sChunkSection * a_Ptr) + { + delete a_Ptr; + } + } Pool; { // Test first segment diff --git a/tests/ChunkData/Coordinates.cpp b/tests/ChunkData/Coordinates.cpp index 1ac600f82..b3c66dde5 100644 --- a/tests/ChunkData/Coordinates.cpp +++ b/tests/ChunkData/Coordinates.cpp @@ -6,14 +6,19 @@ int main(int argc, char** argv) { - class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks + class cMockAllocationPool + : public cAllocationPool { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} - }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + virtual cChunkData::sChunkSection * Allocate() + { + return new cChunkData::sChunkSection(); + } + + virtual void Free(cChunkData::sChunkSection * a_Ptr) + { + delete a_Ptr; + } + } Pool; { cChunkData buffer(Pool); diff --git a/tests/ChunkData/Copies.cpp b/tests/ChunkData/Copies.cpp index 4a672380f..440819e91 100644 --- a/tests/ChunkData/Copies.cpp +++ b/tests/ChunkData/Copies.cpp @@ -6,14 +6,19 @@ int main(int argc, char** argv) { - class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks - { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} - }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + class cMockAllocationPool + : public cAllocationPool + { + virtual cChunkData::sChunkSection * Allocate() + { + return new cChunkData::sChunkSection(); + } + + virtual void Free(cChunkData::sChunkSection * a_Ptr) + { + delete a_Ptr; + } + } Pool; { cChunkData buffer(Pool); diff --git a/tests/ChunkData/CopyBlocks.cpp b/tests/ChunkData/CopyBlocks.cpp index db3d391c3..ec9451099 100644 --- a/tests/ChunkData/CopyBlocks.cpp +++ b/tests/ChunkData/CopyBlocks.cpp @@ -17,14 +17,19 @@ int main(int argc, char ** argv) { // Set up a cChunkData with known contents - all blocks 0x01, all metas 0x02: - class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks - { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} - }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + class cMockAllocationPool + : public cAllocationPool + { + virtual cChunkData::sChunkSection * Allocate() + { + return new cChunkData::sChunkSection(); + } + + virtual void Free(cChunkData::sChunkSection * a_Ptr) + { + delete a_Ptr; + } + } Pool; cChunkData Data(Pool); cChunkDef::BlockTypes BlockTypes; cChunkDef::BlockNibbles BlockMetas; diff --git a/tests/ChunkData/creatable.cpp b/tests/ChunkData/creatable.cpp index 2bb61b7ce..fc786f688 100644 --- a/tests/ChunkData/creatable.cpp +++ b/tests/ChunkData/creatable.cpp @@ -4,14 +4,19 @@ int main(int argc, char** argv) { - class cStarvationCallbacks - : public cAllocationPool::cStarvationCallbacks - { - virtual void OnStartingUsingBuffer() {} - virtual void OnStopUsingBuffer() {} - virtual void OnBufferEmpty() {} - }; - cAllocationPool Pool(std::auto_ptr::cStarvationCallbacks>(new cStarvationCallbacks())); + class cMockAllocationPool + : public cAllocationPool + { + virtual cChunkData::sChunkSection * Allocate() + { + return new cChunkData::sChunkSection(); + } + + virtual void Free(cChunkData::sChunkSection * a_Ptr) + { + delete a_Ptr; + } + } Pool; cChunkData buffer(Pool); return 0; } -- cgit v1.2.3 From 3efbdb1c9b8f83ecb28992282c08aa065e21e1f8 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 19:54:09 +0100 Subject: Moved m_Sections --- src/ChunkData.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ChunkData.h b/src/ChunkData.h index e3b36c581..fe8b068a2 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -111,6 +111,8 @@ private: #endif sChunkSection * m_Sections[NumSections]; + + cAllocationPool & m_Pool; /** Allocates a new section. Entry-point to custom allocators. */ sChunkSection * Allocate(void); @@ -122,7 +124,6 @@ private: /** Sets the data in the specified section to their default values. */ void ZeroSection(sChunkSection * a_Section) const; - cAllocationPool & m_Pool; }; -- cgit v1.2.3 From f97116141f464d4e27515197eec7d42f9d2ba772 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 19:56:01 +0100 Subject: Reformated ChunkMap.h --- src/ChunkMap.cpp | 7 +++++-- src/ChunkMap.h | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index 3b946f9ec..d2ccca94e 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -2677,8 +2677,11 @@ 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) +cChunkMap::cChunkLayer::cChunkLayer( + int a_LayerX, int a_LayerZ, + cChunkMap * a_Parent, + 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 c1dc743e5..0d6f1f80a 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -351,8 +351,11 @@ private: class cChunkLayer { public: - cChunkLayer(int a_LayerX, int a_LayerZ, cChunkMap * a_Parent, - cAllocationPool & a_Pool); + cChunkLayer( + int a_LayerX, int a_LayerZ, + cChunkMap * a_Parent, + cAllocationPool & a_Pool + ); ~cChunkLayer(); /** Always returns an assigned chunkptr, but the chunk needn't be valid (loaded / generated) - callers must check */ -- cgit v1.2.3 From 2643a46c69d7d1354ec324a274dba4ccca315f3c Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 20:04:35 +0100 Subject: Documented cAllocationPool --- src/AllocationPool.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index 88bc132e9..cac5689b9 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -18,10 +18,15 @@ public: virtual ~cAllocationPool() {} + /** Allocates a pointer to T **/ virtual T * Allocate() = 0; + + /** Frees the pointer passed in a_ptr, invalidating it **/ virtual void Free(T * a_ptr) = 0; }; +/** Allocates memory storing unused elements in a linked list. Keeps at least NumElementsInReserve + elements in the list unless malloc fails so that the program has a reserve to handle OOM.**/ template class cListAllocationPool : public cAllocationPool { -- cgit v1.2.3 From 41e5a64ff8f268f2cb0278e691f300e3a1790388 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 20:07:17 +0100 Subject: Documented starvation callbacks --- src/AllocationPool.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index cac5689b9..b7d0bb506 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -11,8 +11,15 @@ public: { public: virtual ~cStarvationCallbacks() {} + + /** Is called when the reserve buffer starts to be used **/ virtual void OnStartingUsingBuffer() = 0; + + /** Is called once the reserve buffer has returned to normal size **/ virtual void OnStopUsingBuffer() = 0; + + /** Is called when the allocation pool is unable to allocate memory. Will be repeatedly + called if it does not free sufficient memory **/ virtual void OnBufferEmpty() = 0; }; -- cgit v1.2.3 From 6ddcf42409b0460091abf0bb94b01043dd1b112b Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 20:10:49 +0100 Subject: Removed spaces --- src/AllocationPool.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index b7d0bb506..9c833302f 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -19,7 +19,7 @@ public: virtual void OnStopUsingBuffer() = 0; /** Is called when the allocation pool is unable to allocate memory. Will be repeatedly - called if it does not free sufficient memory **/ + called if it does not free sufficient memory **/ virtual void OnBufferEmpty() = 0; }; @@ -33,7 +33,7 @@ public: }; /** Allocates memory storing unused elements in a linked list. Keeps at least NumElementsInReserve - elements in the list unless malloc fails so that the program has a reserve to handle OOM.**/ +elements in the list unless malloc fails so that the program has a reserve to handle OOM.**/ template class cListAllocationPool : public cAllocationPool { -- cgit v1.2.3 From 8a2aa6cb169563c7df752821e032b1cab1bac5d1 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 14 Jun 2014 20:24:52 +0100 Subject: Changed names of callbacks --- src/AllocationPool.h | 14 +++++++------- src/ChunkMap.h | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/AllocationPool.h b/src/AllocationPool.h index 9c833302f..5d749a79e 100644 --- a/src/AllocationPool.h +++ b/src/AllocationPool.h @@ -13,14 +13,14 @@ public: virtual ~cStarvationCallbacks() {} /** Is called when the reserve buffer starts to be used **/ - virtual void OnStartingUsingBuffer() = 0; + virtual void OnStartUsingReserve() = 0; /** Is called once the reserve buffer has returned to normal size **/ - virtual void OnStopUsingBuffer() = 0; + virtual void OnEndUsingReserve() = 0; /** Is called when the allocation pool is unable to allocate memory. Will be repeatedly called if it does not free sufficient memory **/ - virtual void OnBufferEmpty() = 0; + virtual void OnOutOfReserve() = 0; }; virtual ~cAllocationPool() {} @@ -47,7 +47,7 @@ class cListAllocationPool : public cAllocationPool void * space = malloc(sizeof(T)); if (space == NULL) { - m_Callbacks->OnStartingUsingBuffer(); + m_Callbacks->OnStartUsingReserve(); break; } m_FreeList.push_front(space); @@ -74,11 +74,11 @@ class cListAllocationPool : public cAllocationPool } else if (m_FreeList.size() == NumElementsInReserve) { - m_Callbacks->OnStartingUsingBuffer(); + m_Callbacks->OnStartUsingReserve(); } else if (m_FreeList.empty()) { - m_Callbacks->OnBufferEmpty(); + m_Callbacks->OnOutOfReserve(); // Try again until the memory is avalable return Allocate(); } @@ -99,7 +99,7 @@ class cListAllocationPool : public cAllocationPool m_FreeList.push_front(a_ptr); if (m_FreeList.size() == NumElementsInReserve) { - m_Callbacks->OnStopUsingBuffer(); + m_Callbacks->OnEndUsingReserve(); } } diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 0d6f1f80a..fd319fbc9 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -406,15 +406,15 @@ private: class cStarvationCallbacks : public cAllocationPool::cStarvationCallbacks { - virtual void OnStartingUsingBuffer() + virtual void OnStartUsingReserve() { LOG("Using backup memory buffer"); } - virtual void OnStopUsingBuffer() + virtual void OnEndUsingReserve() { LOG("Stoped using backup memory buffer"); } - virtual void OnBufferEmpty() + virtual void OnOutOfReserve() { LOG("Out of Memory"); } -- cgit v1.2.3 From 1e57161ecc09ca9aa009b5c4795113aae95d89ea Mon Sep 17 00:00:00 2001 From: Tycho Date: Mon, 16 Jun 2014 14:12:10 +0100 Subject: Added override --- src/ChunkMap.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ChunkMap.h b/src/ChunkMap.h index fd319fbc9..5aad0dd2a 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -406,15 +406,15 @@ private: class cStarvationCallbacks : public cAllocationPool::cStarvationCallbacks { - virtual void OnStartUsingReserve() + virtual void OnStartUsingReserve() override { LOG("Using backup memory buffer"); } - virtual void OnEndUsingReserve() + virtual void OnEndUsingReserve() override { LOG("Stoped using backup memory buffer"); } - virtual void OnOutOfReserve() + virtual void OnOutOfReserve() override { LOG("Out of Memory"); } -- cgit v1.2.3