Rheolef  7.1
an efficient C++ finite element environment
stack_allocator.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_STACK_ALLOCATOR_H
2 #define _RHEOLEF_STACK_ALLOCATOR_H
3 
26 #include <memory>
27 #include <limits>
28 #include "rheolef/compiler.h"
29 #include "rheolef/pretty_name.h"
30 
31 namespace rheolef {
32 
33 /*Class:stack_allocator
34 EXAMPLE:
35 @example
36  const size_t stack_size = 1024;
37  vector<unsigned char> stack (stack_size);
38  stack_allocator<double> stack_alloc (stack.begin().operator->(), stack.size());
39  typedef map <size_t, double, less<size_t>, stack_allocator<pair<size_t,double> > > map_type;
40  map_type a (less<size_t>(), stack_alloc);
41  a.insert (make_pair (0, 3.14));
42  a.insert (make_pair (1, 1.17));
43  for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) @{
44  cout << (*iter).first << " " << (*iter).second << endl;
45  @}
46 @end example
47 End:
48 */
49 //<begin_verbatim:
50 template <typename T>
52 protected:
53  struct handler_type; // forward declaration:
54 public:
55 
56 // typedefs:
57 
58  typedef size_t size_type;
59  typedef std::ptrdiff_t difference_type;
60  typedef T* pointer;
61  typedef const T* const_pointer;
62  typedef T& reference;
63  typedef const T& const_reference;
64  typedef T value_type;
65 
66 // constructors:
67 
68  stack_allocator() throw()
69  : handler (new handler_type)
70  {
71  }
72  stack_allocator (unsigned char* stack, size_t stack_size) throw()
73  : handler (new handler_type (stack, stack_size))
74  {
75  trace_macro ("stack_allocator cstor");
76  }
77  stack_allocator (const stack_allocator& sa) throw()
78  : handler (sa.handler)
79  {
81  }
82  template <typename U>
83  stack_allocator (const stack_allocator<U>& sa) throw()
84  : handler ((typename stack_allocator<T>::handler_type*)(sa.handler))
85  {
87  }
88  ~stack_allocator() throw()
89  {
90  trace_macro ("stack_allocator dstor");
91  check_macro (handler != NULL, "unexpected null mem_info");
92  if (--handler->reference_count == 0) delete handler;
93  }
94  // Rebind to allocators of other types
95  template <typename U>
96  struct rebind {
98  };
99 
100 // assignment:
101 
103  {
104  handler = sa.handler;
106  return *this;
107  }
108 
109 // utility functions:
110 
111  pointer address (reference r) const { return &r; }
112  const_pointer address (const_reference c) const { return &c; }
113  size_type max_size() const { return std::numeric_limits<size_t>::max() / sizeof(T); }
114 
115 // in-place construction/destruction
116 
118  {
119  // placement new operator:
120  new( reinterpret_cast<void*>(p) ) T(c);
121  }
122  // C++ 2011: default construct a value of type T at the location referenced by p
123  void construct (pointer p) { new ( reinterpret_cast<void*>(p) ) T(); }
124 
126  {
127  // call destructor directly:
128  (p)->~T();
129  }
130 
131 // allocate raw memory
132 
133  pointer allocate (size_type n, const void* = NULL)
134  {
135  trace_macro ("allocate "<<n<<" type " << typename_macro(T));
136  check_macro (handler->stack != NULL, "unexpected null stack");
137  void* p = handler->stack + handler->allocated_size;
138  handler->allocated_size += n*sizeof(T);
139 
140  if (handler->allocated_size + 1 > handler->max_size) {
141  trace_macro ("stack is full: throwing...");
142  throw std::bad_alloc();
143  }
144  return pointer (p);
145  }
147  {
148  trace_macro ("deallocate "<<n<<" type "<<typename_macro(T));
149  // No need to free stack memory
150  }
151  const handler_type* get_handler() const {
152  return handler;
153  }
154 
155 // data:
156 
157 protected:
158  struct handler_type {
159  unsigned char* stack;
161  size_t max_size;
163 
165  : stack (NULL),
166  allocated_size (0),
167  max_size (0),
168  reference_count (1)
169  {
170  trace_macro ("stack_allocator::mem_info cstor NULL");
171  }
172  handler_type (unsigned char* stack1, size_t size1)
173  : stack (stack1),
174  allocated_size (0),
175  max_size (size1),
176  reference_count (1)
177  {
178  trace_macro ("stack_allocator::mem_info cstori: size="<<max_size);
179  }
181  {
182  trace_macro ("stack_allocator::mem_info dstor: size="<<max_size);
183  }
184  private:
185  handler_type (const handler_type&);
186  handler_type& operator= (const handler_type&);
187  };
189  template <typename U> friend class stack_allocator;
190 };
191 // Comparison
192 template <typename T1>
193 bool operator==( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw()
194 {
195  return lhs.get_handler() == rhs.get_handler();
196 }
197 template <typename T1>
198 bool operator!=( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw()
199 {
200  return lhs.get_handler() != rhs.get_handler();
201 }
202 //>end_verbatim:
203 
204 } // namespace rheolef
205 #endif // STACK_ALLOCATOR_H
rheolef::stack_allocator::handler_type::handler_type
handler_type(unsigned char *stack1, size_t size1)
Definition: stack_allocator.h:172
rheolef::operator!=
bool operator!=(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:172
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::stack_allocator::difference_type
std::ptrdiff_t difference_type
Definition: stack_allocator.h:59
rheolef::stack_allocator::operator=
stack_allocator & operator=(const stack_allocator &sa)
Definition: stack_allocator.h:102
rheolef::stack_allocator::handler_type::reference_count
size_t reference_count
Definition: stack_allocator.h:162
rheolef::stack_allocator::handler_type::allocated_size
size_t allocated_size
Definition: stack_allocator.h:160
rheolef::stack_allocator::rebind
Definition: stack_allocator.h:96
rheolef::stack_allocator::handler_type::~handler_type
~handler_type()
Definition: stack_allocator.h:180
rheolef::stack_allocator::address
pointer address(reference r) const
Definition: stack_allocator.h:111
mkgeo_ball.c
c
Definition: mkgeo_ball.sh:153
rheolef::stack_allocator::reference
T & reference
Definition: stack_allocator.h:62
rheolef::stack_allocator
Definition: stack_allocator.h:51
rheolef::stack_allocator::~stack_allocator
~stack_allocator()
Definition: stack_allocator.h:88
rheolef::stack_allocator::construct
void construct(pointer p, const_reference c)
Definition: stack_allocator.h:117
rheolef::stack_allocator::handler_type::handler_type
handler_type()
Definition: stack_allocator.h:164
rheolef::stack_allocator::handler_type
Definition: stack_allocator.h:158
rheolef::stack_allocator::stack_allocator
stack_allocator()
Definition: stack_allocator.h:68
rheolef::stack_allocator::allocate
pointer allocate(size_type n, const void *=NULL)
Definition: stack_allocator.h:133
rheolef::operator==
bool operator==(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:167
rheolef::stack_allocator::get_handler
const handler_type * get_handler() const
Definition: stack_allocator.h:151
p
Definition: sphere.icc:25
rheolef::stack_allocator::size_type
size_t size_type
Definition: stack_allocator.h:53
rheolef::stack_allocator::handler
handler_type * handler
Definition: stack_allocator.h:188
rheolef::stack_allocator::max_size
size_type max_size() const
Definition: stack_allocator.h:113
rheolef::stack_allocator::stack_allocator
stack_allocator(const stack_allocator< U > &sa)
Definition: stack_allocator.h:83
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::stack_allocator::const_pointer
const typedef T * const_pointer
Definition: stack_allocator.h:61
rheolef::stack_allocator::handler_type::stack
unsigned char * stack
Definition: stack_allocator.h:159
rheolef::stack_allocator::pointer
T * pointer
Definition: stack_allocator.h:60
rheolef::stack_allocator::destroy
void destroy(pointer p)
Definition: stack_allocator.h:125
rheolef::stack_allocator::handler_type::max_size
size_t max_size
Definition: stack_allocator.h:161
mkgeo_ball.n
n
Definition: mkgeo_ball.sh:150
rheolef::stack_allocator::stack_allocator
stack_allocator(const stack_allocator &sa)
Definition: stack_allocator.h:77
rheolef::stack_allocator::deallocate
void deallocate(pointer p, size_type n)
Definition: stack_allocator.h:146
rheolef::stack_allocator::const_reference
const typedef T & const_reference
Definition: stack_allocator.h:63
rheolef::stack_allocator::construct
void construct(pointer p)
Definition: stack_allocator.h:123
rheolef::stack_allocator::rebind::other
stack_allocator< U > other
Definition: stack_allocator.h:97
rheolef::stack_allocator::address
const_pointer address(const_reference c) const
Definition: stack_allocator.h:112
rheolef::stack_allocator::stack_allocator
stack_allocator(unsigned char *stack, size_t stack_size)
Definition: stack_allocator.h:72
rheolef::stack_allocator::value_type
T value_type
Definition: stack_allocator.h:64
T
Expr1::float_type T
Definition: field_expr.h:218
trace_macro
#define trace_macro(message)
Definition: dis_macros.h:111