rt-thread中动态内存分配之小内存管理模块方法的一点理解

2023-04-26,,

@2019-01-18

【小记】

rt-thread中动态内存分配之小内存管理模块方法的一点理解

> 内存初始化后的布局示意

lfree指向内存空闲区首地址

 /**
* @ingroup SystemInit
*
* This function will initialize system heap memory.
*
* @param begin_addr the beginning address of system heap memory.
* @param end_addr the end address of system heap memory.
*/
void rt_system_heap_init(void *begin_addr, void *end_addr)
{
struct heap_mem *mem;
rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE); RT_DEBUG_NOT_IN_INTERRUPT; /* alignment addr */
if ((end_align > ( * SIZEOF_STRUCT_MEM)) &&
((end_align - * SIZEOF_STRUCT_MEM) >= begin_align))
{
/* calculate the aligned memory size */
mem_size_aligned = end_align - begin_align - * SIZEOF_STRUCT_MEM;
}
else
{
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n",
(rt_ubase_t)begin_addr, (rt_ubase_t)end_addr); return;
} /* point to begin address of heap */
heap_ptr = (rt_uint8_t *)begin_align; RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
(rt_ubase_t)heap_ptr, mem_size_aligned)); /* initialize the start of the heap */
mem = (struct heap_mem *)heap_ptr;
mem->magic = HEAP_MAGIC;
mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
mem->prev = ;
mem->used = ;
#ifdef RT_USING_MEMTRACE
rt_mem_setname(mem, "INIT");
#endif /* initialize the end of the heap */
heap_end = (struct heap_mem *)&heap_ptr[mem->next];
heap_end->magic = HEAP_MAGIC;
heap_end->used = ;
heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
#ifdef RT_USING_MEMTRACE
rt_mem_setname(heap_end, "INIT");
#endif rt_sem_init(&heap_sem, "heap", , RT_IPC_FLAG_FIFO); /* initialize the lowest-free pointer to the start of the heap */
lfree = (struct heap_mem *)heap_ptr;
}

rt-thread中动态内存分配之小内存管理模块方法的一点理解的相关教程结束。

《rt-thread中动态内存分配之小内存管理模块方法的一点理解.doc》

下载本文的Word格式文档,以方便收藏与打印。