通用C++記憶體池代碼

來源:互聯網
上載者:User

原理在這裡http://blog.csdn.net/kevin_qing/article/details/608891

以前代碼找不到了,重寫一次。

加上了一些簡單的錯誤偵測代碼。協助調試記憶體問題。

初步代碼,還未仔細檢查,有bug請留言。

 

//type and macro#include <default.h>//debug 資訊constboolDebugQMem=1;structQMemDebugInfo{uint32_tmask;uint32_tsize;byteguard[128-8];};template <bool flag, typename T, typename U>struct TypeSelectT{private: template<bool> struct In  { typedef T Result; }; template<> struct In<false> { typedef U Result; };public: typedef typename In<flag>::Result Result;};//計算ln(x)/ln(y),向上轉換為整形template <uint64_t x,uint64_t y>struct logT{protected: template <uint64_t x1> struct imp{  enum {   next=(x1+y-1)/y,  };  enum {Result=1+imp<next>::Result}; }; template <>struct imp<1>{  enum {Result=0}; };public: enum {Result=imp<x>::Result};};//x^ytemplate <uint64_t x,uint64_t y>struct powerT{protected: template <uint64_t y1> struct imp{  enum{   next=y1-1  };  enum{   Result=x*imp<next>::Result  }; }; template<> struct imp<0>{  enum{   Result=1  }; };public: enum{  Result=imp<y>::Result };};//緩衝表,template <uint64_t index>struct cacheCount{ //table<>未實現0,1,2,3,所以不允許<16b的對象編譯通過,避免空間太小不能容納調試資訊。template <uint64_t>structtable;template <>struct table<4>{ enum{Result= 4096};};//16b -> 64ktemplate <>struct table<5>{ enum{Result= 4096};};//32b -> 128ktemplate <>struct table<6>{ enum{Result= 4096};};//64b -> 256ktemplate <>struct table<7>{ enum{Result= 4096};};//128b-> 512ktemplate <>struct table<8>{ enum{Result= 2048};};//256b-> 512ktemplate <>struct table<9>{ enum{Result= 2048};};//512b-> 1mtemplate <>struct table<10>{ enum{Result= 2048};};//1k  -> 2mtemplate <>struct table<11>{ enum{Result= 2048};};//2k  -> 4mtemplate <>struct table<12>{ enum{Result= 1024};};//4k  -> 4mtemplate <>struct table<13>{ enum{Result= 1024};};//8k  -> 8mtemplate <>struct table<14>{ enum{Result= 1024};};//16k -> 16m //更多的使用時添加template <>struct table<20>{ enum{Result= 4};};//1m -> 4mstructminCache{enum{Result=2};};enum{Result=TypeSelectT<index<20,table<logT<index,2>::Result>,minCache>::Result::Result };};//鏈表,用於可用空間回收structQMemLink{QMemLink*next;};classQMemTest;byte*QAlloc(size_t s){returnnew byte[s];}template <size_t size> class QFixAllocator{friendclassQMemTest;uint64_tusageBytes;uint64_tallocBytes;uint64_tpoolBytes;QMemLink*memPool;enum{sizeFix=powerT<2,logT<size,2>::Result>::Result};enum{caches=cacheCount<sizeFix>::Result};protected:QFixAllocator(){usageBytes=0;allocBytes=0;poolBytes=0;memPool=NULL;}public:void* alloc(size_t s){assert(s<=sizeFix);//避免new[]call到錯誤的allocator,host class應該使用單獨版本的new[](),而不是預設調用new()QMemLink*r;if(memPool){r=memPool;memPool=memPool->next;poolBytes-=sizeFix;usageBytes+=s;returnr;}byte*p=QAlloc(sizeFix*caches);memPool=(QMemLink*)p;for(int i=1;i<caches;++i){r=(QMemLink*)p;p+=sizeFix;r->next=(QMemLink*)p;;}r->next=NULL;poolBytes+=(caches-1)*sizeFix;usageBytes+=s;allocBytes+=caches*sizeFix;returnp;}void free(void*p,size_t s){QMemLink*l=(QMemLink*)p;l->next=memPool;memPool=l;usageBytes-=s;poolBytes+=size;}voidstatus(uint64_t &u,uint64_t &a,uint64_t& p)const{u=usageBytes;a=allocBytes;p=poolBytes;}typedef     QFixAllocator me;static me Instance;};typedefQFixAllocator<powerT<2,4>::Result>QFixAllocator_4;typedefQFixAllocator<powerT<2,5>::Result>QFixAllocator_5;typedefQFixAllocator<powerT<2,6>::Result>QFixAllocator_6;typedefQFixAllocator<powerT<2,7>::Result>QFixAllocator_7;typedefQFixAllocator<powerT<2,8>::Result>QFixAllocator_8;typedefQFixAllocator<powerT<2,9>::Result>QFixAllocator_9;typedefQFixAllocator<powerT<2,10>::Result>QFixAllocator_10;typedefQFixAllocator<powerT<2,11>::Result>QFixAllocator_11;typedefQFixAllocator<powerT<2,12>::Result>QFixAllocator_12;typedefQFixAllocator<powerT<2,13>::Result>QFixAllocator_13;typedefQFixAllocator<powerT<2,14>::Result>QFixAllocator_14;typedefQFixAllocator<powerT<2,15>::Result>QFixAllocator_15;typedefQFixAllocator<powerT<2,16>::Result>QFixAllocator_16;typedefQFixAllocator<powerT<2,17>::Result>QFixAllocator_17;typedefQFixAllocator<powerT<2,18>::Result>QFixAllocator_18;typedefQFixAllocator<powerT<2,19>::Result>QFixAllocator_19;typedefQFixAllocator<powerT<2,20>::Result>QFixAllocator_20;typedefQFixAllocator<powerT<2,21>::Result>QFixAllocator_21;QFixAllocator_10QFixAllocator_10::Instance;QFixAllocator_11QFixAllocator_11::Instance;QFixAllocator_12QFixAllocator_12::Instance;QFixAllocator_13QFixAllocator_13::Instance;QFixAllocator_14QFixAllocator_14::Instance;QFixAllocator_15QFixAllocator_15::Instance;QFixAllocator_16QFixAllocator_16::Instance;QFixAllocator_17QFixAllocator_17::Instance;QFixAllocator_18QFixAllocator_18::Instance;QFixAllocator_19QFixAllocator_19::Instance;QFixAllocator_20QFixAllocator_20::Instance;QFixAllocator_21QFixAllocator_21::Instance;template<class Host>classQMemImpl{public:void*operatornew(size_t size){returnQFixAllocator<powerT<2,logT<sizeof(Host),2>::Result>::Result>::Instance.alloc(size);}voidoperatordelete(void*p){returnQFixAllocator<powerT<2,logT<sizeof(Host),2>::Result>::Result>::Instance.free(p,sizeof(Host));}void*operatornew[](size_t size){size+=8;//x64 8位元組對齊ULONGr,f;_BitScanReverse(&r,size);_BitScanReverse(&f,size);if(r!=f)//對齊++r;switch(r){case19://這裡只處理了1m位元組的分配{uint32_t *p=(uint32_t*)QFixAllocator_20::Instance.alloc(size);*p=19;//set mask++p;*p=size;return++p;}default:DebugBreak();}throw"bad alloc";}voidoperatordelete[](void*p){uint32_t*p32=(uint32_t*)p;p32-=2;switch(*p32){case19://這裡只處理了1m位元組的釋放QFixAllocator_20::Instance.free(p32,*(p32+1));return;default:DebugBreak();}}};template<class Host>classQMemDebugImpl{public:void*operatornew(size_t size){QMemDebugInfo*p=(QMemDebugInfo*)QFixAllocator<powerT<2,logT<sizeof(Host)+sizeof(QMemDebugInfo),2>::Result>::Result>::Instance.alloc(size);memset(p->guard,0,sizeof(p->guard));p->mask=logT<sizeof(Host)+sizeof(QMemDebugInfo),2>::Result;p->mask|=0x12345600;p->size=size;return++p;}voidoperatordelete(void*p){QMemDebugInfo*pDebug=(QMemDebugInfo*)p;--pDebug;if((pDebug->mask&(~0xff))!=0x12345600){ //標誌錯,可能是亂用delete,new[]DebugBreak();}if(pDebug->size!=sizeof(Host)){//size錯DebugBreak();}//其他檢查returnQFixAllocator<powerT<2,logT<sizeof(Host)+sizeof(QMemDebugInfo),2>::Result>::Result>::Instance.free(pDebug,sizeof(Host));}void*operatornew[](size_t size){size+=sizeof(QMemDebugInfo);ULONGr,f;if(!_BitScanReverse(&r,size))DebugBreak();//size 0,不可能的情況_BitScanReverse(&f,size);if(r!=f)//對齊++r;QMemDebugInfo *p;switch(r){case19:p=(QMemDebugInfo*)QFixAllocator_20::Instance.alloc(size);p->mask=19;break;case20:p=(QMemDebugInfo*)QFixAllocator_21::Instance.alloc(size);p->mask=20;break;default:DebugBreak();}memset(p->guard,0,sizeof(p->guard));p->size=size;return++p;}voidoperatordelete[](void*p){QMemDebugInfo *pDebug=(QMemDebugInfo*)p;--pDebug;if(pDebug->mask&(~0xff)){ //標誌錯,可能是亂用delete[],newDebugBreak();}if(pDebug->size<sizeof(Host)){//size錯DebugBreak();}ULONGr,f;_BitScanReverse(&r,pDebug->size);_BitScanReverse(&f,pDebug->size);if(r!=f)++r;if(pDebug->mask!=r)DebugBreak();switch(pDebug->mask){case19:QFixAllocator_20::Instance.free(pDebug,pDebug->size);return;case20:QFixAllocator_21::Instance.free(pDebug,pDebug->size);return;default:DebugBreak();}}};template<class Host>classQMemPool:public TypeSelectT<DebugQMem,QMemDebugImpl<Host>,QMemImpl<Host>>::Result{};classQMemTest:publicQMemPool<QMemTest>{chartmp[1000];};voidshowMemInfo(){uint64_tu,a,p;QFixAllocator_11::Instance.status(u,a,p);printf("_11 u=%I64u a=%I64u p=%I64u \n",u,a,p);QFixAllocator_20::Instance.status(u,a,p);printf("_20 u=%I64u a=%I64u p=%I64u \n",u,a,p);}void main(){QMemTest*pTest[4096];showMemInfo();for(int i=0;i<countof(pTest);++i){pTest[i]=newQMemTest;}printf("new\n");showMemInfo();for(int i=0;i<countof(pTest);++i){deletepTest[i];}printf("delete\n");showMemInfo();deletenewQMemTest[1024];printf("delete[] new[]\n");showMemInfo();}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.