Skip to content

MemoryManager

class MemoryManager

Manages memory allocations inside the engine.

Types

Name Description
Allocation A single memory allocation.

Functions

Name Description
Initialize Initializes the memory manager.
Deinitialize Unloads the memory manager.
IsInitialized Checks whether the memory manager is initialized.
GetInstance Gets the actual instance of the memory manager.
Malloc Allocates a block of memory.
Malign Allocates an aligned block of memory.
Realloc Updates the size of a previously allocated memory.
Realign Updates the size of a previously allocated aligned memory.
Free Releases an allocated memory block.
TotalReservedMemorySize Gets the total allocated size.
SizeOf Gets the size of the given memory block.
GetMemoryPoolName Gets the name of the given memory pool.
GetStats Returns the memory allocation statistics for the given pool.
InspectMemoryLeaks Inspects the memory manager for memory leaks.

Function Details

Deinitialize

static void Deinitialize()

Unloads the memory manager.

Free

void Free(MemoryPoolKind pool, AmVoidPtr address)

Releases an allocated memory block.

Parameter pool
The memory pool to release from.
Parameter address
The address of the memory to release.

GetInstance

static MemoryManager* GetInstance()

Gets the actual instance of the memory manager.

GetMemoryPoolName

static AmString GetMemoryPoolName(MemoryPoolKind pool)

Gets the name of the given memory pool.

Parameter pool
The memory pool to get the name for.
Return
The name of the memory pool.

GetStats

[[nodiscard]] const MemoryPoolStats& GetStats(MemoryPoolKind pool) const

Returns the memory allocation statistics for the given pool.

Parameter pool
The pool to get the statistics for.

Initialize

static void Initialize(const MemoryManagerConfig& config)

Initializes the memory manager.

@remarks This should be done prior to any call of GetInstance() or any usage of amMemory.

InspectMemoryLeaks

[[nodiscard]] AmString InspectMemoryLeaks() const

Inspects the memory manager for memory leaks.

Tip

This function is most useful after the engine has been deinitialized. Calling it before may just report a lot of false positives (allocated memories which are still in use).

Return
A string containing a report for the detected memory leaks.

IsInitialized

[[maybe_unused]] static bool IsInitialized()

Checks whether the memory manager is initialized.

Return
true if the memory manager is initialized, false otherwise.

Malign

[[nodiscard]] AmVoidPtr Malign(MemoryPoolKind pool, AmSize size, AmUInt32 alignment, const char* file, AmUInt32 line)

Allocates an aligned block of memory.

Parameter pool
The memory pool to allocate from.
Parameter size
The size of the block to allocate.
Parameter alignment
The alignment of the block to allocate.
Parameter file
The file in which the allocation was made.
Parameter line
The line in which the allocation was made.
Return
A pointer to the allocated block.

Malloc

[[nodiscard]] AmVoidPtr Malloc(MemoryPoolKind pool, AmSize size, const char* file, AmUInt32 line)

Allocates a block of memory.

Parameter pool
The memory pool to allocate from.
Parameter size
The size of the block to allocate.
Parameter file
The file in which the allocation was made.
Parameter line
The line in which the allocation was made.
Return
A pointer to the allocated block.

Realign

[[nodiscard]] AmVoidPtr Realign( MemoryPoolKind pool, AmVoidPtr address, AmSize size, AmUInt32 alignment, const char* file, AmUInt32 line)

Updates the size of a previously allocated aligned memory.

Parameter pool
The memory pool to update.
Parameter address
The address of the aligned memory to update.
Parameter size
The new size of the aligned memory.
Parameter alignment
The new alignment of the aligned memory.
Parameter file
The file in which the allocation was made.
Parameter line
The line in which the allocation was made.
Return
A pointer to the allocated block. Maybe equal to address if the original pointer had enough memory.

Realloc

[[nodiscard]] AmVoidPtr Realloc(MemoryPoolKind pool, AmVoidPtr address, AmSize size, const char* file, AmUInt32 line)

Updates the size of a previously allocated memory.

Parameter pool
The memory pool to update.
Parameter address
The address of the memory to update.
Parameter size
The new size of the memory.
Parameter file
The file in which the allocation was made.
Parameter line
The line in which the allocation was made.
Return
A pointer to the allocated block. Maybe equal to address if the original pointer had enough memory.

SizeOf

[[nodiscard]] AmSize SizeOf(MemoryPoolKind pool, AmConstVoidPtr address) const

Gets the size of the given memory block.

Parameter pool
The memory pool to get the size from.
Parameter address
The address of the memory block.
Return
The size of the given memory block.

TotalReservedMemorySize

[[nodiscard]] AmSize TotalReservedMemorySize() const

Gets the total allocated size.

Return
The total currently allocated size.