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

简单的内存池实现

时间:2015-09-02 09:28:15      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 1 #ifndef _MEMPOOL_H_
 2 #define _MEMPOOL_H_
 3 #include<iostream>
 4 template<typename T>
 5 class CMemPool{
 6 private:
 7     CMemPool<T>* m_pFreeList;
 8 public:
 9     enum{EAXPANSION = 32};
10     CMemPool(unsigned int nItemCount = EAXPANSION){
11         ExpandFreeList(nItemCount);
12     }
13     ~CMemPool(){
14         CMemPool<T>* pNext = NULL;
15         for(pNext = m_pFreeList;pNext!=NULL;pNext = m_pFreeList){
16             m_pFreeList = m_pFreeList->m_pFreeList;
17             delete[]  (char*)pNext;
18         }
19     }
20     void* Alloc(size_t){
21         if(m_pFreeList==NULL){
22             ExpandFreeList();
23         }
24         std::cout<<"ALLOC called"<<std::endl;
25         CMemPool<T>* phead = m_pFreeList;
26         m_pFreeList = m_pFreeList->m_pFreeList;
27         return phead;
28     }
29     void Free(void* p){
30         CMemPool<T>* phead = static_cast<CMemPool<T>*>(p);
31         phead->m_pFreeList = m_pFreeList;
32         m_pFreeList = phead;
33         std::cout<<"Free called"<<std::endl;
34     }
35 protected:
36     void ExpandFreeList(unsigned int nItemcount = EAXPANSION){
37         unsigned int nSize = sizeof(T)>sizeof(CMemPool<T>*)?sizeof(T):sizeof(CMemPool<T>*);
38         CMemPool<T>* plastItem = static_cast<CMemPool<T>*>(static_cast<void*> (new char[nSize]));
39         m_pFreeList = plastItem;
40         for(int i=0;i<nItemcount-1;i++){
41             plastItem->m_pFreeList = static_cast<CMemPool<T>*>(static_cast<void*>(new char[nSize]));
42             plastItem = plastItem->m_pFreeList;
43         }
44         plastItem->m_pFreeList = NULL;
45     }
46 };
47 #endif
 1 #include<iostream>
 2 #include<Windows.h>
 3 using namespace std;
 4 #include "mempool.h"
 5 
 6 
 7 class CTest{
 8 public:
 9     int m;
10     int n;
11     void* operator new(size_t size){
12         void* p = s_pool->Alloc(size);
13         return p;
14     }
15     void operator delete(void* p){
16         s_pool->Free(p);
17         return;
18     }
19     static void NewPool(){
20         s_pool = new CMemPool<CTest>;
21     }
22     static void DelPool(){
23         delete s_pool;
24         return;
25     }
26     static CMemPool<CTest>* s_pool;
27 };
28 CMemPool<CTest>* CTest::s_pool=NULL;
29 
30 void TestFun(){
31     int i;
32     const int nLoop = 10;
33     const int nCount = 10;
34    
35     for(int j = 0; j<nLoop; ++j)
36     {
37         typedef CTest* LPTest;
38         LPTest arData[nCount];
39         for(i=0;i <nCount; ++i)
40         {
41             arData[i] = new CTest;
42         }
43 
44         for(i=0;i <nCount; ++i)
45         {
46             delete arData[i];
47         }
48     }
49 }
50 
51 int main(int argc, char* argv[])
52 {
53     {
54         unsigned int dwStartTickCount = GetTickCount();
55 
56         CTest::NewPool();
57 
58         TestFun();
59        
60         CTest::DelPool();
61        
62         cout << "total cost" << GetTickCount() - dwStartTickCount << endl;
63     }
64     system("pause");
65 
66     return 0;
67 }

转:http://www.2cto.com/kf/201205/130457.html

简单的内存池实现

标签:

原文地址:http://www.cnblogs.com/zhang-wen/p/4777648.html

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