|
Embedded SDK
Embedded SDK
|
This page describes the Dynamic Memory Allocator available for Applications.
Basically, this allocator behaves like any memory allocator in usual OS. The main difference is that it can be instantiated, thanks to its initialization function: mem_init()
This function takes as parameters
It returns a memory context (mem_ctx_t) that will be used by other functions
Then, chunks can be allocated with mem_alloc() and released with mem_free()
Both of these function take the memory context as first parameter.
For application developers, a set of high-level wrapper functions is provided in app_mem_utils.h. These functions simplify memory management by hiding the memory context and providing additional safety features.
Initialize the application heap with mem_utils_init():
Use APP_MEM_ALLOC() and APP_MEM_FREE() for simple allocations:
For buffers that need reallocation or safe cleanup, use APP_MEM_CALLOC() and mem_utils_buffer_cleanup(). The allocated buffers are always zero-initialized. The cleanup function frees the buffer and sets the pointer to NULL to avoid dangling pointers.
Duplicate strings with APP_MEM_STRDUP():
The allocator supports memory profiling to detect leaks and track allocations during development.
Compile your application with the flag HAVE_MEMORY_PROFILING.
When profiling is enabled, all allocation and deallocation operations are logged with file and line information.
Use the tools/valground.py script to analyze memory profiling logs from Speculos:
The tool detects:
Exit code is 0 if no errors detected, 1 if leaks or free errors found.
Persistent allocations are those that are intentionally kept without being freed (mem_buffer_persistent()). Such buffers will not be reported as leaks.
This is useful for long-lived buffers that are intentionally kept across multiple operations.