17#include "../error_func.h"
19#include "../saveload/saveload_error.hpp"
25#define DEFINE_POOL_METHOD(type) \
26 template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type, bool Tcache, bool Tzero> \
27 type Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
54 assert(index >= this->data.size());
55 assert(index < Tmax_size);
57 size_t old_size = this->data.size();
58 size_t new_size = std::min(Tmax_size,
Align(index + 1, Tgrowth_step));
60 this->data.resize(new_size);
61 this->used_bitmap.resize(
Align(new_size, BITMAP_SIZE) / BITMAP_SIZE);
62 if (old_size % BITMAP_SIZE != 0) {
64 this->used_bitmap[old_size / BITMAP_SIZE] &= ~((~static_cast<BitmapStorage>(0)) << (old_size % BITMAP_SIZE));
66 if (new_size % BITMAP_SIZE != 0) {
68 this->used_bitmap[new_size / BITMAP_SIZE] |= (~static_cast<BitmapStorage>(0)) << (new_size % BITMAP_SIZE);
78 for (
auto it = std::next(std::begin(this->used_bitmap), this->first_free / BITMAP_SIZE); it != std::end(this->used_bitmap); ++it) {
79 BitmapStorage available = ~(*it);
80 if (available == 0)
continue;
81 return std::distance(std::begin(this->used_bitmap), it) * BITMAP_SIZE +
FindFirstBit(available);
84 assert(this->first_unused == this->data.size());
86 if (this->first_unused < Tmax_size) {
87 this->ResizeFor(this->first_unused);
88 return this->first_unused;
91 assert(this->first_unused == Tmax_size);
105 assert(this->data[index] ==
nullptr);
107 this->first_unused = std::max(this->first_unused, index + 1);
111 if (Tcache && this->alloc_cache !=
nullptr) {
112 assert(
sizeof(Titem) == size);
113 item =
reinterpret_cast<Titem *
>(this->alloc_cache);
114 this->alloc_cache = this->alloc_cache->next;
118 memset(
static_cast<void *
>(item), 0,
sizeof(Titem));
121 item =
reinterpret_cast<Titem *
>(CallocT<uint8_t>(size));
123 item =
reinterpret_cast<Titem *
>(MallocT<uint8_t>(size));
125 this->data[index] = item;
126 SetBit(this->used_bitmap[index / BITMAP_SIZE], index % BITMAP_SIZE);
127 item->index = (Tindex)(uint)index;
139 size_t index = this->FindFirstFree();
142 assert(this->checked != 0);
145 if (index == NO_FREE_ITEM) {
146 FatalError(
"{}: no more free items", this->name);
149 this->first_free = index + 1;
150 return this->AllocateItem(size, index);
162 if (index >= Tmax_size) {
163 SlErrorCorruptFmt(
"{} index {} out of range ({})", this->name, index, Tmax_size);
166 if (index >= this->data.size()) this->ResizeFor(index);
168 if (this->data[index] !=
nullptr) {
169 SlErrorCorruptFmt(
"{} index {} already in use", this->name, index);
172 return this->AllocateItem(size, index);
183 assert(index < this->data.size());
184 assert(this->data[index] !=
nullptr);
186 AllocCache *ac =
reinterpret_cast<AllocCache *
>(this->data[index]);
187 ac->next = this->alloc_cache;
188 this->alloc_cache = ac;
190 free(this->data[index]);
192 this->data[index] =
nullptr;
193 this->first_free = std::min(this->first_free, index);
195 if (!this->cleaning) {
196 ClrBit(this->used_bitmap[index / BITMAP_SIZE], index % BITMAP_SIZE);
197 Titem::PostDestructor(index);
204 this->cleaning =
true;
205 for (
size_t i = 0; i < this->first_unused; i++) {
208 assert(this->items == 0);
210 this->data.shrink_to_fit();
211 this->used_bitmap.clear();
212 this->used_bitmap.shrink_to_fit();
213 this->first_unused = this->first_free = 0;
214 this->cleaning =
false;
217 while (this->alloc_cache !=
nullptr) {
218 AllocCache *ac = this->alloc_cache;
219 this->alloc_cache = ac->next;
225#undef DEFINE_POOL_METHOD
232#define INSTANTIATE_POOL_METHODS(name) \
233 template void * name ## Pool::GetNew(size_t size); \
234 template void * name ## Pool::GetNew(size_t size, size_t index); \
235 template void name ## Pool::FreeItem(size_t index); \
236 template void name ## Pool::CleanPool();
Functions related to the allocation of memory.
Functions related to bit mathematics.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Functions related to memory operations.
#define DEFINE_POOL_METHOD(type)
Helper for defining the method's signature.
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Base class for base of all pools.
Base class for all pools.