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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 7.0)


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