Overview / Foundation

memory

Arena and pool allocators, and a central manager that tracks allocations by tag.

Always compiled: part of the core build.Include #include <ethereal/cpp/memory/memory_inc.hpp>C layer

memory_inc.hpp

#include <ethereal/cpp/memory/memory_inc.hpp>

allocator.hpp

#include <ethereal/cpp/memory/allocator.hpp>

The injectable allocator of the library: IAllocator.

A module that takes an IAllocator does every allocation of its own through it, so the user can put the memory where they want (an arena, a budget, a counter) and the tests can count what is left. Null in a module parameter means the default allocator.

The calls are virtual. The modules only call them on cold paths (load, workspace creation), never in an inner loop.

Everything here is C++17 and inline, except DefaultAllocator, which the library defines. The only Ethereal header it includes is etl.h (types and macros), so the Testing framework (which does not link the library) can still implement IAllocator.

class

IAllocator

Ethereal::Memory#

An allocator a module receives from the user.

class IAllocator

The contract:

  • Allocate returns a block of at least uBytes bytes aligned to uAlign (a power of two), or null when it cannot. It never throws.
  • Deallocate receives the same size that was asked for. Null is ignored.
  • A block is released by the same allocator that gave it.

Thread safety: up to the implementation. A module documents when it calls the allocator from more than one thread.

The destructor is protected and not virtual: an allocator is never owned or destroyed through this interface. That also keeps the default allocator trivially destructible, so it outlives every static object that still releases memory at exit.

function

Allocate

#
[[nodiscard]] virtual void * Allocate( std::size_t uBytes, std::size_t uAlign ) noexcept = 0;

Allocates a block.

uBytes
Size in bytes. Zero may return a valid block or null.
uAlign
Alignment, a power of two.
The block, or null when out of memory or when the alignment is not supported.
function

Deallocate

#
virtual void Deallocate( void * p, std::size_t uBytes ) noexcept = 0;

Releases a block.

p
The block, or null (ignored).
uBytes
The size given to Allocate.
const

kDefaultAllocatorAlignment

Ethereal::Memory#
inline constexpr std::size_t kDefaultAllocatorAlignment = 64u;

Alignment of every block of the default allocator, in bytes.

Deallocate does not receive the alignment, and the aligned 'operator delete' must get the same value as the 'operator new'. So the default allocator uses one alignment for every block, the cache line of the x86 and ARM processors the library targets.

class

CDefaultAllocator

Ethereal::Memory#

The allocator used when a module receives null.

class CDefaultAllocator final : public IAllocator

The aligned 'operator new' with std::nothrow, always with kDefaultAllocatorAlignment. An alignment above it (or not a power of two) returns null: those need an allocator of the user.

Thread safety: full, as the global 'operator new'. No state.

constructor

CDefaultAllocator

#
constexpr CDefaultAllocator() noexcept = default;

Default constructor. Constant, so the instance of DefaultAllocator needs no dynamic initialization.

function

Allocate

#
[[nodiscard]] void * Allocate( const std::size_t uBytes, const std::size_t uAlign ) noexcept override

Allocates a block aligned to kDefaultAllocatorAlignment.

uBytes
Size in bytes.
uAlign
Alignment, a power of two up to kDefaultAllocatorAlignment.
The block, or null.
function

Deallocate

#
void Deallocate( void * const p, const std::size_t uBytes ) noexcept override

Releases a block of this allocator.

p
The block, or null (ignored).
uBytes
The size given to Allocate (not needed here).
function

DefaultAllocator

Ethereal::Memory#
IAllocator & DefaultAllocator() noexcept;

The default allocator, shared by the whole process. Never destroyed.

Defined in the library, not inline: with a shared library, a static inside an inline function gives the library one instance and each program another, and their addresses differ.

arena.hpp

#include <ethereal/cpp/memory/arena.hpp>
struct

SArenaAllocHeader

Ethereal::Memory#
C++
ETL_BEGIN_INTENDED_PADDING
struct    ETL_STRUCT_ALIGN    SArenaAllocHeader     final
{
    /// @brief    Stores the requested size.
    std::size_t Size;

    /// @brief    Text tag associated with the allocation (optional).
    char Tag[ 32 ];
};

Optional per-allocation header structure (for debug/tag).

struct

SArenaChunk

Ethereal::Memory#
C++
ETL_END_INTENDED_PADDING


/// @brief    Structure of a chunk allocated by the arena.
///
struct    ETL_STRUCT_ALIGN    SArenaChunk        final
{
    /// @brief    Links the next chunk.
    SArenaChunk * Next;

    /// @brief    Base address of the chunk (whole block).
    ETL_UINT8 * Base;

    /// @brief    Current pointer of the bump allocator.
    ETL_UINT8 * Current;

    /// @brief    End pointer of the chunk (limit).
    ETL_UINT8 * End;
};
struct

SArenaMarker

Ethereal::Memory#
C++
ETL_BEGIN_INTENDED_PADDING
struct    ETL_STRUCT_ALIGN    SArenaMarker     final
{
    /// @brief    Chunk in which the marker was created.
    SArenaChunk * Chunk;

    /// @brief    Position of the bump pointer within the chunk.
    ETL_UINT8 * Position;
};

Marker for a partial rewind (checkpoint) of the arena.

class

CMemoryArena

Ethereal::Memory#
class CMemoryArena final
constructor

CMemoryArena

#
CMemoryArena() noexcept;

Default constructor.

function

Initialize

#
ETL_INT32 Initialize( const std::size_t iBaseChunkSize, const std::size_t iAlignment, const bool bIsAllowedToExpand = false, const bool bEnableMutexThreadProtection = true ) noexcept;

Initializes the memory arena. Creates the first memory chunk and configures the alignment, expansion and internal mutex protection policies. The arena then operates on the bump allocator model.

The internal mutex can be disabled for maximum performance, but this makes usage not thread-safe .

iBaseChunkSize
Base size of the first chunk of the arena.
iAlignment
Default alignment of the allocations (power of 2).
bIsAllowedToExpand
Allows automatic creation of new chunks.
bEnableMutexThreadProtection
Enables internal mutex protection.
Result code: - 0 : Success - 1 : Arena already initialized - 2 : Failed to allocate the initial memory - 3 : Not licensed (see Bundle::UnlockBundle)
This function is not thread-safe. Must be called before any concurrent access.
function

Shutdown

#
void Shutdown( void ) noexcept;

Shuts down the arena and releases all allocated chunks. Releases all memory owned by the arena and fully resets the internal state. All previously returned pointers become invalid.

This function is not thread-safe . Must not be called while other threads use the arena.
function

Allocate

#
ETL_PTR Allocate( const std::size_t iSize, const char * sTag = nullptr ) noexcept;

Allocates memory in the arena using the default alignment. The allocation is linear (bump allocation), with no support for individual free.

iSize
Allocation size in bytes.
sTag
Optional tag for debug/profiling.
Pointer to the allocated memory or @nullptr on failure.
Thread-safety depends on the internal mutex configuration. - Mutex enabled : thread-safe - Mutex disabled: not thread-safe
function

AllocateAligned

#
ETL_PTR AllocateAligned( const std::size_t iSize, const std::size_t iAlignment, const char * sTag = nullptr ) noexcept;

Allocates memory in the arena with a specific alignment. Allows overriding the default arena alignment for a specific allocation.

iSize
Allocation size in bytes.
iAlignment
Desired alignment (power of 2).
sTag
Optional tag for debug/profiling.
Pointer to the allocated memory or @nullptr on failure.
Thread-safety depends on the internal mutex configuration. - Mutex enabled : thread-safe - Mutex disabled: not thread-safe
function

Deallocate

#
ETL_INT32 Deallocate( ETL_PTR pAddress ) noexcept;

Performs the logical release of an arena allocation. Only logically invalidates the memory block, zeroing its content for safety and debug purposes.

pAddress
Pointer previously returned by @Allocate or @AllocateAligned.
Result code: - 0 : Success - 1 : Arena not initialized - 2 : Invalid pointer
This function is thread-safe . Access to the internal structures is protected by a mutex.
The arena operates on the bump allocator model. This function does not return memory to the arena or to the system. Actual release happens only via Reset() or FreeToMarker().
function

Reset

#
void Reset( void ) noexcept;

Resets the arena to the initial state.

Releases all extra chunks and reuses only the base chunk, moving the bump pointer back. All previous allocations become invalid.

This function is not thread-safe . Must not be called during concurrent use.
function

GetMarker

#
SArenaMarker GetMarker( void ) noexcept;

Gets a marker of the current arena state. The marker can be used later to restore the arena to this point.

SArenaMarker structure representing the current state.
Thread-safety depends on the internal mutex configuration.
function

FreeToMarker

#
void FreeToMarker( const SArenaMarker & Marker ) noexcept;

Releases all allocations made after a marker. Restores the arena state exactly to the point captured by the marker.

Marker
Marker previously obtained with GetMarker().
This function is not thread-safe . Must be used only in controlled contexts.
function

GetDefaultAlignment

#
std::size_t GetDefaultAlignment( void ) const noexcept;

Gets the default alignment of the arena.

Default alignment in bytes.
function

IsInitialized

#
bool IsInitialized( void ) const noexcept;

Checks whether the arena is initialized.

@true if initialized, @false otherwise.

manager.hpp

#include <ethereal/cpp/memory/manager.hpp>
enum

EMemoryPolicy

Ethereal::Memory#
C++
enum class EMemoryPolicy
{
    /// @brief    Memory pool allocation
    ePool,

    /// @brief    Memory arena allocation
    eArena
};

Identification of the allocation policy types.

Declared enum class on purpose. EMemoryTag, just below, also has the values ePool and eArena. With two unscoped enums in the same namespace the two pairs of enumerators collided and the header did not compile (MSVC C2365, redefinition). Every use was already qualified (EMemoryPolicy::ePool), so the scope fixes the collision without changing any caller.
enum

EMemoryTag

Ethereal::Memory#
C++
enum EMemoryTag
{
    /// @brief    Generic (temporary allocations inside functions, or insignificant for statistics in general)
    eGeneric = 0,

    /// @brief    Pool and Arena (Ethereal)
    ePool,
    eArena,

    /// @brief    Base instances (Ethereal, Backends, etc)
    eInstance,

    /// @brief    Managers in general
    eManager,

    /// @brief    Strings of the @ref String::CAString class
    eString,

    /// @brief    Network and related
    eNetwork,

    /// @brief    API asynchronous tasks
    eApiJob,

    /// @brief    Generic asynchronous tasks
    eAsyncJob,

    /// @brief    Objects in general
    eObject,

    /// @brief    Data chunk
    eDataChunk,

    /// @brief    Array-only (for internal checks only, never use)
    eLast
};

Identification of the allocation types.

function

MemoryTagToString

Ethereal::Memory#
const char * MemoryTagToString( const EMemoryTag Tag ) noexcept;

Gets the memory tag as a C String.

Memory tag as a C String.
struct

SAllocationInfo

Ethereal::Memory#
C++
ETL_BEGIN_INTENDED_PADDING
struct   ETL_STRUCT_ALIGN   SAllocationInfo    final
{
    /// @brief    Allocated size in bytes.
    std::size_t Size;

    /// @brief    Alignment of the allocation.
    std::size_t Alignment;

    /// @brief    Flag for an allocation in Array form.
    bool isArray;

    /// @brief    Identification of the file where the allocation happened.
    const char * FileName;

    /// @brief    Identification of the file where the allocation happened.
    ///char FileName[ 32 ];

    /// @brief    Code line in the file where the allocation happened.
    ETL_INT32 FileLine;

    /// @brief    Allocation identification tag.
    Ethereal::Memory::EMemoryTag tag;

    /// @brief    Set when the memory came from the pool or arena of the tag, clear when it
    ///            came from the heap fallback. The release goes back to the same place even
    ///            if a policy was created or released in between (see CMemoryManager::Shutdown).
    bool bFromAllocator = false;
};

Data structure that stores the data of the allocations made.

struct

STagStats

Ethereal::Memory#
C++
ETL_END_INTENDED_PADDING


/// @brief    Data structure for the allocation statistics.
///
ETL_BEGIN_INTENDED_PADDING
struct   ETL_STRUCT_ALIGN   STagStats    final
{
    /// @brief    Allocation counter.
    std::size_t Allocations;

    /// @brief    Allocated bytes counter.
    std::size_t BytesAllocated;
};
struct

SAllocationPolicy

Ethereal::Memory#
C++
ETL_END_INTENDED_PADDING


/// @brief    Allocation policy of a pool/arena.
///
ETL_BEGIN_INTENDED_PADDING
struct   ETL_STRUCT_ALIGN   SAllocationPolicy    final
{
    /// @brief    Size of the pool/arena in bytes.
    std::size_t Size;

    /// @brief    Alignment of the pool/arena.
    std::size_t Alignment;

    /// @brief    Flag that allows the pool/arena to auto-expand when no more memory is available.
    bool AllowToAutoExpand;

    /// @brief    Flag for using a Mutex for @thread-safe operations in the pool/arena.
    ///            A performance impact is expected.
    bool EnableMutexProtection;
};
class

CMemoryManager

Ethereal::Memory#
class CMemoryManager final
function

Instance

#
static CMemoryManager & Instance() noexcept;

Gets the singleton instance of the memory manager.

Reference to the single instance of CMemoryManager.
function

Shutdown

#
static std::size_t Shutdown() noexcept;

Releases the pools and arenas of the manager, and its own bookkeeping when nothing is pending.

The singleton is never destroyed: a static object built before it (a 'CAString' of the library, for one) may free memory through it after 'main' returns, so destroying it at exit would crash that object. The cost is that nothing returns the pools and arenas on its own. In a process that only exits, the system takes them back. A module that links Ethereal statically and is loaded and unloaded many times (a plugin, a DLL) leaks them on every unload. Call Shutdown before the unload: at the end function of the module, after the code of the module stopped allocating.

What it does:

  • every pool and arena created by CreateAllocationPolicy is reset and released, and its tag goes back to the heap fallback. A new policy can be created for the tag afterwards.
  • allocations still pending in those pools and arenas are dropped from the bookkeeping. Their memory is gone, and the pointers must not be used or freed afterwards.
  • allocations from the heap fallback stay valid and tracked, and can still be freed through the manager. When none is pending, the bookkeeping map gives its memory back too.

The manager stays usable after the call. Calling it twice is safe.

Number of allocations still pending after the call: those from the heap fallback, plus those dropped with their pool or arena. '0' means the manager holds no memory any more.
This function is thread-safe against itself, but not against allocations running on other threads at the same time.
function

CreateAllocationPolicy

#
ETL_INT32 CreateAllocationPolicy( const EMemoryPolicy MemoryPolicyID, const EMemoryTag TargetMemoryTag, const SAllocationPolicy PolicyData ) noexcept;

Creates and initializes a memory allocation policy per tag. Associates an allocation policy (Pool or Arena) with a specific memory tag, ensuring that only one policy type exists per tag. The function is responsible for: - Allocating the Pool or Arena structure, if needed. - Initializing the policy with the given data. - Ensuring exclusivity between Pool and Arena per tag.

MemoryPolicyID
Allocation policy type (Pool or Arena).
TargetMemoryTag
Memory tag the policy is associated with.
PolicyData
Structure containing the policy parameters: - Size - Alignment - AllowToAutoExpand - EnableMutexProtection
Result code: - 0 : Success - -1 : Another policy (Pool/Arena) is already associated with the tag - -2 : Failed to allocate the policy structure - -3 : Policy already initialized for this tag - -4 : Not licensed (see Bundle::UnlockBundle) - >0 : Error code returned by Initialize() of the Pool/Arena
This function is not thread-safe. Must be called during the system initialization phase, before any concurrent use of the memory manager.
Only one allocation policy can exist per tag. Mixing Pool and Arena for the same tag is not allowed.
function

AllocateRaw

#
ETL_PTR AllocateRaw( const std::size_t Size, const std::size_t Alignment, const char * File, const ETL_INT32 FileLine, const bool bIsArray, const EMemoryTag Tag ) noexcept;

Performs a traceable memory allocation.

Does not call the class or object constructor.

Size
Allocation size in bytes.
Alignment
Alignment of the memory to allocate.
File
Source file of the allocation (for tracking and debug).
FileLine
Code line of the allocation (for tracking and debug).
bIsArray
Indicates whether the allocation is an array.
Tag
Category tag of the allocation (memory categorized by subsystem or purpose).
Pointer to the allocated memory block (ETL_PTR).
function

DeallocateRaw

#
void DeallocateRaw( ETL_PTR Ptr, const bool bIsArray ) noexcept;

Releases previously allocated memory.

Does not call the class or object destructor.

Ptr
Pointer to the memory block to release (ETL_PTR).
bIsArray
Indicates whether the allocation was made as an array (new[] / delete[]).
function

Allocate

#
template< typename T, typename... Args > T * Allocate( const char * File, const ETL_INT32 FileLine, const EMemoryTag Tag, Args &&... args )

Allocates memory for the element and calls its constructor.

File
Source file of the allocation (for tracking and debug).
FileLine
Code line of the allocation (for tracking and debug).
Tag
Category tag of the allocation (memory categorized by subsystem or purpose).
args
Additional arguments.
Pointer to the allocated memory.
function

Deallocate

#
template< typename T > void Deallocate( T * Ptr )

Releases the memory of the element and calls its destructor.

Ptr
Pointer to the allocated memory to release.
function

AllocateArray

#
template< typename T > T * AllocateArray( const std::size_t ArrayElementCount, const char * File, const ETL_INT32 FileLine, const EMemoryTag Tag )

Allocates memory for the array element and calls its constructor.

ArrayElementCount
Total count of array elements to allocate.
File
Source file of the allocation (for tracking and debug).
FileLine
Code line of the allocation (for tracking and debug).
Tag
Category tag of the allocation (memory categorized by subsystem or purpose).
Pointer to the allocated array memory.
function

DeallocateArray

#
template< typename T > void DeallocateArray( T * Ptr, const std::size_t ArrayElementCount )

Releases the memory of the array element and calls its destructor.

Ptr
Pointer to the allocated memory to release.
ArrayElementCount
Total count of array elements to release.
function

DumpLeaks

#
ETL_INT32 DumpLeaks( const EMemoryTag Filter, const std::size_t iOutBufferLength, char * sOutBuffer ) noexcept;

Generates a memory leak report in a C buffer. Produces a text report containing all pending allocations in the memory manager. The report can be filtered by allocation type.

Filter
Filter by allocation type.
iOutBufferLength
Size of the output buffer.
sOutBuffer
Output buffer.
Result code: - 0 : Success - 3 : Insufficient buffer
This function is thread-safe . Access to the internal structures is protected by a mutex.
function

DumpLeaks

#
ETL_INT32 DumpLeaks( const EMemoryTag Filter, std::string & sLeaksOut ) noexcept;

Generates a memory leak report in a std::string. Builds a text report with detailed information about possible memory leaks.

sLeaksOut
String that receives the report.
'0' on success.
This function is thread-safe . Access to the internal data is protected by a mutex.
function

DumpLeaks

#
ETL_INT32 DumpLeaks( const EMemoryTag Filter, String::CAString & sLeaksOut ) noexcept;

Generates a memory leak report in a CAString. Version of the leak dump function using the String::CAString class of the internal infrastructure.

sLeaksOut
Output string that receives the report.
'0' on success.
This function is thread-safe . Access to the internal data is protected by a mutex.
function

DumpStats

#
ETL_INT32 DumpStats( const std::size_t iOutBufferLength, char * sOutBuffer ) noexcept;

Generates a report of the memory manager statistics in a C buffer. Shows aggregated data on the number of allocations and bytes allocated per type.

iOutBufferLength
Size of the output buffer.
sOutBuffer
Output buffer.
Result code: - 0 : Success - 3 : Insufficient buffer
This function is thread-safe . Access to the internal data is protected by a mutex.
function

DumpStats

#
ETL_INT32 DumpStats( std::string & sStatsOut ) noexcept;

Generates a report of the memory manager statistics. Consolidates global memory usage information organized by allocation type.

sStatsOut
Output string that receives the report.
'0' on success.
This function is thread-safe . Access to the internal structures is protected by a mutex.
function

DumpStats

#
ETL_INT32 DumpStats( String::CAString & sStatsOut ) noexcept;

Generates a report of the memory manager statistics in a CAString. Version optimized for use with the internal string infrastructure of the engine.

sStatsOut
Output string that receives the report.
'0' on success.
This function is thread-safe . Access to the internal data is protected by a mutex.

manager_macros.hpp

#include <ethereal/cpp/memory/manager_macros.hpp>
macro

ETL_NEW_ID

#
#define ETL_NEW_ID( ElementType, AllocationTagID ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, AllocationTagID )

Performs the heap allocation embedding the file name, the code line in the file and the ID (tag) of the allocation type.

@macro ETL_NEW_ID

ElementType
Type of the object to allocate.
AllocationTagID
Memory tag, MemoryManager::EMemoryTag.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_ID

#
#define ETL_NEW_ARRAY_ID( ElementType, ElementCount, AllocationTagID ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, AllocationTagID )

Performs the heap array allocation embedding the file name, the code line in the file and the ID (tag) of the allocation type.

@macro ETL_NEW_ARRAY_ID

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
AllocationTagID
Memory tag, MemoryManager::EMemoryTag.
Pointer to the first element of the allocated array.
macro

ETL_NEW_BLOCK_ID

#
#define ETL_NEW_BLOCK_ID( ElementSize, ElementAlign, AllocationTagID ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, AllocationTagID )

Performs the heap allocation embedding the file name, the code line in the file and the ID (tag) of the allocation type.

@macro ETL_NEW_BLOCK_ID

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
AllocationTagID
Memory tag, MemoryManager::EMemoryTag.
Pointer to the allocated block.
Until 2026-10-11 the macro took only (ElementSize, AllocationTagID) and used an ElementAlign that was not a parameter: it did not compile. Now it takes the same three arguments as the branch without the manager.
macro

ETL_NEW

#
#define ETL_NEW( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eGeneric )

Performs the heap allocation embedding the file name and the code line in the file. Generic allocation, calls the constructor of the base element.

@macro ETL_NEW

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY

#
#define ETL_NEW_ARRAY( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eGeneric )

Performs the heap array allocation embedding the file name and the code line in the file. Generic allocation, calls the base constructor of each array element.

@macro ETL_NEW_ARRAY

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_BLOCK

#
#define ETL_NEW_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag::eGeneri

Performs the heap allocation embedding the file name and the code line in the file. Generic allocation, does not call the constructor.

@macro ETL_NEW_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_DEFAULT

#
#define ETL_NEW_DEFAULT( Element ) new(std::nothrow) Element

Performs the heap allocation emulating the default behavior of @new. Used for allocations that must be made outside the context of the manager or pool.

@macro ETL_NEW_DEFAULT ** Use when the memory must be allocated by third parties or outside the context/logic of the manager or pool.

Element
Element to allocate.
Pointer to the allocated block.
macro

ETL_NEW_INSTANCE

#
#define ETL_NEW_INSTANCE( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eInstance )

Performs the heap allocation embedding the file name and the code line in the file. Instance allocation.

@macro ETL_NEW_INSTANCE

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_INSTANCE

#
#define ETL_NEW_ARRAY_INSTANCE( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eIn

Performs the heap array allocation embedding the file name and the code line in the file. Instance allocation.

@macro ETL_NEW_ARRAY_INSTANCE

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_INSTANCE_BLOCK

#
#define ETL_NEW_INSTANCE_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag

Performs the heap allocation embedding the file name and the code line in the file. Instance allocation, does not call the constructor.

@macro ETL_NEW_INSTANCE_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_MANAGER

#
#define ETL_NEW_MANAGER( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eManager )

Performs the heap allocation embedding the file name and the code line in the file. Manager allocation.

@macro ETL_NEW_MANAGER

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_MANAGER

#
#define ETL_NEW_ARRAY_MANAGER( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eMan

Performs the heap array allocation embedding the file name and the code line in the file. Manager allocation.

@macro ETL_NEW_ARRAY_MANAGER

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_MANAGER_BLOCK

#
#define ETL_NEW_MANAGER_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag:

Performs the heap allocation embedding the file name and the code line in the file. Manager allocation, does not call the constructor.

@macro ETL_NEW_MANAGER_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_STRING

#
#define ETL_NEW_STRING( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eString )

Performs the heap allocation embedding the file name and the code line in the file. String allocation.

@macro ETL_NEW_STRING

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_STRING

#
#define ETL_NEW_ARRAY_STRING( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eStri

Performs the heap array allocation embedding the file name and the code line in the file. String allocation.

@macro ETL_NEW_ARRAY_STRING

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_STRING_BLOCK

#
#define ETL_NEW_STRING_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag::

Performs the heap allocation embedding the file name and the code line in the file. String allocation, does not call the constructor.

@macro ETL_NEW_STRING_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_NETWORK

#
#define ETL_NEW_NETWORK( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eNetwork )

Performs the heap allocation embedding the file name and the code line in the file. Network allocation.

@macro ETL_NEW_NETWORK

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_NETWORK

#
#define ETL_NEW_ARRAY_NETWORK( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eNet

Performs the heap array allocation embedding the file name and the code line in the file. Network allocation.

@macro ETL_NEW_ARRAY_NETWORK

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_NETWORK_BLOCK

#
#define ETL_NEW_NETWORK_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag:

Performs the heap allocation embedding the file name and the code line in the file. Network allocation, does not call the constructor.

@macro ETL_NEW_NETWORK_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_APIJOB

#
#define ETL_NEW_APIJOB( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eApiJob )

Performs the heap allocation embedding the file name and the code line in the file. API task allocation.

@macro ETL_NEW_APIJOB

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_APIJOB

#
#define ETL_NEW_ARRAY_APIJOB( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eApiJ

Performs the heap array allocation embedding the file name and the code line in the file. API task allocation.

@macro ETL_NEW_ARRAY_APIJOB

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_APIJOB_BLOCK

#
#define ETL_NEW_APIJOB_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag::

Performs the heap allocation embedding the file name and the code line in the file. API task allocation, does not call the constructor.

@macro ETL_NEW_APIJOB_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_ASYNCJOB

#
#define ETL_NEW_ASYNCJOB( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eAsyncJob )

Performs the heap allocation embedding the file name and the code line in the file. Asynchronous task allocation.

@macro ETL_NEW_ASYNCJOB

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_ASYNCJOB

#
#define ETL_NEW_ARRAY_ASYNCJOB( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eAs

Performs the heap array allocation embedding the file name and the code line in the file. Asynchronous task allocation.

@macro ETL_NEW_ARRAY_ASYNCJOB

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_ASYNCJOB_BLOCK

#
#define ETL_NEW_ASYNCJOB_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag

Performs the heap allocation embedding the file name and the code line in the file. Asynchronous task allocation, does not call the constructor.

@macro ETL_NEW_ASYNCJOB_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_OBJECT

#
#define ETL_NEW_OBJECT( ElementType ) Ethereal::Memory::CMemoryManager::Instance().Allocate<ElementType>( __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eObject )

Performs the heap allocation embedding the file name and the code line in the file. Object allocation.

@macro ETL_NEW_OBJECT

ElementType
Type of the object to allocate.
Pointer to the allocated object.
macro

ETL_NEW_ARRAY_OBJECT

#
#define ETL_NEW_ARRAY_OBJECT( ElementType, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().AllocateArray<ElementType>( ElementCount, __FILE__, __LINE__, Ethereal::Memory::EMemoryTag::eObje

Performs the heap array allocation embedding the file name and the code line in the file. Object allocation.

@macro ETL_NEW_ARRAY_OBJECT

ElementType
Type of the array elements.
ElementCount
Number of array elements to allocate.
Pointer to the first element of the allocated array.
macro

ETL_NEW_OBJECT_BLOCK

#
#define ETL_NEW_OBJECT_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag::

Performs the heap allocation embedding the file name and the code line in the file. Object allocation, does not call the constructor.

@macro ETL_NEW_OBJECT_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_NEW_CHUNK_BLOCK

#
#define ETL_NEW_CHUNK_BLOCK( ElementSize, ElementAlign ) Ethereal::Memory::CMemoryManager::Instance().AllocateRaw( ElementSize, ElementAlign, __FILE__, __LINE__, false, Ethereal::Memory::EMemoryTag::e

Performs the heap allocation embedding the file name and the code line in the file. Data chunk allocation, does not call the constructor.

@macro ETL_NEW_CHUNK_BLOCK

ElementSize
Size of the block to allocate, in bytes.
ElementAlign
Alignment of the block to allocate.
Pointer to the allocated block.
macro

ETL_DELETE_RAW

#
#define ETL_DELETE_RAW( Element ) Ethereal::Memory::CMemoryManager::Instance().Deallocate( Element )

Releases the memory. Does not null out the memory address in the given pointer. The pointer must be set to null (nullptr) manually after this function.

@macro ETL_DELETE_RAW

Element
Memory pointer of the element to release.
macro

ETL_DELETE_SAFE

#
#define ETL_DELETE_SAFE( Element ) { if( ETL_VALID_PTR( Element ) ) { Ethereal::Memory::CMemoryManager::Instance().Deallocate( Element ); Element = nullptr; } }

Releases the memory safely. Checks whether the memory pointer is valid. After the release, sets the pointer to @nullptr.

@macro ETL_DELETE_SAFE

Element
Memory pointer of the element to release.
macro

ETL_DELETE_ARRAY_RAW

#
#define ETL_DELETE_ARRAY_RAW( Element, ElementCount ) Ethereal::Memory::CMemoryManager::Instance().DeallocateArray( Element, ElementCount )

Releases the memory of the array. Does not null out the memory address in the given pointer. The pointer must be set to null (nullptr) manually after this function.

@macro ETL_DELETE_ARRAY_RAW

Element
Memory pointer of the array element to release.
ElementCount
Number of array elements to delete, the size of the previously allocated array.
macro

ETL_DELETE_ARRAY_SAFE

#
#define ETL_DELETE_ARRAY_SAFE( Element, ElementCount ) { if( ETL_VALID_PTR( Element ) ) { Ethereal::Memory::CMemoryManager::Instance().DeallocateArray( Element, ElementCount ); Element = nullptr; } }

Releases the memory of the array safely. Checks whether the memory pointer is valid. After the release, sets the pointer to @nullptr.

@macro ETL_DELETE_ARRAY_SAFE

Element
Memory pointer of the array element to release.
ElementCount
Number of array elements to delete, the size of the previously allocated array.
macro

ETL_DELETE_BLOCK_RAW

#
#define ETL_DELETE_BLOCK_RAW( Element ) Ethereal::Memory::CMemoryManager::Instance().DeallocateRaw( Element, false )

Releases the memory of the block. Does not null out the memory address in the given pointer. The pointer must be set to null (@nullptr) manually after this function.

@macro ETL_DELETE_BLOCK_RAW

Element
Memory pointer of the block to release.
macro

ETL_DELETE_BLOCK_SAFE

#
#define ETL_DELETE_BLOCK_SAFE( Element ) { if( ETL_VALID_PTR( Element ) ) { Ethereal::Memory::CMemoryManager::Instance().DeallocateRaw( Element, false ); Element = nullptr; } }

Releases the memory of the block safely. Checks whether the memory pointer is valid. After the release, sets the pointer to @nullptr.

@macro ETL_DELETE_BLOCK_SAFE

Element
Memory pointer of the block to release.
macro

ETL_DELETE_RAW_DEFAULT

#
#define ETL_DELETE_RAW_DEFAULT( Element ) delete Element

Releases memory allocated outside the context of the manager or pool. Does not null out the memory address in the given pointer. The pointer must be set to null (@nullptr) manually after this function.

@macro ETL_DELETE_RAW_DEFAULT ** Use when the memory was allocated by third parties or outside the context/logic of the manager or pool.

Element
Memory pointer of the element to release.
macro

ETL_DELETE_SAFE_DEFAULT

#
#define ETL_DELETE_SAFE_DEFAULT( Element ) { if( ETL_VALID_PTR( Element ) ) { delete Element; Element = nullptr; } }

Safely releases memory allocated outside the context of the manager. Checks whether the memory pointer is valid. After the release, sets the pointer to @nullptr.

@macro ETL_DELETE_SAFE_DEFAULT ** Use when the memory was allocated by third parties or outside the context/logic of the manager or pool.

Element
Memory pointer of the element to release.
macro

ETL_NEW_ID

#
#define ETL_NEW_ID( Element, AllocationTagID ) new(std::nothrow) Element
: Keeps the (std::nothrow) syntax so that allocations return @nullptr on failure. todo: update block allocation to aligned_malloc
macro

ETL_NEW_DEFAULT

#
#define ETL_NEW_DEFAULT( Element ) new(std::nothrow) Element
Until 2026-10-11 this one took (Element, AllocationTagID), and the branch with the manager took (Element). A call written for one branch did not compile in the other.

pool.hpp

#include <ethereal/cpp/memory/pool.hpp>
struct

SAllocationHeader

Ethereal::Memory#
C++
ETL_BEGIN_INTENDED_PADDING
struct    ETL_STRUCT_ALIGN    SAllocationHeader    final
{
    /// @brief    Size of the requested allocation.
    std::size_t Size;

    /// @brief    Text tag associated with the allocation (optional).
    char Tag[ 32 ];

    /// @brief    Magic value to detect corruption or double free.
    ETL_UINT32 Magic;

    /// @brief    Pointer to the next free memory block.
    SAllocationHeader * NextFree;
};

Structure stored before each allocated block.

struct

SMemoryChunk

Ethereal::Memory#
C++
ETL_END_INTENDED_PADDING


/// @brief    Memory chunk structure.
///
struct  ETL_STRUCT_ALIGN    SMemoryChunk    final
{
    /// @brief    Pointer to the next linked chunk.
    SMemoryChunk * Next;

    /// @brief    Base address of the memory block.
    ETL_UINT8 * Base;

    /// @brief    Current allocation pointer (bump pointer).
    ETL_UINT8 * Current;

    /// @brief    Valid end address of the chunk.
    ETL_UINT8 * End;
};
class

CMemoryPool

Ethereal::Memory#

Class responsible for managing the memory pool.

class CMemoryPool final

There is no guarantee that it is thread-safe. Some functions are protected both by mutexes of the @CMemoryPool class and by internal mutexes. Synchronization is the responsibility of the caller. Must not be inherited directly by any structure, class or object.

constructor

CMemoryPool

#
CMemoryPool() noexcept;

Default constructor.

function

Initialize

#
ETL_INT32 Initialize( const std::size_t iPoolSize, const std::size_t iAlignment, const bool bIsAllowedToExpand = false, const bool bEnableMutexThreadProtection = false ) noexcept;

Initializes the chunk-based memory pool. Configures the memory pool with a defined base size, a specific alignment and an optional expansion policy. During initialization, the first memory chunk is created as the base for future allocations.

The given alignment must be a power of two (2^x) and nonzero. The base pool size must also be greater than zero.

iPoolSize
Base size of the memory pool, in bytes.
iAlignment
Memory alignment in bytes (power of two).
bIsAllowedToExpand
Indicates whether the pool can grow by creating new chunks.
bEnableMutexThreadProtection
Enables internal mutex protection.
Result code: - 0 : Success - 1 : Pool already initialized - 2 : Failed to create the initial memory chunk - 3 : Not licensed (see Bundle::UnlockBundle)
This function is not reentrant. Although it protects initialization with a mutex, it must be called only once during the application setup phase.
function

Shutdown

#
void Shutdown( void ) noexcept;

Shuts down the memory pool and releases all associated resources. Releases all memory chunks allocated by the pool, including the base block and any expansions made during the lifetime of the pool. After this function runs, the pool returns to the uninitialized state and cannot be used until @Initialize() is called again.

Platform-specific implementation:

  • Windows: operator delete with std::align_val_t
  • Linux/macOS: free (POSIX)
This function is thread-safe. Access to and release of the internal data are protected by a mutex.
All allocations made from this pool must be released before calling @Shutdown(). Using previously obtained pointers after shutdown results in undefined behavior.
function

Allocate

#
ETL_PTR Allocate( const std::size_t iSize, const char * sTag = nullptr ) noexcept;

Allocates a memory block from the memory pool. The function tries, in this order: 1) Reuse an existing block in the freelist (first-fit). 2) Allocate within the existing chunks. 3) Create a new chunk, if the expansion policy allows it.

The returned block is preceded internally by a header containing the allocation metadata.

iSize
Requested allocation size in bytes.
sTag
Optional tag for debug, tracing or profiling.
Pointer to the allocated payload or @nullptr on failure.
This function is thread-safe . Access to the internal structures is protected by a mutex when enabled.
function

Deallocate

#
ETL_INT32 Deallocate( ETL_PTR pAddress ) noexcept;

Releases an allocation previously obtained from the memory pool. Performs the logical release of a memory block allocated by CMemoryPool, validating the associated header and resetting the memory region for safety and debug purposes.

pAddress
Pointer previously returned by Allocate().
Result code: - 0 : Success - 1 : Pool not initialized - 2 : Invalid pointer
This function is thread-safe . Access to the internal structures is protected by a mutex.
This pool operates on an internal reuse model. Memory is only marked as internally reusable and is not returned to the operating system.
function

Reset

#
void Reset( void ) noexcept;

Resets the state of the memory pool. Moves the allocation pointer of every chunk back to the start of its payload, making all previously allocated memory reusable. Does not release memory to the operating system.

This function is thread-safe. The chunk reset is protected by a mutex.
All previously obtained pointers become invalid after this function is called.
function

IsInitialized

#
bool IsInitialized( void ) const noexcept;

Checks the current state of the memory pool.

Memory pool state: @true if initialized. @false if it was not initialized or failed during initialization.