RT-Thread RTOS  1.0.0
An open source embedded real-time operating system
Memory Management

Data Structures

struct  rt_mempool

Functions

void * rt_malloc (rt_size_t size)
void * rt_realloc (void *rmem, rt_size_t newsize)
void * rt_calloc (rt_size_t count, rt_size_t size)
void rt_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_mp_alloc_sethook (void(*hook)(struct rt_mempool *mp, void *block))
void rt_mp_free_sethook (void(*hook)(struct rt_mempool *mp, void *block))
void rt_system_heap_init (void *begin_addr, void *end_addr)
void rt_malloc_sethook (void(*hook)(void *ptr, rt_uint32_t size))
void rt_free_sethook (void(*hook)(void *ptr))

Detailed Description

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

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:
pointer to allocated memory or NULL if no free memory was found.
void * rt_realloc ( void *  rmem,
rt_size_t  newsize 
)

This function will change the previously allocated memory block.

Parameters:
rmempointer to memory allocated by rt_malloc
newsizethe required new size
Returns:
the changed memory block address
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.

The allocated memory is filled with bytes of value zero.

Parameters:
countnumber of objects to allocate
sizesize of the objects to allocate
Returns:
pointer to allocated memory / NULL pointer if there is an error
void rt_free ( void *  rmem)

This function will release the previously allocated memory block by rt_malloc. The released memory block is taken back to system heap.

Parameters:
rmemthe address of memory which will be released
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:
mpthe memory pool object
namethe name of memory pool
startthe star address of memory pool
sizethe total size of memory pool
block_sizethe size for each block
Returns:
RT_EOK
rt_err_t rt_mp_detach ( struct rt_mempool mp)

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

Parameters:
mpthe memory pool object
Returns:
RT_EOK
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:
namethe name of memory pool
block_countthe count of blocks in memory pool
block_sizethe size for each block
Returns:
the created mempool object

This function will delete a memory pool and release the object memory.

Parameters:
mpthe memory pool object
Returns:
RT_EOK
void * rt_mp_alloc ( rt_mp_t  mp,
rt_int32_t  time 
)

This function will allocate a block from memory pool

Parameters:
mpthe memory pool object
timethe waiting time
Returns:
the allocated memory block or RT_NULL on allocated failed
void rt_mp_free ( void *  block)

This function will release a memory block

Parameters:
blockthe address of memory block to be released
void rt_mp_alloc_sethook ( void(*)(struct rt_mempool *mp, void *block)  hook)

This function will set a hook function, which will be invoked when a memory block is allocated from memory pool.

Parameters:
hookthe hook function
void rt_mp_free_sethook ( void(*)(struct rt_mempool *mp, void *block)  hook)

This function will set a hook function, which will be invoked when a memory block is released to memory pool.

Parameters:
hookthe hook function
void rt_system_heap_init ( void *  begin_addr,
void *  end_addr 
)

This function will init system heap

Parameters:
begin_addrthe beginning address of system page
end_addrthe end address of system page

This function will initialize system heap memory.

Parameters:
begin_addrthe beginning address of system heap memory.
end_addrthe end address of system heap memory.
void rt_malloc_sethook ( 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
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
 All Data Structures Variables