Rheolef  7.1
an efficient C++ finite element environment
heap_allocator.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_HEAP_ALLOCATOR_H
2 #define _RHEOLEF_HEAP_ALLOCATOR_H
3 
24 #include <memory>
25 #include <limits>
26 #include "rheolef/compiler.h"
27 #include "rheolef/pretty_name.h"
28 
29 namespace rheolef {
30 
31 /*Class:man
32 NAME: heap_allocator - heap-based allocator
33 DESCRIPTION:
34  Heap allocators are generally used when there is a lot of allocation and deallocation
35  of small objects.
36  For instance, this is often the case when dealing with @code{std::list} and @code{std::map}.
37 
38  Heap-based allocator is conform to the STL specification of allocators.
39  It does not "free" the memory until the heap is destroyed.
40 
41  This allocator handles an a priori unlimited area of memory: a sequence
42  of growing chunks are allocated.
43  For a limited memory handler in the same spirit, see "stack_allocator"(9).
44 EXAMPLE:
45  @example
46  typedef map <size_t, double, less<size_t>, heap_allocator<pair<size_t,double> > > map_type;
47  map_type a;
48  a.insert (make_pair (0, 3.14));
49  a.insert (make_pair (1, 1.17));
50  for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) @{
51  cout << (*iter).first << " " << (*iter).second << endl;
52  @}
53  @end example
54 SEE ALSO: "stack_allocator"(9).
55 AUTHORS:
56  LJK-IMAG, 38041 Grenoble cedex 9, France
57  | Pierre.Saramito@imag.fr
58 DATE: 15 december 2010
59 End:
60 */
61 
62 #define _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
63 #ifdef _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
64 template<class T> using heap_allocator = std::allocator<T>;
65 #else // _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
66 
67 //<verbatim:
68 template <typename T>
70 protected:
71  struct handler_type; // forward declaration:
72 public:
73 
74 // typedefs:
75 
76  typedef size_t size_type;
77  typedef std::ptrdiff_t difference_type;
78  typedef T* pointer;
79  typedef const T* const_pointer;
80  typedef T& reference;
81  typedef const T& const_reference;
82  typedef T value_type;
83 
84 // constructors:
85 
86  heap_allocator() throw()
87  : handler (new handler_type)
88  {
89  }
90  heap_allocator (const heap_allocator& ha) throw()
91  : handler (ha.handler)
92  {
94  }
95  template <typename U>
96  heap_allocator (const heap_allocator<U>& ha) throw()
97  : handler ((typename heap_allocator<T>::handler_type*)(ha.handler))
98  {
100  }
101  ~heap_allocator() throw()
102  {
103  check_macro (handler != NULL, "unexpected null mem_info");
104  if (--handler->reference_count == 0) delete handler;
105  }
106  // Rebind to allocators of other types
107  template <typename U>
108  struct rebind {
110  };
111 
112 // assignment:
113 
115  {
116  handler = ha.handler;
118  return *this;
119  }
120 
121 // utility functions:
122 
123  pointer address (reference r) const { return &r; }
124  const_pointer address (const_reference c) const { return &c; }
125  size_type max_size() const { return std::numeric_limits<size_t>::max() / sizeof(T); }
126 
127 // in-place construction/destruction
128 
130  {
131  // placement new operator:
132  new( reinterpret_cast<void*>(p) ) T(c);
133  }
134  // C++ 2011: default construct a value of type T at the location referenced by p
135  void construct (pointer p) { new ( reinterpret_cast<void*>(p) ) T(); }
136 
138  {
139  // call destructor directly:
140  (p)->~T();
141  }
142 
143 // allocate raw memory
144 
145  pointer allocate (size_type n, const void* = NULL)
146  {
147  return pointer (handler->raw_allocate (n*sizeof(T)));
148  }
150  {
151  // No need to free heap memory
152  }
153  const handler_type* get_handler() const {
154  return handler;
155  }
156 
157 // data:
158 
159 protected:
161  template <typename U> friend class heap_allocator;
162 };
163 //>verbatim:
164 
165 // Comparison
166 template <typename T1>
167 bool operator== (const heap_allocator<T1>& lhs, const heap_allocator<T1>& rhs) throw()
168 {
169  return lhs.get_handler() == rhs.get_handler();
170 }
171 template <typename T1>
172 bool operator!= (const heap_allocator<T1>& lhs, const heap_allocator<T1>& rhs) throw()
173 {
174  return lhs.get_handler() != rhs.get_handler();
175 }
176 
177 // ==========================================================================
178 // heap_allocation::handler class implementation
179 // ==========================================================================
180 template<class T>
182 
183 // cstors:
184  handler_type ();
185  ~handler_type();
186 // modifier:
187  unsigned char* raw_allocate (size_type size);
188 // data:
189  std::list<std::vector<unsigned char> > heap;
193  static const size_type heap_block_size_init = 512*sizeof(T);
194 };
195 template<class T>
196 inline
198  : heap (),
199  heap_block_size (heap_block_size_init),
200  heap_block_last_free (0),
201  reference_count (1)
202 {
203  heap.push_front (std::vector<unsigned char>(heap_block_size));
204 }
205 template<class T>
206 inline
207 unsigned char*
209 {
210  if (heap_block_last_free + size > heap_block_size) {
211  heap_block_size = std::max (size, 2*heap_block_size);
212  heap.push_front (std::vector<unsigned char>(heap_block_size));
213  heap_block_last_free = 0;
214  }
215  std::vector<unsigned char>& block = *(heap.begin());
216  unsigned char* p = &(block [heap_block_last_free]);
217  heap_block_last_free += size;
218  return p;
219 }
220 template<class T>
221 inline
223 {
224  heap.erase (heap.begin(), heap.end());
225 }
226 
227 #endif // _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
228 
229 }// namespace rheolef
230 #endif // _RHEOLEF_HEAP_ALLOCATOR_H
rheolef::heap_allocator::deallocate
void deallocate(pointer p, size_type n)
Definition: heap_allocator.h:149
rheolef::heap_allocator::handler_type::heap_block_size
size_type heap_block_size
Definition: heap_allocator.h:190
rheolef::heap_allocator::get_handler
const handler_type * get_handler() const
Definition: heap_allocator.h:153
mkgeo_ball.n
int n
Definition: mkgeo_ball.sh:150
rheolef::heap_allocator::size_type
size_t size_type
Definition: heap_allocator.h:71
rheolef::operator!=
bool operator!=(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:172
rheolef::heap_allocator::heap_allocator
heap_allocator(const heap_allocator< U > &ha)
Definition: heap_allocator.h:96
check_macro
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
rheolef::heap_allocator::heap_allocator
heap_allocator(const heap_allocator &ha)
Definition: heap_allocator.h:90
rheolef::heap_allocator::reference
T & reference
Definition: heap_allocator.h:80
rheolef::heap_allocator::rebind
Definition: heap_allocator.h:108
rheolef::heap_allocator::pointer
T * pointer
Definition: heap_allocator.h:78
rheolef::heap_allocator::address
pointer address(reference r) const
Definition: heap_allocator.h:123
rheolef::heap_allocator::handler_type::raw_allocate
unsigned char * raw_allocate(size_type size)
Definition: heap_allocator.h:208
rheolef::heap_allocator::handler_type::~handler_type
~handler_type()
Definition: heap_allocator.h:222
rheolef::heap_allocator::rebind::other
heap_allocator< U > other
Definition: heap_allocator.h:109
rheolef::operator==
bool operator==(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:167
p
Definition: sphere.icc:25
rheolef::heap_allocator::handler_type::reference_count
size_type reference_count
Definition: heap_allocator.h:192
rheolef::heap_allocator::handler_type
Definition: heap_allocator.h:181
rheolef::heap_allocator::allocate
pointer allocate(size_type n, const void *=NULL)
Definition: heap_allocator.h:145
rheolef::heap_allocator::difference_type
std::ptrdiff_t difference_type
Definition: heap_allocator.h:77
rheolef::heap_allocator::const_pointer
const T * const_pointer
Definition: heap_allocator.h:79
rheolef::heap_allocator::heap_allocator
heap_allocator()
Definition: heap_allocator.h:86
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::heap_allocator::handler_type::heap_block_last_free
size_type heap_block_last_free
Definition: heap_allocator.h:191
rheolef::heap_allocator::handler_type::handler_type
handler_type()
Definition: heap_allocator.h:197
rheolef::heap_allocator::max_size
size_type max_size() const
Definition: heap_allocator.h:125
rheolef::heap_allocator::address
const_pointer address(const_reference c) const
Definition: heap_allocator.h:124
rheolef::heap_allocator::construct
void construct(pointer p, const_reference c)
Definition: heap_allocator.h:129
rheolef::heap_allocator::operator=
heap_allocator & operator=(const heap_allocator &ha)
Definition: heap_allocator.h:114
rheolef::heap_allocator::destroy
void destroy(pointer p)
Definition: heap_allocator.h:137
rheolef::heap_allocator::construct
void construct(pointer p)
Definition: heap_allocator.h:135
rheolef::heap_allocator::~heap_allocator
~heap_allocator()
Definition: heap_allocator.h:101
rheolef::heap_allocator::value_type
T value_type
Definition: heap_allocator.h:82
rheolef::heap_allocator::handler_type::heap
std::list< std::vector< unsigned char > > heap
Definition: heap_allocator.h:189
mkgeo_ball.c
int c
Definition: mkgeo_ball.sh:153
rheolef::heap_allocator::handler
handler_type * handler
Definition: heap_allocator.h:160
T
Expr1::float_type T
Definition: field_expr.h:261
rheolef::heap_allocator
Definition: heap_allocator.h:69
rheolef::heap_allocator::const_reference
const T & const_reference
Definition: heap_allocator.h:81