Cogs.Core
Public Member Functions | Protected Member Functions | List of all members
Cogs::Collections::PoolBase Struct Reference

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.
 
PoolBaseoperator= (PoolBase &&)=default
 
 PoolBase (const PoolBase &)=delete
 Disallow copying PoolBase instances.
 
PoolBaseoperator= (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.
 

Detailed Description

Base untyped pool.

Used to store and retrieve elements in pages which are limited pools.

Definition at line 21 of file PoolBase.h.

Constructor & Destructor Documentation

◆ PoolBase()

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.

Parameters
elementSizeSize 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.
initialCapacityInitial number of elements to allocate storage for. The storage is divided among pages of pageSize size.
pageSizeNumber of elements per page.

Definition at line 22 of file PoolBase.cpp.

References resize().

Member Function Documentation

◆ addPage()

void Cogs::Collections::PoolBase::addPage ( size_t  pageSize)
protected

Adds a new page to the pool, with the given size in number of elements.

Definition at line 50 of file PoolBase.cpp.

◆ allocate()

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()).

Returns
Pointer to the allocated memory.

Definition at line 85 of file PoolBase.cpp.

◆ deallocate()

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.

◆ getCapacity()

size_t Cogs::Collections::PoolBase::getCapacity ( ) const
inline

Get the capacity of the pool, in number of elements.

Definition at line 79 of file PoolBase.h.

◆ getElement()

void * Cogs::Collections::PoolBase::getElement ( ElementHandle  handle)

Get a pointer to the element indexed by the given handle.

Parameters
handleHandle to the element.
Returns
Pointer to the element in the pool.

Definition at line 153 of file PoolBase.cpp.

◆ getElementOffset()

constexpr ElementOffset Cogs::Collections::PoolBase::getElementOffset ( ElementHandle  handle) const
inlineconstexprprotected

Gets the index inside a page of the element handle.

Definition at line 126 of file PoolBase.h.

◆ getHandle()

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[]().

Parameters
elementPointer to the element to generate the offset for. The pointer must be a valid pointer to an element previously allocated by the same pool.
Returns
An element handle containing an offset that can be used to index to the given resource in the pool efficiently.

Definition at line 125 of file PoolBase.cpp.

◆ getPageIndex()

constexpr uint32_t Cogs::Collections::PoolBase::getPageIndex ( ElementHandle  handle) const
inlineconstexprprotected

Gets the page index of the element handle.

Definition at line 123 of file PoolBase.h.

◆ getPageSize()

size_t Cogs::Collections::PoolBase::getPageSize ( ) const
inline

Get the page size used by the pool, given in number of elements.

Definition at line 84 of file PoolBase.h.

◆ isAllocated()

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.

◆ isValid()

bool Cogs::Collections::PoolBase::isValid ( ElementHandle  handle) const
inline

Checks if the given handle is a valid handle to an allocated or free element.

Definition at line 109 of file PoolBase.h.

◆ resize()

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.

Parameters
capacityDesired 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().

◆ size()

size_t Cogs::Collections::PoolBase::size ( ) const
inline

Get the size of the pool, i.e the number of allocated elements.

Definition at line 74 of file PoolBase.h.


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