summaryrefslogtreecommitdiffstats
path: root/src/ChunkData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ChunkData.cpp')
-rw-r--r--src/ChunkData.cpp48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp
index 79029f0cf..1bc3072c2 100644
--- a/src/ChunkData.cpp
+++ b/src/ChunkData.cpp
@@ -6,11 +6,12 @@
#endif
#include "ChunkData.h"
-cChunkData::cChunkData()
+cChunkData::cChunkData(cAllocationPool<cChunkData::sChunkSection, 1600>& 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));
}
@@ -34,7 +35,8 @@ cChunkData::~cChunkData()
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
cChunkData::cChunkData(const cChunkData& other) :
- IsOwner(true)
+ IsOwner(true),
+ m_Pool(other.m_Pool)
{
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
@@ -60,13 +62,15 @@ cChunkData::~cChunkData()
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(cChunkData&& other)
+ cChunkData::cChunkData(cChunkData&& other) :
+ m_Pool(other.m_Pool)
{
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
@@ -79,6 +83,7 @@ cChunkData::~cChunkData()
{
if (&other != this)
{
+ ASSERT(&m_Pool == &other.m_Pool);
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
Free(m_Sections[i]);;
@@ -236,8 +241,8 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
cChunkData cChunkData::Copy() const
{
- cChunkData copy;
- for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
+ cChunkData copy(m_Pool);
+ for (int i = 0; i < CHUNK_SECTION_NUM; i++)
{
if (m_Sections[i] != NULL)
{
@@ -427,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_COUNT; i++)
+ for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
{
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
if (m_Sections[i] != NULL)
@@ -440,9 +445,6 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src)
}
else
{
- // j counts how many of leading zeros the buffer has
- // if j == segment_length then the buffer is all zeros so there is no point
- // creating the buffer.
size_t j = 0;
// do nothing whilst 0
for (; j < segment_length && a_src[i * segment_length + j] == 0; j++);
@@ -470,6 +472,11 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src)
sizeof(m_Sections[i]->m_BlockSkyLight)
);
}
+ else
+ {
+ Free(m_Sections[i]);
+ m_Sections[i] = 0;
+ }
}
}
}
@@ -477,10 +484,10 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src)
-void cChunkData::SetBlockLight(const NIBBLETYPE * a_src)
+void cChunkData::SetLight(const NIBBLETYPE * a_src)
{
if (!a_src) return;
- for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
+ for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
{
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
if (m_Sections[i] != NULL)
@@ -493,9 +500,6 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_src)
}
else
{
- // j counts how many of leading zeros the buffer has
- // if j == segment_length then the buffer is all zeros so there is no point
- // creating the buffer.
size_t j = 0;
// do nothing whilst 0
for (; j < segment_length && a_src[i * segment_length + j] == 0; j++);
@@ -523,6 +527,11 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_src)
sizeof(m_Sections[i]->m_BlockSkyLight)
);
}
+ else
+ {
+ Free(m_Sections[i]);
+ m_Sections[i] = 0;
+ }
}
}
}
@@ -533,7 +542,7 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_src)
void cChunkData::SetSkyLight (const NIBBLETYPE * a_src)
{
if (!a_src) return;
- for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
+ for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
{
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
if (m_Sections[i] != NULL)
@@ -546,9 +555,6 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src)
}
else
{
- // j counts how many of leading zeros the buffer has
- // if j == segment_length then the buffer is all zeros so there is no point
- // creating the buffer.
size_t j = 0;
// do nothing whilst 0
for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++);
@@ -587,14 +593,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);
}