lwip2.1.0 pool的声明和pool的地址指针数组

1 篇文章 0 订阅

/*lwip2.1.0 POOL的声明和获取每个pool的地址*/

//-----------------------------------------------------------------------------------
//
lwip-2.1.0\src\core\memp.c
//声明每种类型的pool
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
#include "lwip/priv/memp_std.h"

//初始化包含每一种memp_pools的 *const memp_pools指针数组
const struct memp_desc *const memp_pools[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name, //example: &memp_UDP_PCB
#include "lwip/priv/memp_std.h"
};

//-----------------------------------------------------------------------------------

/**lwip-2.1.0\src\include\lwip\memp.h
 * @ingroup mempool
 * Declare a private memory pool
 * Private mempools example:
 * .h: only when pool is used in multiple .c files: LWIP_MEMPOOL_PROTOTYPE(my_private_pool);
 * .c:
 *   - in global variables section: LWIP_MEMPOOL_DECLARE(my_private_pool, 10, sizeof(foo), "Some description")
 *   - call ONCE before using pool (e.g. in some init() function): LWIP_MEMPOOL_INIT(my_private_pool);
 *   - allocate: void* my_new_mem = LWIP_MEMPOOL_ALLOC(my_private_pool);
 *   - free: LWIP_MEMPOOL_FREE(my_private_pool, my_new_mem);
 *
 * To relocate a pool, declare it as extern in cc.h. Example for GCC:
 *   extern u8_t \_\_attribute\_\_((section(".onchip_mem"))) memp_memory_my_private_pool_base[];
 */
#define LWIP_MEMPOOL_DECLARE(name,num,size,desc) \
  LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_ ## name ## _base, ((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))); \
    \
  LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(memp_stats_ ## name) \
    \
  static struct memp *memp_tab_ ## name; \
    \
  const struct memp_desc memp_ ## name = { \
    DECLARE_LWIP_MEMPOOL_DESC(desc) \
    LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(memp_stats_ ## name) \
    LWIP_MEM_ALIGN_SIZE(size), \
    (num), \
    memp_memory_ ## name ## _base, \
    &memp_tab_ ## name \
  };

//-----------------------------------------------------------------------------------
  

 /** Memory pool descriptor *///memp_desc结构  lwip-2.1.0\src\include\lwip\priv\memp_priv.h
struct memp_desc {
#if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
  /** Textual description */
  const char *desc;
#endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */

  /** Element size */
  u16_t size;

#if !MEMP_MEM_MALLOC
  /** Number of elements */
  u16_t num;

  /** Base address */
  u8_t *base;

  /** First free element of each pool. Elements form a linked list. */
  struct memp **tab;
#endif /* MEMP_MEM_MALLOC */
};

  
//-----------------------------------------------------------------------------------  

  
/*******************************************************************************************************************************
LWIP_MEMPOOL(name,num,size,desc)替换为 LWIP_MEMPOOL_DECLARE(name,num,size,desc)
LWIP_MEMPOOL_DECLARE(name,num,size,desc)定义在lwip-2.1.0\src\include\lwip\memp.h

编译器执行以下2句
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
#include "lwip/priv/memp_std.h"

编译器通过memp_std.h内的LWIP_MEMPOOL定义依次替换数据。
如:memp_std.h内定义了
#if LWIP_UDP
LWIP_MEMPOOL(UDP_PCB,        MEMP_NUM_UDP_PCB,         sizeof(struct udp_pcb),        "UDP_PCB")
#endif //LWIP_UDP

会得到如下数据
#define LWIP_MEMPOOL_DECLARE(name,num,size,desc) --->
  LWIP_DECLARE_MEMORY_ALIGNED(memp_memory_UDP_PCB_base, ((MEMP_NUM_UDP_PCB) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(struct udp_pcb)))));//声明memp_memory_UDP_PCB_base 的Pool数组(也可以定义外部数组)
  static struct memp *memp_tab_UDP_PCB; //定义memp_tab_UDP_PCB链表头指针,也就是指向第一个pool的地址
  const struct memp_desc memp_UDP_PCB = { //声明memp_desc类型的memp_UDP_PCB结构体,结构元素定义参考memp_desc结构体
    DECLARE_LWIP_MEMPOOL_DESC("UDP_PCB") //描述字符串
    LWIP_MEM_ALIGN_SIZE(sizeof(struct udp_pcb)), //单个pool的大小
    (MEMP_NUM_UDP_PCB), //memp_UDP_PCB中pool的数量
    memp_memory_UDP_PCB_base, //memp_memory_UDP_PCB_base数组的地址
    &memp_tab_UDP_PCB //获取定义的memp_tab_UDP_PCB链表头指针
  };

  
  这样一个UDP_PCB的pool就声明好好了。
  
  接下来看  
    const struct memp_desc *const memp_pools[MEMP_MAX] = {
    #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name, //example: &memp_UDP_PCB 
    #include "lwip/priv/memp_std.h"
    };
    这几句用来声明了指向memp_desc类型的memp_pools指针数组。
    最开始我们声明了memp_desc类型的memp_UDP_PCB结构体,在这里就相当于把这些结构体的指针存在memp_desc *const memp_pools[MEMP_MAX]数组内
};
****************************************************************************************************************************** */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值