dune-alugrid  2.6-git
memory.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALU3DGRIDMEMORY_HH
2 #define DUNE_ALU3DGRIDMEMORY_HH
3 
5 #include <cstdlib>
6 #include <vector>
7 
8 namespace ALUGrid
9 {
10 
11  template< class T, int length >
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 
63 
65  void freeObject (ObjectType * obj);
66 
67  protected:
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>
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>
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>
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)
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>
176  {
177  protected:
178  typedef ObjectImp ObjectType;
181 
183  {
184  static thread_local MemoryPoolType pool;
185  return pool;
186  }
187 
188  public:
189  // default constructor
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
203  {
204  removeObject();
205  }
206 
207  void getObject()
208  {
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 
223  {
224  // decrease reference counter
225  -- ptr();
226 
227  // if reference count is zero free the object
228  if( ! ptr() )
229  {
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 
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:
275  const ReferenceCountedObjectType& ptr() const { alugrid_assert( ptr_ ); return *ptr_; }
276 
278  };
279 
280 } // namespace ALUGrid
281 
282 #endif // #ifndef DUNE_ALU3DGRIDMEMORY_HH
#define alugrid_assert(EX)
Definition: alugrid_assert.hh:20
Definition: alu3dinclude.hh:50
Definition: memory.hh:12
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 * stackObject()
Definition: memory.hh:68
Object ObjectType
Definition: memory.hh:28
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
Definition: memory.hh:136
void operator++()
increase reference count
Definition: memory.hh:159
const unsigned int & refCount() const
Definition: memory.hh:145
ObjectImp ObjectType
Definition: memory.hh:139
const ObjectType & object() const
Definition: memory.hh:170
ObjectType & object()
Definition: memory.hh:171
unsigned int & refCount()
Definition: memory.hh:144
bool operator!() const
return true if object has no references anymore
Definition: memory.hh:165
void reset()
reset status and reference count
Definition: memory.hh:149
ObjectType object_
Definition: memory.hh:142
bool unique() const
return true if there exists more then on reference
Definition: memory.hh:168
void operator--()
decrease reference count
Definition: memory.hh:162
Definition: memory.hh:176
void removeObject()
Definition: memory.hh:222
void assign(const SharedPointer &other)
Definition: memory.hh:213
ReferenceCountedObjectType * ptr_
Definition: memory.hh:277
SharedPointer & operator=(const SharedPointer &other)
Definition: memory.hh:253
SharedPointer(const SharedPointer &other)
Definition: memory.hh:196
void getObject()
Definition: memory.hh:207
void invalidate()
Definition: memory.hh:237
ObjectType & operator*()
Definition: memory.hh:270
SharedPointer()
Definition: memory.hh:190
const ReferenceCountedObjectType & ptr() const
Definition: memory.hh:275
ObjectImp ObjectType
Definition: memory.hh:178
static MemoryPoolType & memoryPool()
Definition: memory.hh:182
ReferenceCountedObjectType & ptr()
Definition: memory.hh:274
~SharedPointer()
Definition: memory.hh:202
ReferenceCountedObject< ObjectType > ReferenceCountedObjectType
Definition: memory.hh:179
bool unique() const
Definition: memory.hh:267
ALUMemoryProvider< ReferenceCountedObjectType > MemoryPoolType
Definition: memory.hh:180
bool operator==(const SharedPointer &other) const
Definition: memory.hh:265