簡單的記憶體池實現

來源:互聯網
上載者:User

標籤:

 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

簡單的記憶體池實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.