lwip_init
==> mem_init();
/**
* MEM_SIZE: the size of the heap memory. If the application will send
* a lot of data that needs to be copied, this should be set high.
*/
#ifndef MEM_SIZE
#define MEM_SIZE 1600 // opt.h中定义了heap大小[luther.gliethttp]
#endif
#define MIN_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MIN_SIZE)
#define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))
#define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE)
/** the heap. we need one struct mem at the end and some room for alignment */
static u8_t ram_heap[MEM_SIZE_ALIGNED + (2*SIZEOF_STRUCT_MEM) + MEM_ALIGNMENT]; // 之所乘以2,因为ram和ram_end将各占用一个[luther.gliethttp]
/** pointer to the heap (ram_heap): for alignment, ram is now a pointer instead of an array */
static u8_t *ram;
/** the last entry, always unused! */
static struct mem *ram_end;
/** pointer to the lowest free block, this is used for faster search */
static struct mem *lfree;
/**
* Zero the heap and initialize start, end and lowest-free
*/
void
mem_init(void)
{
struct mem *mem;
LWIP_ASSERT("Sanity check alignment",
(SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0);
/* align the heap */
ram = LWIP_MEM_ALIGN(ram_heap); // 经过对齐之后ram指向ram_heap[]内存数组空间[luther.gliethttp]
/* initialize the start of the heap */
mem = (struct mem *)ram;
mem->next = MEM_SIZE_ALIGNED; // 表示mem从0到MEM_SIZE_ALIGNED大小都是归该ram管理,都是空闲的.
mem->prev = 0;
mem->used = 0; // 标识未使用
/* initialize the end of the heap */
ram_end = (struct mem *)&ram[MEM_SIZE_ALIGNED];
ram_end->used = 1;
ram_end->next = MEM_SIZE_ALIGNED;
ram_end->prev = MEM_SIZE_ALIGNED;
mem_sem = sys_sem_new(1); // 创建内存互斥操作时所需的信号量[luther.gliethttp]
/* initialize the lowest-free pointer to the start of the heap */
lfree = (struct mem *)ram; // ram包含的空间0-MEM_SIZE_ALIGNED全部可用.
MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED);
}
阅读(4319) | 评论(0) | 转发(0) |