Rheolef  7.1
an efficient C++ finite element environment
heap_object.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_HEAP_OBJECT_H
2 #define _RHEOLEF_HEAP_OBJECT_H
23 
24 #include "rheolef/compiler.h"
25 namespace rheolef {
26 
27 template<class T>
28 class heap_object {
29 public:
30  typedef size_t size_type;
31  heap_object(size_type sizeof_bucket = sizeof(T));
32  ~heap_object();
33  T* new_bucket() const;
34 #ifdef TO_CLEAN
35  T* malloc() const { return new_bucket(); } // for boost::object_pool compatibility
36 #endif // TO_CLEAN
37  size_type size() const { return _counter; }
38  void reinitialize (size_type sizeof_bucket = sizeof(T));
39  void clear ();
40 protected:
41  mutable std::list<std::vector<char> > _heap;
46  static const size_type _heap_block_size_init = 10;
47 };
48 template<class T>
49 inline
51  : _heap(),
52  _heap_block_size(_heap_block_size_init),
53  _heap_block_last_free(0),
54  _sizeof_bucket(sz),
55  _counter(0)
56 {
57  _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
58 }
59 template<class T>
60 inline
61 void
63 {
64  clear();
65  _heap_block_size = heap_object<T>::_heap_block_size_init;
66  _heap_block_last_free = 0;
67  _sizeof_bucket = sz;
68  _counter = 0;
69  _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
70 }
71 template<class T>
72 inline
73 T*
75 {
76  if (_heap_block_last_free == _heap_block_size) {
77  _heap_block_size *= 2;
78  _heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
79  _heap_block_last_free = 0;
80  }
81  std::vector<char>& block = *(_heap.begin());
82  char* p = &block [_heap_block_last_free*_sizeof_bucket];
83  _heap_block_last_free++;
84  new ((void*) p) T(); // call default T constructor at p
85  _counter++;
86  return (T*)p;
87 }
88 template<class T>
89 inline
91 {
92  clear();
93 }
94 template<class T>
95 void
97 {
98  size_type n = _heap_block_size_init;
99  for (std::list<std::vector<char> >::reverse_iterator i = _heap.rbegin();
100  _counter != 0 && i != _heap.rend(); i++, n *= 2) {
101  std::vector<char>& block = *i;
102  char* p = &(block[0]);
103  for (size_type c = 0; _counter != 0 && c < n; c++, p += _sizeof_bucket) {
104  ((T*)p)->~T();
105  _counter--;
106  }
107  }
108  _heap.erase(_heap.begin(), _heap.end());
109 }
110 }// namespace rheolef
111 #endif // _RHEOLEF_HEAP_OBJECT_H
field::size_type size_type
Definition: branch.cc:425
std::list< std::vector< char > > _heap
Definition: heap_object.h:41
heap_object(size_type sizeof_bucket=sizeof(T))
Definition: heap_object.h:50
size_type size() const
Definition: heap_object.h:37
static const size_type _heap_block_size_init
Definition: heap_object.h:46
void reinitialize(size_type sizeof_bucket=sizeof(T))
Definition: heap_object.h:62
size_type _sizeof_bucket
Definition: heap_object.h:44
T * new_bucket() const
Definition: heap_object.h:74
size_type _heap_block_last_free
Definition: heap_object.h:43
size_type _heap_block_size
Definition: heap_object.h:42
Expr1::float_type T
Definition: field_expr.h:261
This file is part of Rheolef.
Definition: sphere.icc:25