Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Memory

Yuneta’s memory management API: allocate, free, and reallocate memory through the system-wide allocator configured at startup. All gobj-aware code must use these helpers instead of raw malloc/free so that leak tracking and memory limits work correctly.

Source code:

Prints a debug log of memory allocations that have not been freed, helping to track memory leaks.

void print_track_mem(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

None.

Notes

This function is only effective when CONFIG_DEBUG_TRACK_MEMORY is enabled (the Kconfig knob alone; since 7.16.5 it no longer also requires a Debug build type). It logs memory blocks that have not been freed, aiding in debugging memory leaks. On a clean shutdown it emits nothing.


set_memory_check_list()

Sets a global list of memory addresses to be checked for tracking memory allocations.

void set_memory_check_list(unsigned long *memory_check_list);

Parameters

KeyTypeDescription
memory_check_listunsigned long *Pointer to an array of memory addresses to be monitored.

Returns

This function does not return a value.

Notes

This function is only effective when CONFIG_DEBUG_TRACK_MEMORY is defined. It allows tracking specific memory allocations for debugging purposes.


gbmem_calloc()

Allocates memory for an array of elements and initializes them to zero.

void *gbmem_calloc(
    size_t n,
    size_t size
);

Parameters

KeyTypeDescription
nsize_tNumber of elements to allocate.
sizesize_tSize of each element in bytes.

Returns

Pointer to the allocated zeroed memory, or NULL on failure.

Notes

The total allocation is n * size bytes. All bytes are initialized to zero before returning.


gbmem_free()

Frees a block of memory previously allocated by gbmem allocation functions.

void gbmem_free(
    void *ptr
);

Parameters

KeyTypeDescription
ptrvoid *Pointer to the memory block to free. Can be NULL, in which case no action is performed.

Returns

None.

Notes

Only pass pointers obtained from gbmem_malloc(), gbmem_calloc(), gbmem_realloc(), gbmem_strdup(), or gbmem_strndup(). Passing a NULL pointer is safe and has no effect.


gbmem_get_allocators()

Retrieves the current memory allocator function pointers.

int gbmem_get_allocators(
    sys_malloc_fn_t *malloc_func,
    sys_realloc_fn_t *realloc_func,
    sys_calloc_fn_t *calloc_func,
    sys_free_fn_t *free_func
);

Parameters

KeyTypeDescription
malloc_funcsys_malloc_fn_t *Pointer to receive the current malloc function. Can be NULL if not needed.
realloc_funcsys_realloc_fn_t *Pointer to receive the current realloc function. Can be NULL if not needed.
calloc_funcsys_calloc_fn_t *Pointer to receive the current calloc function. Can be NULL if not needed.
free_funcsys_free_fn_t *Pointer to receive the current free function. Can be NULL if not needed.

Returns

0 on success.

Notes

Any parameter can be NULL if you do not need that particular function pointer.


gbmem_get_maximum_block()

Returns the maximum size of a single memory allocation block.

size_t gbmem_get_maximum_block(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

Maximum allocation block size in bytes. The default is 16 MB.

Notes

This limit is set during gbmem_setup() via the mem_max_block parameter.


gbmem_malloc()

Allocates a block of memory of the specified size.

void *gbmem_malloc(
    size_t size
);

Parameters

KeyTypeDescription
sizesize_tNumber of bytes to allocate.

Returns

Pointer to the allocated memory block, or NULL on failure.

Notes

The contents of the allocated block are uninitialized. Use gbmem_calloc() if you need zero-initialized memory.


gbmem_realloc()

Resizes a previously allocated memory block.

void *gbmem_realloc(
    void *ptr,
    size_t size
);

Parameters

KeyTypeDescription
ptrvoid *Pointer to the memory block to resize.
sizesize_tNew size in bytes.

Returns

Pointer to the resized memory block, or NULL on failure.

Notes

If ptr is NULL, this behaves like gbmem_malloc(). The returned pointer can differ from ptr if the block was moved. Contents up to the minimum of the old and new sizes are preserved.

When size is larger than the largest block (gbmem_get_maximum_block()), the answer is NULL and an ERROR “SIZE GREATER THAN MAX_BLOCK” says so. Then ptr is still valid, with its content, and it is still counted by the memory tracking (CONFIG_DEBUG_TRACK_MEMORY). Keep it, and free it later as usual. In 7.25.4 and earlier, a refused gbmem_realloc() took the block out of the tracking first: its free logged “Wrong dl_item_t, WITHOUT links” and the memory counter wrapped.

char *bigger = gbmem_realloc(list, new_size);
if(!bigger) {
    // Error already logged: `list` is unchanged, still valid and still yours
    return -1;
}
list = bigger;

gbmem_set_allocators()

Replaces the memory allocator functions with custom implementations.

int gbmem_set_allocators(
    sys_malloc_fn_t malloc_func,
    sys_realloc_fn_t realloc_func,
    sys_calloc_fn_t calloc_func,
    sys_free_fn_t free_func
);

Parameters

KeyTypeDescription
malloc_funcsys_malloc_fn_tCustom malloc function.
realloc_funcsys_realloc_fn_tCustom realloc function.
calloc_funcsys_calloc_fn_tCustom calloc function.
free_funcsys_free_fn_tCustom free function.

Returns

0 on success.

Notes

All subsequent gbmem_* calls will use the provided functions instead of the defaults. Use gbmem_get_allocators() to retrieve the current set before replacing them.


gbmem_setup()

Initializes the memory manager with custom configuration parameters.

int gbmem_setup(
    size_t mem_max_block,
    size_t mem_max_system_memory,
    BOOL use_own_system_memory,
    size_t mem_min_block,
    size_t mem_superblock
);

Parameters

KeyTypeDescription
mem_max_blocksize_tMaximum size of a single allocation block. Default is 16 MB.
mem_max_system_memorysize_tMaximum total memory the allocator can use. Default is 64 MB.
use_own_system_memoryBOOLReserved for future use.
mem_min_blocksize_tMinimum block size. Default is 512 bytes.
mem_superblocksize_tSuperblock size. Default is 16 MB.

Returns

0 on success.

Notes

Must be called before any other gbmem_* allocation function. Typically called once during yuno initialization.


gbmem_shutdown()

Stops the memory manager and cleans up resources.

void gbmem_shutdown(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

None.

Notes

Must be called during yuno shutdown. After this call, no further gbmem_* allocation functions must be used.


gbmem_strdup()

Duplicates a null-terminated string.

char *gbmem_strdup(
    const char *str
);

Parameters

KeyTypeDescription
strconst char *The null-terminated string to duplicate.

Returns

Pointer to the newly allocated copy of the string, or NULL on failure.

Notes

The returned string must be freed with gbmem_free().


gbmem_strndup()

Duplicates a substring of specified length.

char *gbmem_strndup(
    const char *str,
    size_t size
);

Parameters

KeyTypeDescription
strconst char *The source string.
sizesize_tMaximum number of characters to copy.

Returns

Pointer to the newly allocated string, or NULL on failure.

Notes

Copies at most size characters from str and appends a null terminator. The returned string must be freed with gbmem_free().


get_cur_system_memory()

Returns the current amount of system memory in use by the allocator.

size_t get_cur_system_memory(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

Current memory usage in bytes, or 0 if memory tracking is not enabled.

Notes

Useful for monitoring memory consumption at runtime.


get_max_system_memory()

Returns the maximum amount of system memory allowed by the allocator.

size_t get_max_system_memory(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

Maximum allowed memory in bytes. The default is 64 MB.

Notes

This limit is set during gbmem_setup() via the mem_max_system_memory parameter.