欢迎来到天天文库
浏览记录
ID:44874250
大小:237.51 KB
页数:12页
时间:2019-11-01
《链表式内存池》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、IntroductionWhatismemorypool?IthinkImustanswerthequestionfirstly,becauseitisimportanttobeginner.Inshort,memorypoolisamemoryblockwhichyougotfromsystemandusesomeunitofittoreplacethesystemcallmalloc/freeandnew/delete.Theadvantageofthetechnologyisreuseexistingmemoryblocksothatreducethetimesofsystemcall.
2、It`sahardworktogivethedefinition.Ifyoustillcan`tunderstandtheconcept,pleasegoogleit.BackgroundWhytousememorypool?Ihavetworeason:[1]Toshortenthetimethatprogramallocatememory.NowIshowanexampletoprovethetechnologyhavegoodefficiency:Collapse#include#includeclassCTestClass{charm_ch
3、Buf[4096];};intmain(){DWORDcount=GetTickCount();for(unsignedinti=0;i<0x5fffff;i++){CTestClass*p=newCTestClass;deletep;}cout<<"Interval="<4、lowing:Collapse#include#includecharbuf[4100];//SimpleMemoryPoolclassCTestClass{charm_chBuf[4096];public:void*operatornew(unsignedintuiSize){return(void*)buf;}voidoperatordelete(void*p){}};intmain(){DWORDcount=GetTickCount();for(unsignedinti=0;i<0x5fffff;i++){CTestClass*p=newCT5、estClass;//Didn`tusesystemnewoperator!deletep;}cout<<"Interval="<6、e!Anyway,wecanknowthevalueofmemorypoolfrompreviouscode![2]Toavoidmemory`sfragment.It`sregretting,Ihaven`tsimpleandgoodexampletoprovetheconclusion.Ireachedtheconclusionfromsomebookandtheory!Notethatmemorypooljustisvaluablewhenprogramusememorywithsystemcallmalloc/freeornew/deletefrequently.Otherwise,y7、ouneedn`tuseit.ImplememtationNow,wepayattentiontohowimplementmemorypool.Ibelievememorypoolincludethreeelementatleast.[1]Weneedabigmemoryblock.Wheredowegetit?OScanprovidememoryblockforusfromstack,heap,
4、lowing:Collapse#include#includecharbuf[4100];//SimpleMemoryPoolclassCTestClass{charm_chBuf[4096];public:void*operatornew(unsignedintuiSize){return(void*)buf;}voidoperatordelete(void*p){}};intmain(){DWORDcount=GetTickCount();for(unsignedinti=0;i<0x5fffff;i++){CTestClass*p=newCT
5、estClass;//Didn`tusesystemnewoperator!deletep;}cout<<"Interval="<6、e!Anyway,wecanknowthevalueofmemorypoolfrompreviouscode![2]Toavoidmemory`sfragment.It`sregretting,Ihaven`tsimpleandgoodexampletoprovetheconclusion.Ireachedtheconclusionfromsomebookandtheory!Notethatmemorypooljustisvaluablewhenprogramusememorywithsystemcallmalloc/freeornew/deletefrequently.Otherwise,y7、ouneedn`tuseit.ImplememtationNow,wepayattentiontohowimplementmemorypool.Ibelievememorypoolincludethreeelementatleast.[1]Weneedabigmemoryblock.Wheredowegetit?OScanprovidememoryblockforusfromstack,heap,
6、e!Anyway,wecanknowthevalueofmemorypoolfrompreviouscode![2]Toavoidmemory`sfragment.It`sregretting,Ihaven`tsimpleandgoodexampletoprovetheconclusion.Ireachedtheconclusionfromsomebookandtheory!Notethatmemorypooljustisvaluablewhenprogramusememorywithsystemcallmalloc/freeornew/deletefrequently.Otherwise,y
7、ouneedn`tuseit.ImplememtationNow,wepayattentiontohowimplementmemorypool.Ibelievememorypoolincludethreeelementatleast.[1]Weneedabigmemoryblock.Wheredowegetit?OScanprovidememoryblockforusfromstack,heap,
此文档下载收益归作者所有