muteki
Loading...
Searching...
No Matches
memory.h File Reference

Memory management API. More...

#include <muteki/common.h>

Go to the source code of this file.

Functions

void * lmalloc (size_t size)
 Allocate heap memory.
 
void * lcalloc (size_t nmemb, size_t size)
 Allocate and clear data units on the heap.
 
void * lrealloc (void *ptr, size_t size)
 Resize or reallocate memory.
 
void _lfree (void *ptr)
 Free previously allocated memory.
 
void * AllocBlock (size_t size, unsigned short tag, bool new_segment)
 Allocate memory on the secondary heap.
 
void FreeBlock (void *ptr)
 Free memory previously allocated with AllocBlock().
 
size_t GetFreeMemory (void)
 Return the size of available memory.
 

Detailed Description

Memory management API.

In Besta RTOS, heap memory management is entirely done in the OS kernel. All applications share a single heap managed by the OS and can be accessed through the malloc series API. Conventional address-space based heap memory management such as sbrk() is not supported due to the absence of MMU support in the OS kernel.

The simplest way to get heap memory support in a custom libc is to redirect malloc(), calloc(), realloc() and free() to the corresponding functions provided here.

Function Documentation

◆ _lfree()

void _lfree ( void *  ptr)
extern

Free previously allocated memory.

Parameters
ptrPointer to previously allocated memory.

Returns
None.

◆ AllocBlock()

void * AllocBlock ( size_t  size,
unsigned short  tag,
bool  new_segment 
)
extern

Allocate memory on the secondary heap.

This allocates a block of memory on a secondary heap, that may be backed by a large block of memory on the primary heap. This secondary heap is usually 64KiB in size.

This seems to have a tie to the UI subsystem as UI-related syscalls seem to use this extensively.

Warning
Memory allocated with this function has to be freed with the FreeBlock() call.
Syscall Number
0x10145
Parameters
sizeSize of the memory.
tagWrite this value to <allocated address> + 0x30 when the allocated memory is located on a fresh chunk (i.e. allocated on previously unused memory directly following the last chunk). Seems to be used in UI subsystem to differentiate components in some circumstances.
new_segmentSet to true to start a new segment. This will prevent this chunk and all chunks above from being reused until this memory is freed.
Returns
The allocated memory.

◆ FreeBlock()

void FreeBlock ( void *  ptr)
extern

Free memory previously allocated with AllocBlock().

If a segment chunk is freed, everything below it will also be implicitly freed.

Syscall Number
0x10146
Parameters
ptrPointer to previously allocated memory.

Returns
None.

◆ GetFreeMemory()

size_t GetFreeMemory ( void  )
extern

Return the size of available memory.

Requires -lkrnllib when dynamically linking with the shims.

Parameters
None.

Returns
Size of available memory.

◆ lcalloc()

void * lcalloc ( size_t  nmemb,
size_t  size 
)
extern

Allocate and clear data units on the heap.

Parameters
nmembNumber of data units to allocate.
sizeSize of each data unit.
Returns
Pointer to allocated memory, or NULL if allocation fails.

◆ lmalloc()

void * lmalloc ( size_t  size)
extern

Allocate heap memory.

Parameters
sizeSize of the allocation in bytes.
Returns
Pointer to allocated memory, or NULL if allocation fails.

◆ lrealloc()

void * lrealloc ( void *  ptr,
size_t  size 
)
extern

Resize or reallocate memory.

Parameters
ptrPointer to previously allocated memory.
sizeNew size in bytes.
Returns
Pointer to the newly allocated memory, or NULL if allocation fails.