From 20f3cb34aec0cddfe824d9cdc6696cf15b62c28b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 26 May 2023 13:17:13 +0200 Subject: Anvil: Refactored to use shared_ptr. --- src/WorldStorage/WSSAnvil.cpp | 18 +++++++----------- src/WorldStorage/WSSAnvil.h | 9 ++++++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 95ba1e4bf..dec05f351 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -123,10 +123,7 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor): cWSSAnvil::~cWSSAnvil() { cCSLock Lock(m_CS); - for (cMCAFiles::iterator itr = m_Files.begin(); itr != m_Files.end(); ++itr) - { - delete *itr; - } // for itr - m_Files[] + m_Files.clear(); } @@ -227,7 +224,7 @@ void cWSSAnvil::ChunkLoadFailed(const cChunkCoords a_ChunkCoords, const AString bool cWSSAnvil::GetChunkData(const cChunkCoords & a_Chunk, ContiguousByteBuffer & a_Data) { cCSLock Lock(m_CS); - cMCAFile * File = LoadMCAFile(a_Chunk); + auto File = LoadMCAFile(a_Chunk); if (File == nullptr) { return false; @@ -242,7 +239,7 @@ bool cWSSAnvil::GetChunkData(const cChunkCoords & a_Chunk, ContiguousByteBuffer bool cWSSAnvil::SetChunkData(const cChunkCoords & a_Chunk, const ContiguousByteBufferView a_Data) { cCSLock Lock(m_CS); - cMCAFile * File = LoadMCAFile(a_Chunk); + auto File = LoadMCAFile(a_Chunk); if (File == nullptr) { return false; @@ -254,7 +251,7 @@ bool cWSSAnvil::SetChunkData(const cChunkCoords & a_Chunk, const ContiguousByteB -cWSSAnvil::cMCAFile * cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) +std::shared_ptr cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) { // ASSUME m_CS is locked ASSERT(m_CS.IsLocked()); @@ -267,12 +264,12 @@ cWSSAnvil::cMCAFile * cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) ASSERT(a_Chunk.m_ChunkZ - RegionZ * 32 < 32); // Is it already cached? - for (cMCAFiles::iterator itr = m_Files.begin(); itr != m_Files.end(); ++itr) + for (auto itr = m_Files.begin(); itr != m_Files.end(); ++itr) { if (((*itr) != nullptr) && ((*itr)->GetRegionX() == RegionX) && ((*itr)->GetRegionZ() == RegionZ)) { // Move the file to front and return it: - cMCAFile * f = *itr; + auto f = *itr; if (itr != m_Files.begin()) { m_Files.erase(itr); @@ -286,7 +283,7 @@ cWSSAnvil::cMCAFile * cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) auto FileName = fmt::format(FMT_STRING("{}{}region"), m_World->GetDataPath(), cFile::PathSeparator()); cFile::CreateFolder(FileName); FileName.append(fmt::format(FMT_STRING("/r.{}.{}.mca"), RegionX, RegionZ)); - cMCAFile * f = new cMCAFile(*this, FileName, RegionX, RegionZ); + auto f = std::make_shared(*this, FileName, RegionX, RegionZ); if (f == nullptr) { return nullptr; @@ -296,7 +293,6 @@ cWSSAnvil::cMCAFile * cWSSAnvil::LoadMCAFile(const cChunkCoords & a_Chunk) // If there are too many MCA files cached, delete the last one used: if (m_Files.size() > MAX_MCA_FILES) { - delete m_Files.back(); m_Files.pop_back(); } return f; diff --git a/src/WorldStorage/WSSAnvil.h b/src/WorldStorage/WSSAnvil.h index 69006ec95..e0c9a9320 100644 --- a/src/WorldStorage/WSSAnvil.h +++ b/src/WorldStorage/WSSAnvil.h @@ -83,10 +83,13 @@ protected: /** Opens a MCA file either for a Read operation (fails if doesn't exist) or for a Write operation (creates new if not found) */ bool OpenFile(bool a_IsForReading); } ; - typedef std::list cMCAFiles; + /** Protects m_Files against multithreaded access. */ cCriticalSection m_CS; - cMCAFiles m_Files; // a MRU cache of MCA files + + /** A MRU cache of MCA files. + Protected against multithreaded access by m_CS. */ + std::list> m_Files; Compression::Extractor m_Extractor; Compression::Compressor m_Compressor; @@ -291,7 +294,7 @@ protected: bool GetBlockEntityNBTPos(const cParsedNBT & a_NBT, int a_TagIdx, Vector3i & a_AbsPos); /** Gets the correct MCA file either from cache or from disk, manages the m_MCAFiles cache; assumes m_CS is locked */ - cMCAFile * LoadMCAFile(const cChunkCoords & a_Chunk); + std::shared_ptr LoadMCAFile(const cChunkCoords & a_Chunk); // cWSSchema overrides: virtual bool LoadChunk(const cChunkCoords & a_Chunk) override; -- cgit v1.2.3