diff options
author | Mattes D <github@xoft.cz> | 2023-05-26 13:17:13 +0200 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2023-05-26 23:00:17 +0200 |
commit | 20f3cb34aec0cddfe824d9cdc6696cf15b62c28b (patch) | |
tree | 50e0e6fb485344086cef7ae35ddf6e05cc9c8499 /src/WorldStorage | |
parent | Anvil: Allow loading chunks without HeightMap. (diff) | |
download | cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar.gz cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar.bz2 cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar.lz cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar.xz cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.tar.zst cuberite-20f3cb34aec0cddfe824d9cdc6696cf15b62c28b.zip |
Diffstat (limited to 'src/WorldStorage')
-rw-r--r-- | src/WorldStorage/WSSAnvil.cpp | 18 | ||||
-rw-r--r-- | 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::cMCAFile> 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<cMCAFile>(*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<cMCAFile *> 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<std::shared_ptr<cMCAFile>> 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<cMCAFile> LoadMCAFile(const cChunkCoords & a_Chunk); // cWSSchema overrides: virtual bool LoadChunk(const cChunkCoords & a_Chunk) override; |