Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
stack_allocator.h
Go to the documentation of this file.
1#ifndef _RHEOLEF_STACK_ALLOCATOR_H
2#define _RHEOLEF_STACK_ALLOCATOR_H
23
26#include <memory>
27#include <limits>
28#include "rheolef/compiler.h"
29#include "rheolef/pretty_name.h"
30
31namespace rheolef {
32
33/*Class:stack_allocator
34EXAMPLE:
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
47End:
48*/
49//<begin_verbatim:
50template <typename T>
52protected:
53 struct handler_type; // forward declaration:
54public:
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
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>
84 : handler ((typename stack_allocator<T>::handler_type*)(sa.handler))
85 {
87 }
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");
138 handler->allocated_size += n*sizeof(T);
139
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
157protected:
159 unsigned char* stack;
161 size_t max_size;
163
165 : stack (NULL),
166 allocated_size (0),
167 max_size (0),
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),
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
192template <typename T1>
193bool operator==( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw()
194{
195 return lhs.get_handler() == rhs.get_handler();
196}
197template <typename T1>
198bool 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
const handler_type * get_handler() const
stack_allocator(unsigned char *stack, size_t stack_size)
pointer allocate(size_type n, const void *=NULL)
pointer address(reference r) const
void construct(pointer p, const_reference c)
stack_allocator & operator=(const stack_allocator &sa)
size_type max_size() const
stack_allocator(const stack_allocator &sa)
std::ptrdiff_t difference_type
void deallocate(pointer p, size_type n)
const_pointer address(const_reference c) const
stack_allocator(const stack_allocator< U > &sa)
#define trace_macro(message)
Definition dis_macros.h:111
Expr1::float_type T
Definition field_expr.h:230
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
This file is part of Rheolef.
bool operator!=(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
bool operator==(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition sphere.icc:25
handler_type(unsigned char *stack1, size_t size1)