4#ifndef WPI_MEMORY_MEMORY_STACK_HPP_INCLUDED
5#define WPI_MEMORY_MEMORY_STACK_HPP_INCLUDED
11#define WPI_MEMORY_MEMORY_STACK_HAS_MIN_BLOCK_SIZE
40 const char* e) noexcept
47 if (lhs.index != rhs.index)
50 "stack markers from different "
52 return lhs.top == rhs.top;
62 if (lhs.index != rhs.index)
63 return lhs.index < rhs.index;
65 "stack markers from different "
67 return lhs.top < rhs.top;
101 template <
class BlockOrRawAllocator = default_allocator>
103 : WPI_EBO(detail::default_leak_checker<detail::memory_stack_leak_handler>)
122 template <
typename... Args>
124 : arena_(block_size,
detail::forward<Args>(args)...),
125 stack_(arena_.allocate_block().
memory)
138 void*
allocate(std::size_t size, std::size_t alignment)
144 || fence + offset + size + fence > std::size_t(block_end() - stack_.
top()))
153 auto needed = fence + offset + size + fence;
167 return stack_.
allocate(block_end(), size, alignment);
181 return {arena_.
size() - 1, stack_, block_end()};
198 if (std::size_t to_deallocate = (arena_.
size() - 1) - m.index)
201 for (std::size_t i = 1; i != to_deallocate; ++i)
208 return m.end ==
static_cast<char*
>(cur.memory) + cur.
size;
237 return std::size_t(block_end() - stack_.
top());
262 const char* block_end() const noexcept
265 return static_cast<const char*
>(block.memory) + block.
size;
268 memory_arena<allocator_type> arena_;
269 detail::fixed_memory_stack stack_;
271 friend allocator_traits<memory_stack<BlockOrRawAllocator>>;
272 friend composable_allocator_traits<memory_stack<BlockOrRawAllocator>>;
280 template <
class Stack = memory_stack<>>
296 : marker_(marker), stack_(&
stack)
303 : marker_(other.marker_), stack_(other.stack_)
305 other.stack_ =
nullptr;
313 stack_->unwind(marker_);
321 stack_->unwind(marker_);
323 marker_ = other.marker_;
324 stack_ = other.stack_;
326 other.stack_ =
nullptr;
343 stack_->unwind(marker_);
350 return stack_ !=
nullptr;
374#if WPI_MEMORY_EXTERN_TEMPLATE
375 extern template class memory_stack<>;
376 extern template class memory_stack_raii_unwind<memory_stack<>>;
383 template <
class BlockAllocator>
392 std::size_t alignment)
394 auto mem = state.allocate(size, alignment);
395 state.on_allocate(size);
401 std::size_t alignment)
410 std::size_t)
noexcept
412 state.on_deallocate(size);
416 std::size_t size, std::size_t alignment)
noexcept
426 return state.next_capacity();
431 return state.next_capacity();
439 return std::size_t(-1);
445 template <
class BlockAllocator>
453 std::size_t alignment)
noexcept
455 return state.try_allocate(size, alignment);
460 std::size_t size, std::size_t alignment)
noexcept
462 return state.try_allocate(count * size, alignment);
469 std::size_t)
noexcept
471 return state.arena_.owns(
ptr);
475 std::size_t size, std::size_t alignment)
noexcept
482#if WPI_MEMORY_EXTERN_TEMPLATE
483 extern template class allocator_traits<memory_stack<>>;
484 extern template class composable_allocator_traits<memory_stack<>>;
static void deallocate_node(allocator_type &state, void *, std::size_t size, std::size_t) noexcept
Definition memory_stack.hpp:409
static void * allocate_node(allocator_type &state, std::size_t size, std::size_t alignment)
Definition memory_stack.hpp:391
static void * allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment)
Definition memory_stack.hpp:400
std::true_type is_stateful
Definition memory_stack.hpp:388
static std::size_t max_node_size(const allocator_type &state) noexcept
Definition memory_stack.hpp:424
static std::size_t max_array_size(const allocator_type &state) noexcept
Definition memory_stack.hpp:429
static void deallocate_array(allocator_type &state, void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition memory_stack.hpp:415
static std::size_t max_alignment(const allocator_type &) noexcept
Definition memory_stack.hpp:437
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
static void * allocate_node(allocator_type &state, std::size_t size, std::size_t alignment)
Definition allocator_traits.hpp:298
static void deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:318
static bool try_deallocate_node(allocator_type &state, void *ptr, std::size_t, std::size_t) noexcept
Definition memory_stack.hpp:468
static void * try_allocate_node(allocator_type &state, std::size_t size, std::size_t alignment) noexcept
Definition memory_stack.hpp:452
static bool try_deallocate_array(allocator_type &state, void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition memory_stack.hpp:474
static void * try_allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition memory_stack.hpp:459
The default specialization of the composable_allocator_traits for a ComposableAllocator.
Definition allocator_traits.hpp:500
static bool try_deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:522
Definition memory_stack.hpp:22
void unwind(char *top) noexcept
Definition memory_stack.hpp:100
void * allocate_unchecked(std::size_t size, std::size_t align_offset, std::size_t fence_size=debug_fence_size) noexcept
Definition memory_stack.hpp:87
void * allocate(const char *end, std::size_t size, std::size_t alignment, std::size_t fence_size=debug_fence_size) noexcept
Definition memory_stack.hpp:71
char * top() const noexcept
Definition memory_stack.hpp:107
static constexpr std::size_t implementation_offset() noexcept
Definition memory_arena.hpp:127
Definition memory_stack.hpp:34
friend bool operator<=(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:75
friend bool operator>(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:70
friend bool operator<(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:60
friend bool operator!=(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:55
friend bool operator==(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:45
friend bool operator>=(const stack_marker &lhs, const stack_marker &rhs) noexcept
Definition memory_stack.hpp:80
void shrink_to_fit() noexcept
Definition memory_arena.hpp:387
memory_block allocate_block()
Definition memory_arena.hpp:350
memory_block current_block() const noexcept
Definition memory_arena.hpp:362
std::size_t next_block_size() const noexcept
Definition memory_arena.hpp:415
allocator_type & get_allocator() noexcept
Definition memory_arena.hpp:425
void deallocate_block() noexcept
Definition memory_arena.hpp:371
std::size_t size() const noexcept
Definition memory_arena.hpp:406
Simple utility that automatically unwinds a Stack to a previously saved location.
Definition memory_stack.hpp:282
memory_stack_raii_unwind & operator=(memory_stack_raii_unwind &&other) noexcept
Definition memory_stack.hpp:318
void release() noexcept
Definition memory_stack.hpp:333
bool will_unwind() const noexcept
Definition memory_stack.hpp:348
marker_type get_marker() const noexcept
Definition memory_stack.hpp:355
memory_stack_raii_unwind(stack_type &stack) noexcept
Definition memory_stack.hpp:288
Stack stack_type
Definition memory_stack.hpp:284
memory_stack_raii_unwind(memory_stack_raii_unwind &&other) noexcept
Definition memory_stack.hpp:302
~memory_stack_raii_unwind() noexcept
Definition memory_stack.hpp:310
void unwind() noexcept
Definition memory_stack.hpp:340
typename stack_type::marker marker_type
Definition memory_stack.hpp:285
memory_stack_raii_unwind(stack_type &stack, marker_type marker) noexcept
Definition memory_stack.hpp:295
stack_type & get_stack() const noexcept
Definition memory_stack.hpp:363
A stateful RawAllocator that provides stack-like (LIFO) allocations.
Definition memory_stack.hpp:104
std::size_t next_capacity() const noexcept
Definition memory_stack.hpp:244
make_block_allocator_t< BlockOrRawAllocator > allocator_type
Definition memory_stack.hpp:106
void * try_allocate(std::size_t size, std::size_t alignment) noexcept
Definition memory_stack.hpp:165
void unwind(marker m) noexcept
Definition memory_stack.hpp:192
allocator_type & get_allocator() noexcept
Definition memory_stack.hpp:251
std::size_t capacity_left() const noexcept
Definition memory_stack.hpp:235
memory_stack(std::size_t block_size, Args &&... args)
Definition memory_stack.hpp:123
static constexpr std::size_t min_block_size(std::size_t byte_size) noexcept
Definition memory_stack.hpp:114
implementation_defined marker
The marker type that is used for unwinding.
Definition memory_stack.hpp:176
marker top() const noexcept
Definition memory_stack.hpp:179
void shrink_to_fit() noexcept
Definition memory_stack.hpp:227
void * allocate(std::size_t size, std::size_t alignment)
Definition memory_stack.hpp:138
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:184
#define WPI_MEMORY_LOG_PREFIX
Definition config.hpp:46
implementation_defined make_block_allocator_t
Takes either a BlockAllocator or a RawAllocator.
Definition memory_arena.hpp:622
Class wpi::memory::memory_arena and related functionality regarding BlockAllocators.
detail namespace with internal helper functions
Definition input_adapters.h:32
std::size_t align_offset(std::uintptr_t address, std::size_t alignment) noexcept
Definition align.hpp:26
void check_allocation_size(std::size_t passed, Func f, const allocator_info &info)
Definition error.hpp:264
void * debug_fill_free(void *memory, std::size_t, std::size_t) noexcept
Definition debug_helpers.hpp:57
constexpr std::size_t debug_fence_size
Definition debug_helpers.hpp:22
void debug_check_pointer(Functor condition, const allocator_info &info, void *ptr)
Definition debug_helpers.hpp:71
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
Definition memory_stack.hpp:90
void operator()(std::ptrdiff_t amount)
std::size_t size
The size of the memory block (might be 0).
Definition memory_arena.hpp:30
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46
#define WPI_MEMORY_ASSERT_MSG(Expr, Msg)
Definition assert.hpp:47