microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
Public Member Functions | List of all members
microfmt::scratch_allocator Class Reference

Zero-allocation bump allocator for scratch buffers. More...

#include <scratch_allocator.hpp>

Public Member Functions

constexpr scratch_allocator (span< std::byte > buffer RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
 Constructs a scratch allocator over a provided byte span.
 
RELOCO_BEGIN_UNSAFE_CODE scratch_allocator (span< char > buffer RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
 Constructs a scratch allocator over a provided character span.
 
template<typename T >
RELOCO_END_UNSAFE_CODE RELOCO_BEGIN_UNSAFE_CODE RELOCO_ASSUME_ALIGNED (alignof(T)) T *allocate(size_t count
 Reserves aligned storage for objects without constructing them.
 
RELOCO_END_UNSAFE_CODE RELOCO_BEGIN_UNSAFE_CODE typename Args RELOCO_ASSUME_ALIGNED (alignof(T)) T *create(Args &&...args) noexcept RELOCO_LIFETIMEBOUND
 
constexpr scratch_allocator rest () const noexcept RELOCO_LIFETIMEBOUND
 Creates and returns a new scratch_allocator owning the remaining unallocated space.
 
constexpr span< std::byte > remaining_span () const noexcept RELOCO_LIFETIMEBOUND
 Returns a span over the remaining unallocated space.
 
constexpr void reset () noexcept
 Resets the allocator back to the beginning of the buffer.
 
constexpr size_t size () const noexcept
 
constexpr size_t capacity () const noexcept
 
constexpr size_t available () const noexcept
 

Detailed Description

Zero-allocation bump allocator for scratch buffers.

Restricts object placement strictly to trivially destructible types to avoid manual destructor tracking in low-stack, bare-metal environments. Supports partitioning remaining capacity to pass downstream.

Constructor & Destructor Documentation

◆ scratch_allocator() [1/2]

constexpr microfmt::scratch_allocator::scratch_allocator ( span< std::byte > buffer RELOCO_LIFETIMEBOUND  RELOCO_LIFETIME_CAPTURE_BY_THIS)
inlineexplicitconstexprnoexcept

Constructs a scratch allocator over a provided byte span.

Parameters
bufferCaller-owned backing memory storage.

◆ scratch_allocator() [2/2]

RELOCO_BEGIN_UNSAFE_CODE microfmt::scratch_allocator::scratch_allocator ( span< char > buffer RELOCO_LIFETIMEBOUND  RELOCO_LIFETIME_CAPTURE_BY_THIS)
inlineexplicitnoexcept

Constructs a scratch allocator over a provided character span.

Parameters
bufferCaller-owned backing memory storage.

Member Function Documentation

◆ available()

constexpr size_t microfmt::scratch_allocator::available ( ) const
inlineconstexprnoexcept

◆ capacity()

constexpr size_t microfmt::scratch_allocator::capacity ( ) const
inlineconstexprnoexcept

◆ RELOCO_ASSUME_ALIGNED() [1/2]

template<typename T >
RELOCO_END_UNSAFE_CODE RELOCO_BEGIN_UNSAFE_CODE microfmt::scratch_allocator::RELOCO_ASSUME_ALIGNED ( alignof(T)  )

Reserves aligned storage for objects without constructing them.

Template Parameters
TObject type requiring storage.
Parameters
countNumber of contiguous objects to reserve.
Returns
Pointer to the aligned storage, or nullptr if it does not fit.

◆ RELOCO_ASSUME_ALIGNED() [2/2]

RELOCO_END_UNSAFE_CODE RELOCO_BEGIN_UNSAFE_CODE typename Args microfmt::scratch_allocator::RELOCO_ASSUME_ALIGNED ( alignof(T)  ) &&
inlinenoexcept

◆ remaining_span()

constexpr span< std::byte > microfmt::scratch_allocator::remaining_span ( ) const
inlineconstexprnoexcept

Returns a span over the remaining unallocated space.

◆ reset()

constexpr void microfmt::scratch_allocator::reset ( )
inlineconstexprnoexcept

Resets the allocator back to the beginning of the buffer.

◆ rest()

constexpr scratch_allocator microfmt::scratch_allocator::rest ( ) const
inlineconstexprnoexcept

Creates and returns a new scratch_allocator owning the remaining unallocated space.

Useful for passing the "rest" of the buffer downstream to nested renderers.

Returns
A child scratch_allocator wrapping the remaining memory.

◆ size()

constexpr size_t microfmt::scratch_allocator::size ( ) const
inlineconstexprnoexcept

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