## 1 数据结构

struct heap_mem
{
/* magic and used flag */
rt_uint16_t magic;     //如果此内存块被分配了,则置0x1ea0,以此标志此块内存是正常分配出来的,而不是非法指针
rt_uint16_t used;      //0:未分配;1:已分配

rt_size_t next, prev;   //前一内存块,后一内存块
};

## 2 初始化动态内存堆

/**
* @ingroup SystemInit
*
* This function will init system heap
*
*/
{
struct heap_mem *mem;
rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);     //得到对齐后的堆内存起始地址
rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE);  //得到对齐后的堆内存末尾地址

RT_DEBUG_NOT_IN_INTERRUPT;          //确保此函数不是运行在中断例程内

if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&                 //确保可用动态堆内存大小至少大于可等于2个内存块控制结构大小
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
{
/* calculate the aligned memory size */
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;   //计算出可用的真实可用作动态分配的内存大小
}
else
{

return;
}

/* point to begin address of heap */
heap_ptr = (rt_uint8_t *)begin_align;     //heap_ptr为静态全局变量,指用堆内存起始地址

RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d\n",
(rt_uint32_t)heap_ptr, mem_size_aligned));

/* initialize the start of the heap */
mem        = (struct heap_mem *)heap_ptr;    //将动态堆内存首地址初始化为一个内存块,可用大小为全部可用大小
mem->magic = HEAP_MAGIC;         //初始化为0x1ea0
mem->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;    //下一个指向动态堆内存最靠末尾的内存控制结构
mem->prev  = 0;                 //无前一个内存块
mem->used  = 0;            //初始化为未使用

/* initialize the end of the heap */
heap_end        = (struct heap_mem *)&heap_ptr[mem->next];   //指向末尾内存块
heap_end->magic = HEAP_MAGIC;
heap_end->used  = 1;             //末尾内存块初始化为已使用
heap_end->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;   //下一个内存块指向自己
heap_end->prev  = mem_size_aligned + SIZEOF_STRUCT_MEM;   //前一个内存块也指向自己

rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);    //初始化堆内存信号量为1

/* initialize the lowest-free pointer to the start of the heap */
lfree = (struct heap_mem *)heap_ptr;     //lfree为空闲内存,为静态全局变量,初始化时指向动态堆内存首地址
}

## 2 分配内存

### 2.1 malloc

/**
* Allocate a block of memory with a minimum of 'size' bytes.
*
* @param size is the minimum size of the requested block in bytes.
*
* @return pointer to allocated memory or NULL if no free memory was found.
*/
void *rt_malloc(rt_size_t size)      //分配内存接口
{
rt_size_t ptr, ptr2;
struct heap_mem *mem, *mem2;

RT_DEBUG_NOT_IN_INTERRUPT;     //确保此函数不是在中断例程内执行

if (size == 0)           //如果指定分配的大小为0则直接返回NULL
return RT_NULL;

if (size != RT_ALIGN(size, RT_ALIGN_SIZE))
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d, but align to %d\n",
size, RT_ALIGN(size, RT_ALIGN_SIZE)));
else
RT_DEBUG_LOG(RT_DEBUG_MEM, ("malloc size %d\n", size));

/* alignment size */
size = RT_ALIGN(size, RT_ALIGN_SIZE);      //传入的size不一定是对齐的数据民,因此,这里只取对齐后的大小

if (size > mem_size_aligned)         //如果指定大小比整个可用空间都要大,则直接返回NULL
{
RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));

return RT_NULL;
}

/* every data block must be at least MIN_SIZE_ALIGNED long */  //如果指定大小不得小于MN_SIZE_ALIGNED,即12
if (size < MIN_SIZE_ALIGNED)
size = MIN_SIZE_ALIGNED;

/* take memory semaphore */
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);  //获得堆信号量,从第1章,已知初始化时已将此信号量初始为1,因此,若第一次是可以成功获取

for (ptr = (rt_uint8_t *)lfree - heap_ptr;    //扫描内存空闲链表
ptr < mem_size_aligned - size;
ptr = ((struct heap_mem *)&heap_ptr[ptr])->next)
{
mem = (struct heap_mem *)&heap_ptr[ptr];    //mem指向空闲节点

if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)   //如果当前内存块未分配且大小足够
{
/* mem is not used and at least perfect fit is possible:
* mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */

if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=    //如果当前内存块大小比需求还大,剩余空间还有可能分配另一内存块时
(size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
{
/* (in addition to the above, we test if another struct heap_mem (SIZEOF_STRUCT_MEM) containing
* at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
* -> split large block, create empty remainder,
* remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
* mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
* struct heap_mem would fit in but no data between mem2 and mem2->next
* @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
*       region that couldn't hold data, but when mem->next gets freed,
*       the 2 regions would be combined, resulting in more free memory
*/
ptr2 = ptr + SIZEOF_STRUCT_MEM + size;     //将此内存块分割成两块,ptr2指向后一块

/* create mem2 struct */
mem2       = (struct heap_mem *)&heap_ptr[ptr2];
mem2->used = 0;
mem2->next = mem->next;
mem2->prev = ptr;

/* and insert it between mem and mem->next */    //更新当前块的后指针指向分割后另一块内存块
mem->next = ptr2;
mem->used = 1;     //当前内存块被分配,标志置1

if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)   //如果分割后的内存块不是末尾,则将其后指针指向自己
{
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
}
#ifdef RT_MEM_STATS
used_mem += (size + SIZEOF_STRUCT_MEM);    //更新总共已分配的内存块大小,便于统计
if (max_mem < used_mem)
max_mem = used_mem;
#endif
}
else        //如果当前内存块只够本次分配时
{
/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
* be used at this point: if not we have 2 unused structs in a row, plug_holes should have
* take care of this).
* -> near fit or excact fit: do not split, no mem2 creation
* also can't move mem->next directly behind mem, since mem->next
* will always be used at this point!
*/
mem->used = 1;   //置已分配标志
#ifdef RT_MEM_STATS
used_mem += mem->next - ((rt_uint8_t*)mem - heap_ptr);
if (max_mem < used_mem)
max_mem = used_mem;
#endif
}
/* set memory block magic */
mem->magic = HEAP_MAGIC;

if (mem == lfree)     //如果空闲内存链表头刚好指向当前内存块
{
/* Find next free block after mem and update lowest free pointer */
while (lfree->used && lfree != heap_end)      //则更新空闲链表指向下一个空闲内存块
lfree = (struct heap_mem *)&heap_ptr[lfree->next];

RT_ASSERT(((lfree == heap_end) || (!lfree->used)));   //空闲链表不能指向堆内存末尾或空闲链表指向的内存块有使用标志为已分配
}

rt_sem_release(&heap_sem);  //释放信号量,保证另一分配函数可以运行
RT_ASSERT((rt_uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (rt_uint32_t)heap_end);
RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);

RT_DEBUG_LOG(RT_DEBUG_MEM,
("allocate memory at 0x%x, size: %d\n",
(rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
(rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));

RT_OBJECT_HOOK_CALL(rt_malloc_hook,
(((void *)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));

/* return the memory data except mem struct */
return (rt_uint8_t *)mem + SIZEOF_STRUCT_MEM;    //返回当前内存块的可用内存地址,前面大小为SIZE_STRUCT_MEM的空间表示内存块控制结构所占空间,是不能拿来用的
}
}

rt_sem_release(&heap_sem);

return RT_NULL;  //所有空闲链表上的内存块都不符合,则返回NULL
}

### 2.2 realloc

/**
* This function will change the previously allocated memory block.
*
* @param rmem pointer to memory allocated by rt_malloc
* @param newsize the required new size
*
* @return the changed memory block address
*/
void *rt_realloc(void *rmem, rt_size_t newsize)
{
rt_size_t size;
rt_size_t ptr, ptr2;
struct heap_mem *mem, *mem2;
void *nmem;

RT_DEBUG_NOT_IN_INTERRUPT; //确保此函数不是在中断例程中使用

/* alignment size */
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);//得到对齐后的重新分配的大小
if (newsize > mem_size_aligned)//如果超过上限,则直接返回NULL
{
RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));

return RT_NULL;
}

/* allocate a new memory block */
if (rmem == RT_NULL)//如果传入的参数为空,则为分配好内存则返回
return rt_malloc(newsize);

rt_sem_take(&heap_sem, RT_WAITING_FOREVER);//尝试获取信号量

if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||  //如果原先的内存指针不在合法范围内,则原样返回原先的内存指针
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
{
/* illegal memory */
rt_sem_release(&heap_sem);

return rmem;
}

mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);//得到对应的内存块指针

ptr = (rt_uint8_t *)mem - heap_ptr;           //得到当前指针到内存堆首地址的偏移
size = mem->next - ptr - SIZEOF_STRUCT_MEM; //得到当前内存块的总共可用空间
if (size == newsize)                   //如果当前指针可用内存本身就等于重新需要分配的大小,则不必再重新分配,原样返回即可
{
/* the size is the same as */
rt_sem_release(&heap_sem);

return rmem;
}

if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) //如果当前指针所指向的内存块本身就比重新要分配的大小还大,且还有至少一个MIN_SIZE的剩余则分割当前内存块
{
/* split memory block */
#ifdef RT_MEM_STATS
used_mem -= (size - newsize); //刷新已使用的内存大小
#endif

ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;//ptr2指向分割后的第二块内存
mem2 = (struct heap_mem *)&heap_ptr[ptr2];//转化为内存块,mem2即为第二块内存块
mem2->magic= HEAP_MAGIC;//置maigc
mem2->used = 0;//置未使用标志
mem2->next = mem->next;//更新next
mem2->prev = ptr;
mem->next = ptr2;//当前内存块的下一块指向ptr2
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)//如果分割后的第二块内存块的下一指针不是指向内存堆最后一块
{
((struct heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;//则将下块内存块的前指针指向自己
}

plug_holes(mem2);//将分割后的第二块内存尝试与左右内存块合并

rt_sem_release(&heap_sem);

return rmem;//原样返回指针
}
rt_sem_release(&heap_sem);

/* expand memory */
nmem = rt_malloc(newsize);//如果当前内存块本身可用内存确实不足,则重新分配一块内存
if (nmem != RT_NULL) /* check memory */
{
rt_memcpy(nmem, rmem, size < newsize ? size : newsize); //将原内存块的数据原样复制到新内存块中
rt_free(rmem);//将原内存块释放
}

return nmem;//返回新内存块
}

### 2.3 calloc

/**
* 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.
*
* @param count number of objects to allocate
* @param size size of the objects to allocate
*
* @return pointer to allocated memory / NULL pointer if there is an error
*/
void *rt_calloc(rt_size_t count, rt_size_t size)
{
void *p;

RT_DEBUG_NOT_IN_INTERRUPT;//防止此函数在中断例程中使用

/* allocate 'count' objects of size 'size' */
p = rt_malloc(count * size); //分配内存

/* zero the memory */
if (p)
rt_memset(p, 0, count * size);//初始化分配好的内存内容全部为0

return p;
}

## 3 free

/**
* This function will release the previously allocated memory block by
* rt_malloc. The released memory block is taken back to system heap.
*
* @param rmem the address of memory which will be released
*/
void rt_free(void *rmem)
{
struct heap_mem *mem;

RT_DEBUG_NOT_IN_INTERRUPT;//防止此函数在中断例程中使用

if (rmem == RT_NULL)
return;
RT_ASSERT((((rt_uint32_t)rmem) & (RT_ALIGN_SIZE-1)) == 0);//断言输入参数是否对齐
RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)heap_ptr && //断言输入参数是否在合法范围内
(rt_uint8_t *)rmem < (rt_uint8_t *)heap_end);

RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));//使用钩子函数

if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr ||   //如果输入参数范围不合法,则直接返回
(rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
{
RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));

return;
}

/* Get the corresponding struct heap_mem ... */
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);//获取对应内存块指针

RT_DEBUG_LOG(RT_DEBUG_MEM,
("release memory 0x%x, size: %d\n",
(rt_uint32_t)rmem,
(rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));

/* protect the heap from concurrent access */
rt_sem_take(&heap_sem, RT_WAITING_FOREVER);

/* ... which has to be in a used state ... */
RT_ASSERT(mem->used); //断言当前内存块被使用
RT_ASSERT(mem->magic == HEAP_MAGIC);//断言当前内存块是之前被分配出来的
/* ... and is now unused. */
mem->used  = 0;
mem->magic = 0;

if (mem < lfree)//放入空闲链表
{
/* the newly freed struct is now the lowest */
lfree = mem;
}

#ifdef RT_MEM_STATS
used_mem -= (mem->next - ((rt_uint8_t*)mem - heap_ptr));//更新已使用的内存大小
#endif

/* finally, see if prev or next are free also */
plug_holes(mem); //尝试合并内存
rt_sem_release(&heap_sem);
}

static void plug_holes(struct heap_mem *mem)
{
struct heap_mem *nmem;
struct heap_mem *pmem;

RT_ASSERT((rt_uint8_t *)mem >= heap_ptr);//断言当前输入参数有合法范围
RT_ASSERT((rt_uint8_t *)mem < (rt_uint8_t *)heap_end);
RT_ASSERT(mem->used == 0);//断言当前内存未使用

/* plug hole forward */     //其下代码是尝试向后合并
nmem = (struct heap_mem *)&heap_ptr[mem->next];//指向下一内存块
if (mem != nmem && //如果存在下一内存块,且同样未使用,并且下一内存块不是指向堆内存末尾
nmem->used == 0 &&
(rt_uint8_t *)nmem != (rt_uint8_t *)heap_end)
{
/* if mem->next is unused and not end of heap_ptr,
* combine mem and mem->next
*/
if (lfree == nmem)//如果空闲内存指向下一内存块
{
lfree = mem;//则将空闲内存指向当前内存块
}
mem->next = nmem->next;//合并
((struct heap_mem *)&heap_ptr[nmem->next])->prev = (rt_uint8_t *)mem - heap_ptr;
}

/* plug hole backward */  //其下代码是尝试向前合并
pmem = (struct heap_mem *)&heap_ptr[mem->prev];//指向前一内存块
if (pmem != mem && pmem->used == 0)//如果存在前一内存块且未使用
{
/* if mem->prev is unused, combine mem and mem->prev */
if (lfree == mem)//如果空闲内存指向前一内存,则将空闲内存指向它
{
lfree = pmem;
}
pmem->next = mem->next;//合并
((struct heap_mem *)&heap_ptr[mem->next])->prev = (rt_uint8_t *)pmem - heap_ptr;
}
}