|
OpenTTD Source 20260206-master-g4d4e37dbf1
|
Base class for all pools. More...
#include <pool_type.hpp>
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 | |
| AllocCache * | alloc_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 PoolVector * | GetPools () |
| Function used to access the vector of all pools. | |
| static void | Clean (PoolTypes) |
| Clean all pools of given type. | |
Base class for all pools.
| Titem | Type of the class/struct that is going to be pooled |
| Tindex | Type of the index for this pool |
| Tgrowth_step | Size of growths; if the pool is full increase the size by this amount |
| Tpool_type | Type of this pool |
| Tcache | Whether to perform 'alloc' caching, i.e. don't actually deallocated/allocate just reuse the memory |
Definition at line 137 of file pool_type.hpp.
| using Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::BitmapStorage = size_t |
Definition at line 142 of file pool_type.hpp.
| using Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::IndexType = Tindex |
Definition at line 141 of file pool_type.hpp.
|
inline |
Definition at line 158 of file pool_type.hpp.
|
inlineprivate |
Makes given index valid.
| size | size of item |
| index | index of item |
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().
|
inline |
Tests whether we can allocate 'n' items.
| n | number of items we want to allocate |
Definition at line 188 of file pool_type.hpp.
|
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().
|
inlineprivate |
Searches for first free index.
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().
|
private |
Deallocates memory used by this index and marks item as free.
| size | the size of the freed object |
| index | item to deallocate |
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().
|
inline |
Returns Titem with given index.
| index | of item to get |
Definition at line 167 of file pool_type.hpp.
Referenced by CleanPool(), and Pool< EngineRenew, EngineRenewID, 16 >::IsValidID().
|
private |
Allocates new item.
| size | size of item |
Definition at line 114 of file pool_func.hpp.
References AllocateItem(), FindFirstFree(), first_free, GetNew(), name, and NO_FREE_ITEM.
|
private |
Allocates new item with given index.
| size | size of item |
| index | index of item |
Definition at line 137 of file pool_func.hpp.
References AllocateItem(), data, GetNew(), MAX_SIZE, name, and SlErrorCorruptFmt().
|
inlinestaticconstexprprivate |
Definition at line 469 of file pool_type.hpp.
|
inlinestaticconstexprprivate |
Definition at line 467 of file pool_type.hpp.
|
inline |
Tests whether given index can be used to get valid (non-nullptr) Titem.
| index | index to examine |
Definition at line 178 of file pool_type.hpp.
|
inlineprivate |
Resizes the pool so 'index' can be addressed.
| index | index we will allocate later |
Definition at line 35 of file pool_func.hpp.
References Align(), data, MAX_SIZE, ResizeFor(), and used_bitmap.
Referenced by FindFirstFree(), and ResizeFor().
|
private |
Cache of freed pointers.
Definition at line 455 of file pool_type.hpp.
Referenced by AllocateItem(), CleanPool(), and FreeItem().
|
private |
Definition at line 456 of file pool_type.hpp.
|
staticconstexpr |
Definition at line 143 of file pool_type.hpp.
| 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().
| std::vector<Titem *> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::data {} |
Pointers to Titem.
Definition at line 155 of file pool_type.hpp.
Referenced by AllocateItem(), CleanPool(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::Create(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem< Tpool >::CreateAtIndex(), FindFirstFree(), FreeItem(), GetNew(), and ResizeFor().
| 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().
| 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().
| 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().
|
staticconstexpr |
Make template parameter accessible from outside.
Definition at line 139 of file pool_type.hpp.
Referenced by FindFirstFree(), GetNew(), and ResizeFor().
| 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.
|
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().
| 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().