Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4EnhancedVecAllocator< _Tp > Class Template Reference

G4EnhancedVecAllocator is a class for fast allocation of STL vectors through a static pool. It's meant to be used as alternative allocator for STL vectors. More...

#include <G4EnhancedVecAllocator.hh>

Inheritance diagram for G4EnhancedVecAllocator< _Tp >:

Classes

struct  rebind

Public Member Functions

 G4EnhancedVecAllocator ()
 G4EnhancedVecAllocator (const G4EnhancedVecAllocator< _Tp > &)
template<typename _Tp1>
 G4EnhancedVecAllocator (const G4EnhancedVecAllocator< _Tp1 > &)
 ~G4EnhancedVecAllocator ()
void deallocate (_Tp *_Ptr, std::size_t _Count)
_Tp * allocate (std::size_t _Count)

Detailed Description

template<typename _Tp>
class G4EnhancedVecAllocator< _Tp >

G4EnhancedVecAllocator is a class for fast allocation of STL vectors through a static pool. It's meant to be used as alternative allocator for STL vectors.

Definition at line 76 of file G4EnhancedVecAllocator.hh.

Constructor & Destructor Documentation

◆ G4EnhancedVecAllocator() [1/3]

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( )
inline

Definition at line 83 of file G4EnhancedVecAllocator.hh.

83{;}

◆ G4EnhancedVecAllocator() [2/3]

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp > & )
inline

Definition at line 85 of file G4EnhancedVecAllocator.hh.

G4EnhancedVecAllocator is a class for fast allocation of STL vectors through a static pool....

◆ G4EnhancedVecAllocator() [3/3]

template<typename _Tp>
template<typename _Tp1>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp1 > & )
inline

Definition at line 89 of file G4EnhancedVecAllocator.hh.

◆ ~G4EnhancedVecAllocator()

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::~G4EnhancedVecAllocator ( )
inline

Definition at line 92 of file G4EnhancedVecAllocator.hh.

92{;}

Member Function Documentation

◆ allocate()

template<typename _Tp>
_Tp * G4EnhancedVecAllocator< _Tp >::allocate ( std::size_t _Count)

Definition at line 151 of file G4EnhancedVecAllocator.hh.

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 {
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
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;
248 }
249
250 (chunk.preAllocated[originalchunknumber]).isAllocated = 1;
251
252 return (_Tp*)((chunk.preAllocated[originalchunknumber]).address);
253}
voidp malloc(uInt size)

◆ deallocate()

template<typename _Tp>
void G4EnhancedVecAllocator< _Tp >::deallocate ( _Tp * _Ptr,
std::size_t _Count )

Definition at line 113 of file G4EnhancedVecAllocator.hh.

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}

The documentation for this class was generated from the following file: