1#ifndef DUNE_FEM_COMMON_STACKALLOCATOR_HH
2#define DUNE_FEM_COMMON_STACKALLOCATOR_HH
18 struct UninitializedObjectStack
19 :
public std::stack< void * >
21 explicit UninitializedObjectStack ( std::size_t objectSize )
22 : objectSize_( objectSize )
25 UninitializedObjectStack (
const UninitializedObjectStack &other )
26 :
std::stack< void * >(),
27 objectSize_( other.objectSize_ )
30 UninitializedObjectStack &operator= (
const UninitializedObjectStack &other )
32 if( objectSize_ != other.objectSize_ )
34 objectSize_ = other.objectSize_;
38 ~UninitializedObjectStack () { clear(); }
42 for( ; !
empty(); pop() )
43 ::operator
delete( top() );
46 std::size_t objectSize ()
const {
return objectSize_; }
48 void resize ( std::size_t newSize ) { clear(); objectSize_ = newSize; }
51 std::size_t objectSize_;
59 template<
class T,
class S = UninitializedObjectStack * >
65 typedef const T*const_pointer;
68 typedef const T &const_reference;
70 typedef std::size_t size_type;
71 typedef std::ptrdiff_t difference_type;
74 struct rebind {
typedef StackAllocator< U, S > other; };
76 typedef UninitializedObjectStack Stack;
79 explicit StackAllocator ( StackPtr stack ) : stack_( stack ) {}
82 StackAllocator (
const StackAllocator< U, S > &other ) : stack_( other.stack_ ) {}
85 StackAllocator ( StackAllocator< U, S > &&other ) : stack_(
std::move( other.stack_ ) ) {}
87 StackAllocator (
const StackAllocator &other ) : stack_( other.stack_ ) {}
88 StackAllocator ( StackAllocator && other ) : stack_( other.stack_ ) {}
90 pointer address ( reference x )
const {
return &x; }
91 const_pointer address ( const_reference x )
const {
return &x; }
93 pointer allocate ( size_type n,
typename rebind< void >::other::const_pointer hint =
nullptr )
95 assert( n <= max_size() );
96 if( !stack().
empty() )
98 pointer p = (pointer) stack().top();
103 return (pointer) ::operator
new( stack().objectSize() );
106 void deallocate ( pointer p, size_type n )
108 assert( n <= max_size() );
112 size_type max_size ()
const {
return stack().objectSize() /
sizeof( T ); }
114 template<
class... Args >
115 void construct ( pointer p, Args &&... args )
118 new( p ) T( std::forward< Args >( args )... );
121 void destroy ( pointer p ) { p->~T(); }
124 template<
class,
class >
125 friend struct StackAllocator;
127 const Stack &stack ()
const {
return *stack_; }
128 Stack &stack () {
return *stack_; }
135 struct StackAllocator<void, S>
137 typedef void value_type;
139 typedef void *pointer;
140 typedef const void*const_pointer;
142 typedef std::size_t size_type;
143 typedef std::ptrdiff_t difference_type;
146 struct rebind {
typedef StackAllocator< U, S > other; };
148 typedef UninitializedObjectStack Stack;
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::bool_constant<(sizeof...(II)==0)> empty(std::integer_sequence< T, II... >)
Checks whether the sequence is empty.
Definition: integersequence.hh:80