The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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
62namespace 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//
176template <class Type>
177GaudiUtils::Allocator<Type>::Allocator() throw() : mem( sizeof( Type ) ) {}
178
179// ************************************************************
180// MallocSingle
181// ************************************************************
182//
183template <class Type>
185 return static_cast<Type*>( mem.Alloc() );
186}
187
188// ************************************************************
189// FreeSingle
190// ************************************************************
191//
192template <class Type>
194 mem.Free( anElement );
195 return;
196}
197
198// ************************************************************
199// ResetStorage
200// ************************************************************
201//
202template <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//
214template <class Type>
216 return mem.Size();
217}
218
219// ************************************************************
220// operator==
221// ************************************************************
222//
223template <class T1, class T2>
225 return true;
226}
227
228// ************************************************************
229// operator!=
230// ************************************************************
231//
232template <class T1, class T2>
234 return false;
235}
bool operator==(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition Allocator.h:224
bool operator!=(const GaudiUtils::Allocator< T1 > &, const GaudiUtils::Allocator< T2 > &)
Definition Allocator.h:233
Allocator pool.
void deallocate(pointer p, size_type n)
Deallocates n elements of type Type, but doesn't destroy.
Definition Allocator.h:129
Allocator()
Constructor.
Definition Allocator.h:177
size_type max_size() const
Returns the maximum number of elements that can be allocated.
Definition Allocator.h:145
pointer address(reference r) const
Returns the address of values.
Definition Allocator.h:113
GaudiUtils::AllocatorPool mem
Definition Allocator.h:158
pointer allocate(size_type n, void *=0)
Allocates space for n elements of type Type, but does not initialise.
Definition Allocator.h:117
const_pointer address(const_reference r) const
Definition Allocator.h:114
void FreeSingle(Type *anElement)
Definition Allocator.h:193
void ResetStorage()
Returns allocated storage to the free store, resets allocator and page sizes.
Definition Allocator.h:203
const Type * const_pointer
Definition Allocator.h:104
void construct(pointer p, const Type &val)
Initialises *p by val.
Definition Allocator.h:140
void destroy(pointer p)
Destroy *p but doesn't deallocate.
Definition Allocator.h:142
Type * MallocSingle()
Malloc and Free methods to be used when overloading new and delete operators in the client <Type> obj...
Definition Allocator.h:184
ptrdiff_t difference_type
Definition Allocator.h:102
size_t GetAllocatedSize() const
Returns the size of the total memory allocated.
Definition Allocator.h:215
const Type & const_reference
Definition Allocator.h:106
Allocator(const Allocator< U > &right)
Copy constructor.
Definition Allocator.h:110