Loading [MathJax]/extensions/MathMenu.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Allocator.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
58 
60 #include <cstddef>
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 
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>
177 GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) ) {}
178 
179 // ************************************************************
180 // MallocSingle
181 // ************************************************************
182 //
183 template <class Type>
185  return static_cast<Type*>( mem.Alloc() );
186 }
187 
188 // ************************************************************
189 // FreeSingle
190 // ************************************************************
191 //
192 template <class Type>
194  mem.Free( anElement );
195  return;
196 }
197 
198 // ************************************************************
199 // ResetStorage
200 // ************************************************************
201 //
202 template <class Type>
204  // Clear all allocated storage and return it to the free store
205  //
206  mem.Reset();
207  return;
208 }
209 
210 // ************************************************************
211 // GetAllocatedSize
212 // ************************************************************
213 //
214 template <class Type>
216  return mem.Size();
217 }
218 
219 // ************************************************************
220 // operator==
221 // ************************************************************
222 //
223 template <class T1, class T2>
225  return true;
226 }
227 
228 // ************************************************************
229 // operator!=
230 // ************************************************************
231 //
232 template <class T1, class T2>
234  return false;
235 }
GaudiUtils::Allocator::Allocator
Allocator(const Allocator< U > &right)
Copy constructor.
Definition: Allocator.h:110
GaudiUtils::Allocator::value_type
Type value_type
Definition: Allocator.h:100
GaudiUtils::Allocator::difference_type
ptrdiff_t difference_type
Definition: Allocator.h:102
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:184
GaudiUtils::Allocator::deallocate
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn't destroy.
Definition: Allocator.h:129
GaudiUtils::Allocator::address
pointer address(reference r) const
Returns the address of values.
Definition: Allocator.h:113
AllocatorPool.h
GaudiUtils::Allocator::~Allocator
~Allocator()
destructor
Definition: Allocator.h:74
GaudiUtils::Allocator::construct
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition: Allocator.h:140
operator==
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:224
GaudiUtils::Allocator::ResetStorage
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition: Allocator.h:203
GaudiUtils::Allocator::const_pointer
const Type * const_pointer
Definition: Allocator.h:104
GaudiUtils::Allocator::GetAllocatedSize
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition: Allocator.h:215
GaudiUtils::Allocator::mem
GaudiUtils::AllocatorPool mem
Definition: Allocator.h:158
GaudiUtils::Allocator::destroy
void destroy(pointer p)
Destroy *p but doesn't deallocate.
Definition: Allocator.h:142
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:117
GaudiUtils::Allocator::Allocator
Allocator()
Constructor.
Definition: Allocator.h:177
GaudiUtils::Allocator
Definition: Allocator.h:69
cpluginsvc.n
n
Definition: cpluginsvc.py:234
GaudiUtils::Allocator::address
const_pointer address(const_reference r) const
Definition: Allocator.h:114
operator!=
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition: Allocator.h:233
GaudiUtils::Allocator::reference
Type & reference
Definition: Allocator.h:105
GaudiUtils::Allocator::pointer
Type * pointer
Definition: Allocator.h:103
GaudiUtils::Allocator::const_reference
const Type & const_reference
Definition: Allocator.h:106
GaudiUtils::Allocator::max_size
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition: Allocator.h:145
GaudiUtils::AllocatorPool
Definition: AllocatorPool.h:71
GaudiUtils
Definition: Allocator.h:62
GaudiUtils::Allocator::size_type
size_t size_type
Definition: Allocator.h:101
GaudiUtils::Allocator::rebind
Definition: Allocator.h:153
GaudiUtils::Allocator::rebind::other
Allocator< U > other
Definition: Allocator.h:154
GaudiUtils::Allocator::FreeSingle
void FreeSingle(Type *anElement)
Definition: Allocator.h:193