memory
Typedefs | Static Functions | Member Functions
iteration_allocator< N, BlockOrRawAllocator > Class Template Reference

Detailed Description

template<std::size_t N, class BlockOrRawAllocator = default_allocator>
class foonathan::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.

Typedefs

using allocator_type = detail::iteration_block_allocator< BlockOrRawAllocator >
 

Static Functions

static std::size_t max_iterations () noexcept
 

Member Functions

template<typename... Args>
 iteration_allocator (std::size_t block_size, Args &&... args)
 
 iteration_allocator (iteration_allocator &&other) 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_type & get_allocator () noexcept
 
std::size_t capacity_left (std::size_t i) const noexcept
 
std::size_t capacity_left () const noexcept
 

Constructors

◆ iteration_allocator()

iteration_allocator ( std::size_t  block_size,
Args &&...  args 
)
explicit
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.

Member Functions

◆ allocate()

void* allocate ( std::size_t  size,
std::size_t  alignment 
)
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.

◆ try_allocate()

void* try_allocate ( std::size_t  size,
std::size_t  alignment 
)
noexcept
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.

◆ next_iteration()

void next_iteration ( )
noexcept
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.

◆ max_iterations()

static std::size_t max_iterations ( )
staticnoexcept
Returns:
The number of iteration each allocation will live. This is the template parameter N.

◆ cur_iteration()

std::size_t cur_iteration ( ) const
noexcept
Returns:
The index of the current iteration. This is modulo max_iterations().

◆ get_allocator()

allocator_type& get_allocator ( )
noexcept
Returns:
A reference to the BlockAllocator used for managing the memory.
Requires:
It is undefined behavior to move this allocator out into another object.

◆ capacity_left() [1/2]

std::size_t capacity_left ( std::size_t  i) const
noexcept
Returns:
The amount of memory remaining in the stack with the given index. This is the number of bytes that are available for allocation.

◆ capacity_left() [2/2]

std::size_t capacity_left ( ) const
noexcept
Returns:
The amount of memory remaining in the currently active stack.