Cogs.Core
Public Member Functions | List of all members
Cogs::Collections::Pool< ElementType > Struct Template Reference

Pool used to store elements of ElementType. More...

#include <Pool.h>

Public Member Functions

 Pool (MemBlockType memType=MemBlockType::Bucket)
 Create a pool with default capacity and page size.
 
 Pool (ElementOffset capacity, ElementOffset pageSize=128, MemBlockType memType=MemBlockType::Bucket)
 Create a pool with the given capacity and page sizes given in number of elements.
 
 Pool (ElementOffset capacity, ElementOffset pageSize, Memory::Allocator *allocator, MemBlockType memType=MemBlockType::Bucket)
 Create a pool with the given capacity and page sizes given in number of elements.
 
 Pool (Pool &&) noexcept=default
 Defaulted move constructor.
 
Pooloperator= (Pool &&) noexcept=default
 Defaulted move assignment operator.
 
 Pool (const Pool &)=delete
 Disallow copying Pool instances.
 
Pooloperator= (const Pool &)=delete
 Disallow copy-assigning pool instances.
 
void resize (size_t capacity)
 Resize the pool to the given capacity.
 
template<typename... ARGS>
ElementType * create (ARGS &&... args)
 Allocate and initialize a new element from the pool passing any arguments to the constructor of the new item as necessary.
 
void destroy (ElementType *element)
 Free and destroy the given element in the pool.
 
size_t size () const
 Get the size of the pool, i.e the number of created elements.
 
size_t getCapacity () const
 Get the capacity of the pool, i.e the number of elements that may be allocated before any additional pages must be created.
 
size_t getPageSize () const
 Get the page size in number of elements.
 
ElementHandle getHandle (ElementType *element) const
 Get a handle for the given element.
 
ElementType * operator[] (ElementHandle handle)
 Lookup an element using the handle given, using constant time.
 
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.
 

Detailed Description

template<typename ElementType>
struct Cogs::Collections::Pool< ElementType >

Pool used to store elements of ElementType.

Template Parameters
ElementTypeType of elements to store in the pool. The size requirements for storage are at least the size of a pointer.

Definition at line 16 of file Pool.h.

Constructor & Destructor Documentation

◆ Pool() [1/3]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( MemBlockType  memType = MemBlockType::Bucket)
inline

Create a pool with default capacity and page size.

Definition at line 21 of file Pool.h.

◆ Pool() [2/3]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( ElementOffset  capacity,
ElementOffset  pageSize = 128,
MemBlockType  memType = MemBlockType::Bucket 
)
inline

Create a pool with the given capacity and page sizes given in number of elements.

Parameters
capacityTotal number of elements to allocate storage for, divided between capacity / pageSize number of pages.
pageSizeNumber of elements to store per page. Defaults to 128. Allocations will occur whenever the pool needs to grow into additional pages.

Definition at line 31 of file Pool.h.

◆ Pool() [3/3]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( ElementOffset  capacity,
ElementOffset  pageSize,
Memory::Allocator allocator,
MemBlockType  memType = MemBlockType::Bucket 
)
inline

Create a pool with the given capacity and page sizes given in number of elements.

Parameters
capacityTotal number of elements to allocate storage for, divided between capacity / pageSize number of pages.
pageSizeNumber of elements to store per page. Defaults to 128. Allocations will occur whenever the pool needs to grow into additional pages.
allocatorPointer to an allocator to use when reserving page memory.

Definition at line 42 of file Pool.h.

Member Function Documentation

◆ create()

template<typename ElementType >
template<typename... ARGS>
ElementType * Cogs::Collections::Pool< ElementType >::create ( ARGS &&...  args)
inline

Allocate and initialize a new element from the pool passing any arguments to the constructor of the new item as necessary.

The pool will grow as needed if no free elements are available to use or reuse.

Returns
A pointer to the newly allocated element. The pointer remains valid after resizing the pool.

Definition at line 69 of file Pool.h.

Referenced by Cogs::Core::TaskQueue::createTask().

◆ destroy()

template<typename ElementType >
void Cogs::Collections::Pool< ElementType >::destroy ( ElementType *  element)
inline

Free and destroy the given element in the pool.

The element is returned to the list of free elements maintained by the pool and can be reused the next time an allocation is requested.

Parameters
elementPointer to the element to deallocate.

Definition at line 82 of file Pool.h.

Referenced by Cogs::Core::TaskQueue::destroyTask().

◆ getCapacity()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::getCapacity ( ) const
inline

Get the capacity of the pool, i.e the number of elements that may be allocated before any additional pages must be created.

Definition at line 98 of file Pool.h.

◆ getHandle()

template<typename ElementType >
ElementHandle Cogs::Collections::Pool< ElementType >::getHandle ( ElementType *  element) const
inline

Get a handle for the given element.

Parameters
elementPointer to a previously allocated element in the pool.
Returns
A handle that may later be used with operator[] to retrieve the given pointer.

Definition at line 114 of file Pool.h.

Referenced by Cogs::Core::TaskQueue::createTask().

◆ getPageSize()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::getPageSize ( ) const
inline

Get the page size in number of elements.

Total page size in bytes will be numElements * sizeof(ElementType)

Definition at line 105 of file Pool.h.

◆ isAllocated()

template<typename ElementType >
bool Cogs::Collections::Pool< ElementType >::isAllocated ( ElementHandle  handle) const
inline

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 140 of file Pool.h.

◆ isValid()

template<typename ElementType >
bool Cogs::Collections::Pool< ElementType >::isValid ( ElementHandle  handle) const
inline

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

Definition at line 134 of file Pool.h.

◆ operator[]()

template<typename ElementType >
ElementType * Cogs::Collections::Pool< ElementType >::operator[] ( ElementHandle  handle)
inline

Lookup an element using the handle given, using constant time.

Uses a handle previously fetched with getHandle() to look up and return a raw pointer to the element.

Parameters
handleHandle fetched with getHandle() used to look up the element.
Returns
A pointer to the element.

Definition at line 126 of file Pool.h.

◆ resize()

template<typename ElementType >
void Cogs::Collections::Pool< ElementType >::resize ( size_t  capacity)
inline

Resize the pool to the given capacity.

Definition at line 59 of file Pool.h.

◆ size()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::size ( ) const
inline

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

Definition at line 92 of file Pool.h.


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