Dune Core Modules (2.9.0)

memory.hh
1#ifndef DUNE_ALU3DGRIDMEMORY_HH
2#define DUNE_ALU3DGRIDMEMORY_HH
3
4#include <dune/alugrid/common/alugrid_assert.hh>
5#include <cstdlib>
6#include <vector>
7
8namespace ALUGrid
9{
10
11 template< class T, int length >
12 class ALUGridFiniteStack;
13
15 template <class Object>
17 {
18 enum { maxStackObjects = 256 };
19 typedef ::ALUGrid::ALUGridFiniteStack< Object *, maxStackObjects > StackType;
20
21 // stack to store object pointers
22 StackType objStack_;
23
24 // return reference to object stack
25 StackType &objStack () { return objStack_; }
26 public:
27 // type of object to be stored
28 typedef Object ObjectType;
29
32 : objStack_()
33 {}
34
37 : objStack_()
38 {}
39
42
44 template <class FactoryType>
45 ObjectType * getObject(const FactoryType &factory, int level);
46
48 template <class FactoryType, class EntityImp>
49 inline ObjectType * getEntityObject(const FactoryType& factory, int level, EntityImp* )
50 {
51 if( objStack().empty() )
52 {
53 return new ObjectType( EntityImp(factory,level) );
54 }
55 else
56 {
57 return stackObject();
58 }
59 }
60
62 ObjectType* getEmptyObject ();
63
65 void freeObject (ObjectType * obj);
66
67 protected:
68 inline ObjectType * stackObject()
69 {
70 // make sure stack is not empty
71 alugrid_assert ( ! objStack().empty() );
72 // finite stack does also return object on pop
73 return objStack().pop();
74 }
75 };
76
77
78 //************************************************************************
79 //
80 // ALUMemoryProvider implementation
81 //
82 //************************************************************************
83 template <class Object> template <class FactoryType>
84 inline typename ALUMemoryProvider<Object>::ObjectType*
86 getObject( const FactoryType &factory, int level )
87 {
88 if( objStack().empty() )
89 {
90 return ( new Object (factory, level) );
91 }
92 else
93 {
94 return stackObject();
95 }
96 }
97
98 template <class Object>
99 inline typename ALUMemoryProvider<Object>::ObjectType *
101 {
102 if( objStack().empty() )
103 {
104 return new Object () ;
105 }
106 else
107 {
108 return stackObject();
109 }
110 }
111
112 template <class Object>
114 {
115 StackType& objStk = objStack();
116 while ( ! objStk.empty() )
117 {
118 ObjectType * obj = objStk.pop();
119 delete obj;
120 }
121 }
122
123 template <class Object>
124 inline void ALUMemoryProvider<Object>::freeObject( Object * obj )
125 {
126 // make sure we operate on the correct thread
127 StackType& stk = objStack();
128 if( stk.full() )
129 delete obj;
130 else
131 stk.push( obj );
132 }
133
134 template <class ObjectImp>
135 class ReferenceCountedObject
136 {
137 protected:
138 // type of object to be reference counted
139 typedef ObjectImp ObjectType;
140
141 // object (e.g. geometry impl or intersection impl)
142 ObjectType object_;
143
144 unsigned int& refCount() { return object_.refCount_; }
145 const unsigned int& refCount() const { return object_.refCount_; }
146
147 public:
149 void reset()
150 {
151 // reset reference counter
152 refCount() = 1;
153
154 // reset status of object
155 object_.invalidate();
156 }
157
159 void operator ++ () { ++ refCount(); }
160
162 void operator -- () { alugrid_assert ( refCount() > 0 ); --refCount(); }
163
165 bool operator ! () const { return refCount() == 0; }
166
168 bool unique () const { return refCount() == 1 ; }
169
170 const ObjectType& object() const { return object_; }
171 ObjectType& object() { return object_; }
172 };
173
174 template <class ObjectImp>
175 class SharedPointer
176 {
177 protected:
178 typedef ObjectImp ObjectType;
179 typedef ReferenceCountedObject< ObjectType > ReferenceCountedObjectType;
180 typedef ALUMemoryProvider< ReferenceCountedObjectType > MemoryPoolType;
181
182 static MemoryPoolType& memoryPool()
183 {
184 static thread_local MemoryPoolType pool;
185 return pool;
186 }
187
188 public:
189 // default constructor
190 SharedPointer()
191 {
192 getObject();
193 }
194
195 // copy contructor making shallow copy
196 SharedPointer( const SharedPointer& other )
197 {
198 assign( other );
199 }
200
201 // destructor clearing pointer
202 ~SharedPointer()
203 {
204 removeObject();
205 }
206
207 void getObject()
208 {
209 ptr_ = memoryPool().getEmptyObject();
210 ptr().reset();
211 }
212
213 void assign( const SharedPointer& other )
214 {
215 // copy pointer
216 ptr_ = other.ptr_;
217
218 // increase reference counter
219 ++ ptr();
220 }
221
222 void removeObject()
223 {
224 // decrease reference counter
225 -- ptr();
226
227 // if reference count is zero free the object
228 if( ! ptr() )
229 {
230 memoryPool().freeObject( ptr_ );
231 }
232
233 // reset pointer
234 ptr_ = nullptr;
235 }
236
237 void invalidate()
238 {
239 // if pointer is unique, invalidate status
240 if( ptr().unique() )
241 {
242 ptr().object().invalidate();
243 }
244 else
245 {
246 // if pointer is used elsewhere remove the pointer
247 // and get new object
248 removeObject();
249 getObject();
250 }
251 }
252
253 SharedPointer& operator = ( const SharedPointer& other )
254 {
255 if( ptr_ != other.ptr_ )
256 {
257 removeObject();
258 assign( other );
259 }
260 return *this;
261 }
262
263 operator bool () const { return bool( ptr_ ); }
264
265 bool operator == (const SharedPointer& other ) const { return ptr_ == other.ptr_; }
266
267 bool unique () const { return ptr().unique(); }
268
269 // dereferencing
270 ObjectType& operator* () { return ptr().object(); }
271 const ObjectType& operator* () const { return ptr().object(); }
272
273 protected:
274 ReferenceCountedObjectType& ptr() { alugrid_assert( ptr_ ); return *ptr_; }
275 const ReferenceCountedObjectType& ptr() const { alugrid_assert( ptr_ ); return *ptr_; }
276
277 ReferenceCountedObjectType* ptr_;
278 };
279
280} // namespace ALUGrid
281
282#endif // #ifndef DUNE_ALU3DGRIDMEMORY_HH
organize the memory management for entitys used by the NeighborIterator
Definition: memory.hh:17
ObjectType * getObject(const FactoryType &factory, int level)
i.e. return pointer to Entity
Definition: memory.hh:86
~ALUMemoryProvider()
call deleteEntity
Definition: memory.hh:113
ALUMemoryProvider(const ALUMemoryProvider &org)
copy constructor
Definition: memory.hh:36
void freeObject(ObjectType *obj)
free, move element to stack, returns NULL
Definition: memory.hh:124
ObjectType * getEmptyObject()
return object, if created default constructor is used
Definition: memory.hh:100
ALUMemoryProvider()
default constructor
Definition: memory.hh:31
ObjectType * getEntityObject(const FactoryType &factory, int level, EntityImp *)
i.e. return pointer to Entity
Definition: memory.hh:49
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:237
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)