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

Classes

struct  AllocationResult
 Result type for allocation operations. More...
 
struct  AllocatorStats
 Statistics for tracking allocator usage. More...
 
class  ArenaAllocator
 Lock-free, thread-safe arena allocator. More...
 
class  DoubleFrameAllocator
 Double-buffered frame allocator. More...
 
class  FrameAllocator
 Linear allocator that clears every frame. More...
 
class  FreeListAllocator
 Free list allocator with best-fit strategy. More...
 
class  GrowableAllocator
 Growable allocator adapter that automatically expands capacity. More...
 
class  NFrameAllocator
 N-buffered frame allocator. More...
 
class  PoolAllocator
 Pool allocator for fixed-size allocations. More...
 
class  StackAllocator
 Stack/linear allocator with LIFO deallocation support. More...
 
class  STLAllocatorAdapter
 STL-compatible allocator adapter for custom allocators. More...
 

Concepts

concept  Allocator
 Concept for basic allocator interface.
 
concept  ResettableAllocator
 Concept for allocators that can be reset/cleared.
 
concept  AllocatorWithStats
 Concept for allocators that provide statistics.
 

Typedefs

using TripleFrameAllocator = NFrameAllocator< 3 >
 Triple-buffered frame allocator.
 
using QuadFrameAllocator = NFrameAllocator< 4 >
 Quad-buffered frame allocator.
 
template<typename T >
using STLFrameAllocator = STLAllocatorAdapter< T, FrameAllocator >
 
template<typename T >
using STLPoolAllocator = STLAllocatorAdapter< T, PoolAllocator >
 
template<typename T >
using STLStackAllocator = STLAllocatorAdapter< T, StackAllocator >
 
template<typename T >
using STLFreeListAllocator = STLAllocatorAdapter< T, FreeListAllocator >
 
template<typename T , typename UnderlyingAlloc >
using STLGrowableAllocator = STLAllocatorAdapter< T, GrowableAllocator< UnderlyingAlloc > >
 

Functions

constexpr size_t AlignUp (size_t size, size_t alignment) noexcept
 Helper function to align a size up to the given alignment.
 
void * AlignUpPtr (void *ptr, size_t alignment) noexcept
 Helper function to align a pointer up to the given alignment.
 
bool IsAligned (const void *ptr, size_t alignment) noexcept
 Helper function to check if a pointer is aligned.
 
constexpr bool IsPowerOfTwo (size_t size) noexcept
 Helper function to check if a size is a power of 2.
 
size_t CalculatePadding (const void *ptr, size_t alignment) noexcept
 Calculate padding needed for alignment.
 
size_t CalculatePaddingWithHeader (const void *ptr, size_t alignment, size_t header_size) noexcept
 Calculate padding with header for alignment.
 
template<typename T , Allocator Alloc>
constexpr T * Allocate (Alloc &allocator) noexcept
 
template<typename T , Allocator Alloc>
constexpr T * Allocate (Alloc &allocator, size_t count) noexcept
 
template<typename T , Allocator Alloc, typename... Args>
requires std::constructible_from<T, Args...>
constexpr T * AllocateAndConstruct (Alloc &allocator, Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args... >)
 
template<typename T , Allocator Alloc>
requires std::default_initializable<T>
constexpr T * AllocateAndConstructArray (Alloc &allocator, size_t count) noexcept(std::is_nothrow_default_constructible_v< T >)
 
void * AlignedAlloc (size_t alignment, size_t size)
 Allocate memory with the specified alignment.
 
void AlignedFree (void *ptr)
 Free memory allocated with AlignedAlloc.
 

Variables

constexpr size_t kDefaultAlignment = 64
 Default alignment for allocations (cache line size for most modern CPUs).
 
constexpr size_t kMinAlignment = alignof(std::max_align_t)
 Minimum alignment for any allocation.
 

Typedef Documentation

◆ QuadFrameAllocator

Quad-buffered frame allocator.

Definition at line 323 of file n_frame_allocator.hpp.

◆ STLFrameAllocator

◆ STLFreeListAllocator

◆ STLGrowableAllocator

◆ STLPoolAllocator

◆ STLStackAllocator

◆ TripleFrameAllocator

Triple-buffered frame allocator.

Definition at line 322 of file n_frame_allocator.hpp.

Function Documentation

◆ AlignedAlloc()

void * helios::memory::AlignedAlloc ( size_t  alignment,
size_t  size 
)
inline

Allocate memory with the specified alignment.

Allocates a block of memory of the specified size aligned to the specified alignment boundary. The alignment must be a power of two and non-zero. The size must also be non-zero.

Uses _aligned_malloc on MSVC and std::aligned_alloc on other platforms.

Warning
Triggers assertion in next cases:
  • alignment is zero
  • alignment is not a power of two
  • 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).
Returns
A pointer to the allocated memory block.
Exceptions
std::bad_allocif the allocation fails.
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/frame_allocator.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/free_list_allocator.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/pool_allocator.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stack_allocator.hpp.

Definition at line 30 of file common.hpp.

30 {
31 HELIOS_ASSERT(alignment != 0, "Failed to allocate memory: alignment cannot be zero!");
32 HELIOS_ASSERT(IsPowerOfTwo(alignment), "Failed to allocate memory: alignment must be a power of two!");
33 HELIOS_ASSERT(size != 0, "Failed to allocate memory: size cannot be zero!");
34#ifdef _MSC_VER
35 return _aligned_malloc(size, alignment);
36#else
37 // POSIX aligned_alloc requires size to be a multiple of alignment
38 // Round up size to the next multiple of alignment
39 return std::aligned_alloc(alignment, AlignUp(size, alignment));
40#endif
41}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
constexpr size_t AlignUp(size_t size, size_t alignment) noexcept
Helper function to align a size up to the given alignment.
constexpr bool IsPowerOfTwo(size_t size) noexcept
Helper function to check if a size is a power of 2.

◆ AlignedFree()

void helios::memory::AlignedFree ( void *  ptr)
inline

Free memory allocated with AlignedAlloc.

Frees a block of memory allocated with AlignedAlloc. The ptr must be a pointer returned by AlignedAlloc.

Uses _aligned_free on MSVC and std::free on other platforms.

Warning
Triggers assertion if ptr is nullptr.
Parameters
ptrThe pointer to the memory block to free.
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/frame_allocator.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/free_list_allocator.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/pool_allocator.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stack_allocator.hpp.

Definition at line 53 of file common.hpp.

53 {
54 HELIOS_ASSERT(ptr != nullptr, "Failed to free memory: pointer cannot be nullptr!");
55#ifdef _MSC_VER
56 _aligned_free(ptr);
57#else
58 std::free(ptr);
59#endif
60}

◆ AlignUp()

constexpr size_t helios::memory::AlignUp ( size_t  size,
size_t  alignment 
)
constexprnoexcept

Helper function to align a size up to the given alignment.

Parameters
sizeSize to align
alignmentAlignment boundary (must be power of 2)
Returns
Aligned size
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/pool_allocator.hpp.

Definition at line 91 of file allocator_traits.hpp.

91 {
92 return (size + alignment - 1) & ~(alignment - 1);
93}

◆ AlignUpPtr()

void * helios::memory::AlignUpPtr ( void *  ptr,
size_t  alignment 
)
inlinenoexcept

Helper function to align a pointer up to the given alignment.

Parameters
ptrPointer to align
alignmentAlignment boundary (must be power of 2)
Returns
Aligned pointer
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp.

Definition at line 101 of file allocator_traits.hpp.

101 {
102 const auto addr = reinterpret_cast<uintptr_t>(ptr);
103 const auto aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
104 return reinterpret_cast<void*>(aligned_addr);
105}

◆ Allocate() [1/2]

◆ Allocate() [2/2]

template<typename T , Allocator Alloc>
constexpr T * helios::memory::Allocate ( Alloc &  allocator,
size_t  count 
)
constexprnoexcept

Definition at line 212 of file allocator_traits.hpp.

212 {
213 if (count == 0) [[unlikely]] {
214 return nullptr;
215 }
216 constexpr size_t alignment = std::max(alignof(T), kMinAlignment);
217 const size_t size = sizeof(T) * count;
218 auto result = allocator.Allocate(size, alignment);
219 return static_cast<T*>(result.ptr);
220}

◆ AllocateAndConstruct()

template<typename T , Allocator Alloc, typename... Args>
requires std::constructible_from<T, Args...>
constexpr T * helios::memory::AllocateAndConstruct ( Alloc &  allocator,
Args &&...  args 
)
constexprnoexcept
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp.

Definition at line 241 of file allocator_traits.hpp.

242 {
243 T* ptr = Allocate<T>(allocator);
244 if (ptr != nullptr) [[likely]] {
245 std::construct_at(ptr, std::forward<Args>(args)...);
246 }
247 return ptr;
248}

◆ AllocateAndConstructArray()

template<typename T , Allocator Alloc>
requires std::default_initializable<T>
constexpr T * helios::memory::AllocateAndConstructArray ( Alloc &  allocator,
size_t  count 
)
constexprnoexcept
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp.

Definition at line 268 of file allocator_traits.hpp.

269 {
270 T* ptr = Allocate<T>(allocator, count);
271 if (ptr != nullptr) [[likely]] {
272 for (size_t i = 0; i < count; ++i) {
273 std::construct_at(ptr + i);
274 }
275 }
276 return ptr;
277}

◆ CalculatePadding()

size_t helios::memory::CalculatePadding ( const void *  ptr,
size_t  alignment 
)
inlinenoexcept

Calculate padding needed for alignment.

Parameters
ptrCurrent pointer
alignmentDesired alignment
Returns
Padding needed in bytes
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/arena_allocator.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/frame_allocator.hpp.

Definition at line 132 of file allocator_traits.hpp.

132 {
133 const auto addr = reinterpret_cast<uintptr_t>(ptr);
134 const auto aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
135 return aligned_addr - addr;
136}

◆ CalculatePaddingWithHeader()

size_t helios::memory::CalculatePaddingWithHeader ( const void *  ptr,
size_t  alignment,
size_t  header_size 
)
inlinenoexcept

Calculate padding with header for alignment.

Parameters
ptrCurrent pointer
alignmentDesired alignment
header_sizeSize of header to include
Returns
Padding needed in bytes
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp, /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/free_list_allocator.hpp, and /home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stack_allocator.hpp.

Definition at line 145 of file allocator_traits.hpp.

145 {
146 const auto addr = reinterpret_cast<uintptr_t>(ptr);
147 const auto aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
148 size_t padding = aligned_addr - addr;
149
150 // Check if we need more padding to fit the header
151 if (padding < header_size) {
152 header_size -= padding;
153 // Align header_size to alignment
154 if (header_size % alignment > 0) {
155 padding += alignment * (1 + (header_size / alignment));
156 } else {
157 padding += alignment * (header_size / alignment);
158 }
159 }
160
161 return padding;
162}

◆ IsAligned()

bool helios::memory::IsAligned ( const void *  ptr,
size_t  alignment 
)
inlinenoexcept

Helper function to check if a pointer is aligned.

Parameters
ptrPointer to check
alignmentAlignment boundary
Returns
True if pointer is aligned
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/allocator_traits.hpp.

Definition at line 113 of file allocator_traits.hpp.

113 {
114 return (reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) == 0;
115}

◆ IsPowerOfTwo()

Variable Documentation

◆ kDefaultAlignment

◆ kMinAlignment