Department of Scientific Computing   
Institute for Numerical Simulation   
University of Bonn   
Documentation
Download
Programming References
Bug Reports / Suggestions
FAQ
Authors
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

BlockAllocator.hpp

00001 #ifndef _BLOCK_ALLOCATOR_
00002 # define _BLOCK_ALLOCATOR_
00003 
00004 # include <list>
00005 
00011 
00012 template<class I>
00013 class BlockAllocator {
00014 public:
00016                BlockAllocator() {;}
00018               ~BlockAllocator()  ;
00021   I*    NewItem() ;
00023   void  DeleteItem(I* p) ;
00024 
00025   list<I*> List   ;
00026   list<I*> Blocks ;
00027 
00028   enum { BLOCKSIZE=1000 } ;
00029 } ;
00030  
00031 //
00032 // Implementations
00033 //
00034 template<class I>
00035 BlockAllocator<I>::~BlockAllocator() {
00036   I *p ;
00037   while (!Blocks.empty()) {
00038     p=Blocks.front() ;
00039     for (int i=0; i<BLOCKSIZE; i++) p[i].Delete() ;
00040     delete []p ;
00041     Blocks.pop_front() ;
00042   }
00043 }
00044 
00045 template<class I>
00046 I* BlockAllocator<I>::NewItem() {
00047   I *p ;
00048 
00049   if (List.empty()) {
00050     p=new I[BLOCKSIZE];
00051     Blocks.push_front(p) ;
00052     for (int i=0; i<BLOCKSIZE; i++) {
00053       p[i].Init() ;
00054       List.push_front(&p[i]) ;
00055     }
00056   }
00057   
00058   // return an init. instance
00059   p=List.front() ;
00060   List.pop_front() ;
00061   return p ;
00062 }
00063 
00064 template<class I>
00065 void BlockAllocator<I>::DeleteItem(I *p) {
00066   p->Clear() ;
00067   List.push_front(p) ;
00068 }
00069 
00070 #endif

Generated at Mon Aug 19 10:02:31 2002 for AWFD by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001