4#ifndef WPI_MEMORY_ITERATION_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_ITERATION_ALLOCATOR_HPP_INCLUDED
22 template <
class BlockOrRawAllocator>
36 template <std::
size_t N,
class BlockOrRawAllocator = default_allocator>
38 : WPI_EBO(detail::iteration_block_allocator<BlockOrRawAllocator>)
45 template <
typename... Args>
50 auto cur =
static_cast<char*
>(block_.
memory);
51 auto size_each = block_.
size / N;
52 for (
auto i = 0u; i != N; ++i)
64 for (
auto i = 0u; i != N; ++i)
79 block_ = other.block_;
82 for (
auto i = 0u; i != N; ++i)
95 void*
allocate(std::size_t size, std::size_t alignment)
97 auto&
stack = stacks_[cur_];
102 || (fence + offset + size + fence > std::size_t(block_end(cur_) -
stack.top())))
104 return stack.allocate_unchecked(size, offset);
113 auto&
stack = stacks_[cur_];
114 return stack.allocate(block_end(cur_), size, alignment);
124 cur_ = (cur_ + 1) % N;
125 stacks_[cur_].
unwind(block_start(cur_));
153 return std::size_t(block_end(i) - stacks_[i].top());
168 char* block_start(std::size_t i)
const noexcept
171 auto ptr =
static_cast<char*
>(block_.
memory);
172 return ptr + (i * block_.
size / N);
175 char* block_end(std::size_t i)
const noexcept
178 return block_start(i + 1);
181 detail::fixed_memory_stack stacks_[N];
185 friend allocator_traits<iteration_allocator<N, BlockOrRawAllocator>>;
186 friend composable_allocator_traits<iteration_allocator<N, BlockOrRawAllocator>>;
191 template <
class BlockOrRawAllocator = default_allocator>
195#if WPI_MEMORY_EXTERN_TEMPLATE
203 template <std::
size_t N,
class BlockAllocator>
212 std::size_t alignment)
214 return state.allocate(size, alignment);
219 std::size_t alignment)
221 return allocate_node(state, count * size, alignment);
232 std::size_t)
noexcept
241 return state.capacity_left();
246 return state.capacity_left();
254 return std::size_t(-1);
260 template <std::
size_t N,
class BlockAllocator>
268 std::size_t alignment)
noexcept
270 return state.try_allocate(size, alignment);
275 std::size_t size, std::size_t alignment)
noexcept
277 return state.try_allocate(count * size, alignment);
284 std::size_t)
noexcept
286 return state.block_.contains(
ptr);
290 std::size_t size, std::size_t alignment)
noexcept
297#if WPI_MEMORY_EXTERN_TEMPLATE
298 extern template class allocator_traits<iteration_allocator<2>>;
299 extern template class composable_allocator_traits<iteration_allocator<2>>;
static void * allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment)
Definition iteration_allocator.hpp:218
static void deallocate_node(allocator_type &, void *, std::size_t, std::size_t) noexcept
Definition iteration_allocator.hpp:227
static std::size_t max_node_size(const allocator_type &state) noexcept
Definition iteration_allocator.hpp:239
std::true_type is_stateful
Definition iteration_allocator.hpp:208
static std::size_t max_alignment(const allocator_type &) noexcept
Definition iteration_allocator.hpp:252
static void * allocate_node(allocator_type &state, std::size_t size, std::size_t alignment)
Definition iteration_allocator.hpp:211
static void deallocate_array(allocator_type &, void *, std::size_t, std::size_t, std::size_t) noexcept
Definition iteration_allocator.hpp:231
static std::size_t max_array_size(const allocator_type &state) noexcept
Definition iteration_allocator.hpp:244
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
static void * try_allocate_node(allocator_type &state, std::size_t size, std::size_t alignment) noexcept
Definition iteration_allocator.hpp:267
static bool try_deallocate_array(allocator_type &state, void *ptr, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition iteration_allocator.hpp:289
static void * try_allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition iteration_allocator.hpp:274
static bool try_deallocate_node(allocator_type &state, void *ptr, std::size_t, std::size_t) noexcept
Definition iteration_allocator.hpp:283
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
A stateful RawAllocator that is designed for allocations in a loop.
Definition iteration_allocator.hpp:39
std::size_t cur_iteration() const noexcept
Definition iteration_allocator.hpp:137
void * allocate(std::size_t size, std::size_t alignment)
Definition iteration_allocator.hpp:95
std::size_t capacity_left(std::size_t i) const noexcept
Definition iteration_allocator.hpp:151
void * try_allocate(std::size_t size, std::size_t alignment) noexcept
Definition iteration_allocator.hpp:111
static std::size_t max_iterations() noexcept
Definition iteration_allocator.hpp:130
detail::iteration_block_allocator< BlockOrRawAllocator > allocator_type
Definition iteration_allocator.hpp:41
void next_iteration() noexcept
Definition iteration_allocator.hpp:121
~iteration_allocator() noexcept
Definition iteration_allocator.hpp:70
iteration_allocator(std::size_t block_size, Args &&... args)
Definition iteration_allocator.hpp:46
std::size_t capacity_left() const noexcept
Definition iteration_allocator.hpp:157
allocator_type & get_allocator() noexcept
Definition iteration_allocator.hpp:144
iteration_allocator(iteration_allocator &&other) noexcept
Definition iteration_allocator.hpp:59
iteration_allocator & operator=(iteration_allocator &&other) noexcept
Definition iteration_allocator.hpp:76
A special case of out_of_memory errors thrown when a low-level allocator with a fixed size runs out o...
Definition error.hpp:121
Alias template for an STL container that uses a certain RawAllocator.
Definition container.hpp:184
#define WPI_MEMORY_LOG_PREFIX
Definition config.hpp:46
#define WPI_THROW(Ex)
Definition config.hpp:33
#define WPI_ALIAS_TEMPLATE(Name,...)
Definition config.hpp:73
The typedef wpi::memory::default_allocator.
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
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
constexpr std::size_t debug_fence_size
Definition debug_helpers.hpp:22
make_block_allocator_t< BlockOrRawAllocator, fixed_block_allocator > iteration_block_allocator
Definition iteration_allocator.hpp:23
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
std::size_t size
The size of the memory block (might be 0).
Definition memory_arena.hpp:30
void * memory
The address of the memory block (might be nullptr).
Definition memory_arena.hpp:29
#define WPI_MEMORY_ASSERT_MSG(Expr, Msg)
Definition assert.hpp:47