From ff0b2e10641ac5cfaec5fd7ae049fc7abfd3ba71 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 19 Jan 2025 09:58:31 +0100 Subject: [PATCH] Codechange: use std::vector instead of ReallocT-ed memory --- src/core/pool_func.hpp | 27 +++++++++++---------------- src/core/pool_type.hpp | 3 +-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp index 0012b682d8..178f387cd7 100644 --- a/src/core/pool_func.hpp +++ b/src/core/pool_func.hpp @@ -33,7 +33,6 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) : PoolBase(Tpool_type), name(name), - size(0), first_free(0), first_unused(0), items(0), @@ -41,7 +40,6 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) : checked(0), #endif /* WITH_ASSERT */ cleaning(false), - data(nullptr), alloc_cache(nullptr) { } @@ -53,25 +51,22 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) : */ DEFINE_POOL_METHOD(inline void)::ResizeFor(size_t index) { - assert(index >= this->size); + assert(index >= this->data.size()); assert(index < Tmax_size); + size_t old_size = this->data.size(); size_t new_size = std::min(Tmax_size, Align(index + 1, Tgrowth_step)); - this->data = ReallocT(this->data, new_size); - MemSetT(this->data + this->size, 0, new_size - this->size); - + this->data.resize(new_size); this->used_bitmap.resize(Align(new_size, BITMAP_SIZE) / BITMAP_SIZE); - if (this->size % BITMAP_SIZE != 0) { + if (old_size % BITMAP_SIZE != 0) { /* Already-allocated bits above old size are now unused. */ - this->used_bitmap[this->size / BITMAP_SIZE] &= ~((~static_cast(0)) << (this->size % BITMAP_SIZE)); + this->used_bitmap[old_size / BITMAP_SIZE] &= ~((~static_cast(0)) << (old_size % BITMAP_SIZE)); } if (new_size % BITMAP_SIZE != 0) { /* Bits above new size are considered used. */ this->used_bitmap[new_size / BITMAP_SIZE] |= (~static_cast(0)) << (new_size % BITMAP_SIZE); } - - this->size = new_size; } /** @@ -86,7 +81,7 @@ DEFINE_POOL_METHOD(inline size_t)::FindFirstFree() return std::distance(std::begin(this->used_bitmap), it) * BITMAP_SIZE + FindFirstBit(available); } - assert(this->first_unused == this->size); + assert(this->first_unused == this->data.size()); if (this->first_unused < Tmax_size) { this->ResizeFor(this->first_unused); @@ -168,7 +163,7 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index) SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, Tmax_size); } - if (index >= this->size) this->ResizeFor(index); + if (index >= this->data.size()) this->ResizeFor(index); if (this->data[index] != nullptr) { SlErrorCorruptFmt("{} index {} already in use", this->name, index); @@ -185,7 +180,7 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index) */ DEFINE_POOL_METHOD(void)::FreeItem(size_t index) { - assert(index < this->size); + assert(index < this->data.size()); assert(this->data[index] != nullptr); if (Tcache) { AllocCache *ac = reinterpret_cast(this->data[index]); @@ -211,11 +206,11 @@ DEFINE_POOL_METHOD(void)::CleanPool() delete this->Get(i); // 'delete nullptr;' is very valid } assert(this->items == 0); - free(this->data); + this->data.clear(); + this->data.shrink_to_fit(); this->used_bitmap.clear(); this->used_bitmap.shrink_to_fit(); - this->first_unused = this->first_free = this->size = 0; - this->data = nullptr; + this->first_unused = this->first_free = 0; this->cleaning = false; if (Tcache) { diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index fb0e822ac3..97f92299a4 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -88,7 +88,6 @@ struct Pool : PoolBase { const char * const name; ///< Name of this pool - size_t size; ///< Current allocated size size_t first_free; ///< No item with index lower than this is free (doesn't say anything about this one!) size_t first_unused; ///< This and all higher indexes are free (doesn't say anything about first_unused-1 !) size_t items; ///< Number of used indexes (non-nullptr) @@ -97,7 +96,7 @@ struct Pool : PoolBase { #endif /* WITH_ASSERT */ bool cleaning; ///< True if cleaning pool (deleting all items) - Titem **data; ///< Pointer to array of pointers to Titem + std::vector data; ///< Pointers to Titem std::vector used_bitmap; ///< Bitmap of used indices. Pool(const char *name);