OGLplus  (0.59.0) a C++ wrapper for rendering APIs

stack_alloc.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_MEMORY_STACK_ALLOC_HPP
10 #define EAGINE_MEMORY_STACK_ALLOC_HPP
11 
12 #include "../assert.hpp"
13 #include "byte_alloc.hpp"
14 #include <type_traits>
15 
16 namespace eagine::memory {
17 //------------------------------------------------------------------------------
18 // base_stack_allocator
19 // non-rebindable non-copyable stack allocator
20 // use with care!
21 //------------------------------------------------------------------------------
22 template <typename T>
23 class base_stack_allocator : public block_owner {
24 private:
25  T* _btm{nullptr};
26  T* _top{nullptr};
27  T* _pos{nullptr};
28  T* _min{nullptr};
29  span_size_t _dif{0};
30 
31  auto _store() const noexcept -> const_block;
32  auto _allocated() const noexcept -> const_block;
33  auto _available() const noexcept -> const_block;
34 
35 public:
36  using value_type = T;
37  using pointer = T*;
38  using const_pointer = const T*;
39  using reference = T&;
40  using const_reference = const T&;
41  using size_type = span_size_t;
42  using difference_type = std::ptrdiff_t;
43 
44  base_stack_allocator(base_stack_allocator&& tmp) noexcept;
45  base_stack_allocator(const base_stack_allocator&) = delete;
46  auto operator=(base_stack_allocator&& tmp) = delete;
47  auto operator=(const base_stack_allocator&) = delete;
48 
49  base_stack_allocator() noexcept = default;
50 
51  base_stack_allocator(const block& blk, span_size_t align) noexcept;
52 
53  base_stack_allocator(const block& blk) noexcept;
54 
55  ~base_stack_allocator() noexcept;
56 
57  auto max_size() const noexcept -> size_type {
58  return _available().size();
59  }
60 
61  auto allocated() const noexcept -> const_block {
62  return _allocated();
63  }
64 
65  auto allocated_size() const noexcept -> size_type {
66  return _allocated().size();
67  }
68 
69  auto contains(const owned_block& b) const noexcept -> bool;
70  auto has_allocated(const owned_block& b) const noexcept -> tribool;
71 
72  auto allocate(size_type n) noexcept -> owned_block;
73 
74  auto truncate(owned_block&& b, size_type nn) noexcept -> owned_block;
75 
76  void deallocate(owned_block&& b) noexcept;
77 
78  friend auto operator==(
79  const base_stack_allocator& a,
80  const base_stack_allocator& b) noexcept {
81  if((a._btm == b._btm) && (a._top == b._top)) {
82  EAGINE_ASSERT(a._pos == b._pos);
83  EAGINE_ASSERT(a._min == b._min);
84  EAGINE_ASSERT(a._dif == b._dif);
85 
86  return true;
87  }
88  return false;
89  }
90 };
91 //------------------------------------------------------------------------------
92 // stack_byte_allocator_only
93 //------------------------------------------------------------------------------
94 template <typename Policy = default_byte_allocator_policy>
95 class stack_byte_allocator_only
96  : public byte_allocator_impl<Policy, stack_byte_allocator_only> {
97 private:
98  base_stack_allocator<byte> _alloc;
99 
100 public:
101  using value_type = byte;
102  using size_type = span_size_t;
103 
104  stack_byte_allocator_only(stack_byte_allocator_only&&) noexcept = default;
105  stack_byte_allocator_only(const stack_byte_allocator_only&) = delete;
106  auto operator=(stack_byte_allocator_only&&) = delete;
107  auto operator=(const stack_byte_allocator_only&) = delete;
108 
109  ~stack_byte_allocator_only() noexcept override = default;
110 
111  stack_byte_allocator_only(const block& blk)
112  : _alloc(blk) {}
113 
114  auto equal(byte_allocator* a) const noexcept -> bool override;
115 
116  auto max_size(size_type) noexcept -> size_type override {
117  return _alloc.max_size();
118  }
119 
120  auto allocated() const noexcept -> const_block {
121  return _alloc.allocated();
122  }
123 
124  auto has_allocated(const owned_block& b, span_size_t) noexcept
125  -> tribool override {
126  return _alloc.has_allocated(b);
127  }
128 
129  auto allocate(size_type n, size_type a) noexcept -> owned_block override;
130 
131  void deallocate(owned_block&& b, size_type) noexcept override;
132 };
133 //------------------------------------------------------------------------------
134 // stack_byte_allocator
135 //------------------------------------------------------------------------------
136 template <typename Policy = default_byte_allocator_policy>
137 class stack_byte_allocator
138  : public byte_allocator_impl<Policy, stack_byte_allocator> {
139 private:
140  base_stack_allocator<byte> _alloc;
141 
142 public:
143  using value_type = byte;
144  using size_type = span_size_t;
145 
146  stack_byte_allocator(stack_byte_allocator&&) noexcept = default;
147  stack_byte_allocator(const stack_byte_allocator&) = delete;
148  auto operator=(stack_byte_allocator&&) = delete;
149  auto operator=(const stack_byte_allocator&) = delete;
150  ~stack_byte_allocator() noexcept = default;
151 
152  stack_byte_allocator(const block& blk)
153  : _alloc(blk) {}
154 
155  auto equal(byte_allocator* a) const noexcept -> bool override;
156 
157  auto max_size(size_type a) noexcept -> size_type override {
158  return _alloc.max_size() > a ? _alloc.max_size() - a : 0;
159  }
160 
161  auto has_allocated(const owned_block& b, span_size_t) noexcept
162  -> tribool override {
163  return _alloc.has_allocated(b);
164  }
165 
166  auto allocate(size_type n, size_type a) noexcept -> owned_block override;
167 
168  void deallocate(owned_block&& b, size_type) noexcept override;
169 };
170 //------------------------------------------------------------------------------
171 // stack_aligned_byte_allocator
172 //------------------------------------------------------------------------------
173 template <typename Policy = default_byte_allocator_policy>
174 class stack_aligned_byte_allocator
175  : public byte_allocator_impl<Policy, stack_aligned_byte_allocator> {
176 private:
177  span_size_t _align;
178 
179  base_stack_allocator<byte> _alloc;
180  using _this_class = stack_aligned_byte_allocator;
181 
182 public:
183  using value_type = byte;
184  using size_type = span_size_t;
185 
186  stack_aligned_byte_allocator(_this_class&&) noexcept = default;
187  stack_aligned_byte_allocator(const _this_class&) = delete;
188  auto operator=(stack_aligned_byte_allocator&&) = delete;
189  auto operator=(const stack_aligned_byte_allocator&) = delete;
190 
191  ~stack_aligned_byte_allocator() noexcept = default;
192 
193  stack_aligned_byte_allocator(const block& blk, span_size_t align)
194  : _align(align)
195  , _alloc(blk, _align) {}
196 
197  auto equal(byte_allocator* a) const noexcept -> bool override;
198 
199  auto max_size(size_type) noexcept -> size_type override {
200  return _alloc.max_size();
201  }
202 
203  auto has_allocated(const owned_block& b, span_size_t) noexcept
204  -> tribool override;
205 
206  auto allocate(size_type n, size_type a) noexcept -> owned_block override;
207 
208  void deallocate(owned_block&& b, size_type a) noexcept override;
209 
210  auto _own_end_misalign(_this_class* p) const noexcept -> span_size_t;
211 
212  auto accomodate_self() noexcept -> byte_allocator*;
213 
214  void eject_self() noexcept override;
215 };
216 //------------------------------------------------------------------------------
217 } // namespace eagine::memory
218 
219 #include <eagine/memory/stack_alloc.inl>
220 
221 #endif // EAGINE_MEMORY_STACK_ALLOC_HPP
value_type
Value tree value element data type enumeration.
Definition: interface.hpp:27
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
basic_block< false > block
Alias for non-const byte memory span.
Definition: block.hpp:27
unsigned char byte
Byte type alias.
Definition: types.hpp:24
static constexpr auto contains(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) noexcept -> S1
Indicates if a span contains the contents of what.
Definition: span_algo.hpp:237
auto operator==(message_id l, static_message_id< ClassId, MethodId > r) noexcept
Equality comparison between message_id and static_message_id.
Definition: message_id.hpp:131

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).