Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Allocator.h
Go to the documentation of this file.
1 // $Id: Allocator.h,v 1.1 2006/02/14 15:01:12 hmd Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.1 $
4 // ============================================================================
10 // ============================================================================
11 
12 //
13 // ********************************************************************
14 // * DISCLAIMER *
15 // * *
16 // * The following disclaimer summarizes all the specific disclaimers *
17 // * of contributors to this software. The specific disclaimers,which *
18 // * govern, are listed with their locations in: *
19 // * http://cern.ch/geant4/license *
20 // * *
21 // * Neither the authors of this software system, nor their employing *
22 // * institutes,nor the agencies providing financial support for this *
23 // * work make any representation or warranty, express or implied, *
24 // * regarding this software system or assume any liability for its *
25 // * use. *
26 // * *
27 // * This code implementation is the intellectual property of the *
28 // * GEANT4 collaboration. *
29 // * By copying, distributing or modifying the Program (or any work *
30 // * based on the Program) you indicate your acceptance of this *
31 // * statement, and all its terms. *
32 // ********************************************************************
33 //
34 //
35 // ------------------------------------------------------------
36 // GEANT 4 class header file
37 //
38 // Class Description:
39 //
40 // A class for fast allocation of objects to the heap through a pool of
41 // chunks organised as linked list. It's meant to be used by associating
42 // it to the object to be allocated and defining for it new and delete
43 // operators via MallocSingle() and FreeSingle() methods.
44 
45 // ---------------- G4Allocator ----------------
46 //
47 // Author: G.Cosmo (CERN), November 2000
48 // ------------------------------------------------------------
49 
50 // ============================================================================
51 #ifndef GAUDIKERNEL_Allocator_h
52 #define GAUDIKERNEL_Allocator_h 1
53 // ============================================================================
54 // Incldue files
55 // ============================================================================
56 // STD & STL
57 // ============================================================================
58 #include <cstddef>
59 // ============================================================================
60 // GaudiKernel
61 // ============================================================================
63 // ============================================================================
64 
65 
66 namespace GaudiUtils
67 {
73  template <class Type>
74  class Allocator
75  {
76  public: // with description
77 
79  Allocator() throw();
81  ~Allocator() throw();
82 
86  inline Type* MallocSingle();
87  inline void FreeSingle(Type* anElement);
88 
93  inline void ResetStorage();
94 
96  inline size_t GetAllocatedSize() const;
97 
98  public: // without description
99 
100  // This public section includes standard methods and types
101  // required if the allocator is to be used as alternative
102  // allocator for STL containers.
103  // NOTE: the code below is a trivial implementation to make
104  // this class an STL compliant allocator.
105  // It is anyhow NOT recommended to use this class as
106  // alternative allocator for STL containers !
107 
108  typedef Type value_type;
109  typedef size_t size_type;
110  typedef ptrdiff_t difference_type;
111  typedef Type* pointer;
112  typedef const Type* const_pointer;
113  typedef Type& reference;
114  typedef const Type& const_reference;
115 
117  template <class U> Allocator
118  (const Allocator<U>& right) throw()
119  : mem(right.mem) {}
120 
122  pointer address(reference r) const { return &r; }
123  const_pointer address(const_reference r) const { return &r; }
124 
126  pointer allocate(size_type n, void* hint = 0)
127  {
128  // Allocates space for n elements of type Type, but does not initialise
129  //
130  Type* mem_alloc = 0;
131  if (n == 1)
132  mem_alloc = MallocSingle();
133  else
134  mem_alloc = static_cast<Type*>(::operator new(n*sizeof(Type)));
135  return mem_alloc;
136  }
137 
140  {
141  // Deallocates n elements of type Type, but doesn't destroy
142  //
143  if (n == 1)
144  FreeSingle(p);
145  else
146  ::operator delete((void*)p);
147  return;
148  }
149 
151  void construct(pointer p, const Type& val) { new((void*)p) Type(val); }
153  void destroy(pointer p) { p->~Type(); }
154 
156  size_type max_size() const throw()
157  {
158  // Returns the maximum number of elements that can be allocated
159  //
160  return 2147483647/sizeof(Type);
161  }
162 
163  // Rebind allocator to type U
164  template <class U>
165  struct rebind { typedef Allocator<U> other; };
166 
167  private:
168 
170  // Pool of elements of sizeof(Type)
171  };
172 
173 } // end of the namespace GaudiUtils
174 
175 // ------------------------------------------------------------
176 // Inline implementation
177 // ------------------------------------------------------------
178 
179 // Initialization of the static pool
180 //
181 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
182 
183 // ************************************************************
184 // G4Allocator constructor
185 // ************************************************************
186 //
187 template <class Type>
189  : mem(sizeof(Type))
190 {
191 }
192 
193 // ************************************************************
194 // G4Allocator destructor
195 // ************************************************************
196 //
197 template <class Type>
199 {
200 }
201 
202 // ************************************************************
203 // MallocSingle
204 // ************************************************************
205 //
206 template <class Type>
208 {
209  return static_cast<Type*>(mem.Alloc());
210 }
211 
212 // ************************************************************
213 // FreeSingle
214 // ************************************************************
215 //
216 template <class Type>
218 {
219  mem.Free(anElement);
220  return;
221 }
222 
223 // ************************************************************
224 // ResetStorage
225 // ************************************************************
226 //
227 template <class Type>
229 {
230  // Clear all allocated storage and return it to the free store
231  //
232  mem.Reset();
233  return;
234 }
235 
236 // ************************************************************
237 // GetAllocatedSize
238 // ************************************************************
239 //
240 template <class Type>
242 {
243  return mem.Size();
244 }
245 
246 // ************************************************************
247 // operator==
248 // ************************************************************
249 //
250 template <class T1, class T2>
251 bool operator==
253  const GaudiUtils::Allocator<T2>&) throw()
254 {
255  return true;
256 }
257 
258 // ************************************************************
259 // operator!=
260 // ************************************************************
261 //
262 template <class T1, class T2>
263 bool operator!=
265  const GaudiUtils::Allocator<T2>&) throw()
266 {
267  return false;
268 }
269 
270 // ============================================================================
271 // The END
272 // ============================================================================
273 #endif
274 // ============================================================================
275 

Generated at Wed Nov 28 2012 12:17:12 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004