|
||||
File indexing completed on 2025-01-18 09:57:53
0001 // 0002 // ******************************************************************** 0003 // * License and Disclaimer * 0004 // * * 0005 // * The Geant4 software is copyright of the Copyright Holders of * 0006 // * the Geant4 Collaboration. It is provided under the terms and * 0007 // * conditions of the Geant4 Software License, included in the file * 0008 // * LICENSE and available at http://cern.ch/geant4/license . These * 0009 // * include a list of copyright holders. * 0010 // * * 0011 // * Neither the authors of this software system, nor their employing * 0012 // * institutes,nor the agencies providing financial support for this * 0013 // * work make any representation or warranty, express or implied, * 0014 // * regarding this software system or assume any liability for its * 0015 // * use. Please see the license in the file LICENSE and URL above * 0016 // * for the full disclaimer and the limitation of liability. * 0017 // * * 0018 // * This code implementation is the result of the scientific and * 0019 // * technical work of the GEANT4 collaboration. * 0020 // * By using, copying, modifying or distributing the software (or * 0021 // * any work based on the software) you agree to acknowledge its * 0022 // * use in resulting scientific publications, and indicate your * 0023 // * acceptance of all terms of the Geant4 Software license. * 0024 // ******************************************************************** 0025 // 0026 // G4Allocator 0027 // 0028 // Class Description: 0029 // 0030 // A class for fast allocation of objects to the heap through a pool of 0031 // chunks organised as linked list. It's meant to be used by associating 0032 // it to the object to be allocated and defining for it new and delete 0033 // operators via MallocSingle() and FreeSingle() methods. 0034 0035 // ---------------- G4Allocator ---------------- 0036 // 0037 // Author: G.Cosmo (CERN), November 2000 0038 // -------------------------------------------------------------------- 0039 #ifndef G4Allocator_hh 0040 #define G4Allocator_hh 1 0041 0042 #include <cstddef> 0043 #include <typeinfo> 0044 0045 #include "G4AllocatorPool.hh" 0046 0047 class G4AllocatorBase 0048 { 0049 public: 0050 G4AllocatorBase(); 0051 virtual ~G4AllocatorBase() = default; 0052 virtual void ResetStorage() = 0; 0053 virtual std::size_t GetAllocatedSize() const = 0; 0054 virtual int GetNoPages() const = 0; 0055 virtual std::size_t GetPageSize() const = 0; 0056 virtual void IncreasePageSize(unsigned int sz) = 0; 0057 virtual const char* GetPoolType() const = 0; 0058 }; 0059 0060 template <class Type> 0061 class G4Allocator : public G4AllocatorBase 0062 { 0063 public: 0064 G4Allocator() throw(); 0065 ~G4Allocator() throw() override; 0066 // Constructor & destructor 0067 0068 inline Type* MallocSingle(); 0069 inline void FreeSingle(Type* anElement); 0070 // Malloc and Free methods to be used when overloading 0071 // new and delete operators in the client <Type> object 0072 0073 inline void ResetStorage() override; 0074 // Returns allocated storage to the free store, resets allocator. 0075 // Note: contents in memory are lost using this call ! 0076 0077 inline std::size_t GetAllocatedSize() const override; 0078 // Returns the size of the total memory allocated 0079 inline int GetNoPages() const override; 0080 // Returns the total number of allocated pages 0081 inline std::size_t GetPageSize() const override; 0082 // Returns the current size of a page 0083 inline void IncreasePageSize(unsigned int sz) override; 0084 // Resets allocator and increases default page size of a given factor 0085 0086 inline const char* GetPoolType() const override; 0087 // Returns the type_info Id of the allocated type in the pool 0088 0089 // This public section includes standard methods and types 0090 // required if the allocator is to be used as alternative 0091 // allocator for STL containers. 0092 // NOTE: the code below is a trivial implementation to make 0093 // this class an STL compliant allocator. 0094 // It is anyhow NOT recommended to use this class as 0095 // alternative allocator for STL containers ! 0096 0097 using value_type = Type; 0098 using size_type = std::size_t; 0099 using difference_type = ptrdiff_t; 0100 using pointer = Type*; 0101 using const_pointer = const Type*; 0102 using reference = Type&; 0103 using const_reference = const Type&; 0104 0105 template <class U> 0106 G4Allocator(const G4Allocator<U>& right) throw() 0107 : mem(right.mem) 0108 {} 0109 // Copy constructor 0110 0111 pointer address(reference r) const { return &r; } 0112 const_pointer address(const_reference r) const { return &r; } 0113 // Returns the address of values 0114 0115 pointer allocate(size_type n, void* = nullptr) 0116 { 0117 // Allocates space for n elements of type Type, but does not initialise 0118 // 0119 Type* mem_alloc = 0; 0120 if(n == 1) 0121 mem_alloc = MallocSingle(); 0122 else 0123 mem_alloc = static_cast<Type*>(::operator new(n * sizeof(Type))); 0124 return mem_alloc; 0125 } 0126 void deallocate(pointer p, size_type n) 0127 { 0128 // Deallocates n elements of type Type, but doesn't destroy 0129 // 0130 if(n == 1) 0131 FreeSingle(p); 0132 else 0133 ::operator delete((void*) p); 0134 return; 0135 } 0136 0137 void construct(pointer p, const Type& val) { new((void*) p) Type(val); } 0138 // Initialises *p by val 0139 void destroy(pointer p) { p->~Type(); } 0140 // Destroy *p but doesn't deallocate 0141 0142 size_type max_size() const throw() 0143 { 0144 // Returns the maximum number of elements that can be allocated 0145 // 0146 return 2147483647 / sizeof(Type); 0147 } 0148 0149 template <class U> 0150 struct rebind 0151 { 0152 using other = G4Allocator<U>; 0153 }; 0154 // Rebind allocator to type U 0155 0156 G4AllocatorPool mem; 0157 // Pool of elements of sizeof(Type) 0158 0159 private: 0160 const char* tname; 0161 // Type name identifier 0162 }; 0163 0164 // ------------------------------------------------------------ 0165 // Inline implementation 0166 // ------------------------------------------------------------ 0167 0168 // Initialization of the static pool 0169 // 0170 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type)); 0171 0172 // ************************************************************ 0173 // G4Allocator constructor 0174 // ************************************************************ 0175 // 0176 template <class Type> 0177 G4Allocator<Type>::G4Allocator() throw() 0178 : mem(sizeof(Type)) 0179 { 0180 tname = typeid(Type).name(); 0181 } 0182 0183 // ************************************************************ 0184 // G4Allocator destructor 0185 // ************************************************************ 0186 // 0187 template <class Type> 0188 G4Allocator<Type>::~G4Allocator() throw() = default; 0189 0190 // ************************************************************ 0191 // MallocSingle 0192 // ************************************************************ 0193 // 0194 template <class Type> 0195 Type* G4Allocator<Type>::MallocSingle() 0196 { 0197 return static_cast<Type*>(mem.Alloc()); 0198 } 0199 0200 // ************************************************************ 0201 // FreeSingle 0202 // ************************************************************ 0203 // 0204 template <class Type> 0205 void G4Allocator<Type>::FreeSingle(Type* anElement) 0206 { 0207 mem.Free(anElement); 0208 return; 0209 } 0210 0211 // ************************************************************ 0212 // ResetStorage 0213 // ************************************************************ 0214 // 0215 template <class Type> 0216 void G4Allocator<Type>::ResetStorage() 0217 { 0218 // Clear all allocated storage and return it to the free store 0219 // 0220 mem.Reset(); 0221 return; 0222 } 0223 0224 // ************************************************************ 0225 // GetAllocatedSize 0226 // ************************************************************ 0227 // 0228 template <class Type> 0229 std::size_t G4Allocator<Type>::GetAllocatedSize() const 0230 { 0231 return mem.Size(); 0232 } 0233 0234 // ************************************************************ 0235 // GetNoPages 0236 // ************************************************************ 0237 // 0238 template <class Type> 0239 int G4Allocator<Type>::GetNoPages() const 0240 { 0241 return mem.GetNoPages(); 0242 } 0243 0244 // ************************************************************ 0245 // GetPageSize 0246 // ************************************************************ 0247 // 0248 template <class Type> 0249 size_t G4Allocator<Type>::GetPageSize() const 0250 { 0251 return mem.GetPageSize(); 0252 } 0253 0254 // ************************************************************ 0255 // IncreasePageSize 0256 // ************************************************************ 0257 // 0258 template <class Type> 0259 void G4Allocator<Type>::IncreasePageSize(unsigned int sz) 0260 { 0261 ResetStorage(); 0262 mem.GrowPageSize(sz); 0263 } 0264 0265 // ************************************************************ 0266 // GetPoolType 0267 // ************************************************************ 0268 // 0269 template <class Type> 0270 const char* G4Allocator<Type>::GetPoolType() const 0271 { 0272 return tname; 0273 } 0274 0275 // ************************************************************ 0276 // operator== 0277 // ************************************************************ 0278 // 0279 template <class T1, class T2> 0280 bool operator==(const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 0281 { 0282 return true; 0283 } 0284 0285 // ************************************************************ 0286 // operator!= 0287 // ************************************************************ 0288 // 0289 template <class T1, class T2> 0290 bool operator!=(const G4Allocator<T1>&, const G4Allocator<T2>&) throw() 0291 { 0292 return false; 0293 } 0294 0295 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |