| The Gaudi Framework v30r0 (c919700c)

- 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 namespace GaudiUtils
63 {
69 template <class Type>
70 class Allocator
71 {
72 public: // with description
74 Allocator() throw();
76 ~Allocator() throw();
77
81 inline Type* MallocSingle();
82 inline void FreeSingle( Type* anElement );
83
88 inline void ResetStorage();
89
91 inline size_t GetAllocatedSize() const;
92
93 public: // without description
94 // This public section includes standard methods and types
95 // required if the allocator is to be used as alternative
96 // allocator for STL containers.
97 // NOTE: the code below is a trivial implementation to make
98 // this class an STL compliant allocator.
99 // It is anyhow NOT recommended to use this class as
100 // alternative allocator for STL containers !
101
102 typedef Type value_type;
103 typedef size_t size_type;
104 typedef ptrdiff_t difference_type;
105 typedef Type* pointer;
106 typedef const Type* const_pointer;
107 typedef Type& reference;
108 typedef const Type& const_reference;
109
111 template <class U>
112 Allocator( const Allocator<U>& right ) throw() : mem( right.mem )
113 {
114 }
115
117 pointer address( reference r ) const { return &r; }
118 const_pointer address( const_reference r ) const { return &r; }
119
121 pointer allocate( size_type n, void* /*hint*/ = 0 )
122 {
123 // Allocates space for n elements of type Type, but does not initialise
124 //
125 Type* mem_alloc = 0;
126 if ( n == 1 )
127 mem_alloc = MallocSingle();
128 else
129 mem_alloc = static_cast<Type*>(::operator new( n * sizeof( Type ) ) );
130 return mem_alloc;
131 }
132
134 void deallocate( pointer p, size_type n )
135 {
136 // Deallocates n elements of type Type, but doesn't destroy
137 //
138 if ( n == 1 )
139 FreeSingle( p );
140 else
141 ::operator delete( (void*)p );
142 return;
143 }
144
146 void construct( pointer p, const Type& val ) { new ( (void*)p ) Type( val ); }
148 void destroy( pointer p ) { p->~Type(); }
149
151 size_type max_size() const throw()
152 {
153 // Returns the maximum number of elements that can be allocated
154 //
155 return 2147483647 / sizeof( Type );
156 }
157
158 // Rebind allocator to type U
159 template <class U>
160 struct rebind {
161 typedef Allocator<U> other;
162 };
163
164 private:
165 GaudiUtils::AllocatorPool mem;
166 // Pool of elements of sizeof(Type)
167 };
168
169 } // end of the namespace GaudiUtils
170
171 // ------------------------------------------------------------
172 // Inline implementation
173 // ------------------------------------------------------------
174
175 // Initialization of the static pool
176 //
177 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
178
179 // ************************************************************
180 // G4Allocator constructor
181 // ************************************************************
182 //
183 template <class Type>
184 GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) )
185 {
186 }
187
188 // ************************************************************
189 // G4Allocator destructor
190 // ************************************************************
191 //
192 template <class Type>
193 GaudiUtils::Allocator<Type>::~Allocator() throw()
194 {
195 }
196
197 // ************************************************************
198 // MallocSingle
199 // ************************************************************
200 //
201 template <class Type>
202 Type* GaudiUtils::Allocator<Type>::MallocSingle()
203 {
204 return static_cast<Type*>( mem.Alloc() );
205 }
206
207 // ************************************************************
208 // FreeSingle
209 // ************************************************************
210 //
211 template <class Type>
212 void GaudiUtils::Allocator<Type>::FreeSingle( Type* anElement )
213 {
214 mem.Free( anElement );
215 return;
216 }
217
218 // ************************************************************
219 // ResetStorage
220 // ************************************************************
221 //
222 template <class Type>
223 void GaudiUtils::Allocator<Type>::ResetStorage()
224 {
225 // Clear all allocated storage and return it to the free store
226 //
227 mem.Reset();
228 return;
229 }
230
231 // ************************************************************
232 // GetAllocatedSize
233 // ************************************************************
234 //
235 template <class Type>
236 size_t GaudiUtils::Allocator<Type>::GetAllocatedSize() const
237 {
238 return mem.Size();
239 }
240
241 // ************************************************************
242 // operator==
243 // ************************************************************
244 //
245 template <class T1, class T2>
246 bool operator==( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw()
247 {
248 return true;
249 }
250
251 // ************************************************************
252 // operator!=
253 // ************************************************************
254 //
255 template <class T1, class T2>
256 bool operator!=( const GaudiUtils::Allocator<T1>&, const GaudiUtils::Allocator<T2>& ) throw()
257 {
258 return false;
259 }
260
261 // ============================================================================
262 // The END
263 // ============================================================================
264 #endif
265 // ============================================================================
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:202
operator!=
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:256
GaudiUtils::Allocator::address
const_pointer address(const_reference r) const
Definition: Allocator.h:118
GaudiUtils::Allocator::Allocator
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:112
GaudiUtils::Allocator::difference_type
ptrdiff_t difference_type
Definition: Allocator.h:104
GaudiUtils::Allocator::FreeSingle
void FreeSingle(Type *anElement)
Definition: Allocator.h:212
GaudiUtils::Allocator::deallocate
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn&#39;t destroy.
Definition: Allocator.h:134
GaudiUtils::AllocatorPool::Size
unsigned int Size() const
Return storage size.
Definition: AllocatorPool.h:140
GaudiUtils::Allocator::value_type
Type value_type
Definition: Allocator.h:102
GaudiUtils::Allocator::ResetStorage
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:223
GaudiUtils::Allocator::~Allocator
~Allocator()
destructor
Definition: Allocator.h:193
GaudiUtils::Allocator::address
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:117
GaudiUtils::Allocator::rebind::other
Allocator< U > other
Definition: Allocator.h:161
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
GaudiUtils::Allocator::mem
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:165
GaudiUtils::Allocator::const_pointer
const Type * const_pointer
Definition: Allocator.h:106
GaudiUtils::AllocatorPool::Alloc
void * Alloc()
Allocate one element.
Definition: AllocatorPool.h:115
GaudiUtils::AllocatorPool::Reset
void Reset()
Return storage to the free store.
Definition: AllocatorPool.cpp:88
GaudiUtils::Allocator::rebind
Definition: Allocator.h:160
GaudiUtils::Allocator::destroy
void destroy(pointer p)
Destroy *p but doesn&#39;t deallocate.
Definition: Allocator.h:148
GaudiUtils::Allocator::construct
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:146
operator==
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:246
GaudiUtils::Allocator::max_size
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:151
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:121
GaudiUtils::Allocator::Allocator
Allocator()
Constructor.
Definition: Allocator.h:184
GaudiUtils::AllocatorPool::Free
void Free(void *b)
Return an element back to the pool.
Definition: AllocatorPool.h:129
GaudiUtils::Allocator::const_reference
const Type & const_reference
Definition: Allocator.h:108
AllocatorPool.h
Allocator pool.
GaudiUtils::Allocator::reference
Type & reference
Definition: Allocator.h:107
GaudiUtils
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:140
GaudiUtils::Allocator::size_type
size_t size_type
Definition: Allocator.h:103
GaudiUtils::Allocator::GetAllocatedSize
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:236
GaudiUtils::Allocator::pointer
Type * pointer
Definition: Allocator.h:105
Tuples::Type
Type
the list of available types for ntuples
Definition: TupleObj.h:84
GaudiUtils::Allocator
Allocator.
Definition: Allocator.h:70
GaudiUtils::AllocatorPool
Allocator pool.
Definition: AllocatorPool.h:63