WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
wpi::memory::iteration_allocator< N, BlockOrRawAllocator > Class Template Reference

A stateful RawAllocator that is designed for allocations in a loop. More...

#include <wpi/memory/iteration_allocator.hpp>

Public Types

using allocator_type = detail::iteration_block_allocator<BlockOrRawAllocator>
 

Public Member Functions

template<typename... Args>
 iteration_allocator (std::size_t block_size, Args &&... args)
 
 iteration_allocator (iteration_allocator &&other) noexcept
 
 ~iteration_allocator () noexcept
 
iteration_allocatoroperator= (iteration_allocator &&other) noexcept
 
void * allocate (std::size_t size, std::size_t alignment)
 
void * try_allocate (std::size_t size, std::size_t alignment) noexcept
 
void next_iteration () noexcept
 
std::size_t cur_iteration () const noexcept
 
allocator_typeget_allocator () noexcept
 
std::size_t capacity_left (std::size_t i) const noexcept
 
std::size_t capacity_left () const noexcept
 

Static Public Member Functions

static std::size_t max_iterations () noexcept
 

Detailed Description

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
class wpi::memory::iteration_allocator< N, BlockOrRawAllocator >

A stateful RawAllocator that is designed for allocations in a loop.

It uses N stacks for the allocation, one of them is always active. Allocation uses the currently active stack. Calling iteration_allocator::next_iteration() at the end of the loop, will make the next stack active for allocation, effectively releasing all of its memory. Any memory allocated will thus be usable for N iterations of the loop. This type of allocator is a generalization of the double frame allocator.

Member Typedef Documentation

◆ allocator_type

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
using wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::allocator_type = detail::iteration_block_allocator<BlockOrRawAllocator>

Constructor & Destructor Documentation

◆ iteration_allocator() [1/2]

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
template<typename... Args>
wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::iteration_allocator ( std::size_t block_size,
Args &&... args )
inlineexplicit
Effects:
Creates it with a given initial block size and and other constructor arguments for the BlockAllocator. It will allocate the first (and only) block and evenly divide it on all the stacks it uses.

◆ iteration_allocator() [2/2]

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::iteration_allocator ( iteration_allocator< N, BlockOrRawAllocator > && other)
inlinenoexcept

◆ ~iteration_allocator()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::~iteration_allocator ( )
inlinenoexcept

Member Function Documentation

◆ allocate()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
void * wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::allocate ( std::size_t size,
std::size_t alignment )
inline
Effects:
Allocates a memory block of given size and alignment. It simply moves the top marker of the currently active stack.
Returns:
A node with given size and alignment.
Throws:
out_of_fixed_memory if the current stack does not have any memory left.
Requires:
size and alignment must be valid.

◆ capacity_left() [1/2]

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
std::size_t wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::capacity_left ( ) const
inlinenoexcept
Returns:
The amount of memory remaining in the currently active stack.

◆ capacity_left() [2/2]

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
std::size_t wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::capacity_left ( std::size_t i) const
inlinenoexcept
Returns:
The amount of memory remaining in the stack with the given index. This is the number of bytes that are available for allocation.

◆ cur_iteration()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
std::size_t wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::cur_iteration ( ) const
inlinenoexcept
Returns:
The index of the current iteration. This is modulo max_iterations().

◆ get_allocator()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
allocator_type & wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::get_allocator ( )
inlinenoexcept
Returns:
A reference to the BlockAllocator used for managing the memory.
Requires:
It is undefined behavior to move this allocator out into another object.

◆ max_iterations()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
static std::size_t wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::max_iterations ( )
inlinestaticnoexcept
Returns:
The number of iteration each allocation will live. This is the template parameter N.

◆ next_iteration()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
void wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::next_iteration ( )
inlinenoexcept
Effects:
Goes to the next internal stack. This will clear the stack whose max_iterations() lifetime has reached, and use it for all allocations in this iteration.
Note
This function should be called at the end of the loop.

◆ operator=()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
iteration_allocator & wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::operator= ( iteration_allocator< N, BlockOrRawAllocator > && other)
inlinenoexcept

◆ try_allocate()

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
void * wpi::memory::iteration_allocator< N, BlockOrRawAllocator >::try_allocate ( std::size_t size,
std::size_t alignment )
inlinenoexcept
Effects:
Allocates a memory block of given size and alignment similar to allocate().
Returns:
A node with given size and alignment or nullptr if the current stack does not have any memory left.

The documentation for this class was generated from the following file: