Gaudi Framework, version v25r0
Home
Generated: Mon Feb 17 2014
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
GaudiKernel
GaudiKernel
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
Generated at Mon Feb 17 2014 14:37:42 for Gaudi Framework, version v25r0 by
Doxygen
version 1.8.2 written by
Dimitri van Heesch
, © 1997-2004