Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4EnhancedVecAllocator.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4EnhancedVecAllocator
27//
28// Class Description:
29//
30// A class for fast allocation of STL vectors through a static pool.
31// It's meant to be used as alternative allocator for STL vectors.
32
33// Original author: X.Dong (NorthEastern Univ.), November 2009
34// Reviewed implementation: G.Cosmo (CERN), December 2009
35// ------------------------------------------------------------
36#ifndef G4EnhancedVecAllocator_hh
37#define G4EnhancedVecAllocator_hh
38
39#include "G4Types.hh"
40
41typedef struct
42{
44 char *address;
46
53
55{
56 /**
57 * @brief Utility class, placeholder for global data on allocation.
58 * Initialisation to zero of the data below *must* be added ONCE only
59 * directly in the client code, where this allocator is to be applied
60 */
61
62 public:
63
67};
68
69/**
70 * @brief G4EnhancedVecAllocator is a class for fast allocation of STL vectors
71 * through a static pool. It's meant to be used as alternative allocator
72 * for STL vectors.
73 */
74
75template<typename _Tp>
76class G4EnhancedVecAllocator : public std::allocator<_Tp>
77{
78 public:
79
80 template<typename _Tp1>
82
84
86 : std::allocator<_Tp>() {;}
87
88 template<typename _Tp1>
90 : std::allocator<_Tp>() {;}
91
93
94 // override allocate / deallocate
95 //
96 void deallocate(_Tp* _Ptr, std::size_t _Count);
97#ifdef __IBMCPP__
98 _Tp* allocate(std::size_t _Count, void * const hint = 0); // IBM AIX
99#else
100 _Tp* allocate(std::size_t _Count);
101#endif
102};
103
104// ------------------------------------------------------------
105// Inline implementations
106// ------------------------------------------------------------
107
108// ************************************************************
109// deallocate
110// ************************************************************
111//
112template<typename _Tp>
113void G4EnhancedVecAllocator<_Tp>::deallocate(_Tp* _Ptr, std::size_t _Count)
114{
115 G4int found = -1;
117 {
118 for (auto j = 0 ; j < G4AllocStats::numCat ; ++j)
119 {
120 if (G4AllocStats::allocStat[j].size == (_Count * sizeof(_Tp)))
121 {
122 found = j;
123 break;
124 }
125 }
126 }
127 // assert(found != -1);
128
130
131 for (auto k = 0; k < chunk.totalspace; ++k)
132 {
133 if ( (chunk.preAllocated[k]).address == ((char *) _Ptr))
134 {
135 // assert((chunk.preAllocated[k]).isAllocated==1);
136 (chunk.preAllocated[k]).isAllocated = 0;
137 return;
138 }
139 }
140}
141
142// ************************************************************
143// allocate
144// ************************************************************
145//
146#ifdef __IBMCPP__
147template<typename _Tp>
148_Tp* G4EnhancedVecAllocator<_Tp>::allocate(std::size_t _Count, void * const hint)
149#else
150template<typename _Tp>
152#endif
153{
154 std::size_t totalsize = _Count * sizeof(_Tp);
155
156 G4int found = -1;
158 {
159 for (auto j = 0 ; j < G4AllocStats::numCat ; ++j)
160 {
161 if (G4AllocStats::allocStat[j].size == totalsize)
162 {
163 found = j;
164 break;
165 }
166 }
167 }
168
169 if (found == -1) // Find the new size
170 {
173 {
175 // heuristic parameter for different sizes
176
180 // This value must be different than zero; otherwise means
181 // failure in allocating extra space !
182 // assert(G4AllocStats::allocStat != 0);
183 }
184
188
189 found = G4AllocStats::numCat - 1;
191
192 chunk.totalspace = 512;
193 // heuristic for the number of STL vector instances
194
195 chunk.preAllocated = (G4ChunkType *) realloc(chunk.preAllocated,
196 sizeof(G4ChunkType) * chunk.totalspace);
197 // This value must be different than zero; otherwise means
198 // failure in allocating extra space for pointers !
199 // assert(chunk.preAllocated != 0);
200
201 char *newSpace1 = (char *) malloc(totalsize * 512);
202 // This pointer must be different than zero; otherwise means
203 // failure in allocating extra space for instances !
204 // assert(newSpace1 != 0);
205
206 for (auto k = 0; k < 512 ; ++k)
207 {
208 (chunk.preAllocated[k]).isAllocated = 0;
209 (chunk.preAllocated[k]).address = newSpace1+totalsize*k;
210 }
211
212 (chunk.preAllocated[0]).isAllocated = 1;
213 return (_Tp*)((chunk.preAllocated[0]).address);
214 }
215
217
218 // assert(chunk.size == totalsize);
219
220 for (auto k = 0; k < chunk.totalspace; ++k)
221 {
222 if ((chunk.preAllocated[k]).isAllocated == 0)
223 {
224 (chunk.preAllocated[k]).isAllocated = 1;
225 return (_Tp*)((chunk.preAllocated[k]).address);
226 }
227 }
228
229 G4int originalchunknumber = chunk.totalspace;
230
231 chunk.totalspace += 512; // heuristic for the number of STL vector instances
232
233 chunk.preAllocated = (G4ChunkType *) realloc(chunk.preAllocated,
234 sizeof(G4ChunkType) * chunk.totalspace);
235 // This value must be different than zero; otherwise means
236 // failure in allocating extra space for pointers !
237 // assert(chunk.preAllocated != 0);
238
239 char *newSpace = (char *) malloc(totalsize * 512);
240 // This pointer must be different than zero; otherwise means
241 // failure in allocating extra space for instances !
242 // assert(newSpace != 0);
243
244 for (auto k = 0; k < 512 ; ++k)
245 {
246 (chunk.preAllocated[originalchunknumber+k]).isAllocated = 0;
247 (chunk.preAllocated[originalchunknumber+k]).address = newSpace+totalsize*k;
248 }
249
250 (chunk.preAllocated[originalchunknumber]).isAllocated = 1;
251
252 return (_Tp*)((chunk.preAllocated[originalchunknumber]).address);
253}
254
255// ************************************************************
256// operator==
257// ************************************************************
258//
259template<typename _T1, typename _T2>
262{ return true; }
263
264// ************************************************************
265// operator!=
266// ************************************************************
267//
268template<typename _T1, typename _T2>
271{ return false; }
272
273#endif
G4bool operator!=(const G4EnhancedVecAllocator< _T1 > &, const G4EnhancedVecAllocator< _T2 > &)
G4bool operator==(const G4EnhancedVecAllocator< _T1 > &, const G4EnhancedVecAllocator< _T2 > &)
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
static G4ThreadLocal G4int totSpace
static G4ThreadLocal G4ChunkIndexType * allocStat
Utility class, placeholder for global data on allocation. Initialisation to zero of the data below mu...
static G4ThreadLocal G4int numCat
G4EnhancedVecAllocator is a class for fast allocation of STL vectors through a static pool....
void deallocate(_Tp *_Ptr, std::size_t _Count)
_Tp * allocate(std::size_t _Count)
G4EnhancedVecAllocator(const G4EnhancedVecAllocator< _Tp > &)
G4EnhancedVecAllocator(const G4EnhancedVecAllocator< _Tp1 > &)
voidp malloc(uInt size)
allocation_function xml_memory_management_function_storage< T >::allocate
Definition pugixml.cc:205
G4EnhancedVecAllocator< _Tp1 > other
#define G4ThreadLocal
Definition tls.hh:77