Geant4  10.01.p02
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 //
27 // $Id: G4EnhancedVecAllocator.hh 80286 2014-04-10 09:48:48Z gcosmo $
28 //
29 //
30 // ------------------------------------------------------------
31 // GEANT 4 class header file
32 //
33 // Class Description:
34 //
35 // A class for fast allocation of STL vectors through a static pool.
36 // It's meant to be used as alternative allocator for STL vectors.
37 
38 // ---------------- G4EnhancedVecAllocator ----------------
39 //
40 // Original author: X.Dong (NorthEastern Univ.), November 2009
41 // Reviewed implementation: G.Cosmo (CERN), December 2009
42 // ------------------------------------------------------------
43 
44 #ifndef G4EnhancedVecAllocator_h
45 #define G4EnhancedVecAllocator_h 1
46 
47 #include "G4Types.hh"
48 
49 // #include <cstdlib>
50 
51 typedef struct
52 {
54  char *address;
55 } G4ChunkType;
56 
57 typedef struct
58 {
59  size_t size;
63 
65 {
66  // --------------------------------------------------------------------
67  // Utility class, placeholder for global data on allocation.
68  // Initialisation to zero of the data below *must* be added ONCE only
69  // directly in the client code, where this allocator is to be applied
70  // --------------------------------------------------------------------
71 
72  public:
73 
77 };
78 
79 template<typename _Tp>
80 class G4EnhancedVecAllocator : public std::allocator<_Tp>
81 {
82  public:
83 
84  template<typename _Tp1>
86 
88 
90  : std::allocator<_Tp>() {;}
91 
92  template<typename _Tp1>
94  : std::allocator<_Tp>() {;}
95 
97 
98  // override allocate / deallocate
99  //
100  void deallocate(_Tp* _Ptr, size_t _Count);
101 #ifdef __IBMCPP__
102  _Tp* allocate(size_t _Count, void * const hint = 0); // IBM AIX
103 #else
104  _Tp* allocate(size_t _Count);
105 #endif
106 };
107 
108 // ------------------------------------------------------------
109 // Inline implementations
110 // ------------------------------------------------------------
111 
112 // ************************************************************
113 // deallocate
114 // ************************************************************
115 //
116 template<typename _Tp>
117 void G4EnhancedVecAllocator<_Tp>::deallocate(_Tp* _Ptr, size_t _Count)
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 }
145 
146 // ************************************************************
147 // allocate
148 // ************************************************************
149 //
150 #ifdef __IBMCPP__
151 template<typename _Tp>
152 _Tp* G4EnhancedVecAllocator<_Tp>::allocate(size_t _Count, void * const hint)
153 #else
154 template<typename _Tp>
156 #endif
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  {
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 
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 }
258 
259 // ************************************************************
260 // operator==
261 // ************************************************************
262 //
263 template<typename _T1, typename _T2>
266 { return true; }
267 
268 // ************************************************************
269 // operator!=
270 // ************************************************************
271 //
272 template<typename _T1, typename _T2>
275 { return false; }
276 
277 #endif
static G4ThreadLocal G4ChunkIndexType * allocStat
_Tp * allocate(size_t _Count)
static G4ThreadLocal G4int totSpace
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
G4EnhancedVecAllocator< _Tp1 > other
static G4ThreadLocal G4int numCat
bool operator==(const G4EnhancedVecAllocator< _T1 > &, const G4EnhancedVecAllocator< _T2 > &)
G4EnhancedVecAllocator(const G4EnhancedVecAllocator< _Tp1 > &)
bool operator!=(const G4EnhancedVecAllocator< _T1 > &, const G4EnhancedVecAllocator< _T2 > &)
G4EnhancedVecAllocator(const G4EnhancedVecAllocator< _Tp > &)
void deallocate(_Tp *_Ptr, size_t _Count)