Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4Allocator << 26 // >> 27 // $Id: G4Allocator.hh,v 1.21 2010-04-01 12:43:12 gcosmo Exp $ >> 28 // GEANT4 tag $Name: geant4-09-04-patch-01 $ >> 29 // >> 30 // >> 31 // ------------------------------------------------------------ >> 32 // GEANT 4 class header file 27 // 33 // 28 // Class Description: 34 // Class Description: 29 // 35 // 30 // A class for fast allocation of objects to t 36 // A class for fast allocation of objects to the heap through a pool of 31 // chunks organised as linked list. It's meant 37 // chunks organised as linked list. It's meant to be used by associating 32 // it to the object to be allocated and defini 38 // it to the object to be allocated and defining for it new and delete 33 // operators via MallocSingle() and FreeSingle 39 // operators via MallocSingle() and FreeSingle() methods. 34 << 40 35 // ---------------- G4Allocator --------- 41 // ---------------- G4Allocator ---------------- 36 // 42 // 37 // Author: G.Cosmo (CERN), November 2000 43 // Author: G.Cosmo (CERN), November 2000 38 // ------------------------------------------- << 44 // ------------------------------------------------------------ 39 #ifndef G4Allocator_hh << 45 40 #define G4Allocator_hh 1 << 46 #ifndef G4Allocator_h >> 47 #define G4Allocator_h 1 41 48 42 #include <cstddef> 49 #include <cstddef> 43 #include <typeinfo> << 44 50 45 #include "G4AllocatorPool.hh" 51 #include "G4AllocatorPool.hh" 46 52 47 class G4AllocatorBase << 48 { << 49 public: << 50 G4AllocatorBase(); << 51 virtual ~G4AllocatorBase() = default; << 52 virtual void ResetStorage() << 53 virtual std::size_t GetAllocatedSize() const << 54 virtual int GetNoPages() const << 55 virtual std::size_t GetPageSize() const << 56 virtual void IncreasePageSize(unsigned int s << 57 virtual const char* GetPoolType() const << 58 }; << 59 << 60 template <class Type> 53 template <class Type> 61 class G4Allocator : public G4AllocatorBase << 54 class G4Allocator 62 { 55 { 63 public: << 56 public: // with description 64 G4Allocator() throw(); << 57 65 ~G4Allocator() throw() override; << 58 G4Allocator() throw(); 66 // Constructor & destructor << 59 ~G4Allocator() throw(); 67 << 60 // Constructor & destructor 68 inline Type* MallocSingle(); << 61 69 inline void FreeSingle(Type* anElement); << 62 inline Type* MallocSingle(); 70 // Malloc and Free methods to be used when o << 63 inline void FreeSingle(Type* anElement); 71 // new and delete operators in the client <T << 64 // Malloc and Free methods to be used when overloading 72 << 65 // new and delete operators in the client <Type> object 73 inline void ResetStorage() override; << 66 74 // Returns allocated storage to the free sto << 67 inline void ResetStorage(); 75 // Note: contents in memory are lost using t << 68 // Returns allocated storage to the free store, resets allocator. 76 << 69 // Note: contents in memory are lost using this call ! 77 inline std::size_t GetAllocatedSize() const << 70 78 // Returns the size of the total memory allo << 71 inline size_t GetAllocatedSize() const; 79 inline int GetNoPages() const override; << 72 // Returns the size of the total memory allocated 80 // Returns the total number of allocated pag << 73 inline int GetNoPages() const; 81 inline std::size_t GetPageSize() const overr << 74 // Returns the total number of allocated pages 82 // Returns the current size of a page << 75 inline size_t GetPageSize() const; 83 inline void IncreasePageSize(unsigned int sz << 76 // Returns the current size of a page 84 // Resets allocator and increases default pa << 77 inline void IncreasePageSize( unsigned int sz ); 85 << 78 // Resets allocator and increases default page size of a given factor 86 inline const char* GetPoolType() const overr << 79 87 // Returns the type_info Id of the allocated << 80 public: // without description 88 << 81 89 // This public section includes standard met << 82 // This public section includes standard methods and types 90 // required if the allocator is to be used a << 83 // required if the allocator is to be used as alternative 91 // allocator for STL containers. << 84 // allocator for STL containers. 92 // NOTE: the code below is a trivial impleme << 85 // NOTE: the code below is a trivial implementation to make 93 // this class an STL compliant allocat << 86 // this class an STL compliant allocator. 94 // It is anyhow NOT recommended to use << 87 // It is anyhow NOT recommended to use this class as 95 // alternative allocator for STL conta << 88 // alternative allocator for STL containers ! 96 << 89 97 using value_type = Type; << 90 typedef Type value_type; 98 using size_type = std::size_t; << 91 typedef size_t size_type; 99 using difference_type = ptrdiff_t; << 92 typedef ptrdiff_t difference_type; 100 using pointer = Type*; << 93 typedef Type* pointer; 101 using const_pointer = const Type*; << 94 typedef const Type* const_pointer; 102 using reference = Type&; << 95 typedef Type& reference; 103 using const_reference = const Type&; << 96 typedef const Type& const_reference; 104 << 97 105 template <class U> << 98 template <class U> G4Allocator(const G4Allocator<U>& right) throw() 106 G4Allocator(const G4Allocator<U>& right) thr << 99 : mem(right.mem) {} 107 : mem(right.mem) << 100 // Copy constructor 108 {} << 101 109 // Copy constructor << 102 pointer address(reference r) const { return &r; } 110 << 103 const_pointer address(const_reference r) const { return &r; } 111 pointer address(reference r) const { return << 104 // Returns the address of values 112 const_pointer address(const_reference r) con << 105 113 // Returns the address of values << 106 pointer allocate(size_type n, void* = 0) 114 << 107 { 115 pointer allocate(size_type n, void* = nullpt << 108 // Allocates space for n elements of type Type, but does not initialise 116 { << 109 // 117 // Allocates space for n elements of type << 110 Type* mem_alloc = 0; 118 // << 111 if (n == 1) 119 Type* mem_alloc = 0; << 112 mem_alloc = MallocSingle(); 120 if(n == 1) << 113 else 121 mem_alloc = MallocSingle(); << 114 mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type))); 122 else << 115 return mem_alloc; 123 mem_alloc = static_cast<Type*>(::operato << 116 } 124 return mem_alloc; << 117 void deallocate(pointer p, size_type n) 125 } << 118 { 126 void deallocate(pointer p, size_type n) << 119 // Deallocates n elements of type Type, but doesn't destroy 127 { << 120 // 128 // Deallocates n elements of type Type, bu << 121 if (n == 1) 129 // << 122 FreeSingle(p); 130 if(n == 1) << 123 else 131 FreeSingle(p); << 124 ::operator delete((void*)p); 132 else << 125 return; 133 ::operator delete((void*) p); << 126 } 134 return; << 127 135 } << 128 void construct(pointer p, const Type& val) { new((void*)p) Type(val); } 136 << 129 // Initialises *p by val 137 void construct(pointer p, const Type& val) { << 130 void destroy(pointer p) { p->~Type(); } 138 // Initialises *p by val << 131 // Destroy *p but doesn't deallocate 139 void destroy(pointer p) { p->~Type(); } << 132 140 // Destroy *p but doesn't deallocate << 133 size_type max_size() const throw() 141 << 134 { 142 size_type max_size() const throw() << 135 // Returns the maximum number of elements that can be allocated 143 { << 136 // 144 // Returns the maximum number of elements << 137 return 2147483647/sizeof(Type); 145 // << 138 } 146 return 2147483647 / sizeof(Type); << 139 147 } << 140 template <class U> 148 << 141 struct rebind { typedef G4Allocator<U> other; }; 149 template <class U> << 142 // Rebind allocator to type U 150 struct rebind << 143 151 { << 144 G4AllocatorPool mem; 152 using other = G4Allocator<U>; << 145 // Pool of elements of sizeof(Type) 153 }; << 154 // Rebind allocator to type U << 155 << 156 G4AllocatorPool mem; << 157 // Pool of elements of sizeof(Type) << 158 << 159 private: << 160 const char* tname; << 161 // Type name identifier << 162 }; 146 }; 163 147 164 // ------------------------------------------- 148 // ------------------------------------------------------------ 165 // Inline implementation 149 // Inline implementation 166 // ------------------------------------------- 150 // ------------------------------------------------------------ 167 151 168 // Initialization of the static pool 152 // Initialization of the static pool 169 // 153 // 170 // template <class Type> G4AllocatorPool G4All 154 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); 171 155 172 // ******************************************* 156 // ************************************************************ 173 // G4Allocator constructor 157 // G4Allocator constructor 174 // ******************************************* 158 // ************************************************************ 175 // 159 // 176 template <class Type> 160 template <class Type> 177 G4Allocator<Type>::G4Allocator() throw() 161 G4Allocator<Type>::G4Allocator() throw() 178 : mem(sizeof(Type)) 162 : mem(sizeof(Type)) 179 { 163 { 180 tname = typeid(Type).name(); << 181 } 164 } 182 165 183 // ******************************************* 166 // ************************************************************ 184 // G4Allocator destructor 167 // G4Allocator destructor 185 // ******************************************* 168 // ************************************************************ 186 // 169 // 187 template <class Type> 170 template <class Type> 188 G4Allocator<Type>::~G4Allocator() throw() = de << 171 G4Allocator<Type>::~G4Allocator() throw() >> 172 { >> 173 } 189 174 190 // ******************************************* 175 // ************************************************************ 191 // MallocSingle 176 // MallocSingle 192 // ******************************************* 177 // ************************************************************ 193 // 178 // 194 template <class Type> 179 template <class Type> 195 Type* G4Allocator<Type>::MallocSingle() 180 Type* G4Allocator<Type>::MallocSingle() 196 { 181 { 197 return static_cast<Type*>(mem.Alloc()); 182 return static_cast<Type*>(mem.Alloc()); 198 } 183 } 199 184 200 // ******************************************* 185 // ************************************************************ 201 // FreeSingle 186 // FreeSingle 202 // ******************************************* 187 // ************************************************************ 203 // 188 // 204 template <class Type> 189 template <class Type> 205 void G4Allocator<Type>::FreeSingle(Type* anEle 190 void G4Allocator<Type>::FreeSingle(Type* anElement) 206 { 191 { 207 mem.Free(anElement); 192 mem.Free(anElement); 208 return; 193 return; 209 } 194 } 210 195 211 // ******************************************* 196 // ************************************************************ 212 // ResetStorage 197 // ResetStorage 213 // ******************************************* 198 // ************************************************************ 214 // 199 // 215 template <class Type> 200 template <class Type> 216 void G4Allocator<Type>::ResetStorage() 201 void G4Allocator<Type>::ResetStorage() 217 { 202 { 218 // Clear all allocated storage and return it 203 // Clear all allocated storage and return it to the free store 219 // 204 // 220 mem.Reset(); 205 mem.Reset(); 221 return; 206 return; 222 } 207 } 223 208 224 // ******************************************* 209 // ************************************************************ 225 // GetAllocatedSize 210 // GetAllocatedSize 226 // ******************************************* 211 // ************************************************************ 227 // 212 // 228 template <class Type> 213 template <class Type> 229 std::size_t G4Allocator<Type>::GetAllocatedSiz << 214 size_t G4Allocator<Type>::GetAllocatedSize() const 230 { 215 { 231 return mem.Size(); 216 return mem.Size(); 232 } 217 } 233 218 234 // ******************************************* 219 // ************************************************************ 235 // GetNoPages 220 // GetNoPages 236 // ******************************************* 221 // ************************************************************ 237 // 222 // 238 template <class Type> 223 template <class Type> 239 int G4Allocator<Type>::GetNoPages() const 224 int G4Allocator<Type>::GetNoPages() const 240 { 225 { 241 return mem.GetNoPages(); 226 return mem.GetNoPages(); 242 } 227 } 243 228 244 // ******************************************* 229 // ************************************************************ 245 // GetPageSize 230 // GetPageSize 246 // ******************************************* 231 // ************************************************************ 247 // 232 // 248 template <class Type> 233 template <class Type> 249 size_t G4Allocator<Type>::GetPageSize() const 234 size_t G4Allocator<Type>::GetPageSize() const 250 { 235 { 251 return mem.GetPageSize(); 236 return mem.GetPageSize(); 252 } 237 } 253 238 254 // ******************************************* 239 // ************************************************************ 255 // IncreasePageSize 240 // IncreasePageSize 256 // ******************************************* 241 // ************************************************************ 257 // 242 // 258 template <class Type> 243 template <class Type> 259 void G4Allocator<Type>::IncreasePageSize(unsig << 244 void G4Allocator<Type>::IncreasePageSize( unsigned int sz ) 260 { 245 { 261 ResetStorage(); 246 ResetStorage(); 262 mem.GrowPageSize(sz); << 247 mem.GrowPageSize(sz); 263 } << 264 << 265 // ******************************************* << 266 // GetPoolType << 267 // ******************************************* << 268 // << 269 template <class Type> << 270 const char* G4Allocator<Type>::GetPoolType() c << 271 { << 272 return tname; << 273 } 248 } 274 249 275 // ******************************************* 250 // ************************************************************ 276 // operator== 251 // operator== 277 // ******************************************* 252 // ************************************************************ 278 // 253 // 279 template <class T1, class T2> 254 template <class T1, class T2> 280 bool operator==(const G4Allocator<T1>&, const << 255 bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 281 { 256 { 282 return true; 257 return true; 283 } 258 } 284 259 285 // ******************************************* 260 // ************************************************************ 286 // operator!= 261 // operator!= 287 // ******************************************* 262 // ************************************************************ 288 // 263 // 289 template <class T1, class T2> 264 template <class T1, class T2> 290 bool operator!=(const G4Allocator<T1>&, const << 265 bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 291 { 266 { 292 return false; 267 return false; 293 } 268 } 294 269 295 #endif 270 #endif 296 271