![]() |
CRM64Pro GDK v0.13.0
A free cross-platform game development kit built on top of SDL 3.0
|
High-performance and thread-safe memory allocation system [v25.12.0].
The Memory Manager (CMem) provides high-performance, thread-safe functions for allocating memory blocks while helping to reduce memory fragmentation. All CRM64Pro GDK modules and external libraries (SDL3, libpng, etc.) use CMem for memory allocation and deallocation.
Client applications can also use CMem and define different module numbers for precise and specific statistics tracking.
CMem is composed of three different functional layers:
| Frontend | User-facing API: alloc(), calloc(), realloc(), alloc_aligned(), free(), free_aligned(), setLogLevel(), setVerboseSampleInterval(), info() and destroy() |
|---|---|
| Stats system | Per-module memory tracking, CSV profiling, and logging bridge |
| Low-level allocator | Pluggable backend allocator (Standard C/C++ or ltalloc) |
CMem utilizes a Single Pipe Architecture for outputting statistics:
CMem::destroy() at the very end of main()), CMem automatically falls back to the last known log configuration (File/Console) to ensure final leak reports are not lost. The stats system provides memory information for each of the following modules:
| CRM64Pro | Interfaces, managers, objects, STL containers and network system |
|---|---|
| Core libraries | SDL3, TinyXML2 and zlib-ng |
| Audio libraries | libxmp, dr-flac, dr-mp3 and stb-vorbis |
| Image libraries | libpng |
| User modules | Custom modules defined by the client application |
In ::MSL_VERBOSE mode, two CSV files are produced:
| Allocation histogram | Distribution of all allocation sizes grouped by module |
|---|---|
| Memory evolution | Time-series snapshots of memory allocation per module (default interval: 0.5 seconds, configurable via setVerboseSampleInterval()) |
Two allocator backends are available. The selection affects how exact memory usage is tracked on different platforms:
| Standard C/C++ | Default system allocator. Windows: Uses _msize for exact tracking. Linux: Uses malloc_usable_size. macOS: Uses malloc_size. |
|---|---|
| ltalloc | Modified high-performance allocator. Cross-platform exact tracking via ltmsize. Thread-safe with some memory overhead (see details below) |
ltalloc details
The ltalloc allocator requests memory from the operating system in 1MB blocks and automatically creates memory pools for common allocation sizes:
atexit(CMem::destroy) for automatic cleanup std::string sDest = sSource.c_str();) CRM64Pro::string sSource = "mySourceValue";
std::string sDest = sSource; // Will fail
std::string sDest = sSource.c_str(); // OKC64_OVERRIDE_NEW before including the headers and compiling CRM64Pro GDK. If enabled, all C++ allocations will be tracked by CMem.Enumerations | |
| enum | CRM64Pro::CMem::eMemStatsLevel { CRM64Pro::CMem::MSL_NULL = 0 , CRM64Pro::CMem::MSL_NORMAL = 2 , CRM64Pro::CMem::MSL_HIGH = 4 , CRM64Pro::CMem::MSL_VERBOSE = 8 } |
| Memory statistics verbosity. More... | |
Functions | |
| void | CRM64Pro::CMem::setVerboseSampleInterval (Sint32 iInterval) |
| Set time interval between each sample when the stats level is ::MSL_VERBOSE. By default it is set to 500ms. | |
| void | CRM64Pro::CMem::setLogLevel (eMemStatsLevel eMSL) |
| Set the memory stats gathering level. | |
| Sint32 | CRM64Pro::CMem::setModuleName (Uint32 iModule, const char *szName) |
| Set the memory log mode. | |
| void * | CRM64Pro::CMem::alloc (size_t iSize, Uint32 iModule) |
| Reserve a memory block using C64 Memory Manager. | |
| void * | CRM64Pro::CMem::calloc (size_t iNum, size_t iSize, Uint32 iModule) |
| Reserve a memory block using C64 Memory Manager and reset it to 0. | |
| void * | CRM64Pro::CMem::realloc (void *pMem, size_t iSize, Uint32 iModule) |
| Reallocate a memory block using C64 Memory Manager. | |
| void * | CRM64Pro::CMem::alloc_aligned (size_t iSize, size_t iAlign, Uint32 iModule) |
| Reserve a memory block using C64 Memory Manager aligned to the desired value. | |
| void | CRM64Pro::CMem::free (void *pMem) |
| Deallocate a memory block using C64 Memory Manager. | |
| void | CRM64Pro::CMem::free_aligned (void *pMem) |
| Deallocate a memory block using C64 Memory Manager. | |
| void | CRM64Pro::CMem::destroy () |
| Destroy all internal memory used by the Low-Level Allocator. | |
| Sint32 | CRM64Pro::CMem::info (Sint32 iMode) |
| Output useful information about the memory usage. | |
Memory statistics verbosity.
| Enumerator | |
|---|---|
| MSL_NULL | Disable memory statistics gathering. Default for release builds. |
| MSL_NORMAL | Collect allocations per module (CRM64Pro and user). Default for debug builds. |
| MSL_HIGH | ::MSL_NORMAL plus deallocation logging for each module. |
| MSL_VERBOSE | ::MSL_HIGH plus: memory growth per module (CMem-Evolution.csv) and request size histogram per module (CMem-Histogram.csv). |
| HandleDLL void CRM64Pro::CMem::setVerboseSampleInterval | ( | Sint32 | iInterval | ) |
Set time interval between each sample when the stats level is ::MSL_VERBOSE. By default it is set to 500ms.
| iInterval | integer with time in milliseconds [1, 30000]. |
| HandleDLL void CRM64Pro::CMem::setLogLevel | ( | eMemStatsLevel | eMSL | ) |
Set the memory stats gathering level.
| eMSL | stats gathering level. For further information, please check ::eMemStatsLevel enum. |
| HandleDLL Sint32 CRM64Pro::CMem::setModuleName | ( | Uint32 | iModule, |
| const char * | szName ) |
Set the memory log mode.
| iModule | used for storing who requested the memory for statistics results. Only for custom user modules. |
| szName | name of the custom user module. By default, all custom user modules are shown as "User". |
| HandleDLL void * CRM64Pro::CMem::alloc | ( | size_t | iSize, |
| Uint32 | iModule ) |
Reserve a memory block using C64 Memory Manager.
It is automatically aligned to 4 in 32bits mode and 16 for 64 bits mode.
| iSize | size of the memory block, in bytes. |
| iModule | used for storing who requested the memory for statistics results. |
| HandleDLL void * CRM64Pro::CMem::calloc | ( | size_t | iNum, |
| size_t | iSize, | ||
| Uint32 | iModule ) |
Reserve a memory block using C64 Memory Manager and reset it to 0.
It is automatically aligned to 4 in 32bits mode and 16 for 64 bits mode.
| iNum | number of elements to allocate. |
| iSize | size of each element. |
| iModule | used for storing who requested the memory for statistics results. |
| HandleDLL void * CRM64Pro::CMem::realloc | ( | void * | pMem, |
| size_t | iSize, | ||
| Uint32 | iModule ) |
Reallocate a memory block using C64 Memory Manager.
It is automatically aligned to 4 in 32bits mode and 16 for 64 bits mode.
| pMem | pointer to the memory area to be reallocated. |
| iSize | size of the memory block, in bytes. |
| iModule | used for storing who requested the memory for statistics results. |
| HandleDLL void * CRM64Pro::CMem::alloc_aligned | ( | size_t | iSize, |
| size_t | iAlign, | ||
| Uint32 | iModule ) |
Reserve a memory block using C64 Memory Manager aligned to the desired value.
| iSize | size of the memory block, in bytes. |
| iAlign | specifies the alignment. Must be a valid alignment supported by the implementation. Usually a 2^n number is supported. |
| iModule | used for storing who requested the memory for statistics results. |
| HandleDLL void CRM64Pro::CMem::free | ( | void * | pMem | ) |
| HandleDLL void CRM64Pro::CMem::free_aligned | ( | void * | pMem | ) |
Deallocate a memory block using C64 Memory Manager.
| pMem | pointer to a memory block previously allocated with alloc_aligned(). |
| HandleDLL void CRM64Pro::CMem::destroy | ( | ) |
Destroy all internal memory used by the Low-Level Allocator.
Only applicable when using a Low-Level Allocator. It also call info() for displaying memory stats.
| HandleDLL Sint32 CRM64Pro::CMem::info | ( | Sint32 | iMode | ) |
Output useful information about the memory usage.
| iMode | 1 for displaying extra information about the Low-Level Allocator (default value) or 0 for disabling it. |