Skip to content

Commit

Permalink
WorldRegions memory management update
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 27, 2023
1 parent 5d4e67f commit 69a17c2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
11 changes: 4 additions & 7 deletions src/files/WorldFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,21 @@ WorldFiles::WorldFiles(fs::path directory, const DebugSettings& settings)

WorldFiles::~WorldFiles(){
delete[] compressionBuffer;
for (auto it : regions){
delete it.second;
}
regions.clear();
}

WorldRegion* WorldFiles::getRegion(regionsmap& regions, int x, int z) {
auto found = regions.find(glm::ivec2(x, z));
if (found == regions.end())
return nullptr;
return found->second;
return found->second.get();
}

WorldRegion* WorldFiles::getOrCreateRegion(regionsmap& regions, int x, int z) {
WorldRegion* region = getRegion(regions, x, z);
if (region == nullptr) {
region = new WorldRegion();
regions[glm::ivec2(x, z)] = region;
regions[glm::ivec2(x, z)].reset(region);
}
return region;
}
Expand Down Expand Up @@ -354,8 +351,8 @@ void WorldFiles::writeRegion(int x, int z, WorldRegion* entry, fs::path folder,
}

void WorldFiles::writeRegions(regionsmap& regions, const fs::path& folder, int layer) {
for (auto it : regions){
WorldRegion* region = it.second;
for (auto& it : regions){
WorldRegion* region = it.second.get();
if (region->getChunks() == nullptr || !region->isUnsaved())
continue;
glm::ivec2 key = it.first;
Expand Down
2 changes: 1 addition & 1 deletion src/files/WorldFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class WorldRegion {
uint32_t* getSizes() const;
};

typedef std::unordered_map<glm::ivec2, WorldRegion*> regionsmap;
typedef std::unordered_map<glm::ivec2, std::unique_ptr<WorldRegion>> regionsmap;
class WorldFiles {
std::unordered_map<glm::ivec3, std::unique_ptr<files::rafile>> openRegFiles;

Expand Down

0 comments on commit 69a17c2

Please sign in to comment.