| The Gaudi Framework v32r1 (f65d50dc)

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 namespace GaudiUtils {
68 template <class Type>
69 class Allocator {
70 public: // with description
72 Allocator() throw();
74 ~Allocator() throw();
75
79 inline Type* MallocSingle();
80 inline void FreeSingle( Type* anElement );
81
86 inline void ResetStorage();
87
89 inline size_t GetAllocatedSize() const;
90
91 public: // without description
92 // This public section includes standard methods and types
93 // required if the allocator is to be used as alternative
94 // allocator for STL containers.
95 // NOTE: the code below is a trivial implementation to make
96 // this class an STL compliant allocator.
97 // It is anyhow NOT recommended to use this class as
98 // alternative allocator for STL containers !
99
100 typedef Type value_type;
101 typedef size_t size_type;
102 typedef ptrdiff_t difference_type;
103 typedef Type* pointer;
104 typedef const Type* const_pointer;
105 typedef Type& reference;
106 typedef const Type& const_reference;
107
109 template <class U>
110 Allocator( const Allocator<U>& right ) throw() : mem( right.mem ) {}
111
113 pointer address( reference r ) const { return &r; }
114 const_pointer address( const_reference r ) const { return &r; }
115
117 pointer allocate( size_type n, void* /*hint*/ = 0 ) {
118 // Allocates space for n elements of type Type, but does not initialise
119 //
120 Type* mem_alloc = 0;
121 if ( n == 1 )
122 mem_alloc = MallocSingle();
123 else
124 mem_alloc = static_cast<Type*>( ::operator new( n * sizeof( Type ) ) );
125 return mem_alloc;
126 }
127
129 void deallocate( pointer p, size_type n ) {
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
140 void construct( pointer p, const Type& val ) { new ( (void*)p ) Type( val ); }
142 void destroy( pointer p ) { p->~Type(); }
143
145 size_type max_size() const throw() {
146 // Returns the maximum number of elements that can be allocated
147 //
148 return 2147483647 / sizeof( Type );
149 }
150
151 // Rebind allocator to type U
152 template <class U>
153 struct rebind {
154 typedef Allocator<U> other;
155 };
156
157 private:
158 GaudiUtils::AllocatorPool mem;
159 // Pool of elements of sizeof(Type)
160 };
161
162 } // namespace GaudiUtils
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 //
176 template <class Type>
177 GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) ) {}
178
179 // ************************************************************
180 // G4Allocator destructor
181 // ************************************************************
182 //
183 template <class Type>
184 GaudiUtils::Allocator<Type>::~Allocator() throw() {}
185
186 // ************************************************************
187 // MallocSingle
188 // ************************************************************
189 //
190 template <class Type>
191 Type* GaudiUtils::Allocator<Type>::MallocSingle() {
192 return static_cast<Type*>( mem.Alloc() );
193 }
194
195 // ************************************************************
196 // FreeSingle
197 // ************************************************************
198 //
199 template <class Type>
200 void GaudiUtils::Allocator<Type>::FreeSingle( Type* anElement ) {
201 mem.Free( anElement );
202 return;
203 }
204
205 // ************************************************************
206 // ResetStorage
207 // ************************************************************
208 //
209 template <class Type>
210 void GaudiUtils::Allocator<Type>::ResetStorage() {
211 // Clear all allocated storage and return it to the free store
212 //
213 mem.Reset();
214 return;
215 }
216
217 // ************************************************************
218 // GetAllocatedSize
219 // ************************************************************
220 //
221 template <class Type>
222 size_t GaudiUtils::Allocator<Type>::GetAllocatedSize() const {
223 return mem.Size();
224 }
225
226 // ************************************************************
227 // operator==
228 // ************************************************************
229 //
230 template <class T1, class T2>
231 bool operator==( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw() {
232 return true;
233 }
234
235 // ************************************************************
236 // operator!=
237 // ************************************************************
238 //
239 template <class T1, class T2>
240 bool operator!=( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw() {
241 return false;
242 }
243
244 // ============================================================================
245 // The END
246 // ============================================================================
247 #endif
GaudiUtils::Allocator::MallocSingle
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client <Type> obj...
Definition: Allocator.h:191
operator!=
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:240
GaudiUtils::Allocator::Allocator
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:110
GaudiUtils::Allocator::difference_type
ptrdiff_t difference_type
Definition: Allocator.h:102
GaudiUtils::Allocator::FreeSingle
void FreeSingle(Type *anElement)
Definition: Allocator.h:200
GaudiUtils::Allocator::deallocate
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn't destroy.
Definition: Allocator.h:129
GaudiUtils::Allocator::address
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:113
GaudiUtils::Allocator::value_type
Type value_type
Definition: Allocator.h:100
GaudiUtils::Allocator::ResetStorage
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:210
GaudiUtils::Allocator::GetAllocatedSize
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:222
GaudiUtils::Allocator::~Allocator
~Allocator()
destructor
Definition: Allocator.h:184
GaudiUtils::Allocator::rebind::other
Allocator< U > other
Definition: Allocator.h:154
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
GaudiUtils::Allocator::mem
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:158
GaudiUtils::Allocator::const_pointer
const Type * const_pointer
Definition: Allocator.h:104
GaudiUtils::Allocator::rebind
Definition: Allocator.h:153
GaudiUtils::Allocator::destroy
void destroy(pointer p)
Destroy *p but doesn't deallocate.
Definition: Allocator.h:142
GaudiUtils::Allocator::construct
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:140
operator==
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:231
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:117
GaudiUtils::Allocator::Allocator
Allocator()
Constructor.
Definition: Allocator.h:177
GaudiUtils::Allocator::const_reference
const Type & const_reference
Definition: Allocator.h:106
GaudiUtils::Allocator::address
const_pointer address(const_reference r) const
Definition: Allocator.h:114
AllocatorPool.h
Allocator pool.
GaudiUtils::Allocator::reference
Type & reference
Definition: Allocator.h:105
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
GaudiUtils::Allocator::size_type
size_t size_type
Definition: Allocator.h:101
GaudiUtils::Allocator::max_size
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:145
GaudiUtils::Allocator::pointer
Type * pointer
Definition: Allocator.h:103
Tuples::Type
Type
the list of available types for ntuples
Definition: TupleObj.h:79
GaudiUtils::Allocator
Allocator.
Definition: Allocator.h:69
GaudiUtils::AllocatorPool
Allocator pool.
Definition: AllocatorPool.h:62