码迷,mamicode.com
首页 > 其他好文 > 详细

简单内存池实现

时间:2014-06-14 15:52:04      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   tar   

#ifndef __MEM_POOL_H__
#define __MEM_POOL_H__

typedef struct tagMemItem
{
    int   nSize;
    int   nUsed;
    void* addr;
}MEM_ITEM_S;

/*****************************************************************************************************
 * 4M memory pool
 * +--------+--------+-------+-------+-------+-------+--------+--------+--------+--------+---------+
 * | 256*80 | 512*20 | 1K*10 | 2K*11 | 4K*10 | 8K*10 | 16K*10 | 32K*15 | 64K*16 | 128K*5 | 160K*10 |
 * +--------+--------+-------+-------+-------+-------+--------+--------+--------+--------+---------+
 *
 *****************************************************************************************************/
#define MEM_SIZ_256    (256)
#define MEM_SIZ_512    (512)
#define MEM_SIZ_1K     (1024)
#define MEM_SIZ_2K     (2*MEM_SIZ_1K)
#define MEM_SIZ_4K     (4*MEM_SIZ_1K)
#define MEM_SIZ_8K     (8*MEM_SIZ_1K)
#define MEM_SIZ_16K    (16*MEM_SIZ_1K)
#define MEM_SIZ_32K    (32*MEM_SIZ_1K)
#define MEM_SIZ_64K    (64*MEM_SIZ_1K)
#define MEM_SIZ_128K   (128*MEM_SIZ_1K)
#define MEM_SIZ_160K   (160*MEM_SIZ_1K)

#define MEM_256_NUM  (80)
#define MEM_512_NUM  (20)
#define MEM_1K_NUM   (20)
#define MEM_2K_NUM   (11)
#define MEM_4K_NUM   (10)
#define MEM_8K_NUM   (10)
#define MEM_16K_NUM  (10)
#define MEM_32K_NUM  (15)
#define MEM_64K_NUM  (16)
#define MEM_128K_NUM (5)
#define MEM_160K_NUM (10)

#define MEM_BLOCK_256   (MEM_256_NUM*MEM_SIZ_256)
#define MEM_BLOCK_512   (MEM_512_NUM*MEM_SIZ_512)
#define MEM_BLOCK_1K    (MEM_1K_NUM*MEM_SIZ_1K)
#define MEM_BLOCK_2K    (MEM_2K_NUM*MEM_SIZ_2K)
#define MEM_BLOCK_4K    (MEM_4K_NUM*MEM_SIZ_4K)
#define MEM_BLOCK_8K    (MEM_8K_NUM*MEM_SIZ_8K)
#define MEM_BLOCK_16K   (MEM_16K_NUM*MEM_SIZ_16K)
#define MEM_BLOCK_32K   (MEM_32K_NUM*MEM_SIZ_32K)
#define MEM_BLOCK_64K   (MEM_64K_NUM*MEM_SIZ_64K)
#define MEM_BLOCK_128K  (MEM_128K_NUM*MEM_SIZ_128K)
#define MEM_BLOCK_160K  (MEM_160K_NUM*MEM_SIZ_160K)

#define MEM_OFFSET_256    (0)
#define MEM_OFFSET_512    (MEM_OFFSET_256  + MEM_BLOCK_256)
#define MEM_OFFSET_1K     (MEM_OFFSET_512  + MEM_BLOCK_512)
#define MEM_OFFSET_2K     (MEM_OFFSET_1K   + MEM_BLOCK_1K)
#define MEM_OFFSET_4K     (MEM_OFFSET_2K   + MEM_BLOCK_2K)
#define MEM_OFFSET_8K     (MEM_OFFSET_4K   + MEM_BLOCK_4K)
#define MEM_OFFSET_16K    (MEM_OFFSET_8K   + MEM_BLOCK_8K)
#define MEM_OFFSET_32K    (MEM_OFFSET_16K  + MEM_BLOCK_16K)
#define MEM_OFFSET_64K    (MEM_OFFSET_32K  + MEM_BLOCK_32K)
#define MEM_OFFSET_128K   (MEM_OFFSET_64K  + MEM_BLOCK_64K)
#define MEM_OFFSET_160K   (MEM_OFFSET_128K + MEM_BLOCK_128K)

typedef struct tagMemPool
{
    MEM_ITEM_S item256[MEM_256_NUM];     /* 256*4 *20 = 20K   */
    MEM_ITEM_S item512[MEM_512_NUM];     /* 512*2 *10 = 10K   */
    MEM_ITEM_S item1K[MEM_1K_NUM];       /* 1 *20     = 20K   */
    MEM_ITEM_S item2K[MEM_2K_NUM];       /* 2 *11     = 22K   */
    MEM_ITEM_S item4K[MEM_4K_NUM];       /* 4 *10     = 40K   */
    MEM_ITEM_S item8K[MEM_8K_NUM];       /* 8 *10     = 80K   */
    MEM_ITEM_S item16K[MEM_16K_NUM];     /* 16 * 10   = 160K  */
    MEM_ITEM_S item32K[MEM_32K_NUM];     /* 32 * 15   = 480K  */
    MEM_ITEM_S item64K[MEM_64K_NUM];     /* 64 * 16   = 1024K */
    MEM_ITEM_S item128K[MEM_128K_NUM];   /* 128 * 5   = 640K  */
    MEM_ITEM_S item160K[MEM_160K_NUM];   /* 160 * 10  = 1600K */
}TNMEM_POOLA_S;

#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "MemPool.h"

#define INIT_MEM_ADD_SIZE(start_addr, member, num, size)                do{                                                                         int ii = 0;                                                             for(ii = 0; ii<num; ii++)                                               {                                                                           g_tnmem_pool.member[ii].nSize = size;                                    g_tnmem_pool.member[ii].nUsed = 0;                                        g_tnmem_pool.member[ii].addr  = (char*)start_addr + ii*size;        }                                                                   }while(0)

TNMEM_POOLA_S g_tnmem_pool = {0};

//#define DBG_MEM
#ifndef DBG_MEM
#define TNMEM_INIT_TEMPLATE(num)                                                        void TNMEM_Init##num(void* pMemPoolHeadAddr)                                            {                                                                                           INIT_MEM_ADD_SIZE(pMemPoolHeadAddr, item##num, MEM_##num##_NUM, MEM_SIZ_##num);     }
#else
#define TNMEM_INIT_TEMPLATE(num)                                                         void TNMEM_Init##num(void* pMemPoolHeadAddr)                                             {                                                                                            int ii = 0;                                                                              for(ii = 0; ii<MEM_##num##_NUM; ii++)                                                    {                                                                                            g_tnmem_pool.item##num##[ii].nSize = MEM_SIZ_##num;                                      g_tnmem_pool.item##num##[ii].nUsed = 0;                                                  g_tnmem_pool.item##num##[ii].addr  = (char*)pMemPoolHeadAddr + ii*MEM_SIZ_##num;     }                                                                                    }
#endif

TNMEM_INIT_TEMPLATE(256)
TNMEM_INIT_TEMPLATE(512)
TNMEM_INIT_TEMPLATE(1K)
TNMEM_INIT_TEMPLATE(2K)
TNMEM_INIT_TEMPLATE(4K)
TNMEM_INIT_TEMPLATE(8K)
TNMEM_INIT_TEMPLATE(16K)
TNMEM_INIT_TEMPLATE(32K)
TNMEM_INIT_TEMPLATE(64K)
TNMEM_INIT_TEMPLATE(128K)
TNMEM_INIT_TEMPLATE(160K)


void* TNMEM_CreateMemPool(size_t size)
{
    void* pMemHead = malloc(size);

    if (pMemHead != NULL)
    {
        memset(&g_tnmem_pool, 0, sizeof(g_tnmem_pool));
        TNMEM_Init256(pMemHead);
        TNMEM_Init512(pMemHead);
        TNMEM_Init1K(pMemHead);
        TNMEM_Init2K(pMemHead);
        TNMEM_Init4K(pMemHead);
        TNMEM_Init8K(pMemHead);
        TNMEM_Init16K(pMemHead);
        TNMEM_Init32K(pMemHead);
        TNMEM_Init64K(pMemHead);
        TNMEM_Init128K(pMemHead);
        TNMEM_Init160K(pMemHead);
    }

    return pMemHead;
}

#define MEM_GET_SIZE(mem_header, member, total)    do{                                                    int ii = 0;                                        if (mem_header == NULL)                            {                                                      return NULL;                                   }                                                  for(ii=0; ii<total; ii++)                          {                                                      if (g_tnmem_pool.member[ii].nUsed == 0)            {                                                      g_tnmem_pool.member[ii].nUsed = 1;                 break;                                         }                                              }                                                  if (ii == total)                                   {                                                      return NULL;                                   }                                                  return g_tnmem_pool.member[ii].addr;           }while(0)

void TNMEM_DestroyMemPool(void* addr)
{
    free(addr);
}

void* TNMEM_GetMemFromPool(void* pMemPool, size_t size)
{
    if (size > 160*1024)
    {
        return malloc(size);
    }
    else if (size > 128*1024)
    {
        MEM_GET_SIZE(pMemPool, item160K, MEM_160K_NUM);
    }
    else if (size > 64*1024)
    {
        MEM_GET_SIZE(pMemPool, item128K, MEM_128K_NUM);
    }
    else if (size > 32*1024)
    {
        MEM_GET_SIZE(pMemPool, item64K, MEM_64K_NUM);
    }
    else if (size > 16*1024)
    {
        MEM_GET_SIZE(pMemPool, item32K, MEM_32K_NUM);
    }
    else if (size > 8*1024)
    {
        MEM_GET_SIZE(pMemPool, item16K, MEM_16K_NUM);
    }
    else if (size > 4*1024)
    {
        MEM_GET_SIZE(pMemPool, item8K, MEM_8K_NUM);
    }
    else if (size > 2*1024)
    {
        MEM_GET_SIZE(pMemPool, item4K, MEM_4K_NUM);
    }
    else if (size > 1*1024)
    {
        MEM_GET_SIZE(pMemPool, item2K, MEM_2K_NUM);
    }
    else if (size > 512)
    {
        MEM_GET_SIZE(pMemPool, item1K, MEM_1K_NUM);
    }
    else if (size > 256)
    {
        MEM_GET_SIZE(pMemPool, item512, MEM_512_NUM);
    }
    else
    {
        MEM_GET_SIZE(pMemPool, item256, MEM_256_NUM);
    }
}

#define FREE_MEM(addr, member, total)              do{                                                    int ii = 0;                                        for(ii=0; ii<total; ii++)                          {                                                      if (addr == g_tnmem_pool.member[ii].addr)          {                                                      g_tnmem_pool.member[ii].nUsed = 0;                 return;                                        }                                              }                                              }while(0)

void TNMEM_FreeMemToPool(void* pMemPool, void* addr)
{
    FREE_MEM(addr, item256, MEM_256_NUM);
    FREE_MEM(addr, item512, MEM_512_NUM);
    FREE_MEM(addr, item1K, MEM_1K_NUM);
    FREE_MEM(addr, item2K, MEM_2K_NUM);
    FREE_MEM(addr, item4K, MEM_4K_NUM);
    FREE_MEM(addr, item8K, MEM_8K_NUM);
    FREE_MEM(addr, item16K, MEM_16K_NUM);
    FREE_MEM(addr, item32K, MEM_32K_NUM);
    FREE_MEM(addr, item64K, MEM_64K_NUM);
    FREE_MEM(addr, item128K, MEM_128K_NUM);
    FREE_MEM(addr, item160K, MEM_160K_NUM);

    free(addr);
}

//////////////////////////////////////////////////
辅助测试
int main()
{
    void* pMemPoolHead = TNMEM_CreateMemPool(4*1024*1024);
    void* pMem1 = NULL;
    void* pMem2 = NULL;
    void* pMem3 = NULL;
    void* pMem4 = NULL;
    void* pMem5 = NULL;
    
    if (NULL == pMemPoolHead)
    {
        return -1;
    }

    pMem1 = TNMEM_GetMemFromPool(pMemPoolHead, 64);
    pMem2 = TNMEM_GetMemFromPool(pMemPoolHead, 256);

    pMem3 = TNMEM_GetMemFromPool(pMemPoolHead, 64*1024);
    pMem4 = TNMEM_GetMemFromPool(pMemPoolHead, 160*1024);

    pMem5 = TNMEM_GetMemFromPool(pMemPoolHead, 128*1024);

    TNMEM_FreeMemToPool(pMemPoolHead, pMem1);
    TNMEM_FreeMemToPool(pMemPoolHead, pMem2);
    TNMEM_FreeMemToPool(pMemPoolHead, pMem3);
    TNMEM_FreeMemToPool(pMemPoolHead, pMem4);
    TNMEM_FreeMemToPool(pMemPoolHead, pMem5);
    
    TNMEM_DestroyMemPool(pMemPoolHead);
    
    return 0;
}

 

简单内存池实现,布布扣,bubuko.com

简单内存池实现

标签:des   style   class   blog   code   tar   

原文地址:http://www.cnblogs.com/eric-geoffrey/p/3788023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!