| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Allocator.h
Go to the documentation of this file.
1 // ============================================================================
7 // ============================================================================
8
9 //
10 // ********************************************************************
11 // * DISCLAIMER *
12 // * *
13 // * The following disclaimer summarizes all the specific disclaimers *
14 // * of contributors to this software. The specific disclaimers,which *
15 // * govern, are listed with their locations in: *
16 // * http://cern.ch/geant4/license *
17 // * *
18 // * Neither the authors of this software system, nor their employing *
19 // * institutes,nor the agencies providing financial support for this *
20 // * work make any representation or warranty, express or implied, *
21 // * regarding this software system or assume any liability for its *
22 // * use. *
23 // * *
24 // * This code implementation is the intellectual property of the *
25 // * GEANT4 collaboration. *
26 // * By copying, distributing or modifying the Program (or any work *
27 // * based on the Program) you indicate your acceptance of this *
28 // * statement, and all its terms. *
29 // ********************************************************************
30 //
31 //
32 // ------------------------------------------------------------
33 // GEANT 4 class header file
34 //
35 // Class Description:
36 //
37 // A class for fast allocation of objects to the heap through a pool of
38 // chunks organised as linked list. It's meant to be used by associating
39 // it to the object to be allocated and defining for it new and delete
40 // operators via MallocSingle() and FreeSingle() methods.
41
42 // ---------------- G4Allocator ----------------
43 //
44 // Author: G.Cosmo (CERN), November 2000
45 // ------------------------------------------------------------
46
47 // ============================================================================
48 #ifndef GAUDIKERNEL_Allocator_h
49 #define GAUDIKERNEL_Allocator_h 1
50 // ============================================================================
51 // Include files
52 // ============================================================================
53 // STD & STL
54 // ============================================================================
55 #include <cstddef>
56 // ============================================================================
57 // GaudiKernel
58 // ============================================================================
59 #include "GaudiKernel/AllocatorPool.h"
60 // ============================================================================
61
62
63 namespace GaudiUtils
64 {
70 template <class Type>
71 class Allocator
72 {
73 public: // with description
74
76 Allocator() throw();
78 ~Allocator() throw();
79
83 inline Type* MallocSingle();
84 inline void FreeSingle(Type* anElement);
85
90 inline void ResetStorage();
91
93 inline size_t GetAllocatedSize() const;
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
114 template <class U> Allocator
115 (const Allocator<U>& right) throw()
116 : mem(right.mem) {}
117
119 pointer address(reference r) const { return &r; }
120 const_pointer address(const_reference r) const { return &r; }
121
123 pointer allocate(size_type n, void* /*hint*/ = 0)
124 {
125 // Allocates space for n elements of type Type, but does not initialise
126 //
127 Type* mem_alloc = 0;
128 if (n == 1)
129 mem_alloc = MallocSingle();
130 else
131 mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type)));
132 return mem_alloc;
133 }
134
136 void deallocate(pointer p, size_type n)
137 {
138 // Deallocates n elements of type Type, but doesn't destroy
139 //
140 if (n == 1)
141 FreeSingle(p);
142 else
143 ::operator delete((void*)p);
144 return;
145 }
146
148 void construct(pointer p, const Type& val) { new((void*)p) Type(val); }
150 void destroy(pointer p) { p->~Type(); }
151
153 size_type max_size() const throw()
154 {
155 // Returns the maximum number of elements that can be allocated
156 //
157 return 2147483647/sizeof(Type);
158 }
159
160 // Rebind allocator to type U
161 template <class U>
162 struct rebind { typedef Allocator<U> other; };
163
164 private:
165
166 GaudiUtils::AllocatorPool mem;
167 // Pool of elements of sizeof(Type)
168 };
169
170 } // end of the namespace GaudiUtils
171
172 // ------------------------------------------------------------
173 // Inline implementation
174 // ------------------------------------------------------------
175
176 // Initialization of the static pool
177 //
178 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
179
180 // ************************************************************
181 // G4Allocator constructor
182 // ************************************************************
183 //
184 template <class Type>
185 GaudiUtils::Allocator<Type>::Allocator() throw()
186 : mem(sizeof(Type))
187 {
188 }
189
190 // ************************************************************
191 // G4Allocator destructor
192 // ************************************************************
193 //
194 template <class Type>
195 GaudiUtils::Allocator<Type>::~Allocator() throw()
196 {
197 }
198
199 // ************************************************************
200 // MallocSingle
201 // ************************************************************
202 //
203 template <class Type>
204 Type* GaudiUtils::Allocator<Type>::MallocSingle()
205 {
206 return static_cast<Type*>(mem.Alloc());
207 }
208
209 // ************************************************************
210 // FreeSingle
211 // ************************************************************
212 //
213 template <class Type>
214 void GaudiUtils::Allocator<Type>::FreeSingle(Type* anElement)
215 {
216 mem.Free(anElement);
217 return;
218 }
219
220 // ************************************************************
221 // ResetStorage
222 // ************************************************************
223 //
224 template <class Type>
225 void GaudiUtils::Allocator<Type>::ResetStorage()
226 {
227 // Clear all allocated storage and return it to the free store
228 //
229 mem.Reset();
230 return;
231 }
232
233 // ************************************************************
234 // GetAllocatedSize
235 // ************************************************************
236 //
237 template <class Type>
238 size_t GaudiUtils::Allocator<Type>::GetAllocatedSize() const
239 {
240 return mem.Size();
241 }
242
243 // ************************************************************
244 // operator==
245 // ************************************************************
246 //
247 template <class T1, class T2>
248 bool operator==
249 (const GaudiUtils::Allocator<T1>&,
250 const GaudiUtils::Allocator<T2>&) throw()
251 {
252 return true;
253 }
254
255 // ************************************************************
256 // operator!=
257 // ************************************************************
258 //
259 template <class T1, class T2>
260 bool operator!=
261 (const GaudiUtils::Allocator<T1>&,
262 const GaudiUtils::Allocator<T2>&) throw()
263 {
264 return false;
265 }
266
267 // ============================================================================
268 // The END
269 // ============================================================================
270 #endif
271 // ============================================================================
272
GaudiUtils::Allocator::MallocSingle
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client obj...
Definition: Allocator.h:204
GaudiUtils::Allocator::address
const_pointer address(const_reference r) const
Definition: Allocator.h:120
GaudiUtils::Allocator::difference_type
ptrdiff_t difference_type
Definition: Allocator.h:107
GaudiUtils::Allocator::FreeSingle
void FreeSingle(Type *anElement)
Definition: Allocator.h:214
GaudiUtils::Allocator::deallocate
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn't destroy.
Definition: Allocator.h:136
GaudiUtils::Allocator::value_type
Type value_type
Definition: Allocator.h:105
GaudiUtils::Allocator::ResetStorage
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:225
GaudiUtils::Allocator::~Allocator
~Allocator()
destructor
Definition: Allocator.h:195
GaudiUtils::Allocator::address
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:119
GaudiUtils::Allocator::rebind::other
Allocator< U > other
Definition: Allocator.h:162
GaudiPluginService.cpluginsvc.n
list n
Definition: cpluginsvc.py:217
GaudiUtils::Allocator::mem
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:166
GaudiUtils::Allocator::const_pointer
const Type * const_pointer
Definition: Allocator.h:109
GaudiUtils::Allocator::rebind
Definition: Allocator.h:162
GaudiUtils::Allocator::destroy
void destroy(pointer p)
Destroy *p but doesn't deallocate.
Definition: Allocator.h:150
GaudiUtils::Allocator::construct
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:148
GaudiUtils::Allocator::max_size
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:153
GaudiUtils::Allocator::allocate
pointer allocate(size_type n, void *=0)
Allocates space for n elements of type Type, but does not initialise.
Definition: Allocator.h:123
GaudiUtils::Allocator::Allocator
Allocator()
Constructor.
Definition: Allocator.h:185
GaudiUtils::Allocator::const_reference
const Type & const_reference
Definition: Allocator.h:111
AllocatorPool.h
Allocator pool.
GaudiUtils::Allocator::reference
Type & reference
Definition: Allocator.h:110
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:139
GaudiUtils::Allocator::size_type
size_t size_type
Definition: Allocator.h:106
GaudiUtils::Allocator::GetAllocatedSize
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:238
GaudiUtils::Allocator::pointer
Type * pointer
Definition: Allocator.h:108
Tuples::Type
Type
the list of available types for ntuples
Definition: TupleObj.h:79
GaudiUtils::Allocator
Allocator.
Definition: Allocator.h:71
GaudiUtils::AllocatorPool
Allocator pool.
Definition: AllocatorPool.h:63