[ English | 简体中文 ]
Memory Management API
openvela provides a flexible memory management system that supports the standard POSIX memory allocation interfaces as well as extended memory management features.
Headers: #include <stdlib.h> (standard allocation), #include <nuttx/mm/mm.h> (kernel heap / heap management)
openvela Implementation Notes
- Build mode impact:
- Flat Build: Only one user heap;
malloc/freeoperate on it directly - Protected Build: Kernel heap + user heap, isolated by MPU protection
- Kernel Build: Kernel heap + multiple user heaps (one per task group)
- Flat Build: Only one user heap;
- Alignment guarantee: Memory returned by
malloc()is aligned toMM_ALIGN(default 8 or 16 bytes) - Thread safety: All standard allocation interfaces (malloc/free/calloc, etc.) are thread-safe in a multitasking environment
- Deferred free: The
*_delayfree()family of interfaces is used for scenarios where memory cannot be freed immediately (for example, in interrupt context) - Known incompatibility:
posix_memalign()currently does not check the validity of thealignmentparameter and does not returnEINVAL
Standard Memory Allocation
malloc
void *malloc(size_t size);
Allocate a memory block of the specified size from the user heap. malloc() searches the heap for a free block large enough and allocates the required memory from it. If size is 0, the behavior is implementation-defined; it may return NULL or a unique pointer.
The allocated memory is guaranteed to be aligned to MM_ALIGN (default 2 * sizeof(uintptr_t), i.e. 8 or 16 bytes), which ensures that access to any fundamental data type is properly aligned.
Parameters:
sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the allocated memory on success, or NULL on failure with errno set:
ENOMEMInsufficient memory available.
Note:
- The contents of the allocated memory are uninitialized and may contain arbitrary data.
- The returned pointer can be passed to
free(),realloc(), and related functions. malloc()is thread-safe in a multitasking environment.
POSIX Compatibility: Compatible with the POSIX interface of the same name.
free
void free(void *ptr);
Release a memory block previously allocated by malloc(), calloc(), realloc(), memalign(), etc., making it available for subsequent allocations.
If ptr is NULL, no operation is performed. If ptr was not returned by a previous allocation function, or has already been freed, the behavior is undefined.
Parameters:
ptrPointer to the memory block to be freed.
Returns:
No return value.
Note:
- The freed pointer should not be used afterwards (dangling pointer).
- Do not free the same block twice (double free causes heap corruption).
- Do not free memory that was not dynamically allocated (such as stack variables or globals).
- After freeing, memory is not necessarily returned to the system; it may remain in the heap for subsequent allocations.
POSIX Compatibility: Compatible with the POSIX interface of the same name.
calloc
void *calloc(size_t n, size_t elem_size);
Allocate an array of n elements, each of elem_size bytes, for a total of n * elem_size bytes, and initialize all bytes to zero.
Compared with malloc(), calloc() has two advantages: it zero-initializes the memory, and it can detect multiplication overflow when computing the total size (depending on the implementation).
Parameters:
nNumber of elements to allocate.elem_sizeSize of each element, in bytes.
Returns:
Returns a pointer to the allocated and zeroed memory on success, or NULL on failure with errno set:
ENOMEMInsufficient memory available.
Note:
- If
norelem_sizeis 0, the function may returnNULLor a unique pointer. - All bytes of the returned memory are set to 0.
- For data structures that require zero initialization, prefer
calloc()overmalloc()+memset().
POSIX Compatibility: Compatible with the POSIX interface of the same name.
realloc
void *realloc(void *ptr, size_t size);
Change the size of a previously allocated memory block. realloc() may expand or shrink the block in place, or allocate a new block and copy the existing data.
If ptr is NULL, realloc() behaves like malloc(size). If size is 0 and ptr is not NULL, it behaves like free(ptr) and returns NULL.
Parameters:
ptrPointer to a previously allocated memory block. IfNULL, equivalent tomalloc(size).sizeNew size, in bytes. If 0, the memory is freed andNULLis returned.
Returns:
Returns a pointer to the reallocated memory on success (possibly different from the original), or NULL on failure with errno set:
ENOMEMInsufficient memory available. The original block is left unchanged and remains valid.
Note:
- If the new size is larger than the original, the content of the new area is uninitialized.
- If the new size is smaller than the original, data beyond the new size is lost.
- The returned pointer may differ from the original; after a successful call the original pointer must not be used.
- If
realloc()fails, the original block is not freed and the caller is still responsible for freeing it.
POSIX Compatibility: Compatible with the POSIX interface of the same name.
reallocarray
void *reallocarray(void *ptr, size_t n, size_t elem_size);
Change the size of a previously allocated memory block to n * elem_size bytes. Unlike realloc(), reallocarray() safely checks for multiplication overflow, preventing security issues caused by integer overflow.
This function is particularly useful for reallocating arrays, because using realloc(ptr, n * elem_size) directly may, due to multiplication overflow, allocate a block much smaller than expected.
Parameters:
ptrPointer to a previously allocated memory block. IfNULL, this is equivalent to allocating new memory.nNumber of elements.elem_sizeSize of each element, in bytes.
Returns:
Returns a pointer to the reallocated memory on success, or NULL on failure with errno set:
ENOMEMInsufficient memory available, orn * elem_sizeoverflowed.
Note:
- If
n * elem_sizewould cause integer overflow, the function safely returnsNULL. - As with
realloc(), the original block is left unchanged on failure.
POSIX Compatibility: Compatible with the BSD/glibc extension interface.
zalloc
void *zalloc(size_t size);
Allocate a memory block of the specified size and initialize all bytes to zero. This is a convenience function provided by openvela, functionally equivalent to calloc(1, size) but with clearer semantics.
Parameters:
sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the allocated and zeroed memory on success, or NULL on failure with errno set:
ENOMEMInsufficient memory available.
Note:
- Unlike
malloc(), the returned memory is zero-initialized. - Functionally identical to
calloc(1, size), but more concise to call.
POSIX Compatibility: openvela extension interface.
The following interfaces are used to query heap usage and memory allocation statistics, which is useful for debugging and monitoring memory usage.
Aligned Memory Allocation
memalign
void *memalign(size_t alignment, size_t size);
Allocate memory aligned to the specified boundary. This is useful for hardware access with specific alignment requirements (such as DMA buffers) or for SIMD operations.
Parameters:
alignmentAlignment boundary; must be a power of two (such as 16, 32, 64, 4096).sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the memory aligned to the specified boundary on success, or NULL on failure with errno set:
ENOMEMInsufficient memory available.EINVALalignmentis not a power of two.
Note:
- The allocated memory can be released normally using
free(). - For memory that needs page alignment, use
valloc()orposix_memalign().
POSIX Compatibility: Compatible with the POSIX interface of the same name (obsolete; posix_memalign() is recommended).
posix_memalign
int posix_memalign(void **memptr, size_t alignment, size_t size);
Allocate memory aligned to the specified boundary and store the pointer in memptr. This is the POSIX standard replacement for memalign().
The main difference from memalign() is that posix_memalign() reports errors through the return value instead of errno, which makes error handling more explicit.
Parameters:
memptrAddress at which to store the pointer to the allocated memory. On success,*memptris set to the address of the allocated memory.alignmentAlignment boundary; must be a multiple ofsizeof(void *)and a power of two.sizeSize of memory to allocate, in bytes.
Returns:
0Success;*memptrpoints to the allocated memory.ENOMEMInsufficient memory available.
Note:
- Unlike
memalign(), errors are reported via the return value rather thanerrno. - The allocated memory can be released normally using
free(). - The current openvela implementation does not check the validity of the
alignmentparameter and does not returnEINVAL(the POSIX standard requiresEINVALwhen the parameter is invalid).
POSIX Compatibility: Partially compatible with the POSIX interface of the same name (does not return EINVAL).
aligned_alloc
void *aligned_alloc(size_t alignment, size_t size);
Allocate memory aligned to the specified boundary. This is the aligned memory allocation function introduced by the C11 standard.
Parameters:
alignmentAlignment boundary; must be a power of two.sizeSize of memory to allocate, in bytes. The C11 standard requires thatsizebe a multiple ofalignment, but many implementations do not enforce this.
Returns:
Returns a pointer to the memory aligned to the specified boundary on success, or NULL on failure.
Note:
- The allocated memory can be released normally using
free(). - As part of the C11 standard, this function is more portable than
memalign().
POSIX Compatibility: Compatible with the C11 standard interface.
valloc
void *valloc(size_t size);
Allocate memory aligned to a page boundary. The page size is system-defined, typically 4096 bytes.
This function is equivalent to memalign(sysconf(_SC_PAGESIZE), size).
Parameters:
sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to page-aligned memory on success, or NULL on failure.
Note:
- The page size can be obtained via
sysconf(_SC_PAGESIZE). - The allocated memory can be released normally using
free(). - This function is obsolete; new code should use
posix_memalign()instead.
POSIX Compatibility: Compatible with the BSD extension interface (obsolete; posix_memalign() is recommended).
Memory Information Queries
mallinfo
struct mallinfo mallinfo(void);
Obtain memory allocation statistics for the user heap. This is useful for monitoring application memory usage, detecting memory leaks, and optimizing memory use.
Parameters:
None.
Returns:
Returns a struct mallinfo structure containing the following fields:
arenaTotal memory managed by the heap, in bytes.ordblksNumber of free blocks.aordblksNumber of allocated blocks.mxordblkSize of the largest free block, in bytes; represents the largest memory that a single allocation can obtain.uordblksTotal size of allocated memory, in bytes.fordblksTotal size of free memory, in bytes.usmblksMaximum amount of memory ever allocated (high-water mark).
Note:
uordblks + fordblksshould be close toarena(possibly slightly smaller due to heap management overhead).mxordblkrepresents the largest block that can currently be allocated successfully.
POSIX Compatibility: Compatible with the glibc extension interface.
mallinfo_task
struct mallinfo_task mallinfo_task(FAR const struct malltask *task);
Obtain memory allocation information for a specific task or a specific type. This is an openvela extension that can be used to track per-task memory usage and help locate memory leaks.
Parameters:
taskPointer to astruct malltaskstructure, containing:pidProcess ID. This can be a specific process ID, or one of the following special values:PID_MM_MEMPOOL(-1): Query memory pool allocations.PID_MM_LEAK(-2): Query possible memory leaks (whose allocator has already exited).PID_MM_ALLOC(-3): Query all allocated memory.PID_MM_FREE(-4): Query free memory.PID_MM_BIGGEST(-5): Query the largest memory block.PID_MM_ORPHAN(-6): Query orphan memory blocks.
seqminMinimum sequence number (requiresCONFIG_MM_RECORD_SEQNO).seqmaxMaximum sequence number (requiresCONFIG_MM_RECORD_SEQNO).
Returns:
Returns a struct mallinfo_task structure containing:
aordblksNumber of blocks allocated by the task.uordblksTotal size of memory allocated by the task, in bytes.
Note:
- Using
PID_MM_LEAKquickly surfaces memory blocks left behind by exited tasks, which are very likely to be memory leaks. - The sequence number feature requires
CONFIG_MM_RECORD_SEQNOto be enabled and can be used to track allocations within a specific time range.
POSIX Compatibility: openvela extension interface.
malloc_size
size_t malloc_size(void *ptr);
Return the actual usable size of a previously allocated memory block. Due to memory alignment and heap management requirements, the memory actually allocated may be larger than requested.
Parameters:
ptrPointer to a previously allocated memory block.
Returns:
Returns the actual usable size of the block, in bytes. This value is normally greater than or equal to the size requested at allocation.
Note:
- The alias
malloc_usable_size()(glibc compatible) can also be used. - It is safe to use the entire range of memory indicated by the returned value.
- If
ptris not a valid allocation pointer, the behavior is undefined.
POSIX Compatibility: Compatible with the glibc/macOS extension interface.
mallopt
int mallopt(int param, int value);
Adjust memory allocator parameters. This function provides a way to control the behavior of the memory allocator.
Parameters:
paramParameter to adjust. Defined options include:M_TRIM_THRESHOLDTrim thresholdM_TOP_PADTop paddingM_MMAP_THRESHOLDmmap thresholdM_MMAP_MAXMaximum number of mmap allocationsM_CHECK_ACTIONCheck actionM_PERTURBMemory perturbationM_ARENA_TESTarena testM_ARENA_MAXMaximum number of arenas
valueParameter value.
Returns:
Returns a non-zero value on success, 0 on failure.
Note:
- The current openvela implementation always returns 1 (success) and does not actually process any parameters.
- This interface is provided for compatibility only.
POSIX Compatibility: Compatible with the glibc extension interface (interface-only compatibility, no actual functionality).
Kernel Heap Interfaces
kmm_initialize
void kmm_initialize(void *heap_start, size_t heap_size);
Initialize the kernel heap. This function is usually called early during system startup by board-level initialization code.
Parameters:
heap_startStart address of the kernel heap memory.heap_sizeSize of the kernel heap, in bytes.
Returns:
No return value.
Note:
- This function should be called only once; repeated calls result in undefined behavior.
- Requires
CONFIG_MM_KERNEL_HEAPto be enabled.
kmm_malloc
void *kmm_malloc(size_t size);
Allocate memory from the kernel heap. Behaves like malloc(), but allocates from the kernel heap instead of the user heap.
Parameters:
sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the allocated memory on success, or NULL on failure.
Note:
- Memory allocated here can only be released using
kmm_free(). - Memory allocated on the kernel heap should not be passed to user-mode code.
kmm_free
void kmm_free(void *mem);
Release memory previously allocated from the kernel heap by kmm_malloc(), kmm_calloc(), and similar functions.
Parameters:
memPointer to the memory block to be freed. IfNULL, no operation is performed.
Returns:
No return value.
Note:
- Only memory allocated from the kernel heap may be released here; it must not be used to free user heap memory.
kmm_heapmember()can be used to check whether a pointer belongs to the kernel heap.
kmm_calloc
void *kmm_calloc(size_t n, size_t elem_size);
Allocate zeroed memory from the kernel heap. Behaves like calloc(), but allocates from the kernel heap.
Parameters:
nNumber of elements to allocate.elem_sizeSize of each element, in bytes.
Returns:
Returns a pointer to the allocated and zeroed memory on success, or NULL on failure.
kmm_realloc
void *kmm_realloc(void *oldmem, size_t newsize);
Reallocate kernel heap memory. Behaves like realloc(), but operates on the kernel heap.
Parameters:
oldmemPointer to a block previously allocated from the kernel heap. IfNULL, equivalent tokmm_malloc(newsize).newsizeNew size, in bytes. If 0, the memory is freed.
Returns:
Returns a pointer to the reallocated memory on success (possibly different from the original), or NULL on failure (the original block is left unchanged).
kmm_zalloc
void *kmm_zalloc(size_t size);
Allocate zeroed memory from the kernel heap. Behaves like zalloc(), but allocates from the kernel heap.
Parameters:
sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the allocated and zeroed memory on success, or NULL on failure.
kmm_memalign
void *kmm_memalign(size_t alignment, size_t size);
Allocate aligned memory from the kernel heap. Behaves like memalign(), but allocates from the kernel heap.
Parameters:
alignmentAlignment boundary; must be a power of two.sizeSize of memory to allocate, in bytes.
Returns:
Returns a pointer to the memory aligned to the specified boundary on success, or NULL on failure.
Note:
- Useful for kernel scenarios that require aligned memory, such as DMA buffers.
kmm_malloc_size
size_t kmm_malloc_size(void *mem);
Return the actual usable size of an allocated block in the kernel heap.
Parameters:
memPointer to a block allocated from the kernel heap.
Returns:
Returns the actual usable size of the block, in bytes.
POSIX Compatibility: openvela/NuttX extension interface.
kmm_mallinfo
struct mallinfo kmm_mallinfo(void);
Obtain allocation information for the kernel heap. Behaves like mallinfo(), but returns statistics for the kernel heap.
Parameters:
None.
Returns:
Returns a struct mallinfo structure containing memory allocation statistics for the kernel heap:
arenaTotal size of the kernel heap, in bytes.ordblksNumber of free blocks.uordblksSize of used memory, in bytes.fordblksSize of free memory, in bytes.mxordblkSize of the largest contiguous free block, in bytes.
Note:
- Can be used to monitor kernel memory usage.
kmm_heapmember
bool kmm_heapmember(void *mem);
Check whether a memory address belongs to the kernel heap.
Parameters:
memMemory address to check.
Returns:
Returns true if the address belongs to the kernel heap; otherwise false.
Note:
- Can be used to decide whether a block should be freed with
kmm_free()orfree(). - Used in memory management code to route free requests to the correct heap.
kmm_addregion
void kmm_addregion(void *heapstart, size_t heapsize);
Add a new memory region to the kernel heap. Allows the kernel heap to use non-contiguous memory regions.
Parameters:
heapstartStart address of the new memory region.heapsizeSize of the new memory region, in bytes.
Returns:
No return value.
Note:
- Requires
CONFIG_MM_KERNEL_HEAPto be enabled. - The maximum number of regions is configured by
CONFIG_MM_REGIONS.
POSIX Compatibility: openvela/NuttX extension interface.
kmm_extend
void kmm_extend(void *mem, size_t size, int region);
Extend a specific memory region of the kernel heap. The additional memory must be physically adjacent to the existing region.
Parameters:
memStart address of the additional memory.sizeSize of the additional memory, in bytes.regionRegion index (starting from 0).
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
kmm_delayfree
void kmm_delayfree(void *mem);
Deferred free of kernel heap memory. The free operation is postponed until a safe point, for scenarios in which memory cannot be freed immediately, such as interrupt context or while holding a spinlock.
Parameters:
memPointer to the kernel heap memory to be freed.
Returns:
No return value.
Note:
- Use this function instead of
kmm_free()when freeing memory from an interrupt handler.
POSIX Compatibility: openvela/NuttX extension interface.
kmm_memdump
void kmm_memdump(const struct mm_memdump_s *dump);
Dump memory allocation information for the kernel heap to the system log, used for debugging memory leaks.
Parameters:
dumpPointer to a dump condition structure specifying filter conditions (PID, sequence number range, etc.).
Returns:
No return value.
Note:
- Requires
CONFIG_MM_BACKTRACEto be enabled to obtain allocation backtrace information.
POSIX Compatibility: openvela/NuttX extension interface.
kmm_checkcorruption
void kmm_checkcorruption(void);
Check whether the kernel heap has memory corruption.
Parameters:
None.
Returns:
No return value. If corruption is detected, an assertion is triggered or debug information is printed.
Note:
- Requires
CONFIG_MM_HEAP_CHECKto be enabled. - Used for debugging memory-related issues.
Heap Management Interfaces
mm_initialize
struct mm_heap_s *mm_initialize(const char *name, void *heapstart, size_t heapsize);
Initialize a new heap. Allocates and initializes the heap management structure and sets up the initial free block.
Parameters:
nameName of the heap, used for debugging and identification.heapstartStart address of the heap memory.heapsizeSize of the heap, in bytes.
Returns:
Returns a pointer to the initialized heap structure on success, or NULL on failure.
Note:
- The heap size must be large enough to accommodate the heap management overhead.
- The system can have multiple independent heaps, such as user heap, kernel heap, graphics heap, and so on.
mm_uninitialize
void mm_uninitialize(struct mm_heap_s *heap);
Destroy a heap, releasing the resources occupied by the heap management structure.
Parameters:
heapPointer to the heap structure to be destroyed.
Returns:
No return value.
Note:
- Before destruction, ensure there are no live allocations in the heap.
mm_addregion
void mm_addregion(struct mm_heap_s *heap, void *heapstart, size_t heapsize);
Add a new memory region to an existing heap. Allows the heap to use non-contiguous memory regions.
Parameters:
heapPointer to the heap structure.heapstartStart address of the new memory region.heapsizeSize of the new memory region, in bytes.
Returns:
No return value.
Note:
- The added region may be physically non-contiguous with existing regions.
- The maximum number of regions is configured by
CONFIG_MM_REGIONS.
mm_extend
void mm_extend(struct mm_heap_s *heap, void *mem, size_t size, int region);
Extend a specific memory region of the heap. The additional memory must be adjacent to the existing region.
Parameters:
heapPointer to the heap structure.memStart address of the additional memory region.sizeSize of the additional memory region, in bytes.regionRegion index (starting from 0).
Returns:
No return value.
Note:
- The added memory region must be physically adjacent to the existing region.
- Unlike
mm_addregion(), this function extends an existing region instead of adding a new one.
mm_brkaddr
void *mm_brkaddr(struct mm_heap_s *heap, int region);
Get the current break address (end address of the region) of a specific region of the heap.
Parameters:
heapPointer to the heap structure.regionRegion index (starting from 0).
Returns:
Returns the current break address of the specified region.
Note:
- Used to determine the memory location to which the region can be extended.
mm_sbrk
int mm_sbrk(struct mm_heap_s *heap, intptr_t incr, void **mem);
Extend or shrink the break address of the heap (similar to UNIX sbrk semantics).
Parameters:
heapPointer to the heap structure.incrIncrement (positive to expand, negative to shrink).memOutput parameter; returns the previous break address.
Returns:
Returns 0 on success, -1 on failure.
POSIX Compatibility: openvela/NuttX extension interface.
mm_heapmember
bool mm_heapmember(struct mm_heap_s *heap, void *mem);
Check whether a memory address belongs to any region of the specified heap.
Parameters:
heapPointer to the heap structure.memMemory address to check.
Returns:
Returns true if the address belongs to the specified heap; otherwise false.
Note:
- Used to determine where an allocation came from so that the correct interface is used to free it.
mm_free
void mm_free(struct mm_heap_s *heap, void *mem);
Free memory from the specified heap. This is the low-level heap release interface; both free() and kmm_free() call it internally.
Parameters:
heapPointer to the heap structure.memPointer to the memory block to be freed.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
mm_malloc_size
size_t mm_malloc_size(struct mm_heap_s *heap, void *mem);
Return the actual usable size of an allocated block in the specified heap.
Parameters:
heapPointer to the heap structure.memPointer to an allocated memory block.
Returns:
Returns the actual usable size of the block, in bytes.
POSIX Compatibility: openvela/NuttX extension interface.
mm_delayfree
void mm_delayfree(struct mm_heap_s *heap, void *mem);
Deferred free of memory from the specified heap. Applicable to scenarios in which memory cannot be freed immediately, such as interrupt context or while holding a spinlock.
Parameters:
heapPointer to the heap structure.memPointer to the memory block to be freed.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
mm_heapfree
size_t mm_heapfree(struct mm_heap_s *heap);
Query the total amount of free memory in the specified heap.
Parameters:
heapPointer to the heap structure.
Returns:
Returns the total size of free memory in the heap, in bytes.
POSIX Compatibility: openvela/NuttX extension interface.
mm_heapfree_largest
size_t mm_heapfree_largest(struct mm_heap_s *heap);
Query the size of the largest contiguous free block in the specified heap. This determines the largest memory that a single allocation can obtain.
Parameters:
heapPointer to the heap structure.
Returns:
Returns the size of the largest contiguous free block, in bytes.
POSIX Compatibility: openvela/NuttX extension interface.
mm_notify_pressure
void mm_notify_pressure(size_t remaining, size_t largest);
Send a memory pressure notification. When heap free memory falls below a threshold, this notifies registered listeners to release caches and other reclaimable memory.
Parameters:
remainingCurrent amount of remaining free memory, in bytes.largestCurrent size of the largest contiguous free block, in bytes.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
User Heap Interfaces
umm_initialize
void umm_initialize(void *heap_start, size_t heap_size);
Initialize the user heap. This function is usually called during system startup by initialization code.
Parameters:
heap_startStart address of the heap memory.heap_sizeSize of the heap, in bytes.
Returns:
No return value.
Note:
- This function should be called only once.
- The user heap is the default heap for standard interfaces such as
malloc().
umm_heapmember
bool umm_heapmember(void *mem);
Check whether a memory address belongs to the user heap.
Parameters:
memMemory address to check.
Returns:
Returns true if the address belongs to the user heap; otherwise false.
Note:
- Combined with
kmm_heapmember(), can be used to determine the origin of a memory block.
umm_addregion
void umm_addregion(void *heapstart, size_t heapsize);
Add a new memory region to the user heap.
Parameters:
heapstartStart address of the new memory region.heapsizeSize of the new memory region, in bytes.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
umm_extend
void umm_extend(void *mem, size_t size, int region);
Extend a specific memory region of the user heap. The additional memory must be adjacent to the existing region.
Parameters:
memStart address of the additional memory.sizeSize of the additional memory, in bytes.regionRegion index.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
umm_delayfree
void umm_delayfree(void *mem);
Deferred free of user heap memory. Applicable in interrupt context.
Parameters:
memPointer to the user heap memory to be freed.
Returns:
No return value.
POSIX Compatibility: openvela/NuttX extension interface.
Debugging and Diagnostics
mm_memdump
void mm_memdump(struct mm_heap_s *heap, const struct mm_memdump_s *dump);
Dump memory allocation information of a heap, used for debugging memory leaks and analyzing memory usage.
Parameters:
heapPointer to the heap structure. IfNULL, the user heap is dumped.dumpPointer to astruct mm_memdump_sstructure specifying the dump conditions.
Returns:
No return value.
Note:
mm_memdump_sis a type alias formalltask; the structure fields are the same as themalltaskdescribed inmallinfo_task().- The output includes, for each allocated block, the address, size, allocator PID, and the allocation backtrace (if
CONFIG_MM_BACKTRACEis enabled). - Commonly used to diagnose memory leaks and to identify which code allocated memory but did not free it.
mm_checkcorruption
void mm_checkcorruption(struct mm_heap_s *heap);
Check whether the heap has memory corruption. This function traverses all memory blocks in the heap and verifies their integrity.
Parameters:
heapPointer to the heap structure to be checked.
Returns:
No return value. If corruption is detected, an assertion is triggered or debug information is printed.
Note:
- Requires
CONFIG_DEBUG_MMto be enabled. - Issues detected include: block header corruption, double free, out-of-bounds writes, and so on.
- This function traverses the entire heap and has a performance impact; it is mainly used for debugging.
umm_checkcorruption
void umm_checkcorruption(void);
Check whether the user heap has memory corruption. This is a convenience wrapper around mm_checkcorruption() for the user heap.
Parameters:
None.
Returns:
No return value. If corruption is detected, an assertion is triggered or debug information is printed.
Note:
- Requires
CONFIG_DEBUG_MMto be enabled. - This function can be called to perform a check when memory issues are suspected.
umm_memdump
void umm_memdump(const struct mm_memdump_s *dump);
Dump memory allocation information of the user heap to the system log.
Parameters:
dumpPointer to the dump condition structure.
Returns:
No return value.
Note:
- Requires
CONFIG_MM_BACKTRACEto be enabled to obtain backtrace information.
POSIX Compatibility: openvela/NuttX extension interface.