Memory
MiraOS/MiraMesh uses dynamic memory allocation during the mira_net_init call. The memory can be allocated using a user defined allocation function or a buffer can be given to MiraOS to use for allocation.
After mira_net_init has been called, no more memory is allocated. If it is allocating from a buffer, use mira_mem_buffer_get_usage() to get the total memory usage.
Defines¶
MIRA_MEM_SET_BUFFER¶
#define MIRA_MEM_SET_BUFFER(_SIZE)
Create a buffer of a given size and use it with mira_mem_set_alloc_buffer()
Parameters
Name | Description |
---|---|
_SIZE | The size of the memory block |
Types¶
mira_mem_alloc_callback_t¶
typedef void*(* mira_mem_alloc_callback_t) (mira_size_t size, void *storage);
Callback to return a newly allocated memory block
If successful, return a pointer a memory block of at least size bytes. Make sure the memory block is aligned.
Parameters
Name | Description |
---|---|
size | The size of the memory block |
storage | A pointer passed to mira_mem_set_alloc_callback() |
Functions¶
mira_mem_set_alloc_callback¶
mira_status_t mira_mem_set_alloc_callback(mira_mem_alloc_callback_t alloc_callback, void *storage);
Set a callback to allocate memory.
If the system already implements a version of malloc, set a callback that can map memory allocation to the memory allocation routine available in the system.
If using this method of allocation, no other mira_mem_set_alloc_*() should be used.
Parameters
Name | Description |
---|---|
alloc_callback | Callback to be called for each allocation |
storage | Pointer to be directly passed to callback |
mira_mem_set_alloc_buffer¶
mira_status_t mira_mem_set_alloc_buffer(void *buffer, mira_size_t size);
Set a memory buffer available for memory usage.
If the system doesn't handle memory management itself, a static memory buffer can be used for internal memory management of Mira.
Pass a statically allocated RAM buffer, which can be managed by Mira internally.
The buffer is expected to be aligned to the CPUs native alignement.
Note that once set, the buffer can't be released.
If using this method of allocation, no other mira_mem_set_alloc_*() should be used.
Note
The convenience macro MIRA_SET_MEM_BUFFER(size) creates a buffer and sets it with this function.
Parameters
Name | Description |
---|---|
buffer | Memory block to use for storage, aligned |
size | Size of memory block |
mira_mem_buffer_get_usage¶
mira_size_t mira_mem_buffer_get_usage(void);
Get current dynamic memory usage, when using the static buffer
Get the current usage in the dynamic memory buffer.
When using any other method than alloc_buffer, the return value is undefined.