ddn.memory
ddn.memory — the shared non-GC allocation hook.
This module provides Allocator, one small hook value that every owning type in non-GC code takes as a parameter, and two ready-made implementations: the C heap (Allocator.cHeap) and a bounded bump arena over caller-supplied storage (FixedArena). A bounded, no-growth profile is simply another allocator, so consumers need one API, not two. The test seams CountingAllocator and FailingAllocator ship here because consumers' test suites need them everywhere.
The facility is opt-in: nothing else in core allocates through it and no-GC is not a workspace-wide rule.
Contract:
- The hook function pointers and the Allocator operations are
nothrow @nogc; failure is reported as thenullslice and never by throwing or aborting. The operations are@system: a consumer calls them from its own small@trustedallocation boundary. - A successful
allocatereturns exactlysizebytes at an addresswith at least the requested
alignment(a power of two, at least 1; the default is(void*).sizeof * 2). Contents are uninitialised. - Memory obtained from the hook is not scanned by the GC: a caller
that stores GC-managed references in it roots them itself.
deallocatereceives the original whole slice, exactly once; anempty slice is a documented no-op. A zero-size request is never served —
allocate(0, …)returnsnull— so callers skip the call for empty sizes.- Allocator.init is the "no allocator" hook:
allocatefailsand
deallocateis a no-op. Hooks are compared member-wise over the context pointer and both function pointers.
Example (an owning type that takes the hook):
import ddn.memory : Allocator;
/// A non-copyable byte buffer that owns its storage.
struct Buffer {
private Allocator allocator_;
private void[] block_;
@disable this(this);
this(Allocator allocator, size_t bytes) @trusted nothrow @nogc {
allocator_ = allocator;
block_ = allocator_.allocate(bytes, 16); // the allocation boundary
}
~this() @trusted nothrow @nogc {
allocator_.deallocate(block_);
}
size_t length() const @safe pure nothrow @nogc {
return block_.length;
}
}
Buffer buffer = Buffer(Allocator.cHeap(), 64);
assert(buffer.length == 64);Example (a bounded user over a stack buffer):
import ddn.memory : FixedArena;
ubyte[256] backing; // caller-supplied storage
FixedArena arena = FixedArena(backing[]);
auto hook = arena.allocator();
// hook.allocate hands out slices of `backing`; growth beyond it fails.
assert(arena.used() == 0);Example (a test that counts allocations):
import ddn.memory : Allocator, CountingAllocator;
CountingAllocator counting = CountingAllocator(Allocator.cHeap());
Allocator hook = counting.allocator();
auto block = hook.allocate(32, 8);
assert(counting.liveBlocks() == 1);
assert(counting.liveBytes() == 32);
hook.deallocate(block);
assert(counting.liveBlocks() == 0);
assert(counting.deallocations() == 1);Design record: doc/DDN-MEMORY.md in the repository documents the settled decisions behind this module. The architectural decision is recorded as ADR-2026-11-15-01 (Shared Non-GC Allocation Hook in Core).
Types 4
A non-GC allocation hook: a context pointer and two nothrow @nogc functions.
The value is cheap to copy, store and compare. Owning types take it as a parameter, store the hook they were created with and return their blocks through it. See the module documentation for the full contract; the key points: failure is the null slice, returned memory is uninitialised and not scanned by the GC, and deallocate receives the original block.
void * contextOpaque state the functions receive back; may be `null` for stateless hooks.AllocateFn allocateFnThe allocation function; `null` makes every request fail.DeallocateFn deallocateFnThe deallocation function; `null` makes every release a no-op.void[] allocate(size_t size, size_t alignment = (void *).sizeof * 2) const nothrow @nogcRequests a block from the hook.A non-copyable bump arena over caller-supplied storage.
The bounded ("no growth") profile: the arena allocates nothing of its own, and an aligned request that does not fit returns the null slice. deallocate performs a LIFO rollback when the block is the most recent allocation (its end equals the current watermark) and is a no-op for every earlier block, so out-of-order releases never corrupt the arena. The allocator() hook borrows the arena and must not outlive it.
size_t used() const @safe pure nothrow @nogcThe current watermark: bytes handed out minus LIFO rollbacks.size_t highWater() const @safe pure nothrow @nogcThe peak watermark reached since construction, across `reset` calls.this(return scope ubyte[] storage)Wraps caller-supplied storage; the arena borrows it for its lifetime.A counting allocator wrapper: counts allocations and releases over an inner hook and reports the numbers a test wants to assert on.
Single-owner and non-copyable: it borrows one inner hook, hands out hooks through its own context and must outlive them. It detects the detectable over-release — a returned block whose size exceeds the outstanding bytes, such as a double return of the only live block or a foreign block — counts it as invalidDeallocations and does not forward it, so a test bug never corrupts the inner allocator. Exact per-block double-free detection would need per-block tracking, which would allocate.
size_t failedAllocations() const @safe pure nothrow @nogcAllocations the inner hook could not serve.size_t invalidDeallocations() const @safe pure nothrow @nogcDetectable over-releases seen (double or foreign returns).A fail-at-nth-allocation wrapper for fault-injection tests.
Single-owner and non-copyable, like CountingAllocator; it borrows one inner hook and must outlive the hooks handed out. failNth arms exactly one forced failure: the n-th allocate call after the arm (1-based) returns the null slice without touching the inner hook, and the schedule disarms itself after firing. Deallocation is forwarded untouched, so inner accounting stays truthful.
Functions 1
bool checkedByteCount(size_t count, size_t elementSize, out size_t bytes) @safe pure nothrow @nogcAn overflow-checked `count * elementSize`.