24#define DEFINE_POOL_METHOD(type) \
25 template <class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache> \
26 requires std::is_base_of_v<PoolIDBase, Tindex> \
27 type Pool<Titem, Tindex, Tgrowth_step, Tpool_type, Tcache>
37 assert(index >= this->
data.size());
40 size_t old_size = this->
data.size();
41 size_t new_size = std::min(
MAX_SIZE,
Align(index + 1, Tgrowth_step));
43 this->
data.resize(new_size);
45 if (old_size % BITMAP_SIZE != 0) {
47 this->
used_bitmap[old_size / BITMAP_SIZE] &= ~((~static_cast<BitmapStorage>(0)) << (old_size % BITMAP_SIZE));
49 if (new_size % BITMAP_SIZE != 0) {
51 this->
used_bitmap[new_size / BITMAP_SIZE] |= (~static_cast<BitmapStorage>(0)) << (new_size % BITMAP_SIZE);
62 BitmapStorage available = ~(*it);
63 if (available == 0)
continue;
89 assert(this->
data[index] ==
nullptr);
96 assert(
sizeof(Titem) == size);
97 item =
reinterpret_cast<Titem *
>(this->
alloc_cache);
100 item =
reinterpret_cast<Titem *
>(this->allocator.allocate(size));
102 this->
data[index] = item;
105 return {item,
static_cast<Tindex
>(
static_cast<Tindex::BaseType
>(index))};
119 assert(this->checked != 0);
123 FatalError(
"{}: no more free items", this->
name);
143 if (index >= this->
data.size()) this->ResizeFor(index);
145 if (this->
data[index] !=
nullptr) {
161 assert(index < this->
data.size());
162 assert(this->
data[index] !=
nullptr);
168 this->allocator.deallocate(
reinterpret_cast<uint8_t*
>(this->
data[index]), size);
170 this->
data[index] =
nullptr;
175 Titem::PostDestructor(index);
186 assert(this->
items == 0);
188 this->
data.shrink_to_fit();
198 this->allocator.deallocate(
reinterpret_cast<uint8_t*
>(ac),
sizeof(Titem));
203#undef DEFINE_POOL_METHOD
210#define INSTANTIATE_POOL_METHODS(name) \
211 template AllocationResult<name ## Pool::IndexType> name ## Pool::GetNew(size_t size); \
212 template AllocationResult<name ## Pool::IndexType> name ## Pool::GetNew(size_t size, size_t index); \
213 template void name ## Pool::FreeItem(size_t size, size_t index); \
214 template void name ## Pool::CleanPool();
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.
Error reporting related functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
#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,...
Functions/types related to errors from savegames.
void SlErrorCorruptFmt(const fmt::format_string< Args... > format, Args &&... fmt_args)
Issue an SlErrorCorrupt with a format string.
Helper struct to cache 'freed' PoolItems so we do not need to allocate them again.
AllocCache * next
The next in our 'cache'.
size_t first_free
No item with index lower than this is free (doesn't say anything about this one!).
static const size_t NO_FREE_ITEM
Constant to indicate we can't allocate any more items.
size_t FindFirstFree()
Searches for first free index.
AllocCache * alloc_cache
Cache of freed pointers.
size_t first_unused
This and all higher indexes are free (doesn't say anything about first_unused-1 !).
void CleanPool() override
Destroys all items in the pool and resets all member variables.
const std::string_view name
Name of this pool.
std::vector< Titem * > data
Pointers to Titem.
AllocationResult< Tindex > AllocateItem(size_t size, size_t index)
Makes given index valid.
std::vector< BitmapStorage > used_bitmap
Bitmap of used indices.
bool cleaning
True if cleaning pool (deleting all items).
void ResizeFor(size_t index)
Resizes the pool so 'index' can be addressed.
size_t items
Number of used indexes (non-nullptr).
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
Titem * Get(size_t index)
Returns Titem with given index.
AllocationResult< Tindex > GetNew(size_t size)
Allocates new item.
void FreeItem(size_t size, size_t index)
Deallocates memory used by this index and marks item as free.