Cogs.Core
|
Base untyped pool. More...
#include <PoolBase.h>
Public Member Functions | |
PoolBase (size_t elementSize, size_t initialCapacity, size_t pageSize, Memory::Allocator *allocator=Memory::Allocator::defaultAllocator(), MemBlockType memType=MemBlockType::Bucket) | |
Construct a pool base with the given parameters. | |
PoolBase (PoolBase &&)=default | |
Defaulted move constructor. | |
PoolBase & | operator= (PoolBase &&)=default |
PoolBase (const PoolBase &)=delete | |
Disallow copying PoolBase instances. | |
PoolBase & | operator= (const PoolBase &)=delete |
Disallow copy-assigning PoolBase instances. | |
void | resize (size_t capacity) |
Resize the pool to have storage for the given capacity number of elements. | |
void * | allocate () |
Allocate storage for a single new element, returning the pointer to the uninitialized storage. | |
void | deallocate (void *element) |
Deallocate the given element, returning the memory to be managed by the pool. | |
size_t | size () const |
Get the size of the pool, i.e the number of allocated elements. | |
size_t | getCapacity () const |
Get the capacity of the pool, in number of elements. | |
size_t | getPageSize () const |
Get the page size used by the pool, given in number of elements. | |
ElementHandle | getHandle (void *element) const |
Get a handle to the given element. | |
void * | getElement (ElementHandle handle) |
Get a pointer to the element indexed by the given handle. | |
bool | isValid (ElementHandle handle) const |
Checks if the given handle is a valid handle to an allocated or free element. | |
bool | isAllocated (ElementHandle handle) const |
Checks if the given handle is a valid handle to an allocated element. | |
Protected Member Functions | |
constexpr uint32_t | getPageIndex (ElementHandle handle) const |
Gets the page index of the element handle. | |
constexpr ElementOffset | getElementOffset (ElementHandle handle) const |
Gets the index inside a page of the element handle. | |
void | addPage (size_t pageSize) |
Adds a new page to the pool, with the given size in number of elements. | |
Base untyped pool.
Used to store and retrieve elements in pages which are limited pools.
Definition at line 21 of file PoolBase.h.
Cogs::Collections::PoolBase::PoolBase | ( | size_t | elementSize, |
size_t | initialCapacity, | ||
size_t | pageSize, | ||
Memory::Allocator * | allocator = Memory::Allocator::defaultAllocator() , |
||
MemBlockType | memType = MemBlockType::Bucket |
||
) |
Construct a pool base with the given parameters.
elementSize | Size of the type that should be stored in the pool. The size must be large enough to store a pointer. This is necessary for internal management of free resources without extra allocation. |
initialCapacity | Initial number of elements to allocate storage for. The storage is divided among pages of pageSize size. |
pageSize | Number of elements per page. |
Definition at line 22 of file PoolBase.cpp.
References resize().
|
protected |
Adds a new page to the pool, with the given size in number of elements.
Definition at line 50 of file PoolBase.cpp.
void * Cogs::Collections::PoolBase::allocate | ( | ) |
Allocate storage for a single new element, returning the pointer to the uninitialized storage.
If needed, the pool will grow to accommodate the new element, having the same effect as calling resize(getCapacity() + getPageSize()).
Definition at line 85 of file PoolBase.cpp.
void Cogs::Collections::PoolBase::deallocate | ( | void * | element | ) |
Deallocate the given element, returning the memory to be managed by the pool.
Definition at line 109 of file PoolBase.cpp.
|
inline |
Get the capacity of the pool, in number of elements.
Definition at line 79 of file PoolBase.h.
void * Cogs::Collections::PoolBase::getElement | ( | ElementHandle | handle | ) |
Get a pointer to the element indexed by the given handle.
handle | Handle to the element. |
Definition at line 153 of file PoolBase.cpp.
|
inlineconstexprprotected |
Gets the index inside a page of the element handle.
Definition at line 126 of file PoolBase.h.
Cogs::Collections::ElementHandle Cogs::Collections::PoolBase::getHandle | ( | void * | element | ) | const |
Get a handle to the given element.
Can be used to index to the same element using operator[]().
element | Pointer to the element to generate the offset for. The pointer must be a valid pointer to an element previously allocated by the same pool. |
Definition at line 125 of file PoolBase.cpp.
|
inlineconstexprprotected |
Gets the page index of the element handle.
Definition at line 123 of file PoolBase.h.
|
inline |
Get the page size used by the pool, given in number of elements.
Definition at line 84 of file PoolBase.h.
bool Cogs::Collections::PoolBase::isAllocated | ( | ElementHandle | handle | ) | const |
Checks if the given handle is a valid handle to an allocated element.
NOTE: Has to traverse free list to check. Can be time-consuming.
Definition at line 172 of file PoolBase.cpp.
|
inline |
Checks if the given handle is a valid handle to an allocated or free element.
Definition at line 109 of file PoolBase.h.
void Cogs::Collections::PoolBase::resize | ( | size_t | capacity | ) |
Resize the pool to have storage for the given capacity number of elements.
NOTE: Does NOT support shrinking.
capacity | Desired capacity for the pool. The actual allocated space will be rounded up to the next multiple of the page size. |
Add pages to hold the desired capacity.
Definition at line 34 of file PoolBase.cpp.
Referenced by PoolBase().
|
inline |
Get the size of the pool, i.e the number of allocated elements.
Definition at line 74 of file PoolBase.h.