Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::mem::PoolAllocator Class Referencefinal

Lock-free PMR pool allocator for fixed-size blocks. More...

#include <pool_allocator.hpp>

Inheritance diagram for helios::mem::PoolAllocator:

Public Member Functions

 PoolAllocator (PoolAllocatorOptions options) noexcept
 Constructs pool allocator from options.
 PoolAllocator (size_t block_size, size_t block_count, size_t alignment=kDefaultAlignment) noexcept
 Constructs pool allocator with geometric growth.
 PoolAllocator (const PoolAllocator &)=delete
 PoolAllocator (PoolAllocator &&other) noexcept
 Move-constructs pool allocator from another instance.
 ~PoolAllocator () noexcept override
PoolAllocatoroperator= (const PoolAllocator &)=delete
PoolAllocatoroperator= (PoolAllocator &&other) noexcept
 Move-assigns pool state from another instance.
void Reset () noexcept
 Resets pool to fully free state.
bool Full () const noexcept
 Returns true when no free blocks remain.
bool Empty () const noexcept
 Returns true when all blocks are free.
bool Owns (const void *ptr) const noexcept
 Checks pointer ownership.
AllocatorStats Stats () const noexcept
 Returns runtime stats.
size_t BlockSize () const noexcept
 Returns effective block size.
size_t Alignment () const noexcept
 Returns configured block alignment.
size_t InitialBlockCount () const noexcept
 Returns number of blocks in first chunk.
size_t BlockCount () const noexcept
 Returns total blocks across chunks.
size_t FreeBlockCount () const noexcept
 Returns free block count.
size_t UsedBlockCount () const noexcept
 Returns used block count.
GrowthPolicy Growth () const noexcept
 Returns growth policy.

Static Public Member Functions

template<typename T>
static PoolAllocator ForType (size_t block_count) noexcept
 Helper for constructing pool allocator for specific type.

Detailed Description

Lock-free PMR pool allocator for fixed-size blocks.

Allocates from fixed-size blocks stored in growable chunks.

Fast path uses lock-free freelist push/pop based on embedded next pointers. Growth allocates and links a new chunk, then pushes all its blocks into the freelist.

Definition at line 34 of file pool_allocator.hpp.

Constructor & Destructor Documentation

◆ PoolAllocator() [1/4]

helios::mem::PoolAllocator::PoolAllocator ( PoolAllocatorOptions options)
explicitnoexcept

Constructs pool allocator from options.

Warning
Triggers assertion when block_size or block_count is zero, when alignment is not a power of two, or when alignment is less than alignof(void*).
Parameters
optionsPool allocator options

Definition at line 49 of file pool_allocator.cpp.

◆ PoolAllocator() [2/4]

helios::mem::PoolAllocator::PoolAllocator ( size_t block_size,
size_t block_count,
size_t alignment = kDefaultAlignment )
inlinenoexcept

Constructs pool allocator with geometric growth.

Warning
Triggers assertion when block_size or block_count is zero, when alignment is not a power of two, or when alignment is less than alignof(void*).
Parameters
block_sizeSize of each block in bytes
block_countNumber of blocks in initial chunk
alignmentBlock alignment

Definition at line 54 of file pool_allocator.hpp.

◆ PoolAllocator() [3/4]

helios::mem::PoolAllocator::PoolAllocator ( const PoolAllocator & )
delete

◆ PoolAllocator() [4/4]

helios::mem::PoolAllocator::PoolAllocator ( PoolAllocator && other)
inlinenoexcept

Move-constructs pool allocator from another instance.

Parameters
otherSource allocator; left in empty moved-from state

Definition at line 68 of file pool_allocator.hpp.

◆ ~PoolAllocator()

helios::mem::PoolAllocator::~PoolAllocator ( )
inlineoverridenoexcept

Definition at line 69 of file pool_allocator.hpp.

Member Function Documentation

◆ Alignment()

size_t helios::mem::PoolAllocator::Alignment ( ) const
inlinenodiscardnoexcept

Returns configured block alignment.

Returns
Alignment in bytes

Definition at line 137 of file pool_allocator.hpp.

◆ BlockCount()

size_t helios::mem::PoolAllocator::BlockCount ( ) const
inlinenodiscardnoexcept

Returns total blocks across chunks.

Returns
Total block count

Definition at line 151 of file pool_allocator.hpp.

◆ BlockSize()

size_t helios::mem::PoolAllocator::BlockSize ( ) const
inlinenodiscardnoexcept

Returns effective block size.

Returns
Block size in bytes

Definition at line 131 of file pool_allocator.hpp.

◆ Empty()

bool helios::mem::PoolAllocator::Empty ( ) const
inlinenodiscardnoexcept

Returns true when all blocks are free.

Returns
True if empty, false otherwise

Definition at line 109 of file pool_allocator.hpp.

◆ ForType()

template<typename T>
PoolAllocator helios::mem::PoolAllocator::ForType ( size_t block_count)
inlinestaticnodiscardnoexcept

Helper for constructing pool allocator for specific type.

Template Parameters
TType to pool
Parameters
block_countNumber of blocks in initial chunk
Returns
Configured pool allocator

Definition at line 222 of file pool_allocator.hpp.

◆ FreeBlockCount()

size_t helios::mem::PoolAllocator::FreeBlockCount ( ) const
inlinenodiscardnoexcept

Returns free block count.

Returns
Free block count

Definition at line 159 of file pool_allocator.hpp.

◆ Full()

bool helios::mem::PoolAllocator::Full ( ) const
inlinenodiscardnoexcept

Returns true when no free blocks remain.

Returns
True if full, false otherwise

Definition at line 101 of file pool_allocator.hpp.

◆ Growth()

GrowthPolicy helios::mem::PoolAllocator::Growth ( ) const
inlinenodiscardnoexcept

Returns growth policy.

Returns
Growth policy

Definition at line 173 of file pool_allocator.hpp.

◆ InitialBlockCount()

size_t helios::mem::PoolAllocator::InitialBlockCount ( ) const
inlinenodiscardnoexcept

Returns number of blocks in first chunk.

Returns
Initial block count

Definition at line 143 of file pool_allocator.hpp.

◆ operator=() [1/2]

PoolAllocator & helios::mem::PoolAllocator::operator= ( const PoolAllocator & )
delete

◆ operator=() [2/2]

PoolAllocator & helios::mem::PoolAllocator::operator= ( PoolAllocator && other)
inlinenoexcept

Move-assigns pool state from another instance.

Parameters
otherSource allocator; left in empty moved-from state
Returns
Reference to this allocator

Definition at line 228 of file pool_allocator.hpp.

◆ Owns()

bool helios::mem::PoolAllocator::Owns ( const void * ptr) const
nodiscardnoexcept

Checks pointer ownership.

Parameters
ptrPointer to test
Returns
True when pointer belongs to any pool chunk, false otherwise

Definition at line 74 of file pool_allocator.cpp.

◆ Reset()

void helios::mem::PoolAllocator::Reset ( )
inlinenoexcept

Resets pool to fully free state.

Warning
Must not be called concurrently with allocate/deallocate.

Definition at line 238 of file pool_allocator.hpp.

◆ Stats()

auto helios::mem::PoolAllocator::Stats ( ) const
inlinenodiscardnoexcept

Returns runtime stats.

Returns
Allocator stats

Definition at line 246 of file pool_allocator.hpp.

◆ UsedBlockCount()

size_t helios::mem::PoolAllocator::UsedBlockCount ( ) const
inlinenodiscardnoexcept

Returns used block count.

Returns
Used block count

Definition at line 263 of file pool_allocator.hpp.