Loading...
Searching...
No Matches
stack_allocator.h
Go to the documentation of this file.
#ifndef _RHEOLEF_STACK_ALLOCATOR_H
#define _RHEOLEF_STACK_ALLOCATOR_H
#include <memory>
#include <limits>
#include "rheolef/compiler.h"
#include "rheolef/pretty_name.h"
namespace rheolef {
/*Class:stack_allocator
EXAMPLE:
@example
const size_t stack_size = 1024;
vector<unsigned char> stack (stack_size);
stack_allocator<double> stack_alloc (stack.begin().operator->(), stack.size());
typedef map <size_t, double, less<size_t>, stack_allocator<pair<size_t,double> > > map_type;
map_type a (less<size_t>(), stack_alloc);
a.insert (make_pair (0, 3.14));
a.insert (make_pair (1, 1.17));
for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) @{
cout << (*iter).first << " " << (*iter).second << endl;
@}
@end example
End:
*/
//<begin_verbatim:
template <typename T>
class stack_allocator {
protected:
struct handler_type; // forward declaration:
public:
// typedefs:
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
// constructors:
stack_allocator() throw()
: handler (new handler_type)
{
}
stack_allocator (unsigned char* stack, size_t stack_size) throw()
: handler (new handler_type (stack, stack_size))
{
trace_macro ("stack_allocator cstor");
}
stack_allocator (const stack_allocator& sa) throw()
: handler (sa.handler)
{
++handler->reference_count;
}
template <typename U>
stack_allocator (const stack_allocator<U>& sa) throw()
: handler ((typename stack_allocator<T>::handler_type*)(sa.handler))
{
++handler->reference_count;
}
~stack_allocator() throw()
{
trace_macro ("stack_allocator dstor");
check_macro (handler != NULL, "unexpected null mem_info");
if (--handler->reference_count == 0) delete handler;
}
// Rebind to allocators of other types
template <typename U>
struct rebind {
typedef stack_allocator<U> other;
};
// assignment:
stack_allocator& operator= (const stack_allocator& sa)
{
handler = sa.handler;
++handler->reference_count;
return *this;
}
// utility functions:
pointer address (reference r) const { return &r; }
const_pointer address (const_reference c) const { return &c; }
size_type max_size() const { return std::numeric_limits<size_t>::max() / sizeof(T); }
// in-place construction/destruction
void construct (pointer p, const_reference c)
{
// placement new operator:
new( reinterpret_cast<void*>(p) ) T(c);
}
// C++ 2011: default construct a value of type T at the location referenced by p
void construct (pointer p) { new ( reinterpret_cast<void*>(p) ) T(); }
void destroy (pointer p)
{
// call destructor directly:
(p)->~T();
}
// allocate raw memory
pointer allocate (size_type n, const void* = NULL)
{
trace_macro ("allocate "<<n<<" type " << typename_macro(T));
check_macro (handler->stack != NULL, "unexpected null stack");
void* p = handler->stack + handler->allocated_size;
handler->allocated_size += n*sizeof(T);
if (handler->allocated_size + 1 > handler->max_size) {
trace_macro ("stack is full: throwing...");
throw std::bad_alloc();
}
return pointer (p);
}
void deallocate (pointer p, size_type n)
{
trace_macro ("deallocate "<<n<<" type "<<typename_macro(T));
// No need to free stack memory
}
const handler_type* get_handler() const {
return handler;
}
// data:
protected:
struct handler_type {
unsigned char* stack;
size_t allocated_size;
size_t max_size;
size_t reference_count;
handler_type()
: stack (NULL),
allocated_size (0),
max_size (0),
reference_count (1)
{
trace_macro ("stack_allocator::mem_info cstor NULL");
}
handler_type (unsigned char* stack1, size_t size1)
: stack (stack1),
allocated_size (0),
max_size (size1),
reference_count (1)
{
trace_macro ("stack_allocator::mem_info cstori: size="<<max_size);
}
~handler_type()
{
trace_macro ("stack_allocator::mem_info dstor: size="<<max_size);
}
private:
handler_type (const handler_type&);
handler_type& operator= (const handler_type&);
};
handler_type* handler;
template <typename U> friend class stack_allocator;
};
// Comparison
template <typename T1>
bool operator==( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw()
{
return lhs.get_handler() == rhs.get_handler();
}
template <typename T1>
bool operator!=( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw()
{
return lhs.get_handler() != rhs.get_handler();
}
//>end_verbatim:
} // namespace rheolef
#endif // STACK_ALLOCATOR_H
rheolef::stack_allocator
Definition stack_allocator.h:51
rheolef::stack_allocator< U >::handler
handler_type * handler
Definition stack_allocator.h:188
rheolef::stack_allocator::value_type
T value_type
Definition stack_allocator.h:64
rheolef::stack_allocator::get_handler
const handler_type * get_handler() const
Definition stack_allocator.h:151
rheolef::stack_allocator::size_type
size_t size_type
Definition stack_allocator.h:58
rheolef::stack_allocator::const_pointer
const T * const_pointer
Definition stack_allocator.h:61
rheolef::stack_allocator::stack_allocator
friend class stack_allocator
Definition stack_allocator.h:189
rheolef::stack_allocator::~stack_allocator
~stack_allocator()
Definition stack_allocator.h:88
rheolef::stack_allocator::pointer
T * pointer
Definition stack_allocator.h:60
rheolef::stack_allocator::stack_allocator
stack_allocator(unsigned char *stack, size_t stack_size)
Definition stack_allocator.h:72
rheolef::stack_allocator::allocate
pointer allocate(size_type n, const void *=NULL)
Definition stack_allocator.h:133
rheolef::stack_allocator::address
pointer address(reference r) const
Definition stack_allocator.h:111
rheolef::stack_allocator::construct
void construct(pointer p, const_reference c)
Definition stack_allocator.h:117
rheolef::stack_allocator::operator=
stack_allocator & operator=(const stack_allocator &sa)
Definition stack_allocator.h:102
rheolef::stack_allocator::construct
void construct(pointer p)
Definition stack_allocator.h:123
rheolef::stack_allocator::max_size
size_type max_size() const
Definition stack_allocator.h:113
rheolef::stack_allocator::reference
T & reference
Definition stack_allocator.h:62
rheolef::stack_allocator::destroy
void destroy(pointer p)
Definition stack_allocator.h:125
rheolef::stack_allocator::stack_allocator
stack_allocator(const stack_allocator &sa)
Definition stack_allocator.h:77
rheolef::stack_allocator::difference_type
std::ptrdiff_t difference_type
Definition stack_allocator.h:59
rheolef::stack_allocator::stack_allocator
stack_allocator()
Definition stack_allocator.h:68
rheolef::stack_allocator::deallocate
void deallocate(pointer p, size_type n)
Definition stack_allocator.h:146
rheolef::stack_allocator::address
const_pointer address(const_reference c) const
Definition stack_allocator.h:112
rheolef::stack_allocator::stack_allocator
stack_allocator(const stack_allocator< U > &sa)
Definition stack_allocator.h:83
rheolef::stack_allocator::const_reference
const T & const_reference
Definition stack_allocator.h:63
trace_macro
#define trace_macro(message)
Definition dis_macros.h:111
T
Expr1::float_type T
Definition field_expr.h:230
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
This file is part of Rheolef.
Definition compiler_eigen.h:39
rheolef::operator!=
bool operator!=(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition heap_allocator.h:176
rheolef::operator==
bool operator==(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition heap_allocator.h:171
p
Definition sphere.icc:25
rheolef::stack_allocator::handler_type
Definition stack_allocator.h:158
rheolef::stack_allocator::handler_type::reference_count
size_t reference_count
Definition stack_allocator.h:162
rheolef::stack_allocator::handler_type::stack
unsigned char * stack
Definition stack_allocator.h:159
rheolef::stack_allocator::handler_type::~handler_type
~handler_type()
Definition stack_allocator.h:180
rheolef::stack_allocator::handler_type::handler_type
handler_type(unsigned char *stack1, size_t size1)
Definition stack_allocator.h:172
rheolef::stack_allocator::handler_type::handler_type
handler_type()
Definition stack_allocator.h:164
rheolef::stack_allocator::handler_type::allocated_size
size_t allocated_size
Definition stack_allocator.h:160
rheolef::stack_allocator::handler_type::max_size
size_t max_size
Definition stack_allocator.h:161
rheolef::stack_allocator::rebind
Definition stack_allocator.h:96
rheolef::stack_allocator::rebind::other
stack_allocator< U > other
Definition stack_allocator.h:97