linux有现成的内存池,linux下一个内存池实现

memory_pool.h

#ifndef _MEMORY_POOL_H_

#define _MEMORY_POOL_H_

#ifdef __cplusplus

namespace mem_pool { extern "C" {

#endif

/*Pool's mininum capacity*/

#define MP_MIN_CAPACITY 5

/*Pool's default capacity*/

#define MP_DEF_CAPACITY 30

/*Pool's maximum capacity*/

#define MP_MAX_CAPACITY 100

/*User-defined alloc & free routine*/

typedef void *(*mp_alloc)();

typedef void (*mp_free)(void *);

/*memory pool structure*/

typedef struct{

int m_capacity;//Pool's capacity

int m_itemcnt;//Pool's current item count

void** m_items;//Pool's item pointer array

mp_alloc m_alloc;//Pool's alloc function

mp_free m_free;//Pool's free function

}mp_t;

/*

Function: create a new memory pool

Param:

_alloc--------User-defined alloc function

_free---------User_defined free function

_capacity-----Pool's capacity

Return:

A new memory pool if successful, NULL otherwise

Note:

if _capacity is larger than MP_MAX_CAPACITY or smaller than MP_MIN_CAPACITY, MP_DEF_CAPACITY will be used

*/

mp_t* mempool_create(mp_alloc _alloc, mp_free _free, unsigned int _capacity);

/*

Function: destroy an existing memory pool

Param:

_mp--------the existing memory pool

Return:

None

*/

void mempool_destroy(mp_t *_mp);

/*

Function: alloc a memory block from a memory pool

Param:

_mp--------the memory pool

Return:

a pointer to the allocated memory block, or NULL if failed

*/

void *mempool_alloc(mp_t *_mp);

/*

Function: deallocate a memory block to the memory pool

Param:

_mp--------the memory pool

_item------the memory block to be deallocated

Return:

NULL

*/

void mempool_free(mp_t *_mp, void *_item);

#ifdef __cplusplus

} }

#endif

#endif

memory_pool.c

#include

#include "memory_pool.h"

#ifdef __cplusplus

namespace mem_pool { extern "C" {

#endif

mp_t* mempool_create(mp_alloc _alloc, mp_free _free, unsigned int _capacity)

{

mp_t *mp = NULL;

if(NULL == _alloc || NULL == _free) {return NULL;}

if(_capacity < MP_MIN_CAPACITY || _capacity > MP_MAX_CAPACITY) {_capacity = MP_DEF_CAPACITY;}

mp = malloc(sizeof(mp_t));

if(NULL != mp){

mp->m_capacity = _capacity;

mp->m_itemcnt = 0;

mp->m_items = (void **)malloc(sizeof(void *) * _capacity);

mp->m_alloc = _alloc;

mp->m_free = _free;

if(NULL == mp->m_items){

free(mp);

mp = NULL;

}

}

return mp;

}

void mempool_destroy(mp_t *_mp)

{

register unsigned int i;

if(NULL != _mp){

if(_mp->m_items){

for(i = 0; i < _mp->m_itemcnt; ++i){

if(NULL != _mp->m_items[i]){

_mp->m_free(_mp->m_items[i]);

_mp->m_items[i] = NULL;

}

}

free(_mp->m_items);

_mp->m_items = NULL;

}

free(_mp);

_mp = NULL;

}

}

void *mempool_alloc(mp_t *_mp)

{

void *item = NULL;

if(NULL != _mp){

item = _mp->m_itemcnt > 0 ? _mp->m_items[--_mp->m_itemcnt] : _mp->m_alloc();

}

return item;

}

void mempool_free(mp_t *_mp, void *_item)

{

if(NULL != _mp && NULL != _item){

if(_mp->m_itemcnt < _mp->m_capacity){

_mp->m_items[_mp->m_itemcnt++] = _item;

}else{

_mp->m_free(_item);

}

}

}

#ifdef __cplusplus

} }

#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值