Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 {
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 {
155  };
156 
157  private:
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>
178 
179 // ************************************************************
180 // G4Allocator destructor
181 // ************************************************************
182 //
183 template <class Type>
185 
186 // ************************************************************
187 // MallocSingle
188 // ************************************************************
189 //
190 template <class Type>
192  return static_cast<Type*>( mem.Alloc() );
193 }
194 
195 // ************************************************************
196 // FreeSingle
197 // ************************************************************
198 //
199 template <class Type>
201  mem.Free( anElement );
202  return;
203 }
204 
205 // ************************************************************
206 // ResetStorage
207 // ************************************************************
208 //
209 template <class Type>
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>
223  return mem.Size();
224 }
225 
226 // ************************************************************
227 // operator==
228 // ************************************************************
229 //
230 template <class T1, class T2>
232  return true;
233 }
234 
235 // ************************************************************
236 // operator!=
237 // ************************************************************
238 //
239 template <class T1, class T2>
241  return false;
242 }
243 
244 // ============================================================================
245 // The END
246 // ============================================================================
247 #endif
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client <Type> obj...
Definition: Allocator.h:191
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:240
const_pointer address(const_reference r) const
Definition: Allocator.h:114
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:110
ptrdiff_t difference_type
Definition: Allocator.h:102
void FreeSingle(Type *anElement)
Definition: Allocator.h:200
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn&#39;t destroy.
Definition: Allocator.h:129
unsigned int Size() const
Return storage size.
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:210
~Allocator()
destructor
Definition: Allocator.h:184
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:113
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:158
const Type * const_pointer
Definition: Allocator.h:104
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:142
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:140
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:231
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:145
pointer allocate(size_type n, void *=0)
Allocates space for n elements of type Type, but does not initialise.
Definition: Allocator.h:117
Allocator()
Constructor.
Definition: Allocator.h:177
void Free(void *b)
Return an element back to the pool.
const Type & const_reference
Definition: Allocator.h:106
Allocator pool.
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:136
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:222
Type
the list of available types for ntuples
Definition: TupleObj.h:80