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