00001 // $Id: AllocatorPool.h,v 1.3 2007/05/24 14:39:11 hmd Exp $ 00002 // ============================================================================ 00003 // CVS tag $Name: GAUDI_v20r4-pre $, version $Revision: 1.3 $ 00004 // ============================================================================ 00010 // ============================================================================ 00011 00012 // 00013 // ******************************************************************** 00014 // * DISCLAIMER * 00015 // * * 00016 // * The following disclaimer summarizes all the specific disclaimers * 00017 // * of contributors to this software. The specific disclaimers,which * 00018 // * govern, are listed with their locations in: * 00019 // * http://cern.ch/geant4/license * 00020 // * * 00021 // * Neither the authors of this software system, nor their employing * 00022 // * institutes,nor the agencies providing financial support for this * 00023 // * work make any representation or warranty, express or implied, * 00024 // * regarding this software system or assume any liability for its * 00025 // * use. * 00026 // * * 00027 // * This code implementation is the intellectual property of the * 00028 // * GEANT4 collaboration. * 00029 // * By copying, distributing or modifying the Program (or any work * 00030 // * based on the Program) you indicate your acceptance of this * 00031 // * statement, and all its terms. * 00032 // ******************************************************************** 00033 // 00034 // ------------------------------------------------------------------- 00035 // GEANT 4 class header file 00036 // 00037 // Class description: 00038 // 00039 // Class implementing a memory pool for fast allocation and deallocation 00040 // of memory chunks. The size of the chunks for small allocated objects 00041 // is fixed to 1Kb and takes into account of memory alignment; for large 00042 // objects it is set to 10 times the object's size. 00043 // The implementation is derived from: B.Stroustrup, The C++ Programming 00044 // Language, Third Edition. 00045 00046 // -------------- G4AllocatorPool ---------------- 00047 // 00048 // Author: G.Cosmo (CERN), November 2000 00049 // ------------------------------------------------------------------- 00050 00051 #ifndef GAUDIKERNEL_AllocatorPool_h 00052 #define GAUDIKERNEL_AllocatorPool_h 1 00053 00054 namespace GaudiUtils 00055 { 00063 class AllocatorPool 00064 { 00065 public: 00066 00068 explicit AllocatorPool( unsigned int n=0 ); 00070 ~AllocatorPool(); 00072 AllocatorPool(const AllocatorPool& right); 00074 inline void* Alloc(); 00076 inline void Free( void* b ); 00078 inline unsigned int Size() const; 00080 void Reset(); 00081 00082 private: 00083 00085 AllocatorPool& operator= (const AllocatorPool& right); 00086 00087 private: 00088 00089 struct PoolLink 00090 { 00091 PoolLink* next; 00092 }; 00093 class PoolChunk 00094 { 00095 public: 00096 explicit PoolChunk(unsigned int sz) 00097 : size(sz), mem(new char[size]), next(0) {;} 00098 ~PoolChunk() { delete [] mem; } 00099 const unsigned int size; 00100 char* mem; 00101 PoolChunk* next; 00102 }; 00103 00105 void Grow(); 00106 00107 private: 00108 00109 const unsigned int esize; 00110 const unsigned int csize; 00111 PoolChunk* chunks; 00112 PoolLink* head; 00113 int nchunks; 00114 }; 00115 00116 } // end of namespace GaudiUtils 00117 00118 00119 00120 // ************************************************************ 00121 // Alloc 00122 // ************************************************************ 00123 // 00124 inline void* 00125 GaudiUtils::AllocatorPool::Alloc() 00126 { 00127 if ( head==0 ) { Grow(); } 00128 PoolLink* p = head; // return first element 00129 head = p->next; 00130 return p; 00131 } 00132 00133 // ************************************************************ 00134 // Free 00135 // ************************************************************ 00136 // 00137 inline void 00138 GaudiUtils::AllocatorPool::Free( void* b ) 00139 { 00140 PoolLink* p = static_cast<PoolLink*>(b); 00141 p->next = head; // put b back as first element 00142 head = p; 00143 } 00144 00145 // ************************************************************ 00146 // Size 00147 // ************************************************************ 00148 // 00149 inline unsigned int 00150 GaudiUtils::AllocatorPool::Size() const 00151 { 00152 return nchunks*csize; 00153 } 00154 00155 00156 // ============================================================================ 00157 // The END 00158 // ============================================================================ 00159 #endif 00160 // ============================================================================ 00161