source: trunk/source/geometry/volumes/include/G4EnhancedVecAllocator.hh@ 1350

Last change on this file since 1350 was 1337, checked in by garnier, 15 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 9.3 KB
Line 
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,v 1.3 2010/04/23 10:25:22 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
29//
30//
31// ------------------------------------------------------------
32// GEANT 4 class header file
33//
34// Class Description:
35//
36// A class for fast allocation of STL vectors through a static pool.
37// It's meant to be used as alternative allocator for STL vectors.
38
39// ---------------- G4EnhancedVecAllocator ----------------
40//
41// Original author: X.Dong (NorthEastern Univ.), November 2009
42// Reviewed implementation: G.Cosmo (CERN), December 2009
43// ------------------------------------------------------------
44
45#ifndef G4EnhancedVecAllocator_h
46#define G4EnhancedVecAllocator_h 1
47
48#include "G4Types.hh"
49
50// #include <cstdlib>
51
52typedef struct
53{
54 G4int isAllocated;
55 char *address;
56} G4ChunkType;
57
58typedef struct
59{
60 size_t size;
61 G4int totalspace;
62 G4ChunkType *preAllocated;
63} G4ChunkIndexType;
64
65class G4AllocStats
66{
67 // --------------------------------------------------------------------
68 // Utility class, placeholder for global data on allocation.
69 // Initialisation to zero of the data below *must* be added ONCE only
70 // directly in the client code, where this allocator is to be applied
71 // --------------------------------------------------------------------
72
73 public:
74
75 static G4ChunkIndexType * allocStat;
76 static G4int totSpace;
77 static G4int numCat;
78};
79
80template<typename _Tp>
81class G4EnhancedVecAllocator : public std::allocator<_Tp>
82{
83 public:
84
85 template<typename _Tp1>
86 struct rebind { typedef G4EnhancedVecAllocator<_Tp1> other; };
87
88 G4EnhancedVecAllocator() {;}
89
90 G4EnhancedVecAllocator(const G4EnhancedVecAllocator<_Tp>&)
91 : std::allocator<_Tp>() {;}
92
93 template<typename _Tp1>
94 G4EnhancedVecAllocator(const G4EnhancedVecAllocator<_Tp1>&)
95 : std::allocator<_Tp>() {;}
96
97 ~G4EnhancedVecAllocator() {;}
98
99 // override allocate / deallocate
100 //
101 void deallocate(_Tp* _Ptr, size_t _Count);
102 _Tp* allocate(size_t _Count);
103};
104
105// ------------------------------------------------------------
106// Inline implementations
107// ------------------------------------------------------------
108
109// ************************************************************
110// deallocate
111// ************************************************************
112//
113template<typename _Tp>
114void G4EnhancedVecAllocator<_Tp>::deallocate(_Tp* _Ptr, size_t _Count)
115{
116 G4int found = -1;
117 for (register int j = 0 ; j < G4AllocStats::numCat ; j++)
118 {
119 if ( (G4AllocStats::allocStat != 0)
120 && (G4AllocStats::allocStat[j].size == (_Count * sizeof(_Tp))))
121 {
122 found = j;
123 break;
124 }
125 }
126 // assert(found != -1);
127
128 for (register int k = 0; k < G4AllocStats::allocStat[found].totalspace; k++)
129 {
130 if ( ((G4AllocStats::allocStat[found]).preAllocated[k]).address
131 == ((char *) _Ptr))
132 {
133 // assert(((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated==1);
134 ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 0;
135 return;
136 }
137 }
138}
139
140// ************************************************************
141// allocate
142// ************************************************************
143//
144template<typename _Tp>
145_Tp* G4EnhancedVecAllocator<_Tp>::allocate(size_t _Count)
146{
147 size_t totalsize = _Count * sizeof(_Tp);
148
149 G4int found = -1;
150 for (register int j = 0 ; j < G4AllocStats::numCat ; j++)
151 {
152 if ( (G4AllocStats::allocStat != 0)
153 && (G4AllocStats::allocStat[j].size == totalsize) )
154 {
155 found = j;
156 break;
157 }
158 }
159
160 if (found == -1) // Find the new size
161 {
162 G4AllocStats::numCat++;
163 if (G4AllocStats::numCat > G4AllocStats::totSpace)
164 {
165 G4AllocStats::totSpace = G4AllocStats::totSpace + 128;
166 // heuristic parameter for different sizes
167
168 G4AllocStats::allocStat =
169 (G4ChunkIndexType *) realloc(G4AllocStats::allocStat,
170 sizeof(G4ChunkIndexType) * G4AllocStats::totSpace);
171 // This value must be different than zero; otherwise means
172 // failure in allocating extra space !
173 // assert(G4AllocStats::allocStat != 0);
174 }
175
176 G4AllocStats::allocStat[G4AllocStats::numCat-1].size = totalsize;
177 G4AllocStats::allocStat[G4AllocStats::numCat-1].totalspace = 0;
178 G4AllocStats::allocStat[G4AllocStats::numCat-1].preAllocated = 0;
179
180 found = G4AllocStats::numCat - 1;
181
182 G4AllocStats::allocStat[found].totalspace = 512;
183 // heuristic for the number of STL vector instances
184
185 G4AllocStats::allocStat[found].preAllocated =
186 (G4ChunkType *) realloc(G4AllocStats::allocStat[found].preAllocated,
187 sizeof(G4ChunkType) * G4AllocStats::allocStat[found].totalspace);
188 // This value must be different than zero; otherwise means
189 // failure in allocating extra space for pointers !
190 // assert(G4AllocStats::allocStat[found].preAllocated != 0);
191
192 char *newSpace1 = (char *) malloc(totalsize * 512);
193 // This pointer must be different than zero; otherwise means
194 // failure in allocating extra space for instances !
195 // assert(newSpace1 != 0);
196
197 for (register int k = 0; k < 512 ; k++)
198 {
199 ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 0;
200 ((G4AllocStats::allocStat[found]).preAllocated[k]).address =
201 newSpace1+totalsize*k;
202 }
203
204 ((G4AllocStats::allocStat[found]).preAllocated[0]).isAllocated = 1;
205 return (_Tp*)(((G4AllocStats::allocStat[found]).preAllocated[0]).address);
206 }
207
208 // assert(G4AllocStats::allocStat[found].size == totalsize);
209
210 for (register int k = 0; k < G4AllocStats::allocStat[found].totalspace; k++)
211 {
212 if (((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated == 0)
213 {
214 ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 1;
215 return (_Tp*)(((G4AllocStats::allocStat[found]).preAllocated[k]).address);
216 }
217 }
218
219 G4int originalchunknumber = G4AllocStats::allocStat[found].totalspace;
220
221 G4AllocStats::allocStat[found].totalspace = // heuristic for the number
222 G4AllocStats::allocStat[found].totalspace+512; // of STL vector instances
223
224 G4AllocStats::allocStat[found].preAllocated =
225 (G4ChunkType *) realloc(G4AllocStats::allocStat[found].preAllocated,
226 sizeof(G4ChunkType) * G4AllocStats::allocStat[found].totalspace);
227 // This value must be different than zero; otherwise means
228 // failure in allocating extra space for pointers !
229 // assert(G4AllocStats::allocStat[found].preAllocated != 0);
230
231 char *newSpace = (char *) malloc(totalsize * 512);
232 // This pointer must be different than zero; otherwise means
233 // failure in allocating extra space for instances !
234 // assert(newSpace != 0);
235
236 for (register int k = 0; k < 512 ; k++)
237 {
238 ((G4AllocStats::allocStat[found]).
239 preAllocated[originalchunknumber + k]).isAllocated= 0;
240 ((G4AllocStats::allocStat[found]).
241 preAllocated[originalchunknumber + k]).address= newSpace+totalsize*k;
242 }
243
244 ((G4AllocStats::allocStat[found]).preAllocated[originalchunknumber])
245 .isAllocated = 1;
246
247 return (_Tp*)(((G4AllocStats::allocStat[found]).
248 preAllocated[originalchunknumber]).address);
249}
250
251// ************************************************************
252// operator==
253// ************************************************************
254//
255template<typename _T1, typename _T2>
256inline bool operator==(const G4EnhancedVecAllocator<_T1>&,
257 const G4EnhancedVecAllocator<_T2>&)
258{ return true; }
259
260// ************************************************************
261// operator!=
262// ************************************************************
263//
264template<typename _T1, typename _T2>
265inline bool operator!=(const G4EnhancedVecAllocator<_T1>&,
266 const G4EnhancedVecAllocator<_T2>&)
267{ return false; }
268
269#endif
Note: See TracBrowser for help on using the repository browser.