内存池简单实例

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <iostream>

  5. using namespace std;

  6. class MemPool
  7. {
  8.   public:
  9.      MemPool();
  10.      ~MemPool();
  11.      void* Alloc();
  12.      void Free(void* p);
  13.      
  14.   private:
  15.       const static int blockSize = 1024;
  16.       const static int itemSize = 16;
  17.       
  18.       struct ItemNode
  19.       {
  20.              ItemNode *next;
  21.              char data[itemSize - sizeof(ItemNode*)];
  22.       };
  23.       
  24.       struct BlockNode
  25.       {
  26.              BlockNode *next;
  27.              ItemNode data[blockSize/itemSize];
  28.       };
  29.       
  30.       ItemNode *freeItemNodeHead;
  31.       BlockNode *memBlockNodeHead;
  32. };

  33. MemPool::MemPool():memBlockNodeHead(NULL), freeItemNodeHead(NULL)
  34. {
  35. }

  36. MemPool::~MemPool()
  37. {    
  38.     BlockNode *temp = memBlockNodeHead;
  39.      while(temp)
  40.      {
  41.         memBlockNodeHead = memBlockNodeHead->next;
  42.         delete temp;
  43.         temp = memBlockNodeHead;
  44.     }
  45. }

  46. void* MemPool::Alloc()
  47. {
  48.       if(freeItemNodeHead == NULL)
  49.       {
  50.          BlockNode *tempBlockNode = new BlockNode;
  51.          if(tempBlockNode == NULL)
  52.          {
  53.                    perror("allocate memory error!\n");
  54.                    exit(1);
  55.          }
  56.         
  57.          if(memBlockNodeHead == NULL)
  58.          {
  59.                   memBlockNodeHead = tempBlockNode;
  60.                   memBlockNodeHead->next = NULL;
  61.          }
  62.          else
  63.          {
  64.                   tempBlockNode->next = memBlockNodeHead;
  65.                   memBlockNodeHead = tempBlockNode;
  66.          }
  67.         
  68.          freeItemNodeHead = &(memBlockNodeHead->data[0]);
  69.         
  70.          int i = 0;
  71.          while(i < blockSize/itemSize -1)
  72.          {
  73.                   tempBlockNode->data[i].next = &(tempBlockNode->data[i+1]);
  74.                   ++i;
  75.          }
  76.         
  77.          tempBlockNode->data[i].next = NULL;
  78.      }
  79.     
  80.      ItemNode *allocItemNode = freeItemNodeHead;
  81.      freeItemNodeHead = freeItemNodeHead->next;
  82.     
  83.      return allocItemNode;
  84. }

  85. void MemPool::Free(void* p)
  86. {
  87.       ItemNode *tempItemNode = static_cast<ItemNode*>(p);
  88.       tempItemNode->next = freeItemNodeHead;
  89.       freeItemNodeHead = tempItemNode;
  90. }

  91. int main()
  92. {
  93.      MemPool pool;
  94.      char *p = NULL;
  95.      
  96.      clock_t start, finish;
  97.      
  98.      double duration;
  99.      
  100.      long i = 0;
  101.      
  102.      start = clock();
  103.      
  104.      while(i < 10000000)
  105.      {
  106.              p = static_cast<char*>(pool.Alloc());
  107.              //p = new char[16];
  108.              ++i;
  109.              pool.Free(p);
  110.              //delete p;
  111.     }
  112.     
  113.     finish = clock();
  114.     
  115.     duration = (double)(finish - start)/CLOCKS_PER_SEC;
  116.     
  117.     cout << "total use " << duration << " seconds" << endl;
  118.     
  119.     system("pause");
  120.     
  121.     return 0;
  122. }

 

使用内存池的结果是:
total use 0.245 seconds
使用new,delete操作的结果是:
total use 7.256 seconds
 
结果相差近30倍,可见使用内存池对系统的性能有很大的提高
 

MemPool实现源码参见:http://blog.csdn.net/zhangzhenghe/article/details/5437487

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值