The Gaudi Framework  master (37c0b60a)
Allocator.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
17 // ============================================================================
18 
19 //
20 // ********************************************************************
21 // * DISCLAIMER *
22 // * *
23 // * The following disclaimer summarizes all the specific disclaimers *
24 // * of contributors to this software. The specific disclaimers,which *
25 // * govern, are listed with their locations in: *
26 // * http://cern.ch/geant4/license *
27 // * *
28 // * Neither the authors of this software system, nor their employing *
29 // * institutes,nor the agencies providing financial support for this *
30 // * work make any representation or warranty, express or implied, *
31 // * regarding this software system or assume any liability for its *
32 // * use. *
33 // * *
34 // * This code implementation is the intellectual property of the *
35 // * GEANT4 collaboration. *
36 // * By copying, distributing or modifying the Program (or any work *
37 // * based on the Program) you indicate your acceptance of this *
38 // * statement, and all its terms. *
39 // ********************************************************************
40 //
41 //
42 // ------------------------------------------------------------
43 // GEANT 4 class header file
44 //
45 // Class Description:
46 //
47 // A class for fast allocation of objects to the heap through a pool of
48 // chunks organised as linked list. It's meant to be used by associating
49 // it to the object to be allocated and defining for it new and delete
50 // operators via MallocSingle() and FreeSingle() methods.
51 
52 // ---------------- G4Allocator ----------------
53 //
54 // Author: G.Cosmo (CERN), November 2000
55 // ------------------------------------------------------------
56 
57 // ============================================================================
58 #ifndef GAUDIKERNEL_Allocator_h
59 #define GAUDIKERNEL_Allocator_h 1
60 // ============================================================================
61 // Include files
62 // ============================================================================
63 // STD & STL
64 // ============================================================================
65 #include <cstddef>
66 // ============================================================================
67 // GaudiKernel
68 // ============================================================================
70 // ============================================================================
71 
72 namespace GaudiUtils {
78  template <class Type>
79  class Allocator {
80  public: // with description
82  Allocator() throw();
84  ~Allocator() throw() {}
85 
89  inline Type* MallocSingle();
90  inline void FreeSingle( Type* anElement );
91 
96  inline void ResetStorage();
97 
99  inline size_t GetAllocatedSize() const;
100 
101  public: // without description
102  // This public section includes standard methods and types
103  // required if the allocator is to be used as alternative
104  // allocator for STL containers.
105  // NOTE: the code below is a trivial implementation to make
106  // this class an STL compliant allocator.
107  // It is anyhow NOT recommended to use this class as
108  // alternative allocator for STL containers !
109 
110  typedef Type value_type;
111  typedef size_t size_type;
112  typedef ptrdiff_t difference_type;
113  typedef Type* pointer;
114  typedef const Type* const_pointer;
115  typedef Type& reference;
116  typedef const Type& const_reference;
117 
119  template <class U>
120  Allocator( const Allocator<U>& right ) throw() : mem( right.mem ) {}
121 
123  pointer address( reference r ) const { return &r; }
124  const_pointer address( const_reference r ) const { return &r; }
125 
127  pointer allocate( size_type n, void* /*hint*/ = 0 ) {
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  // Deallocates n elements of type Type, but doesn't destroy
141  //
142  if ( n == 1 )
143  FreeSingle( p );
144  else
145  ::operator delete( (void*)p );
146  return;
147  }
148 
150  void construct( pointer p, const Type& val ) { new ( (void*)p ) Type( val ); }
152  void destroy( pointer p ) { p->~Type(); }
153 
155  size_type max_size() const throw() {
156  // Returns the maximum number of elements that can be allocated
157  //
158  return 2147483647 / sizeof( Type );
159  }
160 
161  // Rebind allocator to type U
162  template <class U>
163  struct rebind {
165  };
166 
167  private:
169  // Pool of elements of sizeof(Type)
170  };
171 
172 } // namespace GaudiUtils
173 
174 // ------------------------------------------------------------
175 // Inline implementation
176 // ------------------------------------------------------------
177 
178 // Initialization of the static pool
179 //
180 // template <class Type> G4AllocatorPool G4Allocator<Type>::mem(sizeof(Type));
181 
182 // ************************************************************
183 // G4Allocator constructor
184 // ************************************************************
185 //
186 template <class Type>
187 GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) ) {}
188 
189 // ************************************************************
190 // MallocSingle
191 // ************************************************************
192 //
193 template <class Type>
195  return static_cast<Type*>( mem.Alloc() );
196 }
197 
198 // ************************************************************
199 // FreeSingle
200 // ************************************************************
201 //
202 template <class Type>
204  mem.Free( anElement );
205  return;
206 }
207 
208 // ************************************************************
209 // ResetStorage
210 // ************************************************************
211 //
212 template <class Type>
214  // Clear all allocated storage and return it to the free store
215  //
216  mem.Reset();
217  return;
218 }
219 
220 // ************************************************************
221 // GetAllocatedSize
222 // ************************************************************
223 //
224 template <class Type>
226  return mem.Size();
227 }
228 
229 // ************************************************************
230 // operator==
231 // ************************************************************
232 //
233 template <class T1, class T2>
235  return true;
236 }
237 
238 // ************************************************************
239 // operator!=
240 // ************************************************************
241 //
242 template <class T1, class T2>
244  return false;
245 }
246 
247 // ============================================================================
248 // The END
249 // ============================================================================
250 #endif
GaudiUtils::Allocator::Allocator
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:120
GaudiUtils::Allocator::value_type
Type value_type
Definition: Allocator.h:110
GaudiUtils::Allocator::difference_type
ptrdiff_t difference_type
Definition: Allocator.h:112
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:194
GaudiUtils::Allocator::deallocate
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn't destroy.
Definition: Allocator.h:139
GaudiUtils::Allocator::address
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:123
AllocatorPool.h
GaudiUtils::Allocator::~Allocator
~Allocator()
destructor
Definition: Allocator.h:84
GaudiUtils::Allocator::construct
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:150
operator==
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:234
GaudiUtils::Allocator::ResetStorage
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:213
GaudiUtils::Allocator::const_pointer
const Type * const_pointer
Definition: Allocator.h:114
GaudiUtils::Allocator::GetAllocatedSize
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:225
GaudiUtils::Allocator::mem
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:168
GaudiUtils::Allocator::destroy
void destroy(pointer p)
Destroy *p but doesn't deallocate.
Definition: Allocator.h:152
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:127
GaudiUtils::Allocator::Allocator
Allocator()
Constructor.
Definition: Allocator.h:187
GaudiUtils::Allocator
Definition: Allocator.h:79
cpluginsvc.n
n
Definition: cpluginsvc.py:234
GaudiUtils::Allocator::address
const_pointer address(const_reference r) const
Definition: Allocator.h:124
operator!=
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:243
GaudiUtils::Allocator::reference
Type & reference
Definition: Allocator.h:115
GaudiUtils::Allocator::pointer
Type * pointer
Definition: Allocator.h:113
GaudiUtils::Allocator::const_reference
const Type & const_reference
Definition: Allocator.h:116
GaudiUtils::Allocator::max_size
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:155
GaudiUtils::AllocatorPool
Definition: AllocatorPool.h:72
GaudiUtils
Definition: Allocator.h:72
GaudiUtils::Allocator::size_type
size_t size_type
Definition: Allocator.h:111
GaudiUtils::Allocator::rebind
Definition: Allocator.h:163
GaudiUtils::Allocator::rebind::other
Allocator< U > other
Definition: Allocator.h:164
GaudiUtils::Allocator::FreeSingle
void FreeSingle(Type *anElement)
Definition: Allocator.h:203