首页 > 其他 > 详细

用模版实现简单的内存池

时间:2015-11-09 18:25:23      阅读:277      评论:0      收藏:0      [点我收藏+]

程序中有时候会遇到这种情况,就是需要不停的去分配以及释放内存。带来的是不停的调用new以及delete带来的开销。

而且由于全局的new以及delete往往对多线程做出了并发保护,所以在单线程情况下这更带来一种浪费,一般的情况下是去实现一个

单线程的内存池来进行性能优化,配合所需的类往往带来很好的性能提升。

首先是作为内存池的模板类:

 1 template<class T>
 2 class MemoryPool{
 3 public:
 4     MemoryPool(size_t size = EXPANSION_SIZE);
 5     ~MemoryPool();
 6 
 7     //从空闲列表中分配T大小的空间
 8     inline void * alloc(size_t size);
 9 
10     //释放内存到空闲列表中
11     inline void free(void * elements);
12 private:
13     MemoryPool<T> * next;   //空闲列表的下一个元素
14     enum { EXPANSION_SIZE = 32 };
15     void expandTheFreeList(int howMany = EXPANSION_SIZE);
16 };
17 
18 template<class T>
19 MemoryPool<T>::MemoryPool(size_t size)
20 {
21     expandTheFreeList(size);
22 }
23 
24 template<class T>
25 MemoryPool<T>::~MemoryPool()
26 {
27     MemoryPool<T> * nextPtr = next;
28     for(nextPtr = next; nextPtr != NULL; nextPtr = next){
29         next = next->next;
30         delete [] nextPtr;
31     }
32 }
33 
34 
35 template<class T>
36 inline
37 void * MemoryPool<T>::alloc(size_t)
38 {
39     if(!next)
40         expandTheFreeList();
41     MemoryPool<T> * head = next;
42     next = head->next;//分配了一块空间
43     return head;
44 }
45 
46 template<class T>
47 inline
48 void MemoryPool<T>::free(void * doomed)
49 {
50     MemoryPool<T> * head = static_cast<MemoryPool<T> *>(doomed);
51     head->next = next;
52     next = head;
53 }
54 
55 template<class T>
56 void MemoryPool<T>::expandTheFreeList(int howMany)
57 {
58     //保证分配的内存大小至少应该是大于指针大小或者是元素大小中的 最大者,因为二者之间是共享内存的
59     size_t sizeAlloc = (sizeof(T) > sizeof(MemoryPool<T> * ))?
60         sizeof(T) : sizeof(MemoryPool<T> * );
61     MemoryPool<T> * runner = (MemoryPool<T>*)(new char [sizeAlloc]); //这里实际上是可以使用static_cast的,但是不知道为什么,
62     next = runner;                                              //gcc下编译不能通过。可能因为gcc的限制比较严格。无奈,只能使用普通的强制类型转换
63     for(int i = 0; i < howMany; ++i){
64         runner->next = (MemoryPool<T>*)(new char [sizeAlloc]);
65         runner = runner->next;
66     }
67     runner->next = 0;
68 }

下面是使用该内存池的一个rational类,代码如下:

 1 class Rational{
 2 public:
 3     Rational(int a = 0, int b = 1):n(a), d(b){}
 4     void * operator new(size_t size){return memPool->alloc(size); }
 5     void operator delete(void * doomed, size_t size){memPool->free(doomed);}
 6     static void newMemPool(){memPool = new MemoryPool<Rational>; }
 7     static void deleteMemPool()
 8     {
 9         if(!memPool)
10             return;
11         delete memPool;
12     }
13 private:
14     int n;
15     int d;
16     static MemoryPool <Rational> * memPool;
17 };

使用代码如下:

 1 MemoryPool<Rational> * Rational::memPool = 0;
 2 
 3 int main()
 4 {
 5     Rational * array[10];
 6     Rational::newMemPool();
 7     for(int j = 0; j < 2; ++j){
 8         for(int i = 0; i < 10; i++){
 9             array[i] = new Rational(i);
10         }
11         for(int i = 0; i < 10; i++){
12             delete array[i];
13         }
14     }
15     Rational::deleteMemPool();
16 }

 

用模版实现简单的内存池

原文:http://www.cnblogs.com/-wang-cheng/p/4950452.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!