proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
stack_alloc.hpp
Go to the documentation of this file.
1#ifndef VEG_STACK_ALLOC_HPP_UTBYR4Y5S
2#define VEG_STACK_ALLOC_HPP_UTBYR4Y5S
3
7
8namespace proxsuite {
9namespace linalg {
10namespace veg {
11
12namespace _detail {
13namespace _mem {
14
15template<usize MaxAlign>
17{
18
22
23 static auto _align(usize byte_size) noexcept -> usize
24 {
26#if VEG_HAS_ASAN
27 MaxAlign > 8 ? MaxAlign : 8
28#else
30#endif
31 ;
32
33 usize const mask = (actual_max_align - 1);
34 return (byte_size + mask) & ~mask;
35 }
36
37 auto _is_last(void* ptr, usize byte_size) noexcept -> bool
38 {
39 return ptr == (current_ptr - _align(byte_size));
40 }
41 void _assert_last(void* ptr, usize byte_size) noexcept
42 {
43 VEG_ASSERT(ptr == (current_ptr - _align(byte_size)));
44 }
45
46 void _dealloc_last_unchecked(void* ptr, mem::Layout layout)
47 {
48 VEG_DEBUG_ASSERT(ptr == (current_ptr - _align(layout.byte_size)));
49 (void)layout;
50 current_ptr = static_cast<mem::byte*>(ptr);
51 }
52
53 void _dealloc_any(void* ptr, mem::Layout layout) noexcept
54 {
55 if (_is_last(ptr, layout.byte_size)) {
56 _dealloc_last_unchecked(ptr, layout);
57 }
58 }
59
60 void _dealloc_last(void* ptr, mem::Layout layout)
61 {
62 _assert_last(ptr, layout.byte_size);
63 _dealloc_last_unchecked(ptr, layout);
64 }
65
66 auto _alloc(mem::Layout layout) noexcept -> mem::AllocBlock
67 {
68 auto given_bytes = _align(layout.byte_size);
71 (layout.align <= MaxAlign),
73
76 return blk;
77 }
78
79 auto _grow_last_unchecked(void* ptr,
81 {
82 auto rem_bytes = usize(end_ptr - static_cast<mem::byte*>(ptr));
84 (void)rem_bytes;
86 current_ptr = static_cast<mem::byte*>(ptr) + given_bytes;
87 return { ptr, given_bytes };
88 }
89
90 auto _grow_last(void* ptr,
93 mem::RelocFn /*reloc*/) noexcept -> mem::AllocBlock
94 {
95 if (ptr == nullptr) {
98 return blk;
99 }
100 _assert_last(ptr, old_layout.byte_size);
102 }
103
104 auto _grow_any(void* ptr,
108 {
109 if (_is_last(ptr, old_layout.byte_size)) {
111 } else {
114 reloc(blk.data, ptr, old_layout.byte_size);
115 return blk;
116 }
117 }
118};
119} // namespace _mem
120} // namespace _detail
121
122namespace mem {
123template<usize MaxAlign>
125{
126#if VEG_HAS_ASAN
127 static_assert(MaxAlign >= 8, ".");
128#endif
129
132 s.ptr_mut(),
133 s.ptr_mut(),
134 s.ptr_mut() + s.len(),
135 }
136 {
137
139 ((std::uintptr_t(s.ptr()) % MaxAlign) == std::uintptr_t(0)),
140 ((usize(s.len()) % usize(MaxAlign)) == usize(0)));
141 }
142};
143template<usize MaxAlign>
144struct StackAlloc : private BumpAlloc<MaxAlign>
145{
147};
148template<usize MaxAlign>
149struct MonotonicAlloc : private BumpAlloc<MaxAlign>
150{
152};
153
154template<usize MaxAlign>
156{
159
160 VEG_INLINE static auto alloc(RefMut ref,
161 mem::Layout layout) noexcept -> AllocBlock
162 {
163 return ImplMut(ref.get())._alloc(layout);
164 }
165 VEG_INLINE static void dealloc(RefMut ref,
166 void* ptr,
167 mem::Layout layout) noexcept
168 {
169 ImplMut(ref.get())._dealloc_any(ptr, layout);
170 }
171 VEG_INLINE static auto grow(RefMut ref,
172 void* ptr,
175 RelocFn reloc) noexcept -> mem::AllocBlock
176 {
177 return ImplMut(ref.get())._grow_any(ptr, old_layout, new_byte_size, reloc);
178 }
179};
180
181template<usize MaxAlign>
183{
186
187 VEG_INLINE static auto alloc(RefMut ref,
188 mem::Layout layout) noexcept -> AllocBlock
189 {
190 return ImplMut(ref.get())._alloc(layout);
191 }
192 VEG_INLINE static void dealloc(RefMut ref,
193 void* ptr,
194 mem::Layout layout) noexcept
195 {
196 ImplMut(ref.get())._dealloc_last(ptr, layout);
197 }
198 VEG_INLINE static auto grow(RefMut ref,
199 void* ptr,
202 RelocFn reloc) noexcept -> mem::AllocBlock
203 {
204 return ImplMut(ref.get())._grow_last(ptr, old_layout, new_byte_size, reloc);
205 }
206};
207
208template<usize MaxAlign>
210{
213
214 VEG_INLINE static auto alloc(RefMut ref,
215 mem::Layout layout) noexcept -> AllocBlock
216 {
217 return ImplMut(ref.get())._alloc(layout);
218 }
219 VEG_INLINE static void dealloc(RefMut /*ref*/,
220 void* /*ptr*/,
221 mem::Layout /*layout*/) noexcept
222 {
223 }
224 VEG_INLINE static auto grow(RefMut ref,
225 void* ptr,
228 RelocFn reloc) noexcept -> mem::AllocBlock
229 {
230 return ImplMut(ref.get())._grow_last(ptr, old_layout, new_byte_size, reloc);
231 }
232};
233} // namespace mem
234} // namespace veg
235} // namespace linalg
236} // namespace proxsuite
237
239#endif /* end of include guard VEG_STACK_ALLOC_HPP_UTBYR4Y5S */
#define VEG_DEBUG_ASSERT(...)
Definition assert.hpp:38
#define VEG_ASSERT_ALL_OF(...)
#define VEG_ASSERT(...)
#define VEG_INLINE
Definition macros.hpp:118
decltype(sizeof(0)) usize
Definition macros.hpp:702
auto _grow_last(void *ptr, mem::Layout old_layout, usize new_byte_size, mem::RelocFn) noexcept -> mem::AllocBlock
static auto _align(usize byte_size) noexcept -> usize
void _assert_last(void *ptr, usize byte_size) noexcept
void _dealloc_last(void *ptr, mem::Layout layout)
auto _grow_last_unchecked(void *ptr, usize new_byte_size) noexcept -> mem::AllocBlock
auto _alloc(mem::Layout layout) noexcept -> mem::AllocBlock
auto _grow_any(void *ptr, mem::Layout old_layout, usize new_byte_size, mem::RelocFn reloc) noexcept -> mem::AllocBlock
void _dealloc_last_unchecked(void *ptr, mem::Layout layout)
void _dealloc_any(void *ptr, mem::Layout layout) noexcept
auto _is_last(void *ptr, usize byte_size) noexcept -> bool
static VEG_INLINE auto alloc(RefMut ref, mem::Layout layout) noexcept -> AllocBlock
static VEG_INLINE auto grow(RefMut ref, void *ptr, mem::Layout old_layout, usize new_byte_size, RelocFn reloc) noexcept -> mem::AllocBlock
static VEG_INLINE void dealloc(RefMut ref, void *ptr, mem::Layout layout) noexcept
static VEG_INLINE auto alloc(RefMut ref, mem::Layout layout) noexcept -> AllocBlock
static VEG_INLINE auto grow(RefMut ref, void *ptr, mem::Layout old_layout, usize new_byte_size, RelocFn reloc) noexcept -> mem::AllocBlock
static VEG_INLINE void dealloc(RefMut, void *, mem::Layout) noexcept
static VEG_INLINE auto alloc(RefMut ref, mem::Layout layout) noexcept -> AllocBlock
static VEG_INLINE auto grow(RefMut ref, void *ptr, mem::Layout old_layout, usize new_byte_size, RelocFn reloc) noexcept -> mem::AllocBlock
static VEG_INLINE void dealloc(RefMut ref, void *ptr, mem::Layout layout) noexcept
BumpAlloc(FromSliceMut, SliceMut< byte > s) noexcept