Guitar
Classes | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
strformat_ns::QuickAlloc Class Reference

Fast arena allocator for short-lived formatting data. More...

#include <strformat.h>

Classes

struct  Header
 

Public Member Functions

 QuickAlloc (const QuickAlloc &)=delete
 
QuickAllocoperator= (const QuickAlloc &)=delete
 
 QuickAlloc (QuickAlloc &&)=delete
 
QuickAllocoperator= (QuickAlloc &&)=delete
 
 QuickAlloc ()
 
 ~QuickAlloc ()
 
void * alloc (size_t size)
 Allocate size bytes from the arena. More...
 
void free (void *p)
 

Private Member Functions

void * x_alloc (size_t size)
 
void x_free (void *p)
 

Static Private Member Functions

static size_t align_up (size_t n)
 

Private Attributes

char default_buffer [default_buffer_size]
 

Static Private Attributes

constexpr static size_t default_buffer_size = 256
 
constexpr static size_t alignment = alignof(std::max_align_t)
 

Detailed Description

Fast arena allocator for short-lived formatting data.

Memory is carved out of a fixed 256-byte in-object buffer first; once it is exhausted, additional blocks are obtained with malloc(). All blocks are chained in a singly linked list whose head is the in-object buffer. free() is a no-op: the formatter allocates many small pieces that all die together, so the heap blocks are released in one sweep by the destructor instead of being tracked individually.

Constructor & Destructor Documentation

◆ QuickAlloc() [1/3]

strformat_ns::QuickAlloc::QuickAlloc ( const QuickAlloc )
delete

◆ QuickAlloc() [2/3]

strformat_ns::QuickAlloc::QuickAlloc ( QuickAlloc &&  )
delete

◆ QuickAlloc() [3/3]

strformat_ns::QuickAlloc::QuickAlloc ( )
inline

◆ ~QuickAlloc()

strformat_ns::QuickAlloc::~QuickAlloc ( )
inline
Here is the call graph for this function:

Member Function Documentation

◆ align_up()

static size_t strformat_ns::QuickAlloc::align_up ( size_t  n)
inlinestaticprivate

◆ alloc()

void* strformat_ns::QuickAlloc::alloc ( size_t  size)
inline

Allocate size bytes from the arena.

Only the first two blocks of the list are probed for free space: the in-object buffer and, if present, the newest standard heap block. The insertion policy below pushes exhausted blocks to third place and beyond, and a block only gets displaced after failing a request, i.e. when its remaining space is already smaller than that request. Blocks past the second therefore have little or no free space left, so scanning them would rarely pay off; capping the search keeps allocation O(1) no matter how many blocks have accumulated.

When both probes fail, a new block is added:

  • A request that fits in a standard block (default_buffer_size bytes including the header) gets one, inserted right behind the in-object buffer so that it becomes the primary heap block for subsequent allocations.
  • A larger request gets a dedicated block sized exactly for it. Such a block is born completely full, so it is inserted behind the current heap block (third place) to keep the two-block search window free of blocks that can never satisfy anything.

Sizes are rounded up to alignof(std::max_align_t) granularity, but the usable memory starts at offset sizeof(Header), so returned pointers are only guaranteed to be aligned to alignof(Header) (pointer alignment) – sufficient for the Part records stored here, but this is not a general-purpose max-aligned allocator.

Here is the call graph for this function:

◆ free()

void strformat_ns::QuickAlloc::free ( void *  p)
inline

◆ operator=() [1/2]

QuickAlloc& strformat_ns::QuickAlloc::operator= ( const QuickAlloc )
delete

◆ operator=() [2/2]

QuickAlloc& strformat_ns::QuickAlloc::operator= ( QuickAlloc &&  )
delete

◆ x_alloc()

void* strformat_ns::QuickAlloc::x_alloc ( size_t  size)
inlineprivate

◆ x_free()

void strformat_ns::QuickAlloc::x_free ( void *  p)
inlineprivate
Here is the call graph for this function:

Member Data Documentation

◆ alignment

constexpr static size_t strformat_ns::QuickAlloc::alignment = alignof(std::max_align_t)
staticconstexprprivate

◆ default_buffer

char strformat_ns::QuickAlloc::default_buffer[default_buffer_size]
private

◆ default_buffer_size

constexpr static size_t strformat_ns::QuickAlloc::default_buffer_size = 256
staticconstexprprivate

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