dune-fem  2.4.1-rc
arrayallocator.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_ARRAYALLOCATOR_HH
2 #define DUNE_FEM_ARRAYALLOCATOR_HH
3 
4 #include <cstdlib>
5 
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
14  template< class Traits >
16  : public BartonNackmanInterface< ArrayAllocatorInterface< Traits >,
17  typename Traits :: ArrayAllocatorType >
18  {
21  < ThisType, typename Traits :: ArrayAllocatorType >
22  BaseType;
23 
24  public:
25  typedef typename Traits :: ArrayAllocatorType ArrayAllocatorType;
26 
27  typedef ThisType ArrayAllocatorInterfaceType;
28 
29  typedef typename Traits :: ElementType ElementType;
30  typedef typename Traits :: ElementPtrType ElementPtrType;
31 
32  protected:
33  using BaseType :: asImp;
34 
35  public:
36  inline void allocate ( unsigned int size,
37  ElementPtrType &array ) const
38  {
39  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().allocate( size, array ) );
40  }
41 
42  inline void free ( ElementPtrType &array ) const
43  {
44  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().free( array ) );
45  }
46 
47  inline void reallocate ( unsigned int oldSize,
48  unsigned int newSize,
49  ElementPtrType &array ) const
50  {
51  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION
52  ( asImp().reallocate( oldSize, newSize, array ) );
53  }
54 
55  inline void reserve ( unsigned int newSize,
56  ElementPtrType &array ) const
57  {
58  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION
59  ( asImp().reserve( newSize, array ) );
60  }
61  };
62 
63 
64  template< class Traits >
66  : public ArrayAllocatorInterface< Traits >
67  {
70 
71  public:
72  using BaseType :: allocate;
73  using BaseType :: free;
74 
75  public:
76  typedef typename Traits :: ElementType ElementType;
77  typedef typename Traits :: ElementPtrType ElementPtrType;
78 
79  public:
80  inline void reallocate ( unsigned int oldSize,
81  unsigned int newSize,
82  ElementPtrType &array ) const
83  {
84  ElementPtrType newArray;
85  allocate( newSize, newArray );
86 
87  const unsigned int copySize = std :: min( oldSize, newSize );
88  for( unsigned int i = 0; i < copySize; ++i )
89  newArray[ i ] = array[ i ];
90 
91  free( array );
92  array = newArray;
93  }
94 
95  // by default, do nothing
96  inline void reserve ( unsigned int newSize,
97  ElementPtrType &array ) const
98  {}
99  };
100 
101 
102  template< class Element >
104 
105  template< class Element >
107 
108 
109  // Choose a default array allocator
110  #ifndef USE_CARRAYALLOCATOR
111  template< class Element >
113  : public StandardArrayAllocator< Element >
114  {};
115  #else
116  template< class Element >
118  : public CArrayAllocator< Element >
119  {};
120  #endif
121 
122 
123  template< class Element >
125  {
126  typedef Element ElementType;
127  typedef Element *ElementPtrType;
128 
130  };
131 
132 
133  template< class Element >
135  : public ArrayAllocatorDefault< StandardArrayAllocatorTraits< Element > >
136  {
137  typedef StandardArrayAllocator< Element > ThisType;
139  typedef ArrayAllocatorDefault< Traits > BaseType;
140 
141  public:
144 
145  public:
146  inline void allocate ( unsigned int size,
147  ElementPtrType &array ) const
148  {
149  if( size > 0 )
150  {
151  array = new ElementType[ size ];
152  assert( array != 0 );
153  }
154  else
155  array = 0;
156  }
157 
158  inline void free ( ElementPtrType &array ) const
159  {
160  if( array != 0 )
161  {
162  delete[]( array );
163  array = 0;
164  }
165  }
166  };
167 
168 
169  template< class Element >
171  {
172  typedef Element ElementType;
173 
174  typedef Element *ElementPtrType;
175 
177  };
178 
179 
180  template< class Element >
181  class CArrayAllocator
182  : public ArrayAllocatorDefault< CArrayAllocatorTraits< Element > >
183  {
184  typedef CArrayAllocator< Element > ThisType;
185  typedef CArrayAllocatorTraits< Element > Traits;
186  typedef ArrayAllocatorDefault< Traits > BaseType;
187 
188  public:
189  typedef typename Traits :: Element ElementType;
191 
192  public:
193  inline void allocate ( unsigned int size,
194  ElementPtrType &array ) const
195  {
196  if( size > 0 )
197  {
198  array = (ElementPtrType)malloc( size * sizeof( ElementType ) );
199  assert( array != 0 );
200  } else
201  array = 0;
202  }
203 
204  inline void free ( ElementPtrType &array ) const
205  {
206  if( array != 0 )
207  {
208  std :: free( array );
209  array = 0;
210  }
211  }
212 
213  inline void reallocate ( unsigned int oldSize,
214  unsigned int newSize,
215  ElementPtrType &array ) const
216  {
217  if( newSize == 0 )
218  {
219  std :: free( array );
220  array = 0;
221  }
222  else
223  array = (ElementPtrType)realloc( array, newSize * sizeof( ElementType ) );
224  }
225  };
226 
227 
228 #if 0
229  template< class Element, template< class > class WrappedArrayAllocator >
230  class ArrayOverAllocator;
231 
232 
233  template< class Element, template< class > class WrappedArrayAllocator >
234  class ArrayOverAllocatorElementPointer
235  {
236  typedef ArrayOverAllocatorElementPointer< Element, WrappedArrayAllocator >
237  ThisType;
238 
239  friend class ArrayOverAllocator< Element, WrappedArrayAllocator >;
240 
241  public:
242  typedef Element ElementType;
243 
244  typedef WrappedArrayAllocator< ElementType > WrappedArrayAllocatorType;
245 
246  typedef typename WrappedArrayAllocatorType :: ElementPtrType ElementPtrType;
247 
248  protected:
249  ElementPtrType ptr_;
250  unsigned int size_;
251 
252  public:
253  inline ArrayOverAllocatorElementPointer ()
254  : ptr_( 0 ),
255  size_( 0 )
256  {}
257 
258  inline ArrayOverAllocatorElementPointer ( const ElementPtrType ptr,
259  const unsigned int size )
260  : ptr_( ptr ),
261  size_( size )
262  {}
263 
264  inline ArrayOverAllocatorElementPointer ( const ThisType &other )
265  : ptr_( other.ptr_ ),
266  size_( other.size_ )
267  {}
268 
269  inline ThisType &operator= ( const ThisType &other )
270  {
271  ptr_ = other.ptr_;
272  size_ = other.size_;
273  }
274 
275  inline operator const ElementType * () const
276  {
277  return (ElementType *)ptr_;
278  }
279 
280  inline operator ElementType * () const
281  {
282  return (ElementType *)ptr_;
283  }
284 
285  inline ElementType &operator* () const
286  {
287  return *ptr_;
288  }
289 
290  inline ElementType &operator[] ( const unsigned int index ) const
291  {
292  assert( index < size_ );
293  return ptr_[ index ];
294  }
295  };
296 
297 
298  template< class Element, template< class > class WrappedArrayAllocator >
299  struct ArrayOverAllocatorTraits
300  {
301  typedef Element ElementType;
302 
303  typedef WrappedArrayAllocator< ElementType > WrappedArrayAllocatorType;
304 
305  typedef ArrayOverAllocatorElementPointer
306  < ElementType, WrappedArrayAllocator >
308 
309  typedef ArrayOverAllocator< ElementType, WrappedArrayAllocator >
311  };
312 
313 
314  template< class Element, template< class > class WrappedArrayAllocator >
315  class ArrayOverAllocator
316  : public ArrayAllocatorDefault
317  < ArrayOverAllocatorTraits< Element, WrappedArrayAllocator > >
318  {
319  typedef ArrayOverAllocator< Element, WrappedArrayAllocator > ThisType;
320  typedef ArrayOverAllocatorTraits< Element, WrappedArrayAllocator > Traits;
322 
323  public:
324  typedef typename Traits :: ElementType ElementType;
325 
326  typedef typename Traits :: ElementPtrType ElementPtrType;
327 
328  typedef typename Traits :: WrappedArrayAllocatorType
329  WrappedArrayAllocatorType;
330 
331  protected:
332  WrappedArrayAllocatorType allocator_;
333  unsigned int memFactor_;
334 
335  public:
336  inline explicit ArrayOverAllocator ( const unsigned int memFactor = 1152 )
337  : allocator_(),
338  memFactor_( memFactor )
339  {}
340 
341  inline explicit ArrayOverAllocator ( const double memFactor )
342  : allocator_(),
343  memFactor_( (int)(memFactor * 1024) )
344  {}
345 
346 
347  inline explicit
348  ArrayOverAllocator ( const WrappedArrayAllocatorType &allocator,
349  const unsigned int memFactor = 1152 )
350  : allocator_( allocator ),
351  memFactor_( memFactor )
352  {}
353 
354  inline ArrayOverAllocator ( const WrappedArrayAllocatorType &allocator,
355  const double memFactor )
356  : allocator_( allocator ),
357  memFactor_( (int)(memFactor * 1024) )
358  {}
359 
360  inline ArrayOverAllocator ( const ThisType &other )
361  : allocator_( other.allocator_ ),
362  memFactor_( other.memFactor_ )
363  {}
364 
365  inline ThisType &operator= ( const ThisType &other )
366  {
367  allocator_ = other.allocator_;
368  memFactor_ = other.memFactor_;
369  }
370 
371  inline void allocate ( unsigned int size,
372  ElementPtrType &array ) const
373  {
374  array.size_ = (size * memFactor_) / 1024;
375  allocator_.allocate( array.size_, array.ptr_ );
376  }
377 
378  inline void free ( ElementPtrType &array ) const
379  {
380  allocator_.free( array.ptr_ );
381  array.size_ = 0;
382  }
383 
384  inline void reallocate ( unsigned int oldSize,
385  unsigned int newSize,
386  ElementPtrType &array ) const
387  {
388  const unsigned int newAllocSize = (newSize * memFactor_) / 1024;
389  if( array.size_ < newSize )
390  reserve( newAllocSize, array );
391  }
392 
393  inline void reserve ( unsigned int newSize,
394  ElementPtrType &array ) const
395  {
396  if( newSize > array.size_ )
397  {
398  allocator_.reallocate( array.size_, newSize, array.ptr_ );
399  array.size_ = newSize;
400  }
401  }
402  };
403 
404 
405  template< class Element >
406  class DefaultArrayOverAllocator
407  : public ArrayOverAllocator< Element, DefaultArrayAllocator >
408  {};
409 #endif
410 
411  } // namespace Fem
412 
413 } // namespace Dune
414 
415 #endif // #ifndef DUNE_FEM_ARRAYALLOCATOR_HH
Element * ElementPtrType
Definition: arrayallocator.hh:127
static constexpr T min(T a)
Definition: utility.hh:81
void free(ElementPtrType &array) const
Definition: arrayallocator.hh:204
Element ElementType
Definition: arrayallocator.hh:126
Definition: arrayallocator.hh:65
void free(ElementPtrType &array) const
Definition: arrayallocator.hh:42
void allocate(unsigned int size, ElementPtrType &array) const
Definition: arrayallocator.hh:36
StandardArrayAllocator< ElementType > ArrayAllocatorType
Definition: arrayallocator.hh:129
Traits::ElementType ElementType
Definition: arrayallocator.hh:29
Traits::ElementPtrType ElementPtrType
Definition: arrayallocator.hh:77
void reallocate(unsigned int oldSize, unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:80
Traits::ElementType ElementType
Definition: arrayallocator.hh:76
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
Element ElementType
Definition: arrayallocator.hh:172
Definition: arrayallocator.hh:124
Element * ElementPtrType
Definition: arrayallocator.hh:174
void allocate(unsigned int size, ElementPtrType &array) const
Definition: arrayallocator.hh:193
void reallocate(unsigned int oldSize, unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:47
void reserve(unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:55
Traits::ElementPtrType ElementPtrType
Definition: arrayallocator.hh:190
Traits::ElementPtrType ElementPtrType
Definition: arrayallocator.hh:30
Definition: coordinate.hh:4
Traits::ElementType ElementType
Definition: arrayallocator.hh:142
Double operator*(const Double &a, const Double &b)
Definition: double.hh:495
Definition: arrayallocator.hh:170
Definition: arrayallocator.hh:103
Traits::ArrayAllocatorType ArrayAllocatorType
Definition: arrayallocator.hh:25
void reallocate(unsigned int oldSize, unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:213
void reserve(unsigned int newSize, ElementPtrType &array) const
Definition: arrayallocator.hh:96
void free(ElementPtrType &array) const
Definition: arrayallocator.hh:158
Traits::ElementPtrType ElementPtrType
Definition: arrayallocator.hh:143
void allocate(unsigned int size, ElementPtrType &array) const
Definition: arrayallocator.hh:146
CArrayAllocator< ElementType > ArrayAllocatorType
Definition: arrayallocator.hh:176
ThisType ArrayAllocatorInterfaceType
Definition: arrayallocator.hh:27
Definition: arrayallocator.hh:15
Definition: bartonnackmaninterface.hh:15
Traits::Element ElementType
Definition: arrayallocator.hh:189
Definition: arrayallocator.hh:112
Definition: arrayallocator.hh:106