Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4EnhancedVecAllocator< _Tp > Class Template Reference

#include <G4EnhancedVecAllocator.hh>

Inheritance diagram for G4EnhancedVecAllocator< _Tp >:
Collaboration 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, size_t _Count)
 
_Tp * allocate (size_t _Count)
 

Detailed Description

template<typename _Tp>
class G4EnhancedVecAllocator< _Tp >

Definition at line 80 of file G4EnhancedVecAllocator.hh.

Constructor & Destructor Documentation

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

Definition at line 87 of file G4EnhancedVecAllocator.hh.

87 {;}
template<typename _Tp >
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp > &  )
inline

Definition at line 89 of file G4EnhancedVecAllocator.hh.

90  : std::allocator<_Tp>() {;}
template<typename _Tp >
template<typename _Tp1 >
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp1 > &  )
inline

Definition at line 93 of file G4EnhancedVecAllocator.hh.

94  : std::allocator<_Tp>() {;}
template<typename _Tp >
G4EnhancedVecAllocator< _Tp >::~G4EnhancedVecAllocator ( )
inline

Definition at line 96 of file G4EnhancedVecAllocator.hh.

96 {;}

Member Function Documentation

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

Definition at line 155 of file G4EnhancedVecAllocator.hh.

157 {
158  size_t totalsize = _Count * sizeof(_Tp);
159 
160  G4int found = -1;
161  if (G4AllocStats::allocStat != 0)
162  {
163  for (G4int j = 0 ; j < G4AllocStats::numCat ; j++)
164  {
165  if (G4AllocStats::allocStat[j].size == totalsize)
166  {
167  found = j;
168  break;
169  }
170  }
171  }
172 
173  if (found == -1) // Find the new size
174  {
175  G4AllocStats::numCat++;
176  if (G4AllocStats::numCat > G4AllocStats::totSpace)
177  {
179  // heuristic parameter for different sizes
180 
184  // This value must be different than zero; otherwise means
185  // failure in allocating extra space !
186  // assert(G4AllocStats::allocStat != 0);
187  }
188 
189  G4AllocStats::allocStat[G4AllocStats::numCat-1].size = totalsize;
190  G4AllocStats::allocStat[G4AllocStats::numCat-1].totalspace = 0;
191  G4AllocStats::allocStat[G4AllocStats::numCat-1].preAllocated = 0;
192 
193  found = G4AllocStats::numCat - 1;
195 
196  chunk.totalspace = 512;
197  // heuristic for the number of STL vector instances
198 
199  chunk.preAllocated = (G4ChunkType *) realloc(chunk.preAllocated,
200  sizeof(G4ChunkType) * chunk.totalspace);
201  // This value must be different than zero; otherwise means
202  // failure in allocating extra space for pointers !
203  // assert(chunk.preAllocated != 0);
204 
205  char *newSpace1 = (char *) malloc(totalsize * 512);
206  // This pointer must be different than zero; otherwise means
207  // failure in allocating extra space for instances !
208  // assert(newSpace1 != 0);
209 
210  for (G4int k = 0; k < 512 ; k++)
211  {
212  (chunk.preAllocated[k]).isAllocated = 0;
213  (chunk.preAllocated[k]).address = newSpace1+totalsize*k;
214  }
215 
216  (chunk.preAllocated[0]).isAllocated = 1;
217  return (_Tp*)((chunk.preAllocated[0]).address);
218  }
219 
221 
222  // assert(chunk.size == totalsize);
223 
224  for (G4int k = 0; k < chunk.totalspace; k++)
225  {
226  if ((chunk.preAllocated[k]).isAllocated == 0)
227  {
228  (chunk.preAllocated[k]).isAllocated = 1;
229  return (_Tp*)((chunk.preAllocated[k]).address);
230  }
231  }
232 
233  G4int originalchunknumber = chunk.totalspace;
234 
235  chunk.totalspace += 512; // heuristic for the number of STL vector instances
236 
237  chunk.preAllocated = (G4ChunkType *) realloc(chunk.preAllocated,
238  sizeof(G4ChunkType) * chunk.totalspace);
239  // This value must be different than zero; otherwise means
240  // failure in allocating extra space for pointers !
241  // assert(chunk.preAllocated != 0);
242 
243  char *newSpace = (char *) malloc(totalsize * 512);
244  // This pointer must be different than zero; otherwise means
245  // failure in allocating extra space for instances !
246  // assert(newSpace != 0);
247 
248  for (G4int k = 0; k < 512 ; k++)
249  {
250  (chunk.preAllocated[originalchunknumber+k]).isAllocated = 0;
251  (chunk.preAllocated[originalchunknumber+k]).address = newSpace+totalsize*k;
252  }
253 
254  (chunk.preAllocated[originalchunknumber]).isAllocated = 1;
255 
256  return (_Tp*)((chunk.preAllocated[originalchunknumber]).address);
257 }
static G4ThreadLocal G4ChunkIndexType * allocStat
static G4ThreadLocal G4int totSpace
int G4int
Definition: G4Types.hh:78
static G4ThreadLocal G4int numCat
template<typename _Tp >
void G4EnhancedVecAllocator< _Tp >::deallocate ( _Tp *  _Ptr,
size_t  _Count 
)

Definition at line 117 of file G4EnhancedVecAllocator.hh.

118 {
119  G4int found = -1;
120  if (G4AllocStats::allocStat != 0)
121  {
122  for (G4int j = 0 ; j < G4AllocStats::numCat ; j++)
123  {
124  if (G4AllocStats::allocStat[j].size == (_Count * sizeof(_Tp)))
125  {
126  found = j;
127  break;
128  }
129  }
130  }
131  // assert(found != -1);
132 
134 
135  for (G4int k = 0; k < chunk.totalspace; k++)
136  {
137  if ( (chunk.preAllocated[k]).address == ((char *) _Ptr))
138  {
139  // assert((chunk.preAllocated[k]).isAllocated==1);
140  (chunk.preAllocated[k]).isAllocated = 0;
141  return;
142  }
143  }
144 }
static G4ThreadLocal G4ChunkIndexType * allocStat
int G4int
Definition: G4Types.hh:78
static G4ThreadLocal G4int numCat

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