memory
Member Functions
temporary_allocator Class Reference

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.

Member Functions

 temporary_allocator ()
 
 temporary_allocator (temporary_stack &stack)
 
 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
 

Constructors

◆ temporary_allocator() [1/2]

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

◆ temporary_allocator() [2/2]

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

Member Functions

◆ allocate()

void * 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.

◆ is_active()

bool 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.

◆ shrink_to_fit()

void 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!

◆ get_stack()

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