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

Classes

struct  AllocatorStats
 Runtime statistics snapshot for PMR allocators. More...
class  ArcFromThis
 CRTP base class that embeds an atomic reference counter into the derived type. More...
class  ArenaAllocator
 PMR arena allocator with lock-free hot allocation path. More...
struct  ArenaOptions
 Configuration for ArenaAllocator. More...
class  AtomicRefCounted
 Thread-safe intrusive reference-counted smart pointer. More...
class  FixedArenaAllocator
 PMR arena with fixed runtime capacity. More...
class  FixedFreeListAllocator
 Fixed-capacity best-fit allocator with coalescing. More...
class  FixedPoolAllocator
 Fixed-capacity intrusive block pool. More...
struct  FixedPoolAllocatorOptions
 Configuration for FixedPoolAllocator. More...
class  FixedStackAllocator
 PMR stack allocator with fixed runtime capacity. More...
class  FrameAllocator
 N-buffered PMR frame allocator. More...
struct  FrameAllocatorOptions
 Configuration for FrameAllocator. More...
class  FreeListAllocator
 General-purpose PMR free-list allocator. More...
struct  FreeListAllocatorOptions
 Configuration for FreeListAllocator. More...
struct  GrowthPolicy
 Capacity growth configuration. More...
class  PoolAllocator
 Lock-free PMR pool allocator for fixed-size blocks. More...
struct  PoolAllocatorOptions
 Configuration for PoolAllocator. More...
class  RcFromThis
 CRTP base class that embeds a non-atomic reference counter into the derived type. More...
class  RefCounted
 Non-atomic intrusive reference-counted smart pointer. More...
class  StackAllocator
 PMR stack allocator with LIFO-aware deallocation. More...
struct  StackAllocatorOptions
 Configuration for StackAllocator. More...

Concepts

concept  PmrAllocator
 Concept for PMR resources used by the memory module.
concept  PmrAllocatorWithStats
 Concept for PMR resources that expose allocator statistics.
concept  ResettablePmrAllocator
 Concept for PMR resources that support reset.

Typedefs

template<typename T, typename Allocator = std::allocator<T>>
using Rc = RefCounted<T, Allocator>
 Alias for RefCounted<T> — non-atomic intrusive reference-counted handle.
template<typename T, typename Allocator = std::allocator<T>>
using Arc = AtomicRefCounted<T, Allocator>
 Alias for AtomicRefCounted<T> — thread-safe intrusive reference-counted handle.
template<typename T>
using PmrRc = RefCounted<T, std::pmr::polymorphic_allocator<T>>
 Alias for RefCounted<T> using polymorphic memory allocator.
template<typename T>
using PmrArc = AtomicRefCounted<T, std::pmr::polymorphic_allocator<T>>
 Alias for AtomicRefCounted<T> using polymorphic memory allocator.

Enumerations

enum class  GrowthMode : uint8_t { kLinear , kGeometric }
 Growth strategy used by growable allocators. More...
enum class  MemoryError : uint8_t {
  kOutOfMemory , kInvalidAlignment , kInvalidSize , kGrowthDisabled ,
  kOwnershipMismatch
}
 Error codes used for explicit non-fatal memory operations. More...

Functions

void * AlignedAlloc (size_t alignment, size_t size, bool enable_profile=true) noexcept
 Allocates memory with the specified alignment.
void AlignedFree (void *ptr, bool enable_profile=true) noexcept
 Frees memory allocated with AlignedAlloc.
template<typename T, PmrAllocator Alloc>
auto TryAllocate (Alloc &allocator) noexcept -> std::expected< T *, MemoryError >
 Tries to allocate one object from a PMR allocator.
template<typename T, PmrAllocator Alloc>
auto TryAllocateSpan (Alloc &allocator, size_t count) noexcept -> std::expected< std::span< T >, MemoryError >
 Tries to allocate an array of objects from a PMR allocator.
template<typename T, PmrAllocator Alloc>
void Deallocate (Alloc &allocator, T *ptr) noexcept
 Deallocates an object allocated by TryAllocate.
template<typename T, PmrAllocator Alloc>
void Deallocate (Alloc &allocator, std::span< T > span) noexcept
 Deallocates an array allocated by TryAllocateSpan.
template<PmrAllocator Alloc>
constexpr AllocatorStats Stats (const Alloc &allocator) noexcept
 Adapts stats-aware PMR resources.
constexpr size_t SaturatingAdd (size_t lhs, size_t rhs) noexcept
 Saturating add for size_t.
constexpr size_t SaturatingMul (size_t lhs, size_t rhs) noexcept
 Saturating multiply for size_t.
constexpr std::string_view MemoryErrorToString (MemoryError error) noexcept
 Converts MemoryError to a readable string.
constexpr bool IsPowerOfTwo (size_t value) noexcept
 Checks if a value is a power of two.
constexpr size_t AlignUp (size_t value, size_t alignment) noexcept
 Aligns value up to alignment.
void * AlignUpPtr (void *ptr, size_t alignment) noexcept
 Aligns pointer up to alignment.
bool IsAligned (const void *ptr, size_t alignment) noexcept
 Checks if pointer is aligned.
size_t CalculatePadding (const void *ptr, size_t alignment) noexcept
 Computes padding required for pointer alignment.
size_t CalculatePaddingWithHeader (const void *ptr, size_t alignment, size_t header_size) noexcept
 Computes padding for aligned storage with a prepended header.
template<typename T, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...>
auto MakeRc (Args &&... args) -> Rc< T >
 Allocates and constructs a T object, returning a Rc<T, std::allocator<T>> handle.
template<typename T, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...>
auto MakeArc (Args &&... args) -> Arc< T >
 Allocates and constructs a T object, returning an Arc<T, std::allocator<T>> handle.
template<typename T, typename Allocator, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...> && (!std::derived_from<std::remove_pointer_t
<Allocator>, std::pmr::memory_resource>)
auto MakeRcWith (Allocator alloc, Args &&... args) -> Rc< T, Allocator >
 Overload of MakeRc accepting a pre-constructed allocator instance.
template<typename T, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...>
auto MakeRcWith (std::pmr::memory_resource *resource, Args &&... args) -> PmrRc< T >
 Allocates and constructs a T object using PMR memory resource, returning a PmrRc<T> handle.
template<typename T, typename... Args>
auto MakeRcWith (std::nullptr_t, Args &&...) -> PmrRc< T >=delete
template<typename T, typename Allocator, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...> && (!std::derived_from<std::remove_pointer_t
<Allocator>, std::pmr::memory_resource>)
auto MakeArcWith (Allocator alloc, Args &&... args) -> Arc< T, Allocator >
 Overload of MakeArc accepting a pre-constructed allocator instance.
template<typename T, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...>
auto MakeArcWith (std::pmr::memory_resource *resource, Args &&... args) -> PmrArc< T >
 Allocates and constructs a T object using PMR memory resource, returning a PmrArc<T> handle.
template<typename T, typename... Args>
auto MakeArcWith (std::nullptr_t, Args &&...) -> PmrArc< T >=delete

Variables

constexpr size_t kDefaultAlignment = 64
 Default alignment used by memory resources.
constexpr size_t kMinAlignment = alignof(std::max_align_t)
 Minimum alignment used by memory resources.

Typedef Documentation

◆ Arc

template<typename T, typename Allocator = std::allocator<T>>
using helios::mem::Arc = AtomicRefCounted<T, Allocator>

Alias for AtomicRefCounted<T> — thread-safe intrusive reference-counted handle.

Definition at line 1139 of file ref_counted.hpp.

◆ PmrArc

template<typename T>
using helios::mem::PmrArc = AtomicRefCounted<T, std::pmr::polymorphic_allocator<T>>

Alias for AtomicRefCounted<T> using polymorphic memory allocator.

Thread-safe intrusive reference-counted handle with PMR support.

Definition at line 1154 of file ref_counted.hpp.

◆ PmrRc

template<typename T>
using helios::mem::PmrRc = RefCounted<T, std::pmr::polymorphic_allocator<T>>

Alias for RefCounted<T> using polymorphic memory allocator.

Allows using std::pmr::memory_resource for dynamic allocator selection.

Definition at line 1147 of file ref_counted.hpp.

◆ Rc

template<typename T, typename Allocator = std::allocator<T>>
using helios::mem::Rc = RefCounted<T, Allocator>

Alias for RefCounted<T> — non-atomic intrusive reference-counted handle.

Definition at line 1134 of file ref_counted.hpp.

Enumeration Type Documentation

◆ GrowthMode

enum class helios::mem::GrowthMode : uint8_t
strong

Growth strategy used by growable allocators.

Enumerator
kLinear 

Adds a fixed byte step on each growth request.

kGeometric 

Multiplies capacity by a configured ratio on growth.

Definition at line 51 of file common.hpp.

◆ MemoryError

enum class helios::mem::MemoryError : uint8_t
strong

Error codes used for explicit non-fatal memory operations.

Enumerator
kOutOfMemory 

Allocation request could not be satisfied.

kInvalidAlignment 

Requested alignment is not supported.

kInvalidSize 

Requested size is zero or overflows.

kGrowthDisabled 

Growable allocator cannot expand further.

kOwnershipMismatch 

Pointer does not belong to the allocator.

Definition at line 179 of file common.hpp.

Function Documentation

◆ AlignedAlloc()

void * helios::mem::AlignedAlloc ( size_t alignment,
size_t size,
bool enable_profile = true )
nodiscardnoexcept

Allocates memory with the specified alignment.

Uses mimalloc when enabled, _aligned_malloc on MSVC, and std::aligned_alloc on other platforms. On POSIX, std::aligned_alloc requires alignment >= sizeof(void*). Smaller alignments are satisfied via malloc, which provides at least alignof(std::max_align_t) alignment.

Warning
Triggers assertion when alignment is zero, alignment is not a power of two, or size is zero.
Parameters
alignmentThe alignment in bytes (must be a power of two and non-zero)
sizeThe size of the memory block to allocate in bytes (must be non-zero)
enable_profileWhen false, skips profiler memory events (for internal allocator backing storage that is tracked separately)
Returns
A pointer to the allocated memory block, or nullptr on failure

Definition at line 18 of file aligned_alloc.cpp.

◆ AlignedFree()

void helios::mem::AlignedFree ( void * ptr,
bool enable_profile = true )
noexcept

Frees memory allocated with AlignedAlloc.

Uses the allocation backend paired with AlignedAlloc. Passing nullptr is a no-op.

Parameters
ptrThe pointer to the memory block to free
enable_profileWhen false, skips profiler memory events

Definition at line 45 of file aligned_alloc.cpp.

◆ AlignUp()

size_t helios::mem::AlignUp ( size_t value,
size_t alignment )
nodiscardconstexprnoexcept

Aligns value up to alignment.

Warning
Triggers assertion when alignment is not a power of two.
Parameters
valueValue to align
alignmentAlignment, must be a power of two
Returns
Aligned value

Definition at line 226 of file common.hpp.

◆ AlignUpPtr()

void * helios::mem::AlignUpPtr ( void * ptr,
size_t alignment )
inlinenodiscardnoexcept

Aligns pointer up to alignment.

Warning
Triggers assertion when alignment is not a power of two.
Parameters
ptrPointer to align
alignmentAlignment, must be a power of two
Returns
Aligned pointer

Definition at line 244 of file common.hpp.

◆ CalculatePadding()

size_t helios::mem::CalculatePadding ( const void * ptr,
size_t alignment )
inlinenodiscardnoexcept

Computes padding required for pointer alignment.

Parameters
ptrCurrent pointer
alignmentRequested alignment
Returns
Padding in bytes

Definition at line 274 of file common.hpp.

◆ CalculatePaddingWithHeader()

size_t helios::mem::CalculatePaddingWithHeader ( const void * ptr,
size_t alignment,
size_t header_size )
inlinenodiscardnoexcept

Computes padding for aligned storage with a prepended header.

Parameters
ptrCurrent pointer
alignmentRequested alignment
header_sizeHeader size stored before aligned user memory
Returns
Padding in bytes, including header space

Definition at line 288 of file common.hpp.

◆ Deallocate() [1/2]

template<typename T, PmrAllocator Alloc>
void helios::mem::Deallocate ( Alloc & allocator,
std::span< T > span )
inlinenoexcept

Deallocates an array allocated by TryAllocateSpan.

Template Parameters
TElement type
AllocAllocator type
Parameters
allocatorAllocator reference
spanSpan returned by TryAllocateSpan

Definition at line 124 of file allocator_traits.hpp.

◆ Deallocate() [2/2]

template<typename T, PmrAllocator Alloc>
void helios::mem::Deallocate ( Alloc & allocator,
T * ptr )
inlinenoexcept

Deallocates an object allocated by TryAllocate.

Template Parameters
TObject type
AllocAllocator type
Parameters
allocatorAllocator reference
ptrPointer returned by TryAllocate

Definition at line 107 of file allocator_traits.hpp.

◆ IsAligned()

bool helios::mem::IsAligned ( const void * ptr,
size_t alignment )
inlinenodiscardnoexcept

Checks if pointer is aligned.

Warning
Triggers assertion when alignment is not a power of two.
Parameters
ptrPointer value
alignmentAlignment to check
Returns
True if pointer is aligned

Definition at line 260 of file common.hpp.

◆ IsPowerOfTwo()

bool helios::mem::IsPowerOfTwo ( size_t value)
nodiscardconstexprnoexcept

Checks if a value is a power of two.

Parameters
valueValue to check
Returns
True if value is a power of two

Definition at line 215 of file common.hpp.

◆ MakeArc()

template<typename T, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...>
auto helios::mem::MakeArc ( Args &&... args) -> Arc< T >
inlinenodiscard

Allocates and constructs a T object, returning an Arc<T, std::allocator<T>> handle.

Preferred way to create AtomicRefCounted objects. The object is allocated via std::allocator<T> and owned by the returned handle.

Note
Only available when std::allocator<T> is default-constructible (it always is). For stateful allocators (e.g. arena, pool), use MakeArcWith instead.
Template Parameters
TType to construct; must inherit ArcFromThis<T>
ArgsConstructor argument types
Parameters
argsArguments forwarded to T's constructor
Returns
A new Arc<T> with ref count 1
auto tex = helios::mem::MakeArc<Texture>("diffuse");
auto MakeArc(Args &&... args) -> Arc< T >
Allocates and constructs a T object, returning an Arc<T, std::allocator<T>> handle.

Definition at line 1204 of file ref_counted.hpp.

◆ MakeArcWith() [1/3]

template<typename T, typename Allocator, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...> && (!std::derived_from<std::remove_pointer_t
<Allocator>, std::pmr::memory_resource>)
auto helios::mem::MakeArcWith ( Allocator alloc,
Args &&... args ) -> Arc< T, Allocator >
inlinenodiscard

Overload of MakeArc accepting a pre-constructed allocator instance.

Use this when your allocator is stateful and must be initialised before the object is created (e.g. a PMR arena).

Template Parameters
TType to construct; must inherit ArcFromThis<T>
AllocatorConcrete allocator type (e.g. MyAllocator<T>)
ArgsConstructor argument types
Parameters
allocAllocator instance to use
argsArguments forwarded to T's constructor
Returns
A new Arc<T, Allocator> with ref count 1

Definition at line 1281 of file ref_counted.hpp.

◆ MakeArcWith() [2/3]

template<typename T, typename... Args>
auto helios::mem::MakeArcWith ( std::nullptr_t ,
Args && ... ) -> PmrArc< T >=delete
delete

◆ MakeArcWith() [3/3]

template<typename T, typename... Args>
requires std::derived_from<T, ArcFromThis<T>> && std::constructible_from<T, Args...>
auto helios::mem::MakeArcWith ( std::pmr::memory_resource * resource,
Args &&... args ) -> PmrArc< T >
inlinenodiscard

Allocates and constructs a T object using PMR memory resource, returning a PmrArc<T> handle.

Convenience overload that wraps std::pmr::memory_resource* in a std::pmr::polymorphic_allocator<T>.

Template Parameters
TType to construct; must inherit ArcFromThis<T>
ArgsConstructor argument types
Parameters
resourcePMR memory resource to use for allocation
argsArguments forwarded to T's constructor
Returns
A new PmrArc<T> with ref count 1
auto tex = helios::mem::MakeArcWith<Texture>(pmr_resource, "diffuse");
auto MakeArcWith(Allocator alloc, Args &&... args) -> Arc< T, Allocator >
Overload of MakeArc accepting a pre-constructed allocator instance.

Definition at line 1307 of file ref_counted.hpp.

◆ MakeRc()

template<typename T, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...>
auto helios::mem::MakeRc ( Args &&... args) -> Rc< T >
inlinenodiscard

Allocates and constructs a T object, returning a Rc<T, std::allocator<T>> handle.

Preferred way to create RefCounted objects. The object is allocated via std::allocator<T> and owned by the returned handle.

Template Parameters
TType to construct; must inherit RcFromThis<T>
ArgsConstructor argument types
Parameters
argsArguments forwarded to T's constructor
Returns
A new Rc<T> with ref count 1
auto mesh = helios::mem::MakeRc<Mesh>(42);
auto MakeRc(Args &&... args) -> Rc< T >
Allocates and constructs a T object, returning a Rc<T, std::allocator<T>> handle.

Definition at line 1174 of file ref_counted.hpp.

◆ MakeRcWith() [1/3]

template<typename T, typename Allocator, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...> && (!std::derived_from<std::remove_pointer_t
<Allocator>, std::pmr::memory_resource>)
auto helios::mem::MakeRcWith ( Allocator alloc,
Args &&... args ) -> Rc< T, Allocator >
inlinenodiscard

Overload of MakeRc accepting a pre-constructed allocator instance.

Use this when your allocator is stateful and must be initialised before the object is created (e.g. a PMR arena).

Template Parameters
TType to construct; must inherit RcFromThis<T>
AllocatorConcrete allocator type (e.g. MyAllocator<T>)
ArgsConstructor argument types
Parameters
allocAllocator instance to use
argsArguments forwarded to T's constructor
Returns
A new Rc<T, Allocator> with ref count 1

Definition at line 1229 of file ref_counted.hpp.

◆ MakeRcWith() [2/3]

template<typename T, typename... Args>
auto helios::mem::MakeRcWith ( std::nullptr_t ,
Args && ... ) -> PmrRc< T >=delete
delete

◆ MakeRcWith() [3/3]

template<typename T, typename... Args>
requires std::derived_from<T, RcFromThis<T>> && std::constructible_from<T, Args...>
auto helios::mem::MakeRcWith ( std::pmr::memory_resource * resource,
Args &&... args ) -> PmrRc< T >
inlinenodiscard

Allocates and constructs a T object using PMR memory resource, returning a PmrRc<T> handle.

Convenience overload that wraps std::pmr::memory_resource* in a std::pmr::polymorphic_allocator<T>.

Template Parameters
TType to construct; must inherit RcFromThis<T>
ArgsConstructor argument types
Parameters
resourcePMR memory resource to use for allocation
argsArguments forwarded to T's constructor
Returns
A new PmrRc<T> with ref count 1
auto mesh = helios::mem::MakeRcWith<Mesh>(pmr_resource, 42);
auto MakeRcWith(Allocator alloc, Args &&... args) -> Rc< T, Allocator >
Overload of MakeRc accepting a pre-constructed allocator instance.

Definition at line 1255 of file ref_counted.hpp.

◆ MemoryErrorToString()

std::string_view helios::mem::MemoryErrorToString ( MemoryError error)
nodiscardconstexprnoexcept

Converts MemoryError to a readable string.

Parameters
errorError value
Returns
String view with the error description

Definition at line 192 of file common.hpp.

◆ SaturatingAdd()

size_t helios::mem::SaturatingAdd ( size_t lhs,
size_t rhs )
nodiscardconstexprnoexcept

Saturating add for size_t.

Parameters
lhsLeft operand
rhsRight operand
Returns
lhs + rhs, clamped to size_t max on overflow

Definition at line 25 of file common.hpp.

◆ SaturatingMul()

size_t helios::mem::SaturatingMul ( size_t lhs,
size_t rhs )
nodiscardconstexprnoexcept

Saturating multiply for size_t.

Parameters
lhsLeft operand
rhsRight operand
Returns
lhs * rhs, clamped to size_t max on overflow

Definition at line 39 of file common.hpp.

◆ Stats()

template<PmrAllocator Alloc>
AllocatorStats helios::mem::Stats ( const Alloc & allocator)
nodiscardconstexprnoexcept

Adapts stats-aware PMR resources.

Template Parameters
AllocAllocator type
Parameters
allocatorAllocator reference
Returns
Stats if available, otherwise zero-initialized stats

Definition at line 140 of file allocator_traits.hpp.

◆ TryAllocate()

template<typename T, PmrAllocator Alloc>
auto helios::mem::TryAllocate ( Alloc & allocator) -> std::expected< T *, MemoryError >
inlinenodiscardnoexcept

Tries to allocate one object from a PMR allocator.

Template Parameters
TObject type
AllocAllocator type
Parameters
allocatorAllocator reference
Returns
Pointer to allocated object memory, or MemoryError

Definition at line 48 of file allocator_traits.hpp.

◆ TryAllocateSpan()

template<typename T, PmrAllocator Alloc>
auto helios::mem::TryAllocateSpan ( Alloc & allocator,
size_t count ) -> std::expected< std::span< T >, MemoryError >
inlinenodiscardnoexcept

Tries to allocate an array of objects from a PMR allocator.

Template Parameters
TElement type
AllocAllocator type
Parameters
allocatorAllocator reference
countNumber of elements
Returns
Span over allocated memory, or MemoryError

Definition at line 76 of file allocator_traits.hpp.

Variable Documentation

◆ kDefaultAlignment

size_t helios::mem::kDefaultAlignment = 64
inlineconstexpr

Default alignment used by memory resources.

Definition at line 14 of file common.hpp.

◆ kMinAlignment

size_t helios::mem::kMinAlignment = alignof(std::max_align_t)
inlineconstexpr

Minimum alignment used by memory resources.

Definition at line 17 of file common.hpp.