LwIP系列--利用其内存池实现定义私有的内存池

一、目的

在之前的博文中我们介绍了LwIP的内存池实现,也介绍了如何使用各种尺寸大小的内存池作为内存池集合作为内存堆的分配算法。

本篇主要介绍一下如何在自己的应用代码中使用LwIP中现有的内存池实现。

二、介绍

在头文件memp_std.h和lwippools.h(可选)中声明的内存池都是LwIP的内部内存池,都是有一个枚举值(memp_t)进行索引的,相应的内存池分配释放接口为

void *memp_malloc(memp_t type);
void  memp_free(memp_t type, void *mem);

那如果我们定义一个私有内存池应该怎么做呢?

/**
 * @ingroup mempool
 * Declare prototype for private memory pool if it is used in multiple files
 */
#define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name

/**
 * @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 \
  };

如果这个内存池要在多个文件中使用就需要通过LWIP_MEMPOOL_PROTOTYPE宏进行声明

/**
 * @ingroup mempool
 * Declare prototype for private memory pool if it is used in multiple files
 */
#define LWIP_MEMPOOL_PROTOTYPE(name) extern const struct memp_desc memp_ ## name

在自己的头文件中进行声明,例如foo.h

#ifndef FOO_H
#define FOO_H
#include "lwip/memp.h"
LWIP_MEMPOOL_PROTOTYPE(foo);
#endif

展开后即

extern const struct memp_desc memp_foo;

在自己的源文件中进行定义,例如foo.c

LWIP_MEMPOOL_DECLARE(foo, 4, 16, "foo pool")

展开后即

u8_t memp_memory_foo_base[4 * 16];
static struct stats_mem memp_stats_foo;
static struct memp *memp_tab_foo;
const struct memp_desc memp_foo = {
    "foo pool",
    &memp_stats_foo,
    16,
    4,
    memp_memory_foo_base,
    &memp_tab_foo
};

现在我们就有了自己的内存池描述变量memp_foo;

通过下面的宏定义我们就可以分配和释放了

/**
 * @ingroup mempool
 * Initialize a private memory pool
 */
#define LWIP_MEMPOOL_INIT(name)    memp_init_pool(&memp_ ## name)
/**
 * @ingroup mempool
 * Allocate from a private memory pool
 */
#define LWIP_MEMPOOL_ALLOC(name)   memp_malloc_pool(&memp_ ## name)
/**
 * @ingroup mempool
 * Free element from a private memory pool
 */
#define LWIP_MEMPOOL_FREE(name, x) memp_free_pool(&memp_ ## name, (x))

展开后即

#define LWIP_MEMPOOL_INIT(foo)    memp_init_pool(&memp_foo)
#define LWIP_MEMPOOL_ALLOC(name)   memp_malloc_pool(&memp_foo)
#define LWIP_MEMPOOL_FREE(name, x) memp_free_pool(&memp_foo, (x))

这样我们就可以省去自行设计内存池的麻烦,至此本篇的内容也介绍完毕。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值