00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // 00027 // $Id$ 00028 // 00029 // 00030 // ------------------------------------------------------------------- 00031 // GEANT 4 class header file 00032 // 00033 // Class description: 00034 // 00035 // Class implementing a memory pool for fast allocation and deallocation 00036 // of memory chunks. The size of the chunks for small allocated objects 00037 // is fixed to 1Kb and takes into account of memory alignment; for large 00038 // objects it is set to 10 times the object's size. 00039 // The implementation is derived from: B.Stroustrup, The C++ Programming 00040 // Language, Third Edition. 00041 00042 // -------------- G4AllocatorPool ---------------- 00043 // 00044 // Author: G.Cosmo (CERN), November 2000 00045 // ------------------------------------------------------------------- 00046 00047 #ifndef G4AllocatorPool_h 00048 #define G4AllocatorPool_h 1 00049 00050 class G4AllocatorPool 00051 { 00052 public: 00053 00054 explicit G4AllocatorPool( unsigned int n=0 ); 00055 // Create a pool of elements of size n 00056 ~G4AllocatorPool(); 00057 // Destructor. Return storage to the free store 00058 00059 inline void* Alloc(); 00060 // Allocate one element 00061 inline void Free( void* b ); 00062 // Return an element back to the pool 00063 00064 inline unsigned int Size() const; 00065 // Return storage size 00066 void Reset(); 00067 // Return storage to the free store 00068 00069 inline int GetNoPages() const; 00070 // Return the total number of allocated pages 00071 inline unsigned int GetPageSize() const; 00072 // Accessor for default page size 00073 inline void GrowPageSize( unsigned int factor ); 00074 // Increase default page size by a given factor 00075 00076 private: 00077 00078 G4AllocatorPool(const G4AllocatorPool& right); 00079 // Provate copy constructor 00080 G4AllocatorPool& operator= (const G4AllocatorPool& right); 00081 // Private equality operator 00082 00083 struct G4PoolLink 00084 { 00085 G4PoolLink* next; 00086 }; 00087 class G4PoolChunk 00088 { 00089 public: 00090 explicit G4PoolChunk(unsigned int sz) 00091 : size(sz), mem(new char[size]), next(0) {;} 00092 ~G4PoolChunk() { delete [] mem; } 00093 const unsigned int size; 00094 char* mem; 00095 G4PoolChunk* next; 00096 }; 00097 00098 void Grow(); 00099 // Make pool larger 00100 00101 private: 00102 00103 const unsigned int esize; 00104 unsigned int csize; 00105 G4PoolChunk* chunks; 00106 G4PoolLink* head; 00107 int nchunks; 00108 }; 00109 00110 // ------------------------------------------------------------ 00111 // Inline implementation 00112 // ------------------------------------------------------------ 00113 00114 // ************************************************************ 00115 // Alloc 00116 // ************************************************************ 00117 // 00118 inline void* 00119 G4AllocatorPool::Alloc() 00120 { 00121 if (head==0) { Grow(); } 00122 G4PoolLink* p = head; // return first element 00123 head = p->next; 00124 return p; 00125 } 00126 00127 // ************************************************************ 00128 // Free 00129 // ************************************************************ 00130 // 00131 inline void 00132 G4AllocatorPool::Free( void* b ) 00133 { 00134 G4PoolLink* p = static_cast<G4PoolLink*>(b); 00135 p->next = head; // put b back as first element 00136 head = p; 00137 } 00138 00139 // ************************************************************ 00140 // Size 00141 // ************************************************************ 00142 // 00143 inline unsigned int 00144 G4AllocatorPool::Size() const 00145 { 00146 return nchunks*csize; 00147 } 00148 00149 // ************************************************************ 00150 // GetNoPages 00151 // ************************************************************ 00152 // 00153 inline int 00154 G4AllocatorPool::GetNoPages() const 00155 { 00156 return nchunks; 00157 } 00158 00159 // ************************************************************ 00160 // GetPageSize 00161 // ************************************************************ 00162 // 00163 inline unsigned int 00164 G4AllocatorPool::GetPageSize() const 00165 { 00166 return csize; 00167 } 00168 00169 // ************************************************************ 00170 // GrowPageSize 00171 // ************************************************************ 00172 // 00173 inline void 00174 G4AllocatorPool::GrowPageSize( unsigned int sz ) 00175 { 00176 csize = (sz) ? sz*csize : csize; 00177 } 00178 00179 #endif