LWIP2.1.3内存管理笔记

时间:2024-04-19 07:06:20

 LWIP_MEM_ALIGN_SIZE(size)



#define MIN_SIZE             12
#define MIN_SIZE_ALIGNED     LWIP_MEM_ALIGN_SIZE(MIN_SIZE)
#define MEM_ALIGNMENT                   4

#define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))

/*111111111*/

 LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U))重点解析这个宏功能

1、         size=12       MEM_ALIGNMENT=4

2、((size) + MEM_ALIGNMENT - 1U)   内存大小+3  也就是有4的余数就进1

3、& ~(MEM_ALIGNMENT-1U)    低2位清0    与上 ((size) + MEM_ALIGNMENT - 1U)之后就变成4的倍数;

整体功能: size大小要  4字节对齐     size大小必须是4的整数倍,有余就补4,没有余就不用补                                                                               

#define MIN_SIZE_ALIGNED     LWIP_MEM_ALIGN_SIZE(MIN_SIZE)//最小内申请内存块对齐
#define SIZEOF_STRUCT_MEM    LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))//结构体内存4字节对齐
#define MEM_SIZE_ALIGNED     LWIP_MEM_ALIGN_SIZE(MEM_SIZE)//内存堆总内存4字节对齐

都是进行四字节对齐

 LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));

#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U)(((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U)))//4字节对齐

#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) u8_t variable_name[LWIP_MEM_ALIGN_BUFFER(size)]//开辟一个数组 定义

LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));
//开辟了 一个内存堆  大小为 可用内存MEM_SIZE_ALIGNED  25*1024字节 +两个管理内存大小  +3个字节

 MEM_ALIGNMENT  =4;

#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1U)(((size) + MEM_ALIGNMENT - 1U) & ~(MEM_ALIGNMENT-1U)))//4字节对齐

MEM_SIZE_ALIGNED  是内存堆大小      (25*1024)

2 *  SIZEOF_STRUCT_MEM   两个管理内存结构体

LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U * SIZEOF_STRUCT_MEM));

//开辟了 一个内存堆  大小为 可用内存MEM_SIZE_ALIGNED  25*1024字节 +两个管理内存大小  +3个字节