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 the null slice and never by throwing or aborting. The operations are @system: a consumer calls them from its own small @trusted allocation boundary.

  • A successful allocate returns exactly size bytes at an address

    with 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.

  • deallocate receives the original whole slice, exactly once; an

    empty slice is a documented no-op. A zero-size request is never served — allocate(0, …) returns null — so callers skip the call for empty sizes.

  • Allocator.init is the "no allocator" hook: allocate fails

    and deallocate is 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

structAllocator

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.

Fields
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.
Methods
void[] allocate(size_t size, size_t alignment = (void *).sizeof * 2) const nothrow @nogcRequests a block from the hook.
void deallocate(void[] block) const nothrow @nogcReturns a block to the hook.
Allocator cHeap() static @safe pure nothrow @nogcThe C heap.

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.

Methods
Allocator allocator() return @safe pure nothrow @nogcThe allocation hook over this arena.
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.
void reset() @safe pure nothrow @nogcReturns the watermark to zero for reuse; `highWater` is kept.
Constructors
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.

Methods
Allocator allocator() return @safe pure nothrow @nogcThe counting hook over the inner allocator.
size_t allocations() const @safe pure nothrow @nogcTotal allocations the inner hook served.
size_t deallocations() const @safe pure nothrow @nogcTotal blocks released to the inner hook.
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).
size_t liveBlocks() const @safe pure nothrow @nogcBlocks currently outstanding.
size_t liveBytes() const @safe pure nothrow @nogcBytes currently outstanding.
size_t highWater() const @safe pure nothrow @nogcThe peak of live bytes reached.
Constructors
this(Allocator inner)Wraps an inner hook; the wrapper borrows it.

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.

Methods
void failNth(size_t nth) @safe pure nothrow @nogcArms one forced allocation failure.
Allocator allocator() return @safe pure nothrow @nogcThe failing hook over the inner allocator.
size_t attempts() const @safe pure nothrow @nogcTotal `allocate` calls seen by this wrapper.
size_t forcedFailures() const @safe pure nothrow @nogcForced failures delivered so far.
Constructors
this(Allocator inner)Wraps an inner hook; the wrapper borrows it. Starts disarmed.

Functions 1

fnbool checkedByteCount(size_t count, size_t elementSize, out size_t bytes) @safe pure nothrow @nogcAn overflow-checked `count * elementSize`.