WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
wpi::memory::memory_arena< BlockAllocator, Cached > Class Template Reference

A memory arena that manages huge memory blocks for a higher-level allocator. More...

#include <wpi/memory/memory_arena.hpp>

Public Types

using allocator_type = BlockAllocator
 
using is_cached = std::integral_constant<bool, Cached>
 

Public Member Functions

template<typename... Args>
 memory_arena (std::size_t block_size, Args &&... args)
 
 ~memory_arena () noexcept
 
memory_block allocate_block ()
 
memory_block current_block () const noexcept
 
void deallocate_block () noexcept
 
bool owns (const void *ptr) const noexcept
 
void shrink_to_fit () noexcept
 
std::size_t capacity () const noexcept
 
std::size_t cache_size () const noexcept
 
std::size_t size () const noexcept
 
std::size_t next_block_size () const noexcept
 
allocator_typeget_allocator () noexcept
 
 memory_arena (memory_arena &&other) noexcept
 
memory_arenaoperator= (memory_arena &&other) noexcept
 

Static Public Member Functions

static constexpr std::size_t min_block_size (std::size_t byte_size) noexcept
 

Friends

void swap (memory_arena &a, memory_arena &b) noexcept
 

Detailed Description

template<class BlockAllocator, bool Cached>
class wpi::memory::memory_arena< BlockAllocator, Cached >

A memory arena that manages huge memory blocks for a higher-level allocator.

Some allocators like memory_stack work on huge memory blocks, this class manages them fro those allocators. It uses a BlockAllocator for the allocation of those blocks. The memory blocks in use are put onto a stack like structure, deallocation will pop from the top, so it is only possible to deallocate the last allocated block of the arena. By default, blocks are not really deallocated but stored in a cache. This can be disabled with the second template parameter, passing it uncached_arena (or false) disables it, cached_arena (or true) enables it explicitly.

Member Typedef Documentation

◆ allocator_type

template<class BlockAllocator , bool Cached>
using wpi::memory::memory_arena< BlockAllocator, Cached >::allocator_type = BlockAllocator

◆ is_cached

template<class BlockAllocator , bool Cached>
using wpi::memory::memory_arena< BlockAllocator, Cached >::is_cached = std::integral_constant<bool, Cached>

Constructor & Destructor Documentation

◆ memory_arena() [1/2]

template<class BlockAllocator , bool Cached>
template<typename... Args>
wpi::memory::memory_arena< BlockAllocator, Cached >::memory_arena ( std::size_t block_size,
Args &&... args )
inlineexplicit
Effects:
Creates it by giving it the size and other arguments for the BlockAllocator. It forwards these arguments to its constructor.
Requires:
block_size must be greater than min_block_size(0) and other requirements depending on the BlockAllocator.
Throws:
Anything thrown by the constructor of the BlockAllocator.

◆ ~memory_arena()

template<class BlockAllocator , bool Cached>
wpi::memory::memory_arena< BlockAllocator, Cached >::~memory_arena ( )
inlinenoexcept
Effects:
Deallocates all memory blocks that where requested back to the BlockAllocator.

◆ memory_arena() [2/2]

template<class BlockAllocator , bool Cached>
wpi::memory::memory_arena< BlockAllocator, Cached >::memory_arena ( memory_arena< BlockAllocator, Cached > && other)
inlinenoexcept
Effects:
Moves the arena. The new arena takes ownership over all the memory blocks from the other arena object, which is empty after that. This does not invalidate any memory blocks.

Member Function Documentation

◆ allocate_block()

template<class BlockAllocator , bool Cached>
memory_block wpi::memory::memory_arena< BlockAllocator, Cached >::allocate_block ( )
inline
Effects:
Allocates a new memory block. It first uses a cache of previously deallocated blocks, if caching is enabled, if it is empty, allocates a new one.
Returns:
The new memory_block.
Throws:
Anything thrown by the BlockAllocator allocation function.

◆ cache_size()

template<class BlockAllocator , bool Cached>
std::size_t wpi::memory::memory_arena< BlockAllocator, Cached >::cache_size ( ) const
inlinenoexcept
Returns:
The size of the cache, i.e. how many blocks can be allocated without allocation.

◆ capacity()

template<class BlockAllocator , bool Cached>
std::size_t wpi::memory::memory_arena< BlockAllocator, Cached >::capacity ( ) const
inlinenoexcept
Returns:
The capacity of the arena, i.e. how many blocks are used and cached.

◆ current_block()

template<class BlockAllocator , bool Cached>
memory_block wpi::memory::memory_arena< BlockAllocator, Cached >::current_block ( ) const
inlinenoexcept
Returns:
The current memory block. This is the memory block that will be deallocated by the next call to deallocate_block().

◆ deallocate_block()

template<class BlockAllocator , bool Cached>
void wpi::memory::memory_arena< BlockAllocator, Cached >::deallocate_block ( )
inlinenoexcept
Effects:
Deallocates the current memory block. The current memory block is the block on top of the stack of blocks. If caching is enabled, it does not really deallocate it but puts it onto a cache for later use, use shrink_to_fit() to purge that cache.

◆ get_allocator()

template<class BlockAllocator , bool Cached>
allocator_type & wpi::memory::memory_arena< BlockAllocator, Cached >::get_allocator ( )
inlinenoexcept
Returns:
A reference of the BlockAllocator object.
Requires:
It is undefined behavior to move this allocator out into another object.

◆ min_block_size()

template<class BlockAllocator , bool Cached>
static constexpr std::size_t wpi::memory::memory_arena< BlockAllocator, Cached >::min_block_size ( std::size_t byte_size)
inlinestaticconstexprnoexcept
Returns:
The minimum block size required for an arena containing the given amount of memory. If an arena is created with the result of min_block_size(n), the resulting capacity will be exactly n.
Requires:
byte_size must be a positive number.

◆ next_block_size()

template<class BlockAllocator , bool Cached>
std::size_t wpi::memory::memory_arena< BlockAllocator, Cached >::next_block_size ( ) const
inlinenoexcept
Returns:
The size of the next memory block, i.e. of the next call to allocate_block(). If there are blocks in the cache, returns size of the next one. Otherwise forwards to the BlockAllocator and subtracts an implementation offset.

◆ operator=()

template<class BlockAllocator , bool Cached>
memory_arena & wpi::memory::memory_arena< BlockAllocator, Cached >::operator= ( memory_arena< BlockAllocator, Cached > && other)
inlinenoexcept

◆ owns()

template<class BlockAllocator , bool Cached>
bool wpi::memory::memory_arena< BlockAllocator, Cached >::owns ( const void * ptr) const
inlinenoexcept
Returns:
If ptr is in memory owned by the arena.

◆ shrink_to_fit()

template<class BlockAllocator , bool Cached>
void wpi::memory::memory_arena< BlockAllocator, Cached >::shrink_to_fit ( )
inlinenoexcept
Effects:
Purges the cache of unused memory blocks by returning them. The memory blocks will be deallocated in reversed order of allocation. Does nothing if caching is disabled.

◆ size()

template<class BlockAllocator , bool Cached>
std::size_t wpi::memory::memory_arena< BlockAllocator, Cached >::size ( ) const
inlinenoexcept
Returns:
The size of the arena, i.e. how many blocks are in use. It is always smaller or equal to the capacity().

Friends And Related Symbol Documentation

◆ swap

template<class BlockAllocator , bool Cached>
void swap ( memory_arena< BlockAllocator, Cached > & a,
memory_arena< BlockAllocator, Cached > & b )
friend
Effects:
Swaps to memory arena objects. This does not invalidate any memory blocks.

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