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.18 2006/06/29 19:01:16 gunter Exp $ >> 28 // GEANT4 tag $Name: geant4-08-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 76 << 69 // allocator and page sizes. 77 inline std::size_t GetAllocatedSize() const << 70 // Note: contents in memory are lost using this call ! 78 // Returns the size of the total memory allo << 71 79 inline int GetNoPages() const override; << 72 inline size_t GetAllocatedSize() const; 80 // Returns the total number of allocated pag << 73 // Returns the size of the total memory allocated 81 inline std::size_t GetPageSize() const overr << 74 82 // Returns the current size of a page << 75 public: // without description 83 inline void IncreasePageSize(unsigned int sz << 76 84 // Resets allocator and increases default pa << 77 // This public section includes standard methods and types 85 << 78 // required if the allocator is to be used as alternative 86 inline const char* GetPoolType() const overr << 79 // allocator for STL containers. 87 // Returns the type_info Id of the allocated << 80 // NOTE: the code below is a trivial implementation to make 88 << 81 // this class an STL compliant allocator. 89 // This public section includes standard met << 82 // It is anyhow NOT recommended to use this class as 90 // required if the allocator is to be used a << 83 // alternative allocator for STL containers ! 91 // allocator for STL containers. << 84 92 // NOTE: the code below is a trivial impleme << 85 typedef Type value_type; 93 // this class an STL compliant allocat << 86 typedef size_t size_type; 94 // It is anyhow NOT recommended to use << 87 typedef ptrdiff_t difference_type; 95 // alternative allocator for STL conta << 88 typedef Type* pointer; 96 << 89 typedef const Type* const_pointer; 97 using value_type = Type; << 90 typedef Type& reference; 98 using size_type = std::size_t; << 91 typedef const Type& const_reference; 99 using difference_type = ptrdiff_t; << 92 100 using pointer = Type*; << 93 template <class U> G4Allocator(const G4Allocator<U>& right) throw() 101 using const_pointer = const Type*; << 94 : mem(right.mem) {} 102 using reference = Type&; << 95 // Copy constructor 103 using const_reference = const Type&; << 96 104 << 97 pointer address(reference r) const { return &r; } 105 template <class U> << 98 const_pointer address(const_reference r) const { return &r; } 106 G4Allocator(const G4Allocator<U>& right) thr << 99 // Returns the address of values 107 : mem(right.mem) << 100 108 {} << 101 pointer allocate(size_type n, void* hint = 0) 109 // Copy constructor << 102 { 110 << 103 // Allocates space for n elements of type Type, but does not initialise 111 pointer address(reference r) const { return << 104 // 112 const_pointer address(const_reference r) con << 105 Type* mem_alloc = 0; 113 // Returns the address of values << 106 if (n == 1) 114 << 107 mem_alloc = MallocSingle(); 115 pointer allocate(size_type n, void* = nullpt << 108 else 116 { << 109 mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type))); 117 // Allocates space for n elements of type << 110 return mem_alloc; 118 // << 111 } 119 Type* mem_alloc = 0; << 112 void deallocate(pointer p, size_type n) 120 if(n == 1) << 113 { 121 mem_alloc = MallocSingle(); << 114 // Deallocates n elements of type Type, but doesn't destroy 122 else << 115 // 123 mem_alloc = static_cast<Type*>(::operato << 116 if (n == 1) 124 return mem_alloc; << 117 FreeSingle(p); 125 } << 118 else 126 void deallocate(pointer p, size_type n) << 119 ::operator delete((void*)p); 127 { << 120 return; 128 // Deallocates n elements of type Type, bu << 121 } 129 // << 122 130 if(n == 1) << 123 void construct(pointer p, const Type& val) { new((void*)p) Type(val); } 131 FreeSingle(p); << 124 // Initialises *p by val 132 else << 125 void destroy(pointer p) { p->~Type(); } 133 ::operator delete((void*) p); << 126 // Destroy *p but doesn't deallocate 134 return; << 127 135 } << 128 size_type max_size() const throw() 136 << 129 { 137 void construct(pointer p, const Type& val) { << 130 // Returns the maximum number of elements that can be allocated 138 // Initialises *p by val << 131 // 139 void destroy(pointer p) { p->~Type(); } << 132 return 2147483647/sizeof(Type); 140 // Destroy *p but doesn't deallocate << 133 } 141 << 134 142 size_type max_size() const throw() << 135 template <class U> 143 { << 136 struct rebind { typedef G4Allocator<U> other; }; 144 // Returns the maximum number of elements << 137 // Rebind allocator to type U 145 // << 138 146 return 2147483647 / sizeof(Type); << 139 private: 147 } << 140 148 << 141 G4AllocatorPool mem; 149 template <class U> << 142 // Pool of elements of sizeof(Type) 150 struct rebind << 151 { << 152 using other = G4Allocator<U>; << 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 }; 143 }; 163 144 164 // ------------------------------------------- 145 // ------------------------------------------------------------ 165 // Inline implementation 146 // Inline implementation 166 // ------------------------------------------- 147 // ------------------------------------------------------------ 167 148 168 // Initialization of the static pool 149 // Initialization of the static pool 169 // 150 // 170 // template <class Type> G4AllocatorPool G4All 151 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); 171 152 172 // ******************************************* 153 // ************************************************************ 173 // G4Allocator constructor 154 // G4Allocator constructor 174 // ******************************************* 155 // ************************************************************ 175 // 156 // 176 template <class Type> 157 template <class Type> 177 G4Allocator<Type>::G4Allocator() throw() 158 G4Allocator<Type>::G4Allocator() throw() 178 : mem(sizeof(Type)) 159 : mem(sizeof(Type)) 179 { 160 { 180 tname = typeid(Type).name(); << 181 } 161 } 182 162 183 // ******************************************* 163 // ************************************************************ 184 // G4Allocator destructor 164 // G4Allocator destructor 185 // ******************************************* 165 // ************************************************************ 186 // 166 // 187 template <class Type> 167 template <class Type> 188 G4Allocator<Type>::~G4Allocator() throw() = de << 168 G4Allocator<Type>::~G4Allocator() throw() >> 169 { >> 170 } 189 171 190 // ******************************************* 172 // ************************************************************ 191 // MallocSingle 173 // MallocSingle 192 // ******************************************* 174 // ************************************************************ 193 // 175 // 194 template <class Type> 176 template <class Type> 195 Type* G4Allocator<Type>::MallocSingle() 177 Type* G4Allocator<Type>::MallocSingle() 196 { 178 { 197 return static_cast<Type*>(mem.Alloc()); 179 return static_cast<Type*>(mem.Alloc()); 198 } 180 } 199 181 200 // ******************************************* 182 // ************************************************************ 201 // FreeSingle 183 // FreeSingle 202 // ******************************************* 184 // ************************************************************ 203 // 185 // 204 template <class Type> 186 template <class Type> 205 void G4Allocator<Type>::FreeSingle(Type* anEle 187 void G4Allocator<Type>::FreeSingle(Type* anElement) 206 { 188 { 207 mem.Free(anElement); 189 mem.Free(anElement); 208 return; 190 return; 209 } 191 } 210 192 211 // ******************************************* 193 // ************************************************************ 212 // ResetStorage 194 // ResetStorage 213 // ******************************************* 195 // ************************************************************ 214 // 196 // 215 template <class Type> 197 template <class Type> 216 void G4Allocator<Type>::ResetStorage() 198 void G4Allocator<Type>::ResetStorage() 217 { 199 { 218 // Clear all allocated storage and return it 200 // Clear all allocated storage and return it to the free store 219 // 201 // 220 mem.Reset(); 202 mem.Reset(); 221 return; 203 return; 222 } 204 } 223 205 224 // ******************************************* 206 // ************************************************************ 225 // GetAllocatedSize 207 // GetAllocatedSize 226 // ******************************************* 208 // ************************************************************ 227 // 209 // 228 template <class Type> 210 template <class Type> 229 std::size_t G4Allocator<Type>::GetAllocatedSiz << 211 size_t G4Allocator<Type>::GetAllocatedSize() const 230 { 212 { 231 return mem.Size(); 213 return mem.Size(); 232 } 214 } 233 215 234 // ******************************************* 216 // ************************************************************ 235 // GetNoPages << 236 // ******************************************* << 237 // << 238 template <class Type> << 239 int G4Allocator<Type>::GetNoPages() const << 240 { << 241 return mem.GetNoPages(); << 242 } << 243 << 244 // ******************************************* << 245 // GetPageSize << 246 // ******************************************* << 247 // << 248 template <class Type> << 249 size_t G4Allocator<Type>::GetPageSize() const << 250 { << 251 return mem.GetPageSize(); << 252 } << 253 << 254 // ******************************************* << 255 // IncreasePageSize << 256 // ******************************************* << 257 // << 258 template <class Type> << 259 void G4Allocator<Type>::IncreasePageSize(unsig << 260 { << 261 ResetStorage(); << 262 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 } << 274 << 275 // ******************************************* << 276 // operator== 217 // operator== 277 // ******************************************* 218 // ************************************************************ 278 // 219 // 279 template <class T1, class T2> 220 template <class T1, class T2> 280 bool operator==(const G4Allocator<T1>&, const << 221 bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 281 { 222 { 282 return true; 223 return true; 283 } 224 } 284 225 285 // ******************************************* 226 // ************************************************************ 286 // operator!= 227 // operator!= 287 // ******************************************* 228 // ************************************************************ 288 // 229 // 289 template <class T1, class T2> 230 template <class T1, class T2> 290 bool operator!=(const G4Allocator<T1>&, const << 231 bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 291 { 232 { 292 return false; 233 return false; 293 } 234 } 294 235 295 #endif 236 #endif 296 237