|
void * | rt_smem_alloc (rt_smem_t m, rt_size_t size) |
|
void * | rt_smem_realloc (rt_smem_t m, void *rmem, rt_size_t newsize) |
|
void | rt_smem_free (void *rmem) |
|
rt_err_t | rt_mp_init (struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size) |
|
rt_err_t | rt_mp_detach (struct rt_mempool *mp) |
|
rt_mp_t | rt_mp_create (const char *name, rt_size_t block_count, rt_size_t block_size) |
|
rt_err_t | rt_mp_delete (rt_mp_t mp) |
|
void * | rt_mp_alloc (rt_mp_t mp, rt_int32_t time) |
|
void | rt_mp_free (void *block) |
|
void | rt_system_heap_init (void *begin_addr, void *end_addr) |
|
void | rt_system_heap_init_generic (void *begin_addr, void *end_addr) |
|
void * | rt_malloc (rt_size_t size) |
|
void | rt_free (void *ptr) |
|
void * | rt_realloc (void *ptr, rt_size_t newsize) |
|
void * | rt_calloc (rt_size_t count, rt_size_t size) |
|
void * | rt_malloc_align (rt_size_t size, rt_size_t align) |
|
void | rt_free_align (void *ptr) |
|
void | rt_memory_info (rt_size_t *total, rt_size_t *used, rt_size_t *max_used) |
|
rt_smem_t | rt_smem_init (const char *name, void *begin_addr, rt_size_t size) |
|
rt_err_t | rt_smem_detach (rt_smem_t m) |
|
void | rt_realloc_set_entry_hook (void(*hook)(void **ptr, rt_size_t size)) |
|
void | rt_realloc_set_exit_hook (void(*hook)(void **ptr, rt_size_t size)) |
|
void | rt_free_sethook (void(*hook)(void **ptr)) |
|
memory management for memory pool and heap memory
RT-Thread operating system supports two types memory management:
- Static memory pool management
- Dynamic memory heap management.
The time to allocate a memory block from the memory pool is determinant. When the memory pool is empty, the allocated thread can be blocked (or immediately return, or waiting for sometime to return, which are determined by a timeout parameter). When other thread releases memory blocks to this memory pool, the blocked thread is wake up.
There are two methods in dynamic memory heap management, one is used for small memory, such as less than 1MB. Another is a SLAB like memory management, which is suitable for large memory system. All of them has no real-time character.
◆ rt_smem_alloc()
void * rt_smem_alloc |
( |
rt_smem_t |
m, |
|
|
rt_size_t |
size |
|
) |
| |
Allocate a block of memory with a minimum of 'size' bytes.
- Parameters
-
m | the small memory management object. |
size | is the minimum size of the requested block in bytes. |
- Returns
- the pointer to allocated memory or NULL if no free memory was found.
◆ rt_smem_realloc()
void * rt_smem_realloc |
( |
rt_smem_t |
m, |
|
|
void * |
rmem, |
|
|
rt_size_t |
newsize |
|
) |
| |
This function will change the size of previously allocated memory block.
- Parameters
-
m | the small memory management object. |
rmem | is the pointer to memory allocated by rt_mem_alloc. |
newsize | is the required new size. |
- Returns
- the changed memory block address.
◆ rt_smem_free()
void rt_smem_free |
( |
void * |
rmem | ) |
|
This function will release the previously allocated memory block by rt_mem_alloc. The released memory block is taken back to system heap.
- Parameters
-
rmem | the address of memory which will be released. |
◆ rt_mp_init()
rt_err_t rt_mp_init |
( |
struct rt_mempool * |
mp, |
|
|
const char * |
name, |
|
|
void * |
start, |
|
|
rt_size_t |
size, |
|
|
rt_size_t |
block_size |
|
) |
| |
This function will initialize a memory pool object, normally which is used for static object.
- Parameters
-
mp | is the memory pool object. |
name | is the name of the memory pool. |
start | is the start address of the memory pool. |
size | is the total size of the memory pool. |
block_size | is the size for each block.. |
- Returns
- RT_EOK
◆ rt_mp_detach()
This function will detach a memory pool from system object management.
- Parameters
-
mp | is the memory pool object. |
- Returns
- RT_EOK
◆ rt_mp_create()
rt_mp_t rt_mp_create |
( |
const char * |
name, |
|
|
rt_size_t |
block_count, |
|
|
rt_size_t |
block_size |
|
) |
| |
This function will create a mempool object and allocate the memory pool from heap.
- Parameters
-
name | is the name of memory pool. |
block_count | is the count of blocks in memory pool. |
block_size | is the size for each block. |
- Returns
- the created mempool object
◆ rt_mp_delete()
rt_err_t rt_mp_delete |
( |
rt_mp_t |
mp | ) |
|
This function will delete a memory pool and release the object memory.
- Parameters
-
mp | is the memory pool object. |
- Returns
- RT_EOK
◆ rt_mp_alloc()
void * rt_mp_alloc |
( |
rt_mp_t |
mp, |
|
|
rt_int32_t |
time |
|
) |
| |
This function will allocate a block from memory pool.
- Parameters
-
mp | is the memory pool object. |
time | is the maximum waiting time for allocating memory.
- 0 for not waiting, allocating memory immediately.
|
- Returns
- the allocated memory block or RT_NULL on allocated failed.
◆ rt_mp_free()
void rt_mp_free |
( |
void * |
block | ) |
|
This function will release a memory block.
- Parameters
-
block | the address of memory block to be released. |
◆ rt_system_heap_init()
void rt_system_heap_init |
( |
void * |
begin_addr, |
|
|
void * |
end_addr |
|
) |
| |
This function will init system heap. User can override this API to complete other works, like heap sanitizer initialization.
- Parameters
-
begin_addr | the beginning address of system page. |
end_addr | the end address of system page. |
◆ rt_system_heap_init_generic()
void rt_system_heap_init_generic |
( |
void * |
begin_addr, |
|
|
void * |
end_addr |
|
) |
| |
This function will do the generic system heap initialization.
- Parameters
-
begin_addr | the beginning address of system page. |
end_addr | the end address of system page. |
◆ rt_malloc()
void* rt_malloc |
( |
rt_size_t |
size | ) |
|
Allocate a block of memory with a minimum of 'size' bytes.
- Parameters
-
size | is the minimum size of the requested block in bytes. |
- Returns
- the pointer to allocated memory or NULL if no free memory was found.
◆ rt_free()
void rt_free |
( |
void * |
ptr | ) |
|
This function will release the previously allocated memory block by rt_malloc. The released memory block is taken back to system heap.
- Parameters
-
ptr | the address of memory which will be released. |
◆ rt_realloc()
void* rt_realloc |
( |
void * |
ptr, |
|
|
rt_size_t |
newsize |
|
) |
| |
This function will change the size of previously allocated memory block.
- Parameters
-
ptr | is the pointer to memory allocated by rt_malloc. |
newsize | is the required new size. |
- Returns
- the changed memory block address.
◆ rt_calloc()
void* rt_calloc |
( |
rt_size_t |
count, |
|
|
rt_size_t |
size |
|
) |
| |
This function will contiguously allocate enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory.
- Note
- The allocated memory is filled with bytes of value zero.
- Parameters
-
count | is the number of objects to allocate. |
size | is the size of one object to allocate. |
- Returns
- pointer to allocated memory / NULL pointer if there is an error.
◆ rt_malloc_align()
void* rt_malloc_align |
( |
rt_size_t |
size, |
|
|
rt_size_t |
align |
|
) |
| |
This function allocates a memory block, which address is aligned to the specified alignment size.
- Parameters
-
size | is the allocated memory block size. |
align | is the alignment size. |
- Returns
- The memory block address was returned successfully, otherwise it was returned empty RT_NULL.
◆ rt_free_align()
void rt_free_align |
( |
void * |
ptr | ) |
|
This function release the memory block, which is allocated by rt_malloc_align function and address is aligned.
- Parameters
-
ptr | is the memory block pointer. |
◆ rt_memory_info()
void rt_memory_info |
( |
rt_size_t * |
total, |
|
|
rt_size_t * |
used, |
|
|
rt_size_t * |
max_used |
|
) |
| |
This function will caculate the total memory, the used memory, and the max used memory.
- Parameters
-
total | is a pointer to get the total size of the memory. |
used | is a pointer to get the size of memory used. |
max_used | is a pointer to get the maximum memory used. |
◆ rt_realloc_set_entry_hook()
void rt_realloc_set_entry_hook |
( |
void(*)(void **ptr, rt_size_t size) |
hook | ) |
|
This function will set a hook function, which will be invoked when a memory block is allocated from heap memory.
- Parameters
-
◆ rt_realloc_set_exit_hook()
void rt_realloc_set_exit_hook |
( |
void(*)(void **ptr, rt_size_t size) |
hook | ) |
|
This function will set a hook function, which will be invoked when a memory block is allocated from heap memory.
- Parameters
-
◆ rt_free_sethook()
void rt_free_sethook |
( |
void(*)(void **ptr) |
hook | ) |
|
This function will set a hook function, which will be invoked when a memory block is released to heap memory.
- Parameters
-
◆ rt_smem_init()
rt_smem_t rt_smem_init |
( |
const char * |
name, |
|
|
void * |
begin_addr, |
|
|
rt_size_t |
size |
|
) |
| |
This function will initialize small memory management algorithm.
small memory object interface
- Parameters
-
name | is the name of the small memory management object. |
begin_addr | the beginning address of memory. |
size | is the size of the memory. |
- Returns
- Return a pointer to the memory object. When the return value is RT_NULL, it means the init failed.
◆ rt_smem_detach()
rt_err_t rt_smem_detach |
( |
rt_smem_t |
m | ) |
|
This function will remove a small mem from the system.
- Parameters
-
m | the small memory management object. |
- Returns
- RT_EOK