From fd87fbe7f0971aab5e8cd184612644b787114662 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 28 May 2024 03:52:13 +0300 Subject: [PATCH] memory-related refactor --- src/files/WorldRegions.cpp | 28 +++++++++++----------------- src/files/WorldRegions.hpp | 6 +++--- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/files/WorldRegions.cpp b/src/files/WorldRegions.cpp index 55f3b3d2..3d2af8c4 100644 --- a/src/files/WorldRegions.cpp +++ b/src/files/WorldRegions.cpp @@ -50,16 +50,11 @@ std::unique_ptr regfile::read(int index, uint32_t& length) { } WorldRegion::WorldRegion() { - chunksData = new ubyte*[REGION_CHUNKS_COUNT]{}; - sizes = new uint32_t[REGION_CHUNKS_COUNT]{}; + chunksData = std::make_unique[]>(REGION_CHUNKS_COUNT); + sizes = std::make_unique(REGION_CHUNKS_COUNT); } WorldRegion::~WorldRegion() { - for (uint i = 0; i < REGION_CHUNKS_COUNT; i++) { - delete[] chunksData[i]; - } - delete[] sizes; - delete[] chunksData; } void WorldRegion::setUnsaved(bool unsaved) { @@ -69,23 +64,22 @@ bool WorldRegion::isUnsaved() const { return unsaved; } -ubyte** WorldRegion::getChunks() const { - return chunksData; +std::unique_ptr* WorldRegion::getChunks() const { + return chunksData.get(); } uint32_t* WorldRegion::getSizes() const { - return sizes; + return sizes.get(); } void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) { size_t chunk_index = z * REGION_SIZE + x; - delete[] chunksData[chunk_index]; - chunksData[chunk_index] = data; + chunksData[chunk_index].reset(data); sizes[chunk_index] = size; } ubyte* WorldRegion::getChunkData(uint x, uint z) { - return chunksData[z * REGION_SIZE + x]; + return chunksData[z * REGION_SIZE + x].get(); } uint WorldRegion::getChunkDataSize(uint x, uint z) { @@ -165,14 +159,14 @@ std::unique_ptr WorldRegions::readChunkData( /// @brief Read missing chunks data (null pointers) from region file void WorldRegions::fetchChunks(WorldRegion* region, int x, int z, regfile* file) { - ubyte** chunks = region->getChunks(); + auto* chunks = region->getChunks(); uint32_t* sizes = region->getSizes(); for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + z * REGION_SIZE; if (chunks[i] == nullptr) { - chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], file).release(); + chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], file); } } } @@ -294,11 +288,11 @@ void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){ char intbuf[4]{}; uint offsets[REGION_CHUNKS_COUNT]{}; - ubyte** region = entry->getChunks(); + auto* region = entry->getChunks(); uint32_t* sizes = entry->getSizes(); for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { - ubyte* chunk = region[i]; + ubyte* chunk = region[i].get(); if (chunk == nullptr){ offsets[i] = 0; } else { diff --git a/src/files/WorldRegions.hpp b/src/files/WorldRegions.hpp index ae727c09..ffab6373 100644 --- a/src/files/WorldRegions.hpp +++ b/src/files/WorldRegions.hpp @@ -38,8 +38,8 @@ public: }; class WorldRegion { - ubyte** chunksData; - uint32_t* sizes; + std::unique_ptr[]> chunksData; + std::unique_ptr sizes; bool unsaved = false; public: WorldRegion(); @@ -52,7 +52,7 @@ public: void setUnsaved(bool unsaved); bool isUnsaved() const; - ubyte** getChunks() const; + std::unique_ptr* getChunks() const; uint32_t* getSizes() const; };