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 should 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_TRACK_MEMORY is enabled. It logs memory blocks that have not been freed, aiding in debugging memory leaks.


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 available when CONFIG_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 may differ from ptr if the block was moved. Contents up to the minimum of the old and new sizes are preserved.


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 may 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()

Shuts down the memory manager and cleans up resources.

void gbmem_shutdown(void);

Parameters

KeyTypeDescription
--This function does not take any parameters.

Returns

None.

Notes

Should be called during yuno shutdown. After this call, no further gbmem_* allocation functions should 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.