Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v28r2p1 (f1a77ff4)
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 
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 
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>
186  : mem(sizeof(Type))
187 {
188 }
189 
190 // ************************************************************
191 // G4Allocator destructor
192 // ************************************************************
193 //
194 template <class Type>
196 {
197 }
198 
199 // ************************************************************
200 // MallocSingle
201 // ************************************************************
202 //
203 template <class Type>
205 {
206  return static_cast<Type*>(mem.Alloc());
207 }
208 
209 // ************************************************************
210 // FreeSingle
211 // ************************************************************
212 //
213 template <class Type>
215 {
216  mem.Free(anElement);
217  return;
218 }
219 
220 // ************************************************************
221 // ResetStorage
222 // ************************************************************
223 //
224 template <class Type>
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>
239 {
240  return mem.Size();
241 }
242 
243 // ************************************************************
244 // operator==
245 // ************************************************************
246 //
247 template <class T1, class T2>
248 bool operator==
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!=
262  const GaudiUtils::Allocator<T2>&) throw()
263 {
264  return false;
265 }
266 
267 // ============================================================================
268 // The END
269 // ============================================================================
270 #endif
271 // ============================================================================
272 
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client <Type> obj...
Definition: Allocator.h:204
const_pointer address(const_reference r) const
Definition: Allocator.h:120
ptrdiff_t difference_type
Definition: Allocator.h:107
void FreeSingle(Type *anElement)
Definition: Allocator.h:214
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn&#39;t destroy.
Definition: Allocator.h:136
unsigned int Size() const
Return storage size.
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:225
~Allocator()
destructor
Definition: Allocator.h:195
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:119
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:166
const Type * const_pointer
Definition: Allocator.h:109
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:150
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:148
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:153
pointer allocate(size_type n, void *=0)
Allocates space for n elements of type Type, but does not initialise.
Definition: Allocator.h:123
Allocator()
Constructor.
Definition: Allocator.h:185
void Free(void *b)
Return an element back to the pool.
const Type & const_reference
Definition: Allocator.h:111
Allocator pool.
Forward declarations for the functions in SerializeSTL.h.
Definition: GaudiHistoID.h:149
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:238
Type
the list of available types for ntuples
Definition: TupleObj.h:79