batchlas::BumpAllocator Struct Reference

Overview

Linear (bump) sub-allocator over a caller-supplied workspace buffer.

Each allocate() carves the next suitably aligned slice off the front of the buffer; nothing is ever freed individually. Every alignment is max(16, MEM_BASE_ADDR_ALIGN / 8, alignof(T)).

A pool made by measuring() is a sizing pool: it runs the same arithmetic over a fictitious region and reports through required_bytes() the buffer size the same call sequence needs. That is how every *_buffer_size query is computed (see workspace_bytes()).

Warning
Size a workspace with measuring() and required_bytes(), never by re-summing allocation_size() by hand: the capacity check uses the rounded size while the cursor advances by the raw size, so an exactly simulated total is too small.

Definition at line 30 of file mempool.hh.

#include <batchlas/util/mempool.hh>

Constructors

◆ BumpAllocator() [1/2]

template<typename T>
batchlas::BumpAllocator::BumpAllocator ( T * data,
size_t byte_size )
inline

A pool over byte_size bytes starting at data.

Definition at line 33 of file mempool.hh.

◆ BumpAllocator() [2/2]

template<typename T>
batchlas::BumpAllocator::BumpAllocator ( Span< T > span)
inline

A pool over the bytes of span.

Definition at line 37 of file mempool.hh.

Functions

◆ measuring()

BumpAllocator batchlas::BumpAllocator::measuring ( )
inlinestatic

A sizing pool: an effectively unbounded, maximally aligned fictitious region.

Give a real pool required_bytes() and the same call sequence succeeds, with at most one alignment quantum to spare.

Warning
The pointers it hands out are non-null and aligned (views can be built over them) but unbacked: sizing code must never touch their contents, launch a kernel, or test data() != nullptr to mean "a workspace was passed".

Definition at line 48 of file mempool.hh.

◆ is_measuring()

bool batchlas::BumpAllocator::is_measuring ( ) const
inline

True for a pool made by measuring().

Definition at line 51 of file mempool.hh.

◆ required_bytes()

size_t batchlas::BumpAllocator::required_bytes ( ) const
inline

Bytes a real pool must be given for the call sequence so far to succeed.

Rounded up to the coarsest alignment the sequence asked for.

Exceptions
batchlas::api_misuseon a pool not made by measuring()

Definition at line 59 of file mempool.hh.

◆ alignment()

template<typename T>
constexpr auto batchlas::BumpAllocator::alignment ( const Device & device)
inlinestaticconstexpr

Alignment in bytes of an allocation of T on device.

Definition at line 69 of file mempool.hh.

◆ allocation_size() [1/2]

template<typename T>
constexpr size_t batchlas::BumpAllocator::allocation_size ( const Device & device,
size_t size )
inlinestaticconstexpr

Bytes size elements of T occupy, rounded up to alignment<T>(); 0 for size == 0.

Definition at line 77 of file mempool.hh.

◆ allocation_size() [2/2]

template<typename T>
constexpr size_t batchlas::BumpAllocator::allocation_size ( Queue & ctx,
size_t size )
inlinestaticconstexpr

allocation_size() for the Queue's device.

Definition at line 85 of file mempool.hh.

◆ allocate() [1/2]

template<typename T>
Span< T > batchlas::BumpAllocator::allocate ( const Device & device,
size_t size )
inlineconstexpr

Carve size elements of T, aligned to alignment<T>(), off the front of the pool.

Template Parameters
Telement type
Parameters
devicedevice whose alignment rule applies
sizenumber of elements; 0 returns an empty Span and consumes nothing
Returns
the allocated elements (uninitialised; unbacked in a sizing pool)
Exceptions
batchlas::workspace_errorif the pool has fewer than allocation_size<T>() bytes left

Definition at line 94 of file mempool.hh.

◆ allocate() [2/2]

template<typename T>
Span< T > batchlas::BumpAllocator::allocate ( Queue & ctx,
size_t size )
inlineconstexpr

allocate() with the Queue's device.

Definition at line 127 of file mempool.hh.

◆ remaining()

Span< std::byte > batchlas::BumpAllocator::remaining ( ) const
inline

The still-unclaimed tail of the pool.

Lets a callee sub-allocate without the caller knowing its size up front; pair with consume() to hand back the bytes it actually took.

Exceptions
batchlas::api_misuseon a sizing pool, whose extent is fictitious

Definition at line 134 of file mempool.hh.

◆ consume()

void batchlas::BumpAllocator::consume ( size_t bytes)
inline

Advance the pool by bytes (after a callee used remaining()).

Exceptions
batchlas::workspace_errorif fewer than bytes remain

Definition at line 145 of file mempool.hh.