Geant4  10.01.p01
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 71573 2013-06-18 10:26:21Z 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 
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 };
63 
64 template <class Type>
66 {
67  public: // with description
68 
69  G4Allocator() throw();
70  ~G4Allocator() throw();
71  // Constructor & destructor
72 
73  inline Type* MallocSingle();
74  inline void FreeSingle(Type* anElement);
75  // Malloc and Free methods to be used when overloading
76  // new and delete operators in the client <Type> object
77 
78  inline void ResetStorage();
79  // Returns allocated storage to the free store, resets allocator.
80  // Note: contents in memory are lost using this call !
81 
82  inline size_t GetAllocatedSize() const;
83  // Returns the size of the total memory allocated
84  inline int GetNoPages() const;
85  // Returns the total number of allocated pages
86  inline size_t GetPageSize() const;
87  // Returns the current size of a page
88  inline void IncreasePageSize( unsigned int sz );
89  // Resets allocator and increases default page size of a given factor
90 
91  public: // without description
92 
93  // This public section includes standard methods and types
94  // required if the allocator is to be used as alternative
95  // allocator for STL containers.
96  // NOTE: the code below is a trivial implementation to make
97  // this class an STL compliant allocator.
98  // It is anyhow NOT recommended to use this class as
99  // alternative allocator for STL containers !
100 
101  typedef Type value_type;
102  typedef size_t size_type;
103  typedef ptrdiff_t difference_type;
104  typedef Type* pointer;
105  typedef const Type* const_pointer;
106  typedef Type& reference;
107  typedef const Type& const_reference;
108 
109  template <class U> G4Allocator(const G4Allocator<U>& right) throw()
110  : mem(right.mem) {}
111  // Copy constructor
112 
113  pointer address(reference r) const { return &r; }
114  const_pointer address(const_reference r) const { return &r; }
115  // Returns the address of values
116 
117  pointer allocate(size_type n, void* = 0)
118  {
119  // Allocates space for n elements of type Type, but does not initialise
120  //
121  Type* mem_alloc = 0;
122  if (n == 1)
123  mem_alloc = MallocSingle();
124  else
125  mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type)));
126  return mem_alloc;
127  }
128  void deallocate(pointer p, size_type n)
129  {
130  // Deallocates n elements of type Type, but doesn't destroy
131  //
132  if (n == 1)
133  FreeSingle(p);
134  else
135  ::operator delete((void*)p);
136  return;
137  }
138 
139  void construct(pointer p, const Type& val) { new((void*)p) Type(val); }
140  // Initialises *p by val
141  void destroy(pointer p) { p->~Type(); }
142  // Destroy *p but doesn't deallocate
143 
144  size_type max_size() const throw()
145  {
146  // Returns the maximum number of elements that can be allocated
147  //
148  return 2147483647/sizeof(Type);
149  }
150 
151  template <class U>
152  struct rebind { typedef G4Allocator<U> other; };
153  // Rebind allocator to type U
154 
156  // Pool of elements of sizeof(Type)
157 };
158 
159 // ------------------------------------------------------------
160 // Inline implementation
161 // ------------------------------------------------------------
162 
163 // Initialization of the static pool
164 //
165 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
166 
167 // ************************************************************
168 // G4Allocator constructor
169 // ************************************************************
170 //
171 template <class Type>
173  : mem(sizeof(Type))
174 {
175 }
176 
177 // ************************************************************
178 // G4Allocator destructor
179 // ************************************************************
180 //
181 template <class Type>
183 {
184 }
185 
186 // ************************************************************
187 // MallocSingle
188 // ************************************************************
189 //
190 template <class Type>
192 {
193  return static_cast<Type*>(mem.Alloc());
194 }
195 
196 // ************************************************************
197 // FreeSingle
198 // ************************************************************
199 //
200 template <class Type>
201 void G4Allocator<Type>::FreeSingle(Type* anElement)
202 {
203  mem.Free(anElement);
204  return;
205 }
206 
207 // ************************************************************
208 // ResetStorage
209 // ************************************************************
210 //
211 template <class Type>
213 {
214  // Clear all allocated storage and return it to the free store
215  //
216  mem.Reset();
217  return;
218 }
219 
220 // ************************************************************
221 // GetAllocatedSize
222 // ************************************************************
223 //
224 template <class Type>
226 {
227  return mem.Size();
228 }
229 
230 // ************************************************************
231 // GetNoPages
232 // ************************************************************
233 //
234 template <class Type>
236 {
237  return mem.GetNoPages();
238 }
239 
240 // ************************************************************
241 // GetPageSize
242 // ************************************************************
243 //
244 template <class Type>
246 {
247  return mem.GetPageSize();
248 }
249 
250 // ************************************************************
251 // IncreasePageSize
252 // ************************************************************
253 //
254 template <class Type>
255 void G4Allocator<Type>::IncreasePageSize( unsigned int sz )
256 {
257  ResetStorage();
258  mem.GrowPageSize(sz);
259 }
260 
261 // ************************************************************
262 // operator==
263 // ************************************************************
264 //
265 template <class T1, class T2>
266 bool operator== (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
267 {
268  return true;
269 }
270 
271 // ************************************************************
272 // operator!=
273 // ************************************************************
274 //
275 template <class T1, class T2>
276 bool operator!= (const G4Allocator<T1>&, const G4Allocator<T2>&) throw()
277 {
278  return false;
279 }
280 
281 #endif
Type * MallocSingle()
Definition: G4Allocator.hh:191
Type * pointer
Definition: G4Allocator.hh:104
bool operator!=(const G4Allocator< T1 > &, const G4Allocator< T2 > &)
Definition: G4Allocator.hh:276
size_t GetAllocatedSize() const
Definition: G4Allocator.hh:225
virtual void IncreasePageSize(unsigned int sz)=0
ptrdiff_t difference_type
Definition: G4Allocator.hh:103
virtual size_t GetAllocatedSize() const =0
G4AllocatorPool mem
Definition: G4Allocator.hh:155
size_type max_size() const
Definition: G4Allocator.hh:144
void FreeSingle(Type *anElement)
Definition: G4Allocator.hh:201
size_t size_type
Definition: G4Allocator.hh:102
G4Allocator< U > other
Definition: G4Allocator.hh:152
void ResetStorage()
Definition: G4Allocator.hh:212
void construct(pointer p, const Type &val)
Definition: G4Allocator.hh:139
void deallocate(pointer p, size_type n)
Definition: G4Allocator.hh:128
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:109
void destroy(pointer p)
Definition: G4Allocator.hh:141
const Type * const_pointer
Definition: G4Allocator.hh:105
const_pointer address(const_reference r) const
Definition: G4Allocator.hh:114
bool operator==(const G4Allocator< T1 > &, const G4Allocator< T2 > &)
Definition: G4Allocator.hh:266
Type & reference
Definition: G4Allocator.hh:106
size_t GetPageSize() const
Definition: G4Allocator.hh:245
pointer address(reference r) const
Definition: G4Allocator.hh:113
pointer allocate(size_type n, void *=0)
Definition: G4Allocator.hh:117
const Type & const_reference
Definition: G4Allocator.hh:107
virtual ~G4AllocatorBase()
Definition: G4Allocator.cc:39
int GetNoPages() const
Definition: G4Allocator.hh:235
void IncreasePageSize(unsigned int sz)
Definition: G4Allocator.hh:255