The Gaudi Framework  v30r3 (a5ef0a68)
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 // ============================================================================
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 {
162  };
163 
164  private:
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>
185 {
186 }
187 
188 // ************************************************************
189 // G4Allocator destructor
190 // ************************************************************
191 //
192 template <class Type>
194 {
195 }
196 
197 // ************************************************************
198 // MallocSingle
199 // ************************************************************
200 //
201 template <class Type>
203 {
204  return static_cast<Type*>( mem.Alloc() );
205 }
206 
207 // ************************************************************
208 // FreeSingle
209 // ************************************************************
210 //
211 template <class Type>
213 {
214  mem.Free( anElement );
215  return;
216 }
217 
218 // ************************************************************
219 // ResetStorage
220 // ************************************************************
221 //
222 template <class Type>
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>
237 {
238  return mem.Size();
239 }
240 
241 // ************************************************************
242 // operator==
243 // ************************************************************
244 //
245 template <class T1, class T2>
247 {
248  return true;
249 }
250 
251 // ************************************************************
252 // operator!=
253 // ************************************************************
254 //
255 template <class T1, class T2>
257 {
258  return false;
259 }
260 
261 // ============================================================================
262 // The END
263 // ============================================================================
264 #endif
265 // ============================================================================
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client <Type> obj...
Definition: Allocator.h:202
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:256
const_pointer address(const_reference r) const
Definition: Allocator.h:118
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:112
ptrdiff_t difference_type
Definition: Allocator.h:104
void FreeSingle(Type *anElement)
Definition: Allocator.h:212
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn&#39;t destroy.
Definition: Allocator.h:134
unsigned int Size() const
Return storage size.
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:223
~Allocator()
destructor
Definition: Allocator.h:193
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:117
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:165
const Type * const_pointer
Definition: Allocator.h:106
void * Alloc()
Allocate one element.
void Reset()
Return storage to the free store.
void destroy(pointer p)
Destroy *p but doesn&#39;t deallocate.
Definition: Allocator.h:148
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:146
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:246
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:151
pointer allocate(size_type n, void *=0)
Allocates space for n elements of type Type, but does not initialise.
Definition: Allocator.h:121
Allocator()
Constructor.
Definition: Allocator.h:184
void Free(void *b)
Return an element back to the pool.
const Type & const_reference
Definition: Allocator.h:108
Allocator pool.
Forward declarations for the functions in SerializeSTL.h.
Definition: __init__.py:1
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:236
Type
the list of available types for ntuples
Definition: TupleObj.h:83