Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4CacheDetails.hh
이 파일의 문서화 페이지로 가기
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 // $Id$
27 //
28 // ---------------------------------------------------------------
29 // GEANT 4 class header file
30 //
31 // Class Description:
32 //
33 // The classes contained in this header files are used by
34 // G4Cache to store a TLS instance of the cached object.
35 // These classes should not be used by client code, but
36 // are used by one of the G4Cache classes.
37 //
38 // G4Cache is a container of the cached value.
39 // Not memory efficient, but CPU efficient (constant time access)
40 // A different version with a map instead of a vector should be
41 // memory efficient and less CPU efficient (log-time access).
42 // If really a lot of these objects are used
43 // we may want to consider the map version to save some memory
44 //
45 // These are simplified "split-classes" without any
46 // copy-from-master logic. Each cached object is associated
47 // a unique identified (an integer), that references an instance
48 // of the cached value (of template type VALTYPE) in a
49 // static TLS data structure
50 //
51 // In case the cache is used for a cached object the object class
52 // has to provide a default constructor. Alternatively pointers to
53 // objects can be stored in the cache and this limitation is removed
54 // but explicit handling of memory (new/delete) of cached object becomes
55 // client responsibility.
56 
57 // History:
58 // 21 Oct 2013: A. Dotti - First implementation
59 //
60 // Todos: - Understand if map based class can be more efficent than
61 // vector one
62 // - Evaluate use of specialized allocator for TLS "new"
63 // ------------------------------------------------------------
64 
65 #ifndef G4CacheDetails_hh
66 #define G4CacheDetails_hh
67 
68 #include <vector>
69 #include "G4Threading.hh"
70 #include "globals.hh"
71 
72 #ifdef g4cdebug
73  #include <iostream>
74  #include <sstream>
75  using std::cout;
76  using std::endl;
77 #endif
78 
79 // A TLS storage for a cache of type VALTYPE
80 //
81 template<class VALTYPE> class G4CacheReference
82 {
83  public:
84 
85  inline void Initialize( unsigned int id );
86  // Initliaze TLS storage
87 
88  inline void Destroy( unsigned int id , G4bool last);
89  // Cleanup TLS storage for instance id. If last==true
90  // destroy and cleanup object container
91 
92  inline VALTYPE& GetCache(unsigned int id) const;
93  // Returns cached value for instance id
94 
95  private:
96 
97  typedef std::vector<VALTYPE*> cache_container;
98  // Implementation detail: the cached object is stored as a
99  // pointer. Object is stored as a pointer to avoid too large
100  // std::vector in case of stored objects and allow use of
101  // specialized allocators
102 
103  static cache_container*& cache();
104 };
105 
106 // Template specialization for pointers
107 // Note: Objects are not owned by cache, for this version of the cache
108 // the explicit new/delete of the cached object
109 //
110 template<class VALTYPE> class G4CacheReference<VALTYPE*>
111 {
112  public:
113  inline void Initialize( unsigned int id );
114 
115  inline void Destroy( unsigned int id , G4bool last);
116 
117  inline VALTYPE*& GetCache(unsigned int id) const;
118 
119  private:
120  typedef std::vector<VALTYPE*> cache_container;
121  static cache_container*& cache();
122 };
123 
124 // Template specialization for probably the most used case: double
125 // Be more efficient avoiding unnecessary "new/delete"
126 //
127 template<> class G4CacheReference<G4double>
128 {
129  public:
130 
131  inline void Initialize( unsigned int id );
132 
133  inline void Destroy( unsigned int id , G4bool last);
134 
135  inline G4double& GetCache(unsigned int id) const;
136 
137  private:
138 
139  typedef std::vector<G4double> cache_container;
140  static G4GLOB_DLL cache_container*& cache();
141 };
142 
143 
144 //================================
145 // Implementation details follow
146 //================================
147 
148 //======= Implementation: G4CacheReference<V>
149 //===========================================
150 
151 template<class V>
152 void G4CacheReference<V>::Initialize( unsigned int id )
153 {
154 #ifdef g4cdebug
155  if ( cache() == 0 )
156  cout<<"Generic template"<<endl;
157 #endif
158 
159  // Create cache container
160  if ( cache() == 0 )
161  cache() = new cache_container;
162  if ( cache()->size() <= id )
163  cache()->resize(id+1,static_cast<V*>(0));
164  if ( (*cache())[id] == 0 )
165  (*cache())[id]=new V;
166 }
167 
168 template<class V>
169 void G4CacheReference<V>::Destroy( unsigned int id, G4bool last )
170 {
171  if ( cache() )
172  {
173 #ifdef g4cdebug
174  cout<<"Destroying element"<<id<<" is last?"<<last<<endl;
175 #endif
176  if ( cache()->size() < id )
177  {
179  msg << "Internal fatal error. Invalid G4Cache size (requested id: "
180  << id << " but cache has size: "<< cache()->size();
181  msg << " Possibly client created G4Cache object in a thread and"
182  << " tried to delete it from another thread!";
183  G4Exception("G4CacheReference<V>::Destroy", "Cache001",
184  FatalException, msg);
185  return;
186  }
187  if ( cache()->size() > id && (*cache())[id] )
188  {
189  delete (*cache())[id];
190  (*cache())[id]=0;
191  }
192  if (last)
193  {
194  delete cache();
195  cache() = 0;
196  }
197  }
198 }
199 
200 template<class V>
201 V& G4CacheReference<V>::GetCache( unsigned int id ) const
202 {
203  return *(cache()->operator[](id));
204 }
205 
206 template<class V>
209 {
210  G4ThreadLocalStatic cache_container* _instance = nullptr;
211  return _instance;
212 }
213 
214 //======= Implementation: G4CacheReference<V*>
215 //============================================
216 
217 template<class V>
218 void G4CacheReference<V*>::Initialize( unsigned int id )
219 {
220 #ifdef g4cdebug
221  if ( cache() == 0 )
222  cout<<"Pointer template"<<endl;
223 #endif
224  if ( cache() == 0 )
225  cache() = new cache_container;
226  if ( cache()->size() <= id )
227  cache()->resize(id+1,static_cast<V*>(0));
228 }
229 
230 template<class V>
231 inline void G4CacheReference<V*>::Destroy( unsigned int id , G4bool last )
232 {
233  if ( cache() )
234  {
235 #ifdef g4cdebug
236  cout << "Destroying element" << id << " is last?" << last
237  << "-Pointer template specialization-" << endl;
238 #endif
239  if ( cache()->size() < id )
240  {
242  msg << "Internal fatal error. Invalid G4Cache size (requested id: "
243  << id << " but cache has size: " << cache()->size();
244  msg << " Possibly client created G4Cache object in a thread and"
245  << " tried to delete it from another thread!";
246  G4Exception("G4CacheReference<V*>::Destroy", "Cache001",
247  FatalException, msg);
248  return;
249  }
250  if ( cache()->size() > id && (*cache())[id] )
251  {
252  // Ownership is for client
253  // delete (*cache)[id];
254  (*cache())[id]=0;
255  }
256  if (last )
257  {
258  delete cache();
259  cache() = 0;
260  }
261  }
262 }
263 
264 template<class V>
265 V*& G4CacheReference<V*>::GetCache(unsigned int id) const
266 {
267  return (cache()->operator[](id));
268 }
269 
270 template<class V>
273 {
274  G4ThreadLocalStatic cache_container* _instance = nullptr;
275  return _instance;
276 }
277 
278 //======= Implementation: G4CacheReference<double>
279 //============================================
280 
282 {
283 #ifdef g4cdebug
284  cout<<"Specialized template for G4double"<<endl;
285 #endif
286  if ( cache() == 0 )
287  cache() = new cache_container;
288  if ( cache()->size() <= id )
289  cache()->resize(id+1,static_cast<G4double>(0));
290 }
291 
292 #ifdef g4cdebug
293 void G4CacheReference<G4double>::Destroy( unsigned int id , G4bool last)
294 #else
295 void G4CacheReference<G4double>::Destroy( unsigned int /*id*/ , G4bool last)
296 #endif
297 {
298  if ( cache() && last )
299  {
300 #ifdef g4cdebug
301  cout << "Destroying element" << id << " is last?" << last
302  << "-Pointer template specialization-" << endl;
303 #endif
304  delete cache();
305  cache() = 0;
306  }
307 }
308 
310 {
311  return cache()->operator[](id);
312 }
313 
314 #endif
void Initialize(unsigned int id)
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
void Initialize()
Definition: errprop.cc:101
#define G4ThreadLocalStatic
Definition: tls.hh:68
static cache_container *& cache()
std::vector< G4double > cache_container
VALTYPE & GetCache(unsigned int id) const
double G4double
Definition: G4Types.hh:76
bool G4bool
Definition: G4Types.hh:79
#define G4GLOB_DLL
Definition: G4Types.hh:64
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
std::vector< VALTYPE * > cache_container
void Destroy(unsigned int id, G4bool last)
std::vector< VALTYPE * > cache_container