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