Warning, file /include/Geant4/G4INCLAllocationPool.hh was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #define INCLXX_IN_GEANT4_MODE 1
0035
0036 #include "globals.hh"
0037
0038
0039
0040
0041
0042
0043
0044
0045 #ifndef G4INCLALLOCATIONPOOL_HH
0046 #define G4INCLALLOCATIONPOOL_HH
0047
0048 #if defined(INCL_USE_ALLOCATION_POOL) || defined(INCLXX_IN_GEANT4_MODE)
0049
0050 #include <stack>
0051 #include <new>
0052 #include <cstddef>
0053
0054 namespace G4INCL {
0055
0056 template<typename T>
0057 class AllocationPool {
0058 public:
0059 static AllocationPool &getInstance() {
0060 if(!theInstance)
0061 theInstance = new AllocationPool<T>;
0062 return *theInstance;
0063 }
0064
0065 T *getObject() {
0066 if(theStack.empty())
0067 return static_cast<T*>(::operator new(sizeof(T)));
0068 else {
0069 T *t = theStack.top();
0070 theStack.pop();
0071 return t;
0072 }
0073 }
0074
0075 void recycleObject(T *t) {
0076 theStack.push(t);
0077 }
0078
0079 void clear() {
0080 while(!theStack.empty()) {
0081 ::operator delete(theStack.top());
0082 theStack.pop();
0083 }
0084 }
0085
0086 protected:
0087 AllocationPool() {}
0088 virtual ~AllocationPool() {
0089 clear();
0090 }
0091
0092 static G4ThreadLocal AllocationPool *theInstance;
0093
0094 std::stack<T*> theStack;
0095
0096 };
0097
0098 template<typename T>
0099 G4ThreadLocal AllocationPool<T> *AllocationPool<T>::theInstance = 0;
0100
0101 }
0102
0103 #define INCL_DECLARE_ALLOCATION_POOL(T) \
0104 public: \
0105 static void *operator new(size_t ) { \
0106 ::G4INCL::AllocationPool<T> &allocator = ::G4INCL::AllocationPool<T>::getInstance(); \
0107 return allocator.getObject(); \
0108 } \
0109 static void operator delete(void *a, size_t ) { \
0110 ::G4INCL::AllocationPool<T> &allocator = ::G4INCL::AllocationPool<T>::getInstance(); \
0111 allocator.recycleObject(static_cast<T *>(a)); \
0112 }
0113
0114 #else
0115 #define INCL_DECLARE_ALLOCATION_POOL(T)
0116 #endif
0117
0118 #endif