OpenTTD Source 20260206-master-g4d4e37dbf1
Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache > Struct Template Reference

Base class for all pools. More...

#include <pool_type.hpp>

Inheritance diagram for Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >:
PoolBase

Data Structures

struct  PoolIterator
 Iterator to iterate all valid T of a pool. More...
struct  IterateWrapper
struct  PoolIteratorFiltered
 Iterator to iterate all valid T of a pool. More...
struct  IterateWrapperFiltered
struct  PoolItem
 Base class for all PoolItems. More...
struct  AllocCache
 Helper struct to cache 'freed' PoolItems so we do not need to allocate them again. More...

Public Types

using IndexType = Tindex
using BitmapStorage = size_t

Public Member Functions

 Pool (std::string_view name)
void CleanPool () override
 Destroys all items in the pool and resets all member variables.
Titem * Get (size_t index)
 Returns Titem with given index.
bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-nullptr) Titem.
bool CanAllocate (size_t n=1)
 Tests whether we can allocate 'n' items.
Public Member Functions inherited from PoolBase
 PoolBase (PoolType pt)
 Constructor registers this object in the pool vector.
virtual ~PoolBase ()
 Destructor removes this object from the pool vector and deletes the vector itself if this was the last item removed.

Data Fields

const std::string_view name {}
 Name of this pool.
size_t first_free = 0
 No item with index lower than this is free (doesn't say anything about this one!).
size_t first_unused = 0
 This and all higher indexes are free (doesn't say anything about first_unused-1 !).
size_t items = 0
 Number of used indexes (non-nullptr).
bool cleaning = false
 True if cleaning pool (deleting all items).
std::vector< Titem * > data {}
 Pointers to Titem.
std::vector< BitmapStorage > used_bitmap {}
 Bitmap of used indices.
Data Fields inherited from PoolBase
const PoolType type
 Type of this pool.

Static Public Attributes

static constexpr size_t MAX_SIZE = Tindex::End().base()
 Make template parameter accessible from outside.
static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits

Private Member Functions

AllocationResult< Tindex > AllocateItem (size_t size, size_t index)
 Makes given index valid.
void ResizeFor (size_t index)
 Resizes the pool so 'index' can be addressed.
size_t FindFirstFree ()
 Searches for first free index.
AllocationResult< Tindex > GetNew (size_t size)
 Allocates new item.
AllocationResult< Tindex > GetNew (size_t size, size_t index)
 Allocates new item with given index.
void FreeItem (size_t size, size_t index)
 Deallocates memory used by this index and marks item as free.

Static Private Member Functions

static constexpr size_t GetRawIndex (size_t index)
template<typename T>
static constexpr size_t GetRawIndex (const T &index)

Private Attributes

AllocCachealloc_cache = nullptr
 Cache of freed pointers.
std::allocator< uint8_t > allocator {}

Static Private Attributes

static const size_t NO_FREE_ITEM = std::numeric_limits<size_t>::max()
 Constant to indicate we can't allocate any more items.

Additional Inherited Members

Static Public Member Functions inherited from PoolBase
static PoolVectorGetPools ()
 Function used to access the vector of all pools.
static void Clean (PoolTypes)
 Clean all pools of given type.

Detailed Description

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
requires std::is_base_of_v<PoolIDBase, Tindex>
struct Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >

Base class for all pools.

Template Parameters
TitemType of the class/struct that is going to be pooled
TindexType of the index for this pool
Tgrowth_stepSize of growths; if the pool is full increase the size by this amount
Tpool_typeType of this pool
TcacheWhether to perform 'alloc' caching, i.e. don't actually deallocated/allocate just reuse the memory
Warning
when Tcache is enabled all instances of this pool's item must be of the same size.

Definition at line 137 of file pool_type.hpp.

Member Typedef Documentation

◆ BitmapStorage

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
using Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::BitmapStorage = size_t

Definition at line 142 of file pool_type.hpp.

◆ IndexType

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
using Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::IndexType = Tindex

Definition at line 141 of file pool_type.hpp.

Constructor & Destructor Documentation

◆ Pool()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Pool ( std::string_view name)
inline

Definition at line 158 of file pool_type.hpp.

Member Function Documentation

◆ AllocateItem()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
AllocationResult< Tindex > Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::AllocateItem ( size_t size,
size_t index )
inlineprivate

Makes given index valid.

Parameters
sizesize of item
indexindex of item
Precondition
index < this->size
this->Get(index) == nullptr
Returns
The resulting allocation and pool-type index.

Definition at line 87 of file pool_func.hpp.

References alloc_cache, AllocateItem(), data, first_unused, items, SetBit(), and used_bitmap.

Referenced by AllocateItem(), GetNew(), and GetNew().

◆ CanAllocate()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::CanAllocate ( size_t n = 1)
inline

Tests whether we can allocate 'n' items.

Parameters
nnumber of items we want to allocate
Returns
true if 'n' items can be allocated

Definition at line 188 of file pool_type.hpp.

◆ CleanPool()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
void Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::CleanPool ( )
overridevirtual

Destroys all items in the pool and resets all member variables.

Implements PoolBase.

Definition at line 180 of file pool_func.hpp.

References alloc_cache, cleaning, CleanPool(), data, first_free, first_unused, Get(), items, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::AllocCache::next, and used_bitmap.

Referenced by CleanPool().

◆ FindFirstFree()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::FindFirstFree ( )
inlineprivate

Searches for first free index.

Returns
first free index, NO_FREE_ITEM on failure

Definition at line 59 of file pool_func.hpp.

References data, FindFirstBit(), FindFirstFree(), first_free, first_unused, MAX_SIZE, NO_FREE_ITEM, ResizeFor(), and used_bitmap.

Referenced by FindFirstFree(), and GetNew().

◆ FreeItem()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
void Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::FreeItem ( size_t size,
size_t index )
private

Deallocates memory used by this index and marks item as free.

Parameters
sizethe size of the freed object
indexitem to deallocate
Precondition
unit is allocated (non-nullptr)
Note
'delete nullptr' doesn't cause call of this function, so it is safe

Definition at line 159 of file pool_func.hpp.

References alloc_cache, cleaning, ClrBit(), data, first_free, FreeItem(), items, Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::AllocCache::next, and used_bitmap.

Referenced by FreeItem().

◆ Get()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
Titem * Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Get ( size_t index)
inline

Returns Titem with given index.

Parameters
indexof item to get
Returns
pointer to Titem
Precondition
index < this->first_unused

Definition at line 167 of file pool_type.hpp.

Referenced by CleanPool(), and Pool< EngineRenew, EngineRenewID, 16 >::IsValidID().

◆ GetNew() [1/2]

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
AllocationResult< Tindex > Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetNew ( size_t size)
private

Allocates new item.

Parameters
sizesize of item
Returns
The resulting allocation and pool-type index.
Note
FatalError() on failure! (no free item)

Definition at line 114 of file pool_func.hpp.

References AllocateItem(), FindFirstFree(), first_free, GetNew(), name, and NO_FREE_ITEM.

Referenced by GetNew(), and GetNew().

◆ GetNew() [2/2]

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
AllocationResult< Tindex > Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetNew ( size_t size,
size_t index )
private

Allocates new item with given index.

Parameters
sizesize of item
indexindex of item
Returns
The resulting allocation and pool-type index.
Note
SlErrorCorruptFmt() on failure! (index out of range or already used)

Definition at line 137 of file pool_func.hpp.

References AllocateItem(), data, GetNew(), MAX_SIZE, name, and SlErrorCorruptFmt().

◆ GetRawIndex() [1/2]

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
template<typename T>
constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetRawIndex ( const T & index)
inlinestaticconstexprprivate

Definition at line 469 of file pool_type.hpp.

◆ GetRawIndex() [2/2]

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetRawIndex ( size_t index)
inlinestaticconstexprprivate

Definition at line 467 of file pool_type.hpp.

◆ IsValidID()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::IsValidID ( size_t index)
inline

Tests whether given index can be used to get valid (non-nullptr) Titem.

Parameters
indexindex to examine
Returns
true if PoolItem::Get(index) will return non-nullptr pointer

Definition at line 178 of file pool_type.hpp.

◆ ResizeFor()

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type, bool Tcache>
void Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::ResizeFor ( size_t index)
inlineprivate

Resizes the pool so 'index' can be addressed.

Parameters
indexindex we will allocate later
Precondition
index >= this->size
index < MAX_SIZE

Definition at line 35 of file pool_func.hpp.

References Align(), data, MAX_SIZE, ResizeFor(), and used_bitmap.

Referenced by FindFirstFree(), and ResizeFor().

Field Documentation

◆ alloc_cache

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
AllocCache* Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::alloc_cache = nullptr
private

Cache of freed pointers.

Definition at line 455 of file pool_type.hpp.

Referenced by AllocateItem(), CleanPool(), and FreeItem().

◆ allocator

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::allocator<uint8_t> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::allocator {}
private

Definition at line 456 of file pool_type.hpp.

◆ BITMAP_SIZE

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits
staticconstexpr

Definition at line 143 of file pool_type.hpp.

◆ cleaning

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::cleaning = false

True if cleaning pool (deleting all items).

Definition at line 153 of file pool_type.hpp.

Referenced by CleanPool(), and FreeItem().

◆ data

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::vector<Titem *> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::data {}

◆ first_free

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::first_free = 0

No item with index lower than this is free (doesn't say anything about this one!).

Definition at line 147 of file pool_type.hpp.

Referenced by CleanPool(), FindFirstFree(), FreeItem(), and GetNew().

◆ first_unused

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::first_unused = 0

This and all higher indexes are free (doesn't say anything about first_unused-1 !).

Definition at line 148 of file pool_type.hpp.

Referenced by AllocateItem(), CleanPool(), and FindFirstFree().

◆ items

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::items = 0

Number of used indexes (non-nullptr).

Definition at line 149 of file pool_type.hpp.

Referenced by AllocateItem(), CleanPool(), and FreeItem().

◆ MAX_SIZE

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::MAX_SIZE = Tindex::End().base()
staticconstexpr

Make template parameter accessible from outside.

Definition at line 139 of file pool_type.hpp.

Referenced by FindFirstFree(), GetNew(), and ResizeFor().

◆ name

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
const std::string_view Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::name {}

Name of this pool.

Definition at line 145 of file pool_type.hpp.

Referenced by GetNew(), and GetNew().

◆ NO_FREE_ITEM

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
const size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::NO_FREE_ITEM = std::numeric_limits<size_t>::max()
staticprivate

Constant to indicate we can't allocate any more items.

Definition at line 443 of file pool_type.hpp.

Referenced by FindFirstFree(), and GetNew().

◆ used_bitmap

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::vector<BitmapStorage> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::used_bitmap {}

Bitmap of used indices.

Definition at line 156 of file pool_type.hpp.

Referenced by AllocateItem(), CleanPool(), FindFirstFree(), FreeItem(), and ResizeFor().


The documentation for this struct was generated from the following files: