RT-Thread RTOS  1.2.0
An open source embedded real-time operating system
Memory Management
+ Collaboration diagram for Memory Management:

Data Structures

struct  rt_mempool
 

Functions

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

Detailed Description

memory management for memory pool and heap memory

RT-Thread operating system supports two types memory 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.

Function Documentation

◆ 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
mthe small memory management object.
sizeis 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
mthe small memory management object.
rmemis the pointer to memory allocated by rt_mem_alloc.
newsizeis 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
rmemthe 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
mpis the memory pool object.
nameis the name of the memory pool.
startis the start address of the memory pool.
sizeis the total size of the memory pool.
block_sizeis the size for each block..
Returns
RT_EOK

◆ rt_mp_detach()

rt_err_t rt_mp_detach ( struct rt_mempool mp)

This function will detach a memory pool from system object management.

Parameters
mpis 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
nameis the name of memory pool.
block_countis the count of blocks in memory pool.
block_sizeis 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
mpis 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
mpis the memory pool object.
timeis 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
blockthe 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_addrthe beginning address of system page.
end_addrthe 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_addrthe beginning address of system page.
end_addrthe 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
sizeis 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
ptrthe 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
ptris the pointer to memory allocated by rt_malloc.
newsizeis 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
countis the number of objects to allocate.
sizeis 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
sizeis the allocated memory block size.
alignis 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
ptris 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
totalis a pointer to get the total size of the memory.
usedis a pointer to get the size of memory used.
max_usedis 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
hookthe hook function.

◆ 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
hookthe hook function.

◆ 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
hookthe hook function

◆ 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
nameis the name of the small memory management object.
begin_addrthe beginning address of memory.
sizeis 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
mthe small memory management object.
Returns
RT_EOK