OpenTTD Source  20241108-master-g80f628063a
Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero > Struct Template Reference

Base class for all pools. More...

#include <pool_type.hpp>

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

Data Structures

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

Public Types

using BitmapStorage = size_t
 

Public Member Functions

 Pool (const char *name)
 
void CleanPool () override
 Virtual method that deletes all items in the pool.
 
Titem * Get (size_t index)
 Returns Titem with given index. More...
 
bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-nullptr) Titem. More...
 
bool CanAllocate (size_t n=1)
 Tests whether we can allocate 'n' items. More...
 
- Public Member Functions inherited from PoolBase
 PoolBase (PoolType pt)
 Constructor registers this object in the pool vector. More...
 
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 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)
 
bool cleaning
 True if cleaning pool (deleting all items)
 
Titem ** data
 Pointer to array of 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 = Tmax_size
 Make template parameter accessible from outside.
 
static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits
 

Private Member Functions

void * AllocateItem (size_t size, size_t index)
 
void ResizeFor (size_t index)
 
size_t FindFirstFree ()
 
void * GetNew (size_t size)
 
void * GetNew (size_t size, size_t index)
 
void FreeItem (size_t index)
 

Private Attributes

AllocCachealloc_cache
 Cache of freed pointers.
 

Static Private Attributes

static const size_t NO_FREE_ITEM = MAX_UVALUE(size_t)
 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. More...
 
static void Clean (PoolType)
 Clean all pools of given type. More...
 

Detailed Description

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
struct Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >

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
Tmax_sizeMaximum size of the pool
Tpool_typeType of this pool
TcacheWhether to perform 'alloc' caching, i.e. don't actually free/malloc just reuse the memory
TzeroWhether to zero the memory
Warning
when Tcache is enabled all instances of this pool's item must be of the same size.

Definition at line 80 of file pool_type.hpp.

Member Function Documentation

◆ CanAllocate()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 133 of file pool_type.hpp.

◆ Get()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
Titem* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 112 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::first_unused.

Referenced by Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::IsValidID().

◆ IsValidID()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 123 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::Get().


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