libds allocator routing#
How libds routes heap allocation — its own C code and every vendored C library that allocates — through a single layer that an integrator may redirect to their own allocator with no per-call function-pointer indirection, bound either at compile time (a macro layer) or at link time (weak symbols), selected by one build flag. It realises the [IDAT.1] ownership model and binds the source, build, symbol, and public-ABI shapes inside libds.
Prefix IALC.
Takes as fixed:
[IDAT.1] —
ds_result_tat every call site, per-objectmallocwith single ownership, per-engine state (no globals), caller-ownedconst char *. This spec routes that allocation; it does not change the ownership model.The public ABI (
runtime/include/libds.h) — this spec does not change it; the one symbol it owns there isds_free([IALC.4]).The libc chokepoint — every allocation in libds's C code and protobuf-c bottoms out at libc
malloc/calloc/realloc/free(+strdup/strndup); no aligned-alloc family is used. The regex engine allocates through C++'s globaloperator newinstead, and is outside the layer ([IALC.3.5]).
What this is not#
Not a conforming-runtime contract. Allocation routing is internal to libds; another runtime is unaffected, and nothing here binds one.
Not an error-number registry. No new error code: an allocation failure surfaces as the existing
DS_ERR_OOM.Not the integrator memory-ownership contract — that is the callback contract ([NCBK]). This spec is the internal routing that makes a custom allocator total.
1. The allocation macro layer [IALC.1]#
[IALC.1.1] libds defines six function-like macros — DS_MALLOC(sz),
DS_CALLOC(n,sz), DS_REALLOC(p,sz), DS_FREE(p), DS_STRDUP(s),
DS_STRNDUP(s,n) — in an internal header runtime/src/libds/ds_alloc.h.
Every hand-written libds translation unit that allocates must include
ds_alloc.h and must express allocation through these macros and the
public ds_free ([IALC.4]); it must not call libc
malloc/calloc/realloc/free/strdup/strndup directly. The sole
exception is ds_alloc.c ([IALC.1.3]). Each macro must parenthesise
every argument.
[IALC.1.2] A build-time selector DS_ALLOC_MODE chooses how the macros
resolve. Its values are DS_ALLOC_MACRO (1) and DS_ALLOC_WEAK (2); it
must default to DS_ALLOC_WEAK, so every build is link-time-overridable
without a recompile. DS_ALLOC_MACRO is the explicit opt-out for a
byte-identical, zero-indirection build. When DS_ALLOC_MODE is unset but
DS_ALLOC_HEADER is defined, the mode is DS_ALLOC_MACRO — a named header is
an explicit request for inline expansion, so the weak default must never
silently ignore it.
In
DS_ALLOC_WEAKmode (the default) each macro expands to a call to the correspondingly-named weak functionds_malloc/ds_calloc/… declared inds_alloc.hand defined inds_alloc.c([IALC.5]). The wrapper is a single tail-callable cold-path frame, never on the allocation-freerealtime/constspacehot path ([IALC.8.2]).In
DS_ALLOC_MACROmode the macros expand inline: to the integrator's functions when a header named byDS_ALLOC_HEADERis included and defines them, otherwise to the libc functions of the same name. With noDS_ALLOC_HEADERthis must be byte-identical to a direct libc call — the macro is transparent.
see also: IALC.4, IALC.5, IALC.8.2
[IALC.1.3] ds_alloc.c holds the only legitimate raw libc allocation
calls. In DS_ALLOC_WEAK mode it defines the six ds_* functions with
__attribute__((weak)), each forwarding to its libc counterpart. In
DS_ALLOC_MACRO mode it defines the real (non-weak) exported functions
the public ABI and the protobuf shim require (at least ds_free and
ds_malloc) as thin wrappers over the macros.
[IALC.1.4] __attribute__((weak)) is the GCC/Clang spelling and is
sufficient for libds's toolchains; a #pragma weak equivalent on other
toolchains is permitted but not required for v1.
2. What routes through the layer [IALC.2]#
[IALC.2.1] All hand-written allocation under runtime/src/libds routes
through the DS_* macros, and every such translation unit includes
ds_alloc.h.
[IALC.2.2] Excluded from the sweep: generated *.pb-c.c (routed via the
ProtobufCAllocator, [IALC.3.1], and regenerated); vendored trees under
runtime/vendor/ (routed per [IALC.3]); and ds_alloc.c ([IALC.1.3]).
see also: IALC.1.3, IALC.3, IALC.3.1
[IALC.2.3] Only DS-owned allocation routes through the layer. A free of a
pointer obtained from a foreign allocator must retain that allocator's free.
[IALC.2.4] strdup/strndup are first-class members of the layer.
Every site becomes DS_STRDUP/DS_STRNDUP; a missed one is a
cross-allocator free under an override and must be caught by [IALC.10.1].
see also: IALC.10.1
3. Per-vendor routing [IALC.3]#
[IALC.3.1] protobuf-c. libds defines one allocator that forwards to the layer and passes it to every protobuf entry point:
Every …__unpack(NULL, …) becomes …__unpack(&ds_pb_allocator, …) and
every …__free_unpacked(msg, NULL) becomes
…__free_unpacked(msg, &ds_pb_allocator). No protobuf call site in
hand-written libds may pass a NULL allocator ([IALC.9.2]). The shim's
struct dispatch is inherent to protobuf-c and occurs only on the
cold DSC-load path; it adds no hot-path cost.
see also: IALC.9.2
[IALC.3.3] Newly vendored libraries. Every vendored library that
allocates must be routed to the DS_* layer before it lands — by a
compile-time hook or a header edit. A library that offers neither and
calls libc directly cannot be reached by either build mode of
[IALC.1.2], and is a documented coverage gap rather than a silent one.
see also: IALC.1.2
[IALC.3.4] Libraries that allocate nothing. fast_float, Dragonbox and CORE-MATH make no allocation call, so nothing of theirs is routed. A change to any of their pins must re-check this, and a version that allocates is routed or documented under [IALC.3.3] before it lands.
see also: IALC.3.3
[IALC.3.5] The regex engine is not routed. RE2, Abseil, libds's C++
seam over them, and the C++ standard library they use allocate through C++'s
global operator new and operator delete, not through the layer. Neither
mode of [IALC.1.2] reaches that memory. libds must not define a global
operator new or operator delete to reach it: either would replace the
integrator's for their whole program. This is the documented coverage gap of
[IALC.3.3].
The engine allocates there in three places:
When |
What allocates |
|---|---|
Loading an artefact |
One compiled pattern per distinct regex, freed with the engine |
A match, during a pump |
RE2's match cache. libds sets no budget for it, so RE2's default per-pattern budget bounds it |
A capture, during a pump |
The submatch array, freed before the capture returns |
A build without an RE2-class engine ([LNRE.4.1]) has no such gap.
see also: IALC.1.2, IALC.3.3, LNRE.4.1
[IALC.3.6] An allocation failure in the regex engine is an allocation
failure like any other. The C++ seam must catch a failed allocation in every
function it exports to C, so that no C++ exception crosses into libds's C code,
and must report the failure to its caller ([IERR.8.7]). The caller must
surface it as it surfaces a failed allocation at the same site: compiling a
pattern while an artefact loads refuses the load with DS_ERR_OOM; a match or
a capture after the load, in an alpha REGEX test as a fact propagates or in a
builtin during a firing, is the EngineError ALLOCATION, which halts the tier
([LERR.1.4.7]). These are the three places [IALC.3.5] lists, and at none of
them is a failed allocation read as a non-match.
ratified — see also: IALC.3.5, IERR.8.7, LERR.1.4.7
4. The public free symbol and the cross-ABI boundary [IALC.4]#
[IALC.4.1] Buffers libds returns to the caller fall in three classes.
Borrowed values (the callback contract) are owned and freed by
libds; the integrator copies out. Typed handles (ds_json_t,
ds_fact_t, ds_schema_t, ds_engine_t, …) are released by their typed
destructors (ds_json_free, …), which route through DS_FREE and are
therefore automatically consistent under any override. Neither class
needs new surface.
[IALC.4.2] For the third class — raw buffers the caller frees
directly — libds exports one public function void ds_free(void *).
In DS_ALLOC_WEAK mode it is the weak ds_free ([IALC.5]); in
DS_ALLOC_MACRO mode it is a real wrapper over DS_FREE. An API that
returns an owned raw buffer documents release with ds_free.
see also: IALC.5
[IALC.4.3] The owned-raw-buffer set is closed and complete: exactly two
APIs and three buffer shapes — a string, a scalar array, and a pointer
array — across four freeable allocations (the ds_json_serialize
string; and ds_engine_get_justifiers' *out_timetags scalar array,
*out_rule_names pointer array, and each *out_rule_names[i] string).
API |
buffer(s) the caller releases |
|---|---|
|
the NUL-terminated |
|
the |
Both APIs' documentation must state release with ds_free (for
rule_names, each string then the array). No other public API returns
an owned raw buffer: void **out_state (instance_new) is backend-owned
and freed by instance_free; the const char * callback fields are
integrator borrows.
ratified
[IALC.4.4] libds exports no public allocation entry point.
Integrators allocate their own memory or use the ds_json_*
constructors (which route through the layer); no API requires the caller
to hand libds a raw buffer that libds later reallocates. A public
allocator is added only if such an API is later introduced.
5. The weak-symbol mode [IALC.5]#
[IALC.5.1] In DS_ALLOC_WEAK mode the six ds_* weak defaults
([IALC.1.3]) pass through to libc. An integrator overrides allocation by
linking a strong definition of any of them against the libds
archive; the linker prefers the strong symbol. No recompile of libds is
required.
see also: IALC.1.3
[IALC.5.2] An override must supply a coherent matched set —
{malloc, calloc, realloc, free, strdup, strndup} from one allocator.
Overriding ds_malloc without ds_free is a cross-allocator free; an
override lacking native strdup/strndup must implement them over its
own malloc. [IALC.10.1] catches a partial override.
see also: IALC.10.1
[IALC.5.3] The six ds_* symbols must have default visibility and must
NOT be static. For the shipped static archive this is automatic and
override resolves cleanly at link. For shared-object build paths
(the pyds extension) the build must not apply
-fvisibility=hidden or -Bsymbolic to these symbols, which would bind
libds's internal calls to its own weak defaults and defeat override.
6. Build integration [IALC.6]#
[IALC.6.1] libds.build, the one source of libds's build
([IARC.10.1.7]), compiles ds_alloc.c into libds.a and threads
DS_ALLOC_MODE (and optional DS_ALLOC_HEADER) into the compile of libds's
own C sources through its alloc_mode and alloc_header parameters.
see also: IARC.10.1.7
[IALC.6.2] Build mode per artifact:
Artifact |
mode |
|---|---|
from-source build (default) |
|
released prebuilt |
|
pyds extension |
the default |
see also: IALC.5.3
[IALC.6.3] DS_ALLOC_WEAK is the default for every artifact. The
from-source build and the prebuilt archive both carry the weak wrapper, so
any consumer can override at link without a recompile. A build that wants
the zero-indirection, byte-identical-to-libc form sets
DS_ALLOC_MODE=DS_ALLOC_MACRO explicitly ([IALC.10.3]).
see also: IALC.10.3
7. Required override semantics [IALC.7]#
[IALC.7.1] An override (a DS_ALLOC_WEAK strong definition or
DS_ALLOC_MACRO DS_ALLOC_HEADER macros) must match libc semantics,
because libds relies on them:
ds_free(NULL)/DS_FREE(NULL)is a no-op.ds_realloc(NULL, n)equalsmalloc(n);ds_realloc(p, 0)follows libc.ds_calloc(n, s)zero-initialises and checksn*sfor overflow.ds_malloc(0)returnsNULLor a freeable pointer; libds handles either.ds_strdup/ds_strndupreturn layer-owned memory freeable byds_free. libds never callsDS_STRDUP(NULL).Returned memory must be aligned to
max_align_t.
ratified
8. Determinism and real-time [IALC.8]#
[IALC.8.1] Routing changes only where bytes come from, not what is computed; it is orthogonal to libds's bit-identical guarantee.
[IALC.8.2] Under realtime/constspace the pump is allocation-free,
so an override affects only INIT and non-RT allocation. The
DS_ALLOC_WEAK wrapper is a single tail-callable cold-path frame and is
never on the hot path. -flto does not erase it for a prebuilt archive
(LTO bitcode is toolchain-version-locked, and inlining an interposable
weak symbol would defeat override); it is a permanent, negligible
cold-path cost.
9. Anti-rot guards [IALC.9]#
[IALC.9.1] A build-failing lint forbids raw
malloc/calloc/realloc/free/strdup/strndup in
runtime/src/libds, excluding ds_alloc.c, generated *.pb-c.c, and
runtime/vendor/. Matches must use DS_*/ds_*.
[IALC.9.2] A build-failing lint forbids a NULL (or 0) allocator
argument to any protobuf …__unpack / …__free_unpacked in
non-generated libds; it must be &ds_pb_allocator.
[IALC.9.3] A new vendored library that allocates is routed to the layer before merge ([IALC.3.3]).
see also: IALC.3.3
10. Conformance [IALC.10]#
[IALC.10.1] Completeness. Built DS_ALLOC_WEAK against a strong
ds_* set that tags and counts, a run of the test corpus must show
zero allocations reaching libc except through the tag — i.e. every
byte libds's C code and protobuf-c allocate passed through the layer.
The regex engine is outside the layer ([IALC.3.5]), and the corpus
evaluates no regex, so the check measures nothing of it.
This catches a missed raw malloc, a stray NULL protobuf allocator,
or an unconverted strdup.
see also: IALC.3.5
[IALC.10.2] Round-trip. Allocation via the override and release via
ds_free/the override must be ASAN-clean, including the [IALC.4.3]
owned raw buffers.
see also: IALC.4.3
[IALC.10.3] Transparency. Under DS_ALLOC_MACRO with no
DS_ALLOC_HEADER, each macro must compile to the direct libc call and
nothing else ([IALC.1.2]). The comparison is self-contained: one
translation unit built through the DS_* macros, the same source built
with the libc names written out, objects byte-identical. It is not a
comparison against the pre-sweep tree — those objects cannot be built any
more, and a conformance test may not depend on a build nobody can produce.
see also: IALC.1.2
[IALC.10.4] Both modes. [IALC.10.1]–[IALC.10.2] run in both
DS_ALLOC_MACRO and DS_ALLOC_WEAK.
11. Layering [IALC.11]#
[IALC.11.1] The macro substrate ([IALC.1]–[IALC.4], [IALC.9]–[IALC.10]) is the layer; the weak-symbol mode ([IALC.5], [IALC.6.3]) is an extension of it for prebuilt-link override, not a parallel implementation.