Skip to content

ludus::foundation::AllocationDomain

Ludus Wiki · API overview · Browse API

Class

Stable, immutable identity for an allocation backend and its context.
A domain binds the backend that must release a block to the allocation origin that its accounting charges. Routing is immutable; only its counters change. Every successful nonzero allocation and every non-null free updates the domain's checked counters with relaxed atomic operations, so a block freed on another thread, or after its allocating thread exited, still charges the domain that allocated it. Read them through GetAllocationStatistics in allocation_statistics.hpp.

Note

The domain and context must outlive all allocations made through them. Callbacks must be thread safe and non-reentrant into their caller. No global allocation replacement is implied, and allocations that bypass a domain are not counted.

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

#include <ludus/foundation/memory/allocation_domain.hpp>
class ludus::foundation::AllocationDomain final

Types

AllocateFunction

using ludus::foundation::AllocationDomain::AllocateFunction = void* (*)(void*, usize, usize) noexcept
Allocates bytes with the requested power-of-two alignment; returns nullptr on failure.

Related types: usize

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

FreeFunction

using ludus::foundation::AllocationDomain::FreeFunction = void (*)(void*, void*, usize, usize) noexcept
Frees storage using its original context, byte count and alignment.

Related types: usize

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

Methods

AllocationDomain

constexpr ludus::foundation::AllocationDomain::AllocationDomain(void *context, AllocateFunction allocate, FreeFunction free) noexcept
Binds an immutable domain to caller-owned callback state.
The domain starts with zero counters and valid accounting. Declare a process-lifetime domain constinit so it is usable before dynamic initialization.
ParameterDescription
context
Borrowed backend state; callbacks decide whether nullptr is valid.
allocate
Thread-safe callback that reports allocation failure with nullptr.
free
Thread-safe callback that releases allocations from this backend.

Note

Both callbacks are required for TryAllocate to accept a request.

Related types: FreeFunction · AllocateFunction

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

AllocationDomain

ludus::foundation::AllocationDomain::AllocationDomain(const AllocationDomain &)=delete
Domains cannot be copied; their identity must remain stable.

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

operator=

AllocationDomain & ludus::foundation::AllocationDomain::operator=(const AllocationDomain &)=delete
Domains cannot be assigned; allocations retain the original identity.

Related types: AllocationDomain

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

AllocationDomain

ludus::foundation::AllocationDomain::AllocationDomain(AllocationDomain &&)=delete
Domains cannot be moved while allocations may refer to their identity.

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

operator=

AllocationDomain & ludus::foundation::AllocationDomain::operator=(AllocationDomain &&)=delete
Domains cannot be move-assigned.

Related types: AllocationDomain

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

TryAllocate

void * ludus::foundation::AllocationDomain::TryAllocate(usize bytes, usize alignment) const noexcept
Attempts an allocation through the bound backend.
A zero-byte request with a valid alignment is an empty success: it returns nullptr, calls no backend and changes no counter. An invalid alignment or missing callback is a rejected request, counted separately from a backend failure. Successful accounting happens after the backend returns and before the pointer is returned.
ParameterDescription
bytes
Requested storage size in bytes.
alignment
Nonzero power-of-two storage alignment.

Returns

Allocated storage, or nullptr for a zero-byte request, invalid input, missing callbacks, or backend allocation failure.

Note

The caller frees successful allocations through this same domain with the same size and alignment.

Related types: usize

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp

Free

void ludus::foundation::AllocationDomain::Free(void *pointer, usize bytes, usize alignment) const noexcept
Releases an allocation through its original backend; nullptr is a no-op.
The domain subtracts the requested bytes before calling the backend, so a reused address cannot be charged against an older lifetime. A null pointer changes no counter.
ParameterDescription
pointer
Storage previously allocated through this domain, or nullptr.
bytes
Original requested size in bytes.
alignment
Original allocation alignment.

Precondition

A non-null pointer requires the original live context and free callback and its original nonzero size and alignment. A violation that the counters can observe (a zero size, an invalid alignment, or more bytes or blocks freed than are live) marks the domain's accounting invalid; the storage is still passed to the backend.

Related types: usize

Declared in modules/foundation/memory/include/ludus/foundation/memory/allocation_domain.hpp