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