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