block-allocator.hpp /usr/include/gecode/support.hh Gecode::Support::BlockAllocator Gecode::Support::BlockAllocator::Block Gecode::Support::BlockClient Gecode Gecode::Support /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> * *Copyright: *ChristianSchulte,2004 * *Lastmodified: *$Date:2010-07-2816:13:53+0200(Wed,28Jul2010)$by$Author:schulte$ *$Revision:11292$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ namespaceGecode{namespaceSupport{ template<classT,classA,intblocksize=512> classBlockAllocator{ private: A&a; classBlock{ public: Tb[blocksize]; Block*next; }; Block*b; T*n; size_t_size; voidallocate(void); public: BlockAllocator(A&a); ~BlockAllocator(void); A&allocator(void); T*operator ()(void); size_tsize(void)const; }; template<classT,classA,intblocksize=512> classBlockClient{ public: staticvoid*operatornew(size_ts,BlockAllocator<T,A,blocksize>&ba); staticvoidoperatordelete(void*,BlockAllocator<T,A,blocksize>&ba); staticvoidoperatordelete(void*); }; template<classT,classA,intblocksize> forceinline BlockAllocator<T,A,blocksize>::BlockAllocator(A&a0):a(a0){ b=static_cast<Block*>(a.ralloc(sizeof(Block))); b->next=NULL; n=&b->b[blocksize]; _size=sizeof(Block); } template<classT,classA,intblocksize> forceinline BlockAllocator<T,A,blocksize>::~BlockAllocator(void){ while(b!=NULL){ Block*f=b;b=b->next; a.rfree(f,sizeof(Block)); } } template<classT,classA,intblocksize> forceinlineA& BlockAllocator<T,A,blocksize>::allocator(void){ returna; } template<classT,classA,intblocksize> forceinlineT* BlockAllocator<T,A,blocksize>::operator ()(void){ T*t=--n; if(t==&b->b[0]) allocate(); returnt; } template<classT,classA,intblocksize> void BlockAllocator<T,A,blocksize>::allocate(void){ //Allocateanotherblock Block*nb=static_cast<Block*>(a.ralloc(sizeof(Block))); nb->next=b;b=nb; n=&nb->b[blocksize]; _size+=sizeof(Block); } template<classT,classA,intblocksize> forceinlinesize_t BlockAllocator<T,A,blocksize>::size(void)const{ return_size; } template<classT,classA,intblocksize> forceinlinevoid BlockClient<T,A,blocksize>::operator delete(void*,BlockAllocator<T,A,blocksize>&){ } template<classT,classA,intblocksize> forceinlinevoid BlockClient<T,A,blocksize>::operatordelete(void*){ } template<classT,classA,intblocksize> forceinlinevoid* BlockClient<T,A,blocksize>::operatornew(size_t, BlockAllocator<T,A,blocksize>&ba){ returnba(); } }} //STATISTICS:support-any