|
Gaudi Framework, version v22r2 |
| Home | Generated: Tue May 10 2011 |
00001 // $Id: AllocatorPool.h,v 1.3 2007/05/24 14:39:11 hmd Exp $ 00002 // ============================================================================ 00003 // CVS tag $Name: $, 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 #include "GaudiKernel/Kernel.h" 00055 00056 namespace GaudiUtils 00057 { 00065 class GAUDI_API AllocatorPool 00066 { 00067 public: 00068 00070 explicit AllocatorPool( unsigned int n=0 ); 00072 ~AllocatorPool(); 00074 AllocatorPool(const AllocatorPool& right); 00076 inline void* Alloc(); 00078 inline void Free( void* b ); 00080 inline unsigned int Size() const; 00082 void Reset(); 00083 00084 private: 00085 00087 AllocatorPool& operator= (const AllocatorPool& right); 00088 00089 private: 00090 00091 struct PoolLink 00092 { 00093 PoolLink* next; 00094 }; 00095 class PoolChunk 00096 { 00097 public: 00098 explicit PoolChunk(unsigned int sz) 00099 : size(sz), mem(new char[size]), next(0) {;} 00100 ~PoolChunk() { delete [] mem; } 00101 const unsigned int size; 00102 char* mem; 00103 PoolChunk* next; 00104 }; 00105 00107 void Grow(); 00108 00109 private: 00110 00111 const unsigned int esize; 00112 const unsigned int csize; 00113 PoolChunk* chunks; 00114 PoolLink* head; 00115 int nchunks; 00116 }; 00117 00118 } // end of namespace GaudiUtils 00119 00120 00121 00122 // ************************************************************ 00123 // Alloc 00124 // ************************************************************ 00125 // 00126 inline void* 00127 GaudiUtils::AllocatorPool::Alloc() 00128 { 00129 if ( head==0 ) { Grow(); } 00130 PoolLink* p = head; // return first element 00131 head = p->next; 00132 return p; 00133 } 00134 00135 // ************************************************************ 00136 // Free 00137 // ************************************************************ 00138 // 00139 inline void 00140 GaudiUtils::AllocatorPool::Free( void* b ) 00141 { 00142 PoolLink* p = static_cast<PoolLink*>(b); 00143 p->next = head; // put b back as first element 00144 head = p; 00145 } 00146 00147 // ************************************************************ 00148 // Size 00149 // ************************************************************ 00150 // 00151 inline unsigned int 00152 GaudiUtils::AllocatorPool::Size() const 00153 { 00154 return nchunks*csize; 00155 } 00156 00157 00158 // ============================================================================ 00159 // The END 00160 // ============================================================================ 00161 #endif 00162 // ============================================================================ 00163