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