下面这段宏定义了内存堆
#ifndef LWIP_RAM_HEAP_POINTER
LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));
#define LWIP_RAM_HEAP_POINTER ram_heap //内存堆指针
#endif
将宏LWIP_DECLARE_MEMORY_ALIGNED展开,发现内存堆就是一个数组。
#ifndef LWIP_DECLARE_MEMORY_ALIGNED
#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]
#endif
动态堆最后被组织成内存块分配给程序,或者从程序释放到内存堆。
所谓的管理就是管理这些内存块,下面是对内存块的定义。而这些内存块最终会被挂接到一条链表。
/* 内存块结构体 */
struct mem {
/* 指向后一个内存块 */
mem_size_t next;
/* 指向前一个内存块 */
mem_size_t prev;
/* 内存块是否被使用 1:被使用 0:未使用 */
u8_t used;
#if MEM_OVERFLOW_CHECK
mem_size_t user_size;
#endif
};
初始化堆内存,将对内部组织成内存块链表。
/* 初始化堆内存 */
void mem_init(void)
{
struct mem *mem;
LWIP_ASSERT("Sanity check alignment", (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT - 1)) == 0);
/* 内存堆对齐 */
ram = (u8_t *)LWIP_MEM_ALIGN(LWIP_RAM_HEAP_POINTER);
/* 在内存堆起始处组织一个内存块 */
mem = (struct mem *)(void *)ram;
/* 下一个内存块指向内存堆结尾 */
mem->next = MEM_SIZE_ALIGNED;
/* 上一个内存块为空 */
mem->prev = 0;
/* 该内存块未使用 */
mem->used = 0;
/* 在内存堆结尾处组织一个内存块 */
ram_end = ptr_to_mem(MEM_SIZE_ALIGNED);
/* 该内存块已使用 */
ram_end->used = 1;
/* 上一个内存块指向自身 */
ram_end->next = MEM_SIZE_ALIGNED;
/* 下一个内存块指向自身 */
ram_end->prev = MEM_SIZE_ALIGNED;
MEM_SANITY();
/* 内存堆起始处初始化为地址最低的空闲块 */
lfree = (struct mem *)(void *)ram;
MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED);
/* 创建一个互斥锁 */
if (sys_mutex_new(&mem_mutex) != ERR_OK) {
LWIP_ASSERT("failed to create mem_mutex", 0);
}
}
内存分配,从链表中找到一个合适的内存块,分配给程序。将剩下的内存组织成新的内存块,并挂接到链表。
/* 申请内存 */
void *mem_malloc(mem_size_t size_in)
{
mem_size_t ptr, ptr2, size;
struct mem *mem, *mem2;
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
u8_t local_mem_free_count = 0;
#endif
LWIP_MEM_ALLOC_DECL_PROTECT();
if (size_in == 0) {
return NULL;
}
/* 字节数对齐 */
size = (mem_size_t)LWIP_MEM_ALIGN_SIZE(size_in);
/* 内存最小12字节 */
if (size < MIN_SIZE_ALIGNED) {
size = MIN_SIZE_ALIGNED;
}
#if MEM_OVERFLOW_CHECK
size += MEM_SANITY_REGION_BEFORE_ALIGNED + MEM_SANITY_REGION_AFTER_ALIGNED;
#endif
if ((size > MEM_SIZE_ALIGNED) || (size < size_in)) {
return NULL;
}
/* 获取互斥锁 */
sys_mutex_lock(&mem_mutex);
LWIP_MEM_ALLOC_PROTECT();
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
do {
local_mem_free_count = 0;
#endif
/* 遍历所有内存块 */
for (ptr = mem_to_ptr(lfree); ptr < MEM_SIZE_ALIGNED - size; ptr = ptr_to_mem(ptr)->next)
{
/* 内存块地址 */
mem = ptr_to_mem(ptr);
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
mem_free_count = 0;
LWIP_MEM_ALLOC_UNPROTECT();
LWIP_MEM_ALLOC_PROTECT();
if (mem_free_count != 0)
{
local_mem_free_count = 1;
break;
}
#endif
/* 内存块未使用,且满足分配大小(请求大小+内存块结构体大小) */
if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
{
/* 内存块过大(剩余内存还够分配最小内存块) */
if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
{
/* 剩下的内存地址 */
ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + size);
LWIP_ASSERT("invalid next ptr",ptr2 != MEM_SIZE_ALIGNED);
/* 将剩下的内存地址转换为内存块结构体指针 */
mem2 = ptr_to_mem(ptr2);
/* 新的内存块未使用 */
mem2->used = 0;
/* 将新的内存块插入链表 */
mem2->next = mem->next;
mem2->prev = ptr;
mem->next = ptr2;
/* 该内存块设置为已使用 */
mem->used = 1;
/* 如果不是最后一个内存块,则下一个内存块指向新内存块 */
if (mem2->next != MEM_SIZE_ALIGNED) {
ptr_to_mem(mem2->next)->prev = ptr2;
}
MEM_STATS_INC_USED(used, (size + SIZEOF_STRUCT_MEM));
}
/* 如果剩余内存不够重新组织一个新内存块 */
else
{
/* 该内存块已使用 */
mem->used = 1;
MEM_STATS_INC_USED(used, mem->next - mem_to_ptr(mem));
}
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
mem_malloc_adjust_lfree:
#endif
/* 如果该内存块为地址最低的空闲块,则要更新地址最低空闲块 */
if (mem == lfree)
{
struct mem *cur = lfree;
/* 遍历向后的所有内存块,找出地址最低的空闲块 */
while (cur->used && cur != ram_end)
{
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
mem_free_count = 0;
LWIP_MEM_ALLOC_UNPROTECT();
LWIP_MEM_ALLOC_PROTECT();
if (mem_free_count != 0) {
goto mem_malloc_adjust_lfree;
}
#endif
cur = ptr_to_mem(cur->next);
}
/* 更新地址最低的空闲块 */
lfree = cur;
LWIP_ASSERT("mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->used)));
}
LWIP_MEM_ALLOC_UNPROTECT();
/* 释放互斥锁 */
sys_mutex_unlock(&mem_mutex);
LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
(mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end);
LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
((mem_ptr_t)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
LWIP_ASSERT("mem_malloc: sanity check alignment",
(((mem_ptr_t)mem) & (MEM_ALIGNMENT - 1)) == 0);
#if MEM_OVERFLOW_CHECK
mem_overflow_init_element(mem, size_in);
#endif
MEM_SANITY();
/* 返回内存地址 */
return (u8_t *)mem + SIZEOF_STRUCT_MEM + MEM_SANITY_OFFSET;
}
}
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
}
while (local_mem_free_count != 0);
#endif
MEM_STATS_INC(err);
LWIP_MEM_ALLOC_UNPROTECT();
sys_mutex_unlock(&mem_mutex);
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size));
return NULL;
}
释放内存,将内存重新组织成内存块插入链表。相邻内存堆都空闲,需要合并。
/* 释放内存 */
void mem_free(void *rmem)
{
struct mem *mem;
LWIP_MEM_FREE_DECL_PROTECT();
/* 参数合法性验证,释放的指针不能为空 */
if (rmem == NULL)
{
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("mem_free(p == NULL) was called.\n"));
return;
}
/* 参数合法性验证,释放的指针必须要字节对齐 */
if ((((mem_ptr_t)rmem) & (MEM_ALIGNMENT - 1)) != 0)
{
LWIP_MEM_ILLEGAL_FREE("mem_free: sanity check alignment");
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: sanity check alignment\n"));
MEM_STATS_INC_LOCKED(illegal);
return;
}
/* 内存指针向前偏移一个内存块结构体大小 */
mem = (struct mem *)(void *)((u8_t *)rmem - (SIZEOF_STRUCT_MEM + MEM_SANITY_OFFSET));
/* 参数合法性验证,释放的指针必须在内存堆中 */
if ((u8_t *)mem < ram || (u8_t *)rmem + MIN_SIZE_ALIGNED > (u8_t *)ram_end)
{
LWIP_MEM_ILLEGAL_FREE("mem_free: illegal memory");
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory\n"));
MEM_STATS_INC_LOCKED(illegal);
return;
}
#if MEM_OVERFLOW_CHECK
mem_overflow_check_element(mem);
#endif
LWIP_MEM_FREE_PROTECT();
/* 参数合法性验证,内存块必须已经分配 */
if (!mem->used)
{
LWIP_MEM_ILLEGAL_FREE("mem_free: illegal memory: double free");
LWIP_MEM_FREE_UNPROTECT();
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory: double free?\n"));
MEM_STATS_INC_LOCKED(illegal);
return;
}
/* 参数合法性验证,检查内存块是否链接在链表中 */
if (!mem_link_valid(mem))
{
LWIP_MEM_ILLEGAL_FREE("mem_free: illegal memory: non-linked: double free");
LWIP_MEM_FREE_UNPROTECT();
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory: non-linked: double free?\n"));
MEM_STATS_INC_LOCKED(illegal);
return;
}
/* 将内存块设置为未使用 */
mem->used = 0;
/* 内存块地址小于最低地址空闲块 */
if (mem < lfree)
{
/* 更新最低地址空闲块 */
lfree = mem;
}
MEM_STATS_DEC_USED(used, mem->next - (mem_size_t)(((u8_t *)mem - ram)));
/* 合并空闲块 */
plug_holes(mem);
MEM_SANITY();
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
mem_free_count = 1;
#endif
LWIP_MEM_FREE_UNPROTECT();
}
/* 合并空闲块 */
static void plug_holes(struct mem *mem)
{
struct mem *nmem;
struct mem *pmem;
LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram);
LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end);
LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0);
LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE_ALIGNED", mem->next <= MEM_SIZE_ALIGNED);
/* 下一个内存块 */
nmem = ptr_to_mem(mem->next);
/* 下一个内存块未被使用,并且不是最后一个内存块 */
if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end)
{
/* 下一个空闲块是最小地址空闲块 */
if (lfree == nmem)
{
/* 更新最小地址空闲块 */
lfree = mem;
}
/* 合并后一个内存块 */
mem->next = nmem->next;
if (nmem->next != MEM_SIZE_ALIGNED)
{
ptr_to_mem(nmem->next)->prev = mem_to_ptr(mem);
}
}
/* 上一个内存块 */
pmem = ptr_to_mem(mem->prev);
/* 上一个内存块未使用 */
if (pmem != mem && pmem->used == 0)
{
/* 该内存块是最小地址空闲块 */
if (lfree == mem)
{
/* 更新最小地址空闲块 */
lfree = pmem;
}
/* 合并上一个内存块 */
pmem->next = mem->next;
if (mem->next != MEM_SIZE_ALIGNED)
{
ptr_to_mem(mem->next)->prev = mem_to_ptr(pmem);
}
}
}
除了基本的分配和释放,协议栈还mem_trim函数和mem_calloc函数
mem_trim函数,能够收缩内存,将原有的内存缩小。缩小的部分重新组织成内存块,挂接到链表。
/* 收缩内存 */
void *mem_trim(void *rmem, mem_size_t new_size)
{
mem_size_t size, newsize;
mem_size_t ptr, ptr2;
struct mem *mem, *mem2;
LWIP_MEM_FREE_DECL_PROTECT();
/* 新的内存大小字节对齐 */
newsize = (mem_size_t)LWIP_MEM_ALIGN_SIZE(new_size);
/* 字节大小大于最小内存字节 */
if (newsize < MIN_SIZE_ALIGNED)
{
newsize = MIN_SIZE_ALIGNED;
}
#if MEM_OVERFLOW_CHECK
newsize += MEM_SANITY_REGION_BEFORE_ALIGNED + MEM_SANITY_REGION_AFTER_ALIGNED;
#endif
/* 参数合法性验证,新的内存大小不能大于内存堆 */
if ((newsize > MEM_SIZE_ALIGNED) || (newsize < new_size)) {
return NULL;
}
LWIP_ASSERT("mem_trim: legal memory", (u8_t *)rmem >= (u8_t *)ram && (u8_t *)rmem < (u8_t *)ram_end);
/* 参数合法性验证,内存必须在内存堆中 */
if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end)
{
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_trim: illegal memory\n"));
MEM_STATS_INC_LOCKED(illegal);
return rmem;
}
/* 内存指针向前偏移内存块结构体大小 */
mem = (struct mem *)(void *)((u8_t *)rmem - (SIZEOF_STRUCT_MEM + MEM_SANITY_OFFSET));
#if MEM_OVERFLOW_CHECK
mem_overflow_check_element(mem);
#endif
/* 内存指针转偏移量 */
ptr = mem_to_ptr(mem);
/* 当前内存块,内存大小 */
size = (mem_size_t)((mem_size_t)(mem->next - ptr) - (SIZEOF_STRUCT_MEM + MEM_SANITY_OVERHEAD));
LWIP_ASSERT("mem_trim can only shrink memory", newsize <= size);
/* 参数合法性验证,新内存大小不能大于原内存大小 */
if (newsize > size)
{
return NULL;
}
/* 新内存大小如果等于原内存大小,什么也不用做 */
if (newsize == size)
{
return rmem;
}
LWIP_MEM_FREE_PROTECT();
/* 下一个内存块指针 */
mem2 = ptr_to_mem(mem->next);
/* 下一个内存块未使用,需要合并 */
if (mem2->used == 0)
{
mem_size_t next;
LWIP_ASSERT("invalid next ptr", mem->next != MEM_SIZE_ALIGNED);
/* 下下个内存偏移量 */
next = mem2->next;
/* 截取之后,剩余内存块指针 */
ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + newsize);
/* 下一个内存块是地址最低空闲块 */
if (lfree == mem2)
{
/* 更新最低空闲块指针 */
lfree = ptr_to_mem(ptr2);
}
/* 剩余内存块指针 */
mem2 = ptr_to_mem(ptr2);
/* 剩余内存块设置为未使用 */
mem2->used = 0;
/* 将剩余空闲块和下一个内存块合并插入链表 */
mem2->next = next;
mem2->prev = ptr;
mem->next = ptr2;
/* 不是最后一个内存块,要让下一个内存块指向自己 */
if (mem2->next != MEM_SIZE_ALIGNED)
{
ptr_to_mem(mem2->next)->prev = ptr2;
}
MEM_STATS_DEC_USED(used, (size - newsize));
}
/* 下一个内存块不空闲,并且剩余的内存还够组织新的内存块 */
else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size)
{
/* 剩余的内存块指针 */
ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + newsize);
LWIP_ASSERT("invalid next ptr", mem->next != MEM_SIZE_ALIGNED);
/* 内存块转换为偏移量 */
mem2 = ptr_to_mem(ptr2);
/* 剩余内存块地址小于最小地址空闲块 */
if (mem2 < lfree)
{
/* 更新最小地址空闲块 */
lfree = mem2;
}
/* 剩余内存块未使用 */
mem2->used = 0;
/* 将剩余内存块插入链表 */
mem2->next = mem->next;
mem2->prev = ptr;
mem->next = ptr2;
if (mem2->next != MEM_SIZE_ALIGNED)
{
ptr_to_mem(mem2->next)->prev = ptr2;
}
MEM_STATS_DEC_USED(used, (size - newsize));
}
#if MEM_OVERFLOW_CHECK
mem_overflow_init_element(mem, new_size);
#endif
MEM_SANITY();
#if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
mem_free_count = 1;
#endif
LWIP_MEM_FREE_UNPROTECT();
return rmem;
}
mem_calloc函数,申请内存的同时,将内存清空。
/* 申请并清空内存 */
void *mem_calloc(mem_size_t count, mem_size_t size)
{
void *p;
size_t alloc_size = (size_t)count * (size_t)size;
if ((size_t)(mem_size_t)alloc_size != alloc_size)
{
LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("mem_calloc: could not allocate %"SZT_F" bytes\n", alloc_size));
return NULL;
}
/* 申请内存 */
p = mem_malloc((mem_size_t)alloc_size);
/* 申请成功 */
if (p)
{
/* 清空内存 */
memset(p, 0, alloc_size);
}
return p;
}