00001 // $Id: Allocator.h,v 1.1 2006/02/14 15:01:12 hmd Exp $ 00002 // ============================================================================ 00003 // CVS tag $Name: GAUDI_v20r4-pre $, version $Revision: 1.1 $ 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 // ------------------------------------------------------------ 00036 // GEANT 4 class header file 00037 // 00038 // Class Description: 00039 // 00040 // A class for fast allocation of objects to the heap through a pool of 00041 // chunks organised as linked list. It's meant to be used by associating 00042 // it to the object to be allocated and defining for it new and delete 00043 // operators via MallocSingle() and FreeSingle() methods. 00044 00045 // ---------------- G4Allocator ---------------- 00046 // 00047 // Author: G.Cosmo (CERN), November 2000 00048 // ------------------------------------------------------------ 00049 00050 // ============================================================================ 00051 #ifndef GAUDIKERNEL_Allocator_h 00052 #define GAUDIKERNEL_Allocator_h 1 00053 // ============================================================================ 00054 // Incldue files 00055 // ============================================================================ 00056 // STD & STL 00057 // ============================================================================ 00058 #include <cstddef> 00059 // ============================================================================ 00060 // GaudiKernel 00061 // ============================================================================ 00062 #include "GaudiKernel/AllocatorPool.h" 00063 // ============================================================================ 00064 00065 00066 namespace GaudiUtils 00067 { 00073 template <class Type> 00074 class Allocator 00075 { 00076 public: // with description 00077 00079 Allocator() throw(); 00081 ~Allocator() throw(); 00082 00086 inline Type* MallocSingle(); 00087 inline void FreeSingle(Type* anElement); 00088 00093 inline void ResetStorage(); 00094 00096 inline size_t GetAllocatedSize() const; 00097 00098 public: // without description 00099 00100 // This public section includes standard methods and types 00101 // required if the allocator is to be used as alternative 00102 // allocator for STL containers. 00103 // NOTE: the code below is a trivial implementation to make 00104 // this class an STL compliant allocator. 00105 // It is anyhow NOT recommended to use this class as 00106 // alternative allocator for STL containers ! 00107 00108 typedef Type value_type; 00109 typedef size_t size_type; 00110 typedef ptrdiff_t difference_type; 00111 typedef Type* pointer; 00112 typedef const Type* const_pointer; 00113 typedef Type& reference; 00114 typedef const Type& const_reference; 00115 00117 template <class U> Allocator 00118 (const Allocator<U>& right) throw() 00119 : mem(right.mem) {} 00120 00122 pointer address(reference r) const { return &r; } 00123 const_pointer address(const_reference r) const { return &r; } 00124 00126 pointer allocate(size_type n, void* hint = 0) 00127 { 00128 // Allocates space for n elements of type Type, but does not initialise 00129 // 00130 Type* mem_alloc = 0; 00131 if (n == 1) 00132 mem_alloc = MallocSingle(); 00133 else 00134 mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type))); 00135 return mem_alloc; 00136 } 00137 00139 void deallocate(pointer p, size_type n) 00140 { 00141 // Deallocates n elements of type Type, but doesn't destroy 00142 // 00143 if (n == 1) 00144 FreeSingle(p); 00145 else 00146 ::operator delete((void*)p); 00147 return; 00148 } 00149 00151 void construct(pointer p, const Type& val) { new((void*)p) Type(val); } 00153 void destroy(pointer p) { p->~Type(); } 00154 00156 size_type max_size() const throw() 00157 { 00158 // Returns the maximum number of elements that can be allocated 00159 // 00160 return 2147483647/sizeof(Type); 00161 } 00162 00163 // Rebind allocator to type U 00164 template <class U> 00165 struct rebind { typedef Allocator<U> other; }; 00166 00167 private: 00168 00169 GaudiUtils::AllocatorPool mem; 00170 // Pool of elements of sizeof(Type) 00171 }; 00172 00173 } ; // end of the namespace GaudiUtils 00174 00175 // ------------------------------------------------------------ 00176 // Inline implementation 00177 // ------------------------------------------------------------ 00178 00179 // Initialization of the static pool 00180 // 00181 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); 00182 00183 // ************************************************************ 00184 // G4Allocator constructor 00185 // ************************************************************ 00186 // 00187 template <class Type> 00188 GaudiUtils::Allocator<Type>::Allocator() throw() 00189 : mem(sizeof(Type)) 00190 { 00191 } 00192 00193 // ************************************************************ 00194 // G4Allocator destructor 00195 // ************************************************************ 00196 // 00197 template <class Type> 00198 GaudiUtils::Allocator<Type>::~Allocator() throw() 00199 { 00200 } 00201 00202 // ************************************************************ 00203 // MallocSingle 00204 // ************************************************************ 00205 // 00206 template <class Type> 00207 Type* GaudiUtils::Allocator<Type>::MallocSingle() 00208 { 00209 return static_cast<Type*>(mem.Alloc()); 00210 } 00211 00212 // ************************************************************ 00213 // FreeSingle 00214 // ************************************************************ 00215 // 00216 template <class Type> 00217 void GaudiUtils::Allocator<Type>::FreeSingle(Type* anElement) 00218 { 00219 mem.Free(anElement); 00220 return; 00221 } 00222 00223 // ************************************************************ 00224 // ResetStorage 00225 // ************************************************************ 00226 // 00227 template <class Type> 00228 void GaudiUtils::Allocator<Type>::ResetStorage() 00229 { 00230 // Clear all allocated storage and return it to the free store 00231 // 00232 mem.Reset(); 00233 return; 00234 } 00235 00236 // ************************************************************ 00237 // GetAllocatedSize 00238 // ************************************************************ 00239 // 00240 template <class Type> 00241 size_t GaudiUtils::Allocator<Type>::GetAllocatedSize() const 00242 { 00243 return mem.Size(); 00244 } 00245 00246 // ************************************************************ 00247 // operator== 00248 // ************************************************************ 00249 // 00250 template <class T1, class T2> 00251 bool operator== 00252 (const GaudiUtils::Allocator<T1>&, 00253 const GaudiUtils::Allocator<T2>&) throw() 00254 { 00255 return true; 00256 } 00257 00258 // ************************************************************ 00259 // operator!= 00260 // ************************************************************ 00261 // 00262 template <class T1, class T2> 00263 bool operator!= 00264 (const GaudiUtils::Allocator<T1>&, 00265 const GaudiUtils::Allocator<T2>&) throw() 00266 { 00267 return false; 00268 } 00269 00270 // ============================================================================ 00271 // The END 00272 // ============================================================================ 00273 #endif 00274 // ============================================================================ 00275