WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
wpi::memory::temporary_allocator Class Reference

A stateful RawAllocator that handles temporary allocations. More...

#include <wpi/memory/temporary_allocator.hpp>

Public Member Functions

 temporary_allocator ()
 
 temporary_allocator (temporary_stack &stack)
 
 ~temporary_allocator () noexcept
 
 temporary_allocator (temporary_allocator &&)=delete
 
temporary_allocatoroperator= (temporary_allocator &&)=delete
 
void * allocate (std::size_t size, std::size_t alignment)
 
bool is_active () const noexcept
 
void shrink_to_fit () noexcept
 
temporary_stackget_stack () const noexcept
 

Detailed Description

A stateful RawAllocator that handles temporary allocations.

It works similar to alloca() but uses a seperate memory_stack for the allocations, instead of the actual program stack. This avoids the stack overflow error and is portable, with a similar speed. All allocations done in the scope of the allocator object are automatically freed when the object is destroyed.

Constructor & Destructor Documentation

◆ temporary_allocator() [1/3]

wpi::memory::temporary_allocator::temporary_allocator ( )
Effects:
Creates it by using the get_temporary_stack() to get the temporary stack.
Requires:
There must be a per-thread temporary stack (WPI_MEMORY_TEMPORARY_STACK_MODE must not be equal to 0).

◆ temporary_allocator() [2/3]

wpi::memory::temporary_allocator::temporary_allocator ( temporary_stack & stack)
explicit
Effects:
Creates it by giving it the temporary_stack it uses for allocation.

◆ ~temporary_allocator()

wpi::memory::temporary_allocator::~temporary_allocator ( )
noexcept

◆ temporary_allocator() [3/3]

wpi::memory::temporary_allocator::temporary_allocator ( temporary_allocator && )
delete

Member Function Documentation

◆ allocate()

void * wpi::memory::temporary_allocator::allocate ( std::size_t size,
std::size_t alignment )
Effects:
Allocates memory from the internal memory_stack by forwarding to it.
Returns:
The result of memory_stack::allocate().
Requires:
is_active() must return true.

◆ get_stack()

temporary_stack & wpi::memory::temporary_allocator::get_stack ( ) const
inlinenoexcept
Returns:
The internal stack the temporary allocator is using.
Requires:
is_active() must return true.

◆ is_active()

bool wpi::memory::temporary_allocator::is_active ( ) const
noexcept
Returns:
Whether or not the allocator object is active.
Note
The active allocator object is the last object created for one stack. Moving changes the active allocator.

◆ operator=()

temporary_allocator & wpi::memory::temporary_allocator::operator= ( temporary_allocator && )
delete

◆ shrink_to_fit()

void wpi::memory::temporary_allocator::shrink_to_fit ( )
noexcept
Effects:
Instructs it to release unnecessary memory after automatic unwinding occurs. This will effectively forward to memory_stack::shrink_to_fit() of the internal stack.
Note
Like the use of the temporary_stack_initializer this can be used as an optimization, to tell when the thread's temporary_stack isn't needed anymore and can be destroyed.
It doesn't call shrink to fit immediately, only in the destructor!

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