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5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /global/management/include/G4Allocator.hh (Version 11.3.0) and /global/management/include/G4Allocator.hh (Version 10.5)


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