dune-fem 2.8.0
Loading...
Searching...
No Matches
stackallocator.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_COMMON_STACKALLOCATOR_HH
2#define DUNE_FEM_COMMON_STACKALLOCATOR_HH
3
4#include <cassert>
5#include <new>
6#include <stack>
7#include <utility>
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
15 // UninitializedObjectStack
16 // ------------------------
17
19 : public std::stack< void * >
20 {
21 explicit UninitializedObjectStack ( std::size_t objectSize )
22 : objectSize_( objectSize )
23 {}
24
26 : std::stack< void * >(),
27 objectSize_( other.objectSize_ )
28 {}
29
31 {
32 if( objectSize_ != other.objectSize_ )
33 clear();
34 objectSize_ = other.objectSize_;
35 return *this;
36 }
37
39
40 void clear ()
41 {
42 for( ; !empty(); pop() )
43 ::operator delete( top() );
44 }
45
46 std::size_t objectSize () const { return objectSize_; }
47
48 void resize ( std::size_t newSize ) { clear(); objectSize_ = newSize; }
49
50 private:
51 std::size_t objectSize_;
52 };
53
54
55
56 // StackAllocator
57 // --------------
58
59 template< class T, class S = UninitializedObjectStack * >
61 {
62 typedef T value_type;
63
64 typedef T *pointer;
65 typedef const T*const_pointer;
66
67 typedef T &reference;
68 typedef const T &const_reference;
69
70 typedef std::size_t size_type;
71 typedef std::ptrdiff_t difference_type;
72
73 template< class U >
74 struct rebind { typedef StackAllocator< U, S > other; };
75
77 typedef S StackPtr;
78
79 explicit StackAllocator ( StackPtr stack ) : stack_( stack ) {}
80
81 template< class U >
82 StackAllocator ( const StackAllocator< U, S > &other ) : stack_( other.stack_ ) {}
83
84 template< class U >
85 StackAllocator ( StackAllocator< U, S > &&other ) : stack_( std::move( other.stack_ ) ) {}
86
87 StackAllocator ( const StackAllocator &other ) : stack_( other.stack_ ) {}
88 StackAllocator ( StackAllocator && other ) : stack_( other.stack_ ) {}
89
90 pointer address ( reference x ) const { return &x; }
91 const_pointer address ( const_reference x ) const { return &x; }
92
94 {
95 assert( n <= max_size() );
96 if( !stack().empty() )
97 {
98 pointer p = (pointer) stack().top();
99 stack().pop();
100 return p;
101 }
102 else
103 return (pointer) ::operator new( stack().objectSize() );
104 }
105
107 {
108 assert( n <= max_size() );
109 stack().push( p );
110 }
111
112 size_type max_size () const { return stack().objectSize() / sizeof( T ); }
113
114 template< class... Args >
115 void construct ( pointer p, Args &&... args )
116 {
117 assert( p );
118 new( p ) T( std::forward< Args >( args )... );
119 }
120
121 void destroy ( pointer p ) { p->~T(); }
122
123 private:
124 template< class, class >
125 friend struct StackAllocator;
126
127 const Stack &stack () const { return *stack_; }
128 Stack &stack () { return *stack_; }
129
130 StackPtr stack_;
131 };
132
133
134 template<class S>
135 struct StackAllocator<void, S>
136 {
137 typedef void value_type;
138
139 typedef void *pointer;
140 typedef const void*const_pointer;
141
142 typedef std::size_t size_type;
143 typedef std::ptrdiff_t difference_type;
144
145 template< class U >
146 struct rebind { typedef StackAllocator< U, S > other; };
147
149 };
150
151 } // namespace
152
153} //namespace Dune
154
155#endif // #ifndef DUNE_FEM_COMMON_STACKALLOCATOR_HH
STL namespace.
Definition: bindguard.hh:11
Definition: stackallocator.hh:20
std::size_t objectSize() const
Definition: stackallocator.hh:46
UninitializedObjectStack & operator=(const UninitializedObjectStack &other)
Definition: stackallocator.hh:30
void resize(std::size_t newSize)
Definition: stackallocator.hh:48
void clear()
Definition: stackallocator.hh:40
UninitializedObjectStack(const UninitializedObjectStack &other)
Definition: stackallocator.hh:25
UninitializedObjectStack(std::size_t objectSize)
Definition: stackallocator.hh:21
~UninitializedObjectStack()
Definition: stackallocator.hh:38
Definition: stackallocator.hh:61
T value_type
Definition: stackallocator.hh:62
std::ptrdiff_t difference_type
Definition: stackallocator.hh:71
const_pointer address(const_reference x) const
Definition: stackallocator.hh:91
void deallocate(pointer p, size_type n)
Definition: stackallocator.hh:106
S StackPtr
Definition: stackallocator.hh:77
StackAllocator(StackAllocator &&other)
Definition: stackallocator.hh:88
const T & const_reference
Definition: stackallocator.hh:68
T & reference
Definition: stackallocator.hh:67
StackAllocator(StackAllocator< U, S > &&other)
Definition: stackallocator.hh:85
size_type max_size() const
Definition: stackallocator.hh:112
pointer allocate(size_type n, typename rebind< void >::other::const_pointer hint=nullptr)
Definition: stackallocator.hh:93
StackAllocator(const StackAllocator &other)
Definition: stackallocator.hh:87
StackAllocator(const StackAllocator< U, S > &other)
Definition: stackallocator.hh:82
std::size_t size_type
Definition: stackallocator.hh:70
const T * const_pointer
Definition: stackallocator.hh:65
UninitializedObjectStack Stack
Definition: stackallocator.hh:76
pointer address(reference x) const
Definition: stackallocator.hh:90
void construct(pointer p, Args &&... args)
Definition: stackallocator.hh:115
void destroy(pointer p)
Definition: stackallocator.hh:121
StackAllocator(StackPtr stack)
Definition: stackallocator.hh:79
T * pointer
Definition: stackallocator.hh:64
Definition: stackallocator.hh:74
StackAllocator< U, S > other
Definition: stackallocator.hh:74
std::ptrdiff_t difference_type
Definition: stackallocator.hh:143
std::size_t size_type
Definition: stackallocator.hh:142
UninitializedObjectStack Stack
Definition: stackallocator.hh:148
void * pointer
Definition: stackallocator.hh:139
const void * const_pointer
Definition: stackallocator.hh:140
void value_type
Definition: stackallocator.hh:137
StackAllocator< U, S > other
Definition: stackallocator.hh:146