From 47ace554ccfdf870d38d1af75fc01a912e220c7b Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 14 Sep 2014 11:47:17 +0200 Subject: [PATCH] Making use of shared_ptr to track blocks. --- examples/Paging/main.cpp | 4 ++-- .../include/PolyVoxCore/FilePager.h | 4 ++-- .../include/PolyVoxCore/LargeVolume.h | 6 +++--- .../include/PolyVoxCore/LargeVolume.inl | 19 ++++++++++--------- .../PolyVoxCore/LargeVolumeSampler.inl | 2 +- .../PolyVoxCore/include/PolyVoxCore/Pager.h | 6 ++++-- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index a01779fb..04d20067 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -90,7 +90,7 @@ public: /// Destructor virtual ~PerlinNoisePager() {}; - virtual void pageIn(const PolyVox::Region& region, UncompressedBlock* pBlockData) + virtual void pageIn(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock > pBlockData) { // FIXME - this isn't a great example... it's a shame we have to hard clode the block size and also create/destroy // a compressor each time. These could at least be moved outside somewhere if we can't fix it in a better way... @@ -143,7 +143,7 @@ public: //delete compressor; } - virtual void pageOut(const PolyVox::Region& region, UncompressedBlock* /*pBlockData*/) + virtual void pageOut(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock > /*pBlockData*/) { std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h index 8e3e36ed..8da3adcf 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h @@ -69,7 +69,7 @@ namespace PolyVox m_vecCreatedFiles.clear(); } - virtual void pageIn(const Region& region, UncompressedBlock* pBlockData) + virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page in NULL block"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); @@ -113,7 +113,7 @@ namespace PolyVox } } - virtual void pageOut(const Region& region, UncompressedBlock* pBlockData) + virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page out NULL block"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index 57e90120..ca4f60d6 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -300,7 +300,7 @@ namespace PolyVox } }; - typedef std::map*, BlockPositionCompare> UncompressedBlockMap; + typedef std::map >, BlockPositionCompare> UncompressedBlockMap; void ensureUncompressedBlockMapHasFreeSpace(void) const; @@ -314,7 +314,7 @@ namespace PolyVox VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType, VoxelType tBorder) const; VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType, VoxelType tBorder) const; - UncompressedBlock* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; + std::shared_ptr< UncompressedBlock > getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; void eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const; @@ -323,7 +323,7 @@ namespace PolyVox mutable uint32_t m_uTimestamper; mutable Vector3DInt32 m_v3dLastAccessedBlockPos; - mutable UncompressedBlock* m_pLastAccessedBlock; + mutable std::shared_ptr< UncompressedBlock > m_pLastAccessedBlock; uint32_t m_uMaxNumberOfUncompressedBlocks; // The size of the volume diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index a7bd07a7..4cbeb632 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -186,7 +186,7 @@ namespace PolyVox const uint16_t yOffset = static_cast(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast(uZPos - (blockZ << m_uBlockSideLengthPower)); - const UncompressedBlock* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); + auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset); } @@ -262,7 +262,7 @@ namespace PolyVox const uint16_t yOffset = static_cast(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast(uZPos - (blockZ << m_uBlockSideLengthPower)); - UncompressedBlock* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); + auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue); } @@ -299,7 +299,7 @@ namespace PolyVox const uint16_t yOffset = static_cast(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast(uZPos - (blockZ << m_uBlockSideLengthPower)); - UncompressedBlock* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); + auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue); @@ -486,7 +486,7 @@ namespace PolyVox template void LargeVolume::eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const { - UncompressedBlock* pUncompressedBlock = itUncompressedBlock->second; + std::shared_ptr< UncompressedBlock > pUncompressedBlock = itUncompressedBlock->second; // This should never happen as blocks are deleted based on being least recently used. // I the case that we are flushing we delete all blocks, but the flush function will @@ -511,14 +511,14 @@ namespace PolyVox pUncompressedBlock->m_bDataModified = false; } - delete itUncompressedBlock->second; + //delete itUncompressedBlock->second; // We can now remove the block data from memory. m_pBlocks.erase(itUncompressedBlock); } template - UncompressedBlock* LargeVolume::getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const + std::shared_ptr< UncompressedBlock > LargeVolume::getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const { Vector3DInt32 v3dBlockPos(uBlockX, uBlockY, uBlockZ); @@ -531,7 +531,7 @@ namespace PolyVox return m_pLastAccessedBlock; } - UncompressedBlock* pUncompressedBlock = 0; + std::shared_ptr< UncompressedBlock > pUncompressedBlock = nullptr; typename UncompressedBlockMap::iterator itUncompressedBlock = m_pBlocks.find(v3dBlockPos); // check whether the block is already loaded @@ -546,7 +546,8 @@ namespace PolyVox ensureUncompressedBlockMapHasFreeSpace(); // We can now create a new block. - pUncompressedBlock = new UncompressedBlock(m_uBlockSideLength); + //pUncompressedBlock = new UncompressedBlock(m_uBlockSideLength); + pUncompressedBlock = std::make_shared< UncompressedBlock >(m_uBlockSideLength); // An uncompressed bock is always backed by a compressed one, and this is created by getCompressedBlock() if it doesn't // already exist. If it does already exist and has data then we bring this across into the ucompressed version. @@ -677,7 +678,7 @@ namespace PolyVox const uint16_t yOffset = static_cast(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast(uZPos - (blockZ << m_uBlockSideLengthPower)); - UncompressedBlock* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); + auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset); } } diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl index c5e4e12c..df07bb77 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl @@ -119,7 +119,7 @@ namespace PolyVox uYPosInBlock * this->mVolume->m_uBlockSideLength + uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; - UncompressedBlock* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); + auto pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); mCurrentVoxel = pUncompressedCurrentBlock->m_tData + uVoxelIndexInBlock; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Pager.h b/library/PolyVoxCore/include/PolyVoxCore/Pager.h index c11d8afd..0970c943 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Pager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Pager.h @@ -27,6 +27,8 @@ freely, subject to the following restrictions: #include "PolyVoxCore/UncompressedBlock.h" #include "PolyVoxCore/Impl/TypeDef.h" +#include + namespace PolyVox { /** @@ -41,8 +43,8 @@ namespace PolyVox /// Destructor virtual ~Pager() {}; - virtual void pageIn(const Region& region, UncompressedBlock* pBlockData) = 0; - virtual void pageOut(const Region& region, UncompressedBlock* pBlockData) = 0; + virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) = 0; + virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) = 0; }; }