Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(mem): a central allocation core, a linter that enforces it, and phase attribution in the index pipeline
Martin Vogelda3258f3c7e4
diff --git a/Makefile.cbm b/Makefile.cbmindex 2c2aeb193..92c31031f 100644--- a/Makefile.cbm+++ b/Makefile.cbm@@ -248,6 +248,7 @@ FOUNDATION_SRCS = \src/foundation/compat_fs.c \src/foundation/compat_regex.c \src/foundation/mem.c \+ src/foundation/mem_core.c \src/foundation/diagnostics.c \src/foundation/profile.c \src/foundation/dump_verify.c \@@ -1188,8 +1189,17 @@ lint-no-suppress:lint: lint-tidy lint-cppcheck lint-format lint-no-suppress@echo "=== All linters passed ==="+# Memory-core linter: memory is allocated through src/foundation/mem_core.h,+# not through raw malloc/calloc/realloc/free/strdup. A checked-in baseline+# (scripts/memory-core-baseline.txt) records each file's raw-site count and the+# gate goes red the moment any file grows. Files only ever go down; lower the+# baseline line in the same change that migrates the file.+lint-memory-core:+ @echo "=== memory-core linter ==="+ @python3 scripts/lint-memory-core.py+# CI linters (no clang-tidy — platform-dependent, enforced locally via pre-commit)-lint-ci: lint-cppcheck lint-format lint-no-suppress+lint-ci: lint-cppcheck lint-format lint-no-suppress lint-memory-core@echo "=== CI linters passed ==="# ── Local memory-diagnostic lanes (not PR-CI gates by decision: the diagdiff --git a/scripts/lint-memory-core.py b/scripts/lint-memory-core.pynew file mode 100644index 000000000..a8f714ca0--- /dev/null+++ b/scripts/lint-memory-core.py@@ -0,0 +1,222 @@+#!/usr/bin/env python3+"""+lint-memory-core.py — the memory-core linter.++Memory in this project is allocated through ONE core (src/foundation/mem_core.h),+not through scattered raw malloc/calloc/realloc/free/strdup. This linter is the+gate that keeps that true.++WHY A RATCHET, NOT A BAN. An audit on 2026-09-13 found ~800 raw allocation+sites in src/ and none of them can be migrated in one change. So the gate works+like every other honest debt gate: a checked-in baseline records how many raw+sites each file has TODAY, and the build goes red the moment any file has MORE+than its baseline, or a file not in the baseline grows one. Files can only ever+go down. When a file is migrated, its baseline line is lowered (or removed) in+the same change -- and --strict turns "below baseline" into a failure too, so+the baseline cannot silently rot behind the code.++WHAT COUNTS AS RAW. A call to malloc/calloc/realloc/free/strdup/strndup that is+not part of a longer identifier. cbm_alloc, cbm_free, cbm_calloc, mi_malloc,+cbm_arena_alloc and heap_strdup are all NOT matches: the character before the+name is an identifier character. Comments and string literals are stripped+first, so prose that mentions malloc( does not trip the gate -- the security+audit already bit us once on exactly that with fork(.++EXEMPT. The core itself and the allocator plumbing it sits on:+ src/foundation/mem_core.c the route+ src/foundation/arena.c bulk allocator that reports INTO the core+ src/foundation/slab_alloc.c same+ src/foundation/mem.c policy/measurement, probes with malloc+ src/foundation/mem_override_*.c the --wrap / override shims+ src/foundation/compat*.c libc replacement surface+ internal/cbm/vendored/** not ours+ vendored/** not ours++Usage:+ lint-memory-core.py check against the baseline (CI)+ lint-memory-core.py --strict also fail when a file is BELOW baseline+ lint-memory-core.py --write-baseline regenerate scripts/memory-core-baseline.txt+ lint-memory-core.py --list print every raw site (file:line: call)++Exit 0 = clean. Exit 1 = a file grew. Exit 2 = usage/IO error.+"""+from __future__ import annotations++import argparse+import pathlib+import re+import sys++ROOT = pathlib.Path(__file__).resolve().parent.parent+BASELINE = ROOT / "scripts" / "memory-core-baseline.txt"+SCAN_ROOTS = ("src", "internal/cbm")+RAW = re.compile(r"(?<![A-Za-z0-9_])(malloc|calloc|realloc|free|strdup|strndup)\s*\(")++EXEMPT_EXACT = {+ "src/foundation/mem_core.c",+ "src/foundation/arena.c",+ "src/foundation/slab_alloc.c",+ "src/foundation/mem.c",+}+EXEMPT_PREFIX = (+ "src/foundation/mem_override_",+ "src/foundation/compat",+ "internal/cbm/vendored/",+ "vendored/",+)+++def is_exempt(rel: str) -> bool:+ return rel in EXEMPT_EXACT or rel.startswith(EXEMPT_PREFIX)+++def strip_comments_and_strings(text: str) -> str:+ """Blank out comments and string literals, preserving line structure so+ reported line numbers stay right. Character-class aware enough for C:+ handles escapes inside strings and does not treat // inside a string as a+ comment."""+ out = []+ i, n = 0, len(text)+ while i < n:+ c = text[i]+ nxt = text[i + 1] if i + 1 < n else ""+ if c == "/" and nxt == "*":+ j = text.find("*/", i + 2)+ j = n if j < 0 else j + 2+ out.append("".join(ch if ch == "\n" else " " for ch in text[i:j]))+ i = j+ elif c == "/" and nxt == "/":+ j = text.find("\n", i)+ j = n if j < 0 else j+ out.append(" " * (j - i))+ i = j+ elif c == '"' or c == "'":+ q = c+ j = i + 1+ while j < n and text[j] != q:+ if text[j] == "\\":+ j += 1+ if j < n and text[j] == "\n":+ break+ j += 1+ j = min(j + 1, n)+ out.append(q + " " * max(0, j - i - 2) + (q if j - i >= 2 else ""))+ i = j+ else:+ out.append(c)+ i += 1+ return "".join(out)+++def scan() -> dict[str, list[tuple[int, str]]]:+ hits: dict[str, list[tuple[int, str]]] = {}+ for root in SCAN_ROOTS:+ base = ROOT / root+ if not base.exists():+ continue+ for path in sorted(base.rglob("*")):+ if path.suffix not in (".c", ".h") or not path.is_file():+ continue+ rel = path.relative_to(ROOT).as_posix()+ if is_exempt(rel):+ continue+ text = strip_comments_and_strings(path.read_text(encoding="utf-8", errors="replace"))+ for lineno, line in enumerate(text.splitlines(), 1):+ for m in RAW.finditer(line):+ hits.setdefault(rel, []).append((lineno, m.group(1)))+ return hits+++def read_baseline() -> dict[str, int]:+ if not BASELINE.exists():+ return {}+ out: dict[str, int] = {}+ for raw in BASELINE.read_text(encoding="utf-8").splitlines():+ line = raw.strip()+ if not line or line.startswith("#"):+ continue+ rel, _, count = line.rpartition("\t")+ if not rel:+ continue+ try:+ out[rel] = int(count)+ except ValueError:+ continue+ return out+++def write_baseline(hits: dict[str, list]) -> None:+ lines = [+ "# memory-core-baseline.txt -- raw allocator call sites per file.",+ "# Generated by scripts/lint-memory-core.py --write-baseline.",+ "# A file may only ever go DOWN. Lower a line in the same change that",+ "# migrates the file to src/foundation/mem_core.h; never raise one.",+ "",+ ]+ for rel in sorted(hits):+ lines.append(f"{rel}\t{len(hits[rel])}")+ BASELINE.write_text("\n".join(lines) + "\n", encoding="utf-8")+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--write-baseline", action="store_true")+ ap.add_argument("--strict", action="store_true", help="fail when a file is BELOW its baseline")+ ap.add_argument("--list", action="store_true", help="print every raw site")+ args = ap.parse_args()++ hits = scan()+ total = sum(len(v) for v in hits.values())++ if args.write_baseline:+ write_baseline(hits)+ print(f"memory-core baseline written: {len(hits)} files, {total} raw sites")+ return 0++ if args.list:+ for rel in sorted(hits):+ for lineno, call in hits[rel]:+ print(f"{rel}:{lineno}: {call}(")+ print(f"-- {total} raw sites in {len(hits)} files")+ return 0++ baseline = read_baseline()+ if not baseline:+ print(f"ERROR: no baseline at {BASELINE.relative_to(ROOT)}; run --write-baseline", file=sys.stderr)+ return 2++ grew: list[str] = []+ shrank: list[str] = []+ for rel, sites in sorted(hits.items()):+ now = len(sites)+ allowed = baseline.get(rel, 0)+ if now > allowed:+ # Show the LAST sites: new code is usually appended, so these are the+ # likeliest culprits. The linter cannot know which sites are new+ # without a diff; --list prints them all.+ delta = now - allowed+ tail = ", ".join(f"{ln}:{c}" for ln, c in sites[-max(delta, 1):][-6:])+ grew.append(f" {rel}: grew by {delta} ({allowed} -> {now}); latest sites: {tail}"+ f" [run --list for all]")+ elif now < allowed:+ shrank.append(f" {rel}: {now} (baseline {allowed}) -- lower the baseline")+ for rel, allowed in sorted(baseline.items()):+ if rel not in hits and allowed > 0:+ shrank.append(f" {rel}: 0 (baseline {allowed}) -- remove the line")++ if grew:+ print("memory-core linter FAILED: raw allocator use grew. Allocate through")+ print("src/foundation/mem_core.h (cbm_alloc/cbm_calloc/cbm_realloc/cbm_free/")+ print("cbm_mem_strdup) instead of malloc/calloc/realloc/free/strdup.")+ print("\n".join(grew))+ if shrank:+ print("memory-core ratchet: these files improved; tighten scripts/memory-core-baseline.txt:")+ print("\n".join(shrank))+ if grew or (args.strict and shrank):+ return 1+ print(f"memory-core linter passed: {total} raw sites across {len(hits)} files, none grew")+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/memory-core-baseline.txt b/scripts/memory-core-baseline.txtnew file mode 100644index 000000000..67a2434d3--- /dev/null+++ b/scripts/memory-core-baseline.txt@@ -0,0 +1,97 @@+# memory-core-baseline.txt -- raw allocator call sites per file.+# Generated by scripts/lint-memory-core.py --write-baseline.+# A file may only ever go DOWN. Lower a line in the same change that+# migrates the file to src/foundation/mem_core.h; never raise one.++internal/cbm/ac.c 20+internal/cbm/arena.c 3+internal/cbm/cbm.c 13+internal/cbm/extract_defs.c 5+internal/cbm/helpers.c 3+internal/cbm/lsp/c_lsp.c 4+internal/cbm/lsp/perl_lsp.c 21+internal/cbm/lsp/py_lsp.c 2+internal/cbm/macro_table.c 1+internal/cbm/service_patterns.c 2+internal/cbm/sqlite_writer.c 92+src/cli/activation_transaction.c 80+src/cli/agent_clients.c 73+src/cli/agent_profiles.c 7+src/cli/cli.c 173+src/cli/client_adapter.c 3+src/cli/config_edit_path.c 2+src/cli/config_json_like.c 142+src/cli/config_text_edit.c 72+src/cli/config_toml_edit.c 85+src/cli/config_yaml_edit.c 117+src/cli/hook_augment.c 31+src/cypher/cypher.c 132+src/daemon/application.c 107+src/daemon/bootstrap.c 13+src/daemon/daemon.c 31+src/daemon/frontend.c 12+src/daemon/ipc.c 146+src/daemon/project_lock.c 6+src/daemon/runtime.c 45+src/daemon/service.c 6+src/daemon/version_cohort.c 9+src/discover/discover.c 31+src/discover/gitignore.c 12+src/discover/userconfig.c 15+src/foundation/diagnostics.c 7+src/foundation/dyn_array.h 4+src/foundation/hash_table.c 3+src/foundation/lock_registry.c 18+src/foundation/platform.c 11+src/foundation/platform.h 5+src/foundation/private_file_lock.c 56+src/foundation/str_intern.c 7+src/foundation/subprocess.c 24+src/foundation/win_utf8.h 17+src/foundation/workspace.c 3+src/foundation/yaml.c 10+src/git/git_context.c 18+src/graph_buffer/graph_buffer.c 83+src/main.c 32+src/mcp/compact_out.c 39+src/mcp/index_supervisor.c 8+src/mcp/mcp.c 814+src/pipeline/artifact.c 38+src/pipeline/fqn.c 23+src/pipeline/lsp_resolve.h 4+src/pipeline/lsp_surface.c 5+src/pipeline/pass_calls.c 23+src/pipeline/pass_compile_commands.c 17+src/pipeline/pass_complexity.c 15+src/pipeline/pass_configlink.c 3+src/pipeline/pass_cross_repo.c 8+src/pipeline/pass_definitions.c 14+src/pipeline/pass_enrichment.c 15+src/pipeline/pass_ensemble_routing.c 10+src/pipeline/pass_envscan.c 2+src/pipeline/pass_githistory.c 27+src/pipeline/pass_importance.c 4+src/pipeline/pass_infrascan.c 2+src/pipeline/pass_k8s.c 13+src/pipeline/pass_lsp_cross.c 50+src/pipeline/pass_parallel.c 31+src/pipeline/pass_pkgmap.c 82+src/pipeline/pass_semantic.c 13+src/pipeline/pass_semantic_edges.c 48+src/pipeline/pass_similarity.c 12+src/pipeline/pass_tests.c 3+src/pipeline/pass_usages.c 4+src/pipeline/path_alias.c 42+src/pipeline/pipeline.c 134+src/pipeline/pipeline_delta.c 19+src/pipeline/pipeline_incremental.c 144+src/pipeline/registry.c 19+src/pipeline/worker_pool.c 2+src/semantic/semantic.c 54+src/simhash/minhash.c 10+src/store/store.c 358+src/ui/config.c 12+src/ui/http_server.c 31+src/ui/httpd.c 21+src/ui/layout3d.c 66+src/watcher/watcher.c 33diff --git a/src/foundation/mem.c b/src/foundation/mem.cindex 61d13fe91..5f64bcb25 100644--- a/src/foundation/mem.c+++ b/src/foundation/mem.c@@ -514,6 +514,22 @@ bool cbm_mem_over_budget(void) {return rss > g_budget;}+/* Reclaimable memory below this share of total RAM is where paging starts to+ * hurt, so it is the point at which pressing on stops being reasonable. */+enum { MEM_PRESSURE_AVAIL_DIVISOR = 8 }; /* 12.5% of total RAM */++bool cbm_mem_system_under_pressure(void) {+ size_t available = cbm_system_available_ram();+ if (available == 0) {+ return false; /* platform cannot answer - do not abort on a guess */+ }+ cbm_system_info_t info = cbm_system_info();+ if (info.total_ram == 0) {+ return false;+ }+ return available < info.total_ram / MEM_PRESSURE_AVAIL_DIVISOR;+}+size_t cbm_mem_worker_budget(int num_workers) {if (num_workers <= 0) {num_workers = SKIP_ONE;diff --git a/src/foundation/mem.h b/src/foundation/mem.hindex bbd9d769f..826744789 100644--- a/src/foundation/mem.h+++ b/src/foundation/mem.h@@ -75,6 +75,21 @@ bool cbm_mem_over_budget(void);/* Per-worker budget hint: budget / num_workers. */size_t cbm_mem_worker_budget(int num_workers);+/* True only when the SYSTEM is genuinely short of memory, not merely when this+ * process is over its advisory budget.+ *+ * The budget is a static fraction of TOTAL ram, so on a large host it refuses+ * work the machine can plainly do: measured 2026-09-13, the linux kernel needs+ * 31.75 GB of a 48 GB host against a 24 GB budget, and the previous release+ * completed the very same index by overshooting to 33.56 GB. Aborting on the+ * budget alone therefore turned a working index into a refusal.+ *+ * So the budget keeps its job as the BACKPRESSURE trigger (workers park, peers+ * return transients) and this answers the different question of whether giving+ * up is warranted. Returns false when availability is unknown: never abort on+ * a guess. */+bool cbm_mem_system_under_pressure(void);+/* Return unused pages to the OS. Call between files to bound per-file peak. */void cbm_mem_collect(void);diff --git a/src/foundation/mem_core.c b/src/foundation/mem_core.cnew file mode 100644index 000000000..8120d7252--- /dev/null+++ b/src/foundation/mem_core.c@@ -0,0 +1,289 @@+/*+ * mem_core.c — the allocation route. See mem_core.h for why it exists.+ */+#include "foundation/mem_core.h"++#include "foundation/constants.h"+#include "foundation/log.h"++#include <stdatomic.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++/* Usable-size query, per platform.+ *+ * Deliberately NOT mi_usable_size: the mimalloc global override is off on+ * macOS (permanently — the two-level namespace aborts on cross-boundary+ * frees), so a pointer from plain malloc there is not a mimalloc block and+ * mi_usable_size would be undefined behaviour on it. Each platform's own query+ * is correct under whichever allocator is actually installed, including when+ * that allocator IS mimalloc via the Linux/MinGW override. */+#if defined(__APPLE__)+#include <malloc/malloc.h> /* malloc_size */+#define CBM_USABLE_SIZE(p) malloc_size(p)+#elif defined(_WIN32)+#include <malloc.h> /* _msize */+#define CBM_USABLE_SIZE(p) _msize((void *)(p))+#elif defined(__GLIBC__) || defined(__linux__)+#include <malloc.h> /* malloc_usable_size */+#define CBM_USABLE_SIZE(p) malloc_usable_size((void *)(p))+#else+/* BSD and anything unknown: no portable query. Accounting then tracks the+ * REQUESTED size, which understates by the rounding. Understating is the safe+ * direction for a diagnostic (it never invents memory), and the alternative --+ * a per-block header -- costs 1.6 GB at kernel scale. */+#define CBM_USABLE_SIZE_UNAVAILABLE 1+#endif++enum { MEM_CORE_REPORT_MIN = 64 };++typedef struct {+ atomic_size_t live_bytes;+ atomic_size_t live_blocks;+ atomic_size_t peak_bytes;+} mem_class_stats_t;++static mem_class_stats_t g_classes[CBM_MEM_CLASS_COUNT];++static const char *const g_class_names[CBM_MEM_CLASS_COUNT] = {+ "other", "gbuf_node", "gbuf_edge", "gbuf_string", "gbuf_index",+ "extract", "semantic", "dump", "store",+};++const char *cbm_mem_class_name(cbm_mem_class_t cls) {+ if ((int)cls < 0 || (int)cls >= CBM_MEM_CLASS_COUNT) {+ return "invalid";+ }+ return g_class_names[cls];+}++/* Out-of-range classes are folded into OTHER rather than rejected: a+ * mis-tagged allocation must still be freed correctly. Accounting accuracy is+ * worth less than not corrupting the heap. */+static mem_class_stats_t *class_slot(cbm_mem_class_t cls) {+ if ((int)cls < 0 || (int)cls >= CBM_MEM_CLASS_COUNT) {+ return &g_classes[CBM_MEM_CLASS_OTHER];+ }+ return &g_classes[cls];+}++static void class_add(cbm_mem_class_t cls, size_t bytes, size_t blocks) {+ mem_class_stats_t *st = class_slot(cls);+ size_t now = atomic_fetch_add_explicit(&st->live_bytes, bytes, memory_order_relaxed) + bytes;+ if (blocks) {+ (void)atomic_fetch_add_explicit(&st->live_blocks, blocks, memory_order_relaxed);+ }+ /* Peak is best-effort under concurrency: a CAS loop here would serialise+ * every allocation in the hot path to make a DIAGNOSTIC exact. Racing+ * writers can leave the peak one increment low; that never changes a+ * decision, and the cost of exactness would. */+ size_t seen = atomic_load_explicit(&st->peak_bytes, memory_order_relaxed);+ while (now > seen) {+ if (atomic_compare_exchange_weak_explicit(&st->peak_bytes, &seen, now, memory_order_relaxed,+ memory_order_relaxed)) {+ break;+ }+ }+}++static void class_sub(cbm_mem_class_t cls, size_t bytes, size_t blocks) {+ mem_class_stats_t *st = class_slot(cls);+ /* Never wrap. A mismatched class on free (the one way a caller can get+ * this wrong) would otherwise turn a small drift into a colossal bogus+ * number that looks like a leak and sends someone hunting a phantom. */+ size_t seen = atomic_load_explicit(&st->live_bytes, memory_order_relaxed);+ while (seen > 0) {+ size_t want = bytes > seen ? 0 : seen - bytes;+ if (atomic_compare_exchange_weak_explicit(&st->live_bytes, &seen, want,+ memory_order_relaxed, memory_order_relaxed)) {+ break;+ }+ }+ if (blocks) {+ size_t b = atomic_load_explicit(&st->live_blocks, memory_order_relaxed);+ while (b > 0) {+ if (atomic_compare_exchange_weak_explicit(&st->live_blocks, &b, b - 1,+ memory_order_relaxed, memory_order_relaxed)) {+ break;+ }+ }+ }+}++/* Charge what the allocator actually handed us where the platform can say, and+ * fall back to the request otherwise. Called once per successful allocation.+ * Two definitions rather than one with a constant-folded branch: on a platform+ * with no usable-size query the fallback simply IS the request, and cppcheck+ * rightly objects to a condition that can never be true. */+#ifdef CBM_USABLE_SIZE_UNAVAILABLE+static size_t charge_size(const void *block, size_t requested) {+ (void)block;+ return requested;+}++size_t cbm_mem_usable_size(const void *block) {+ (void)block;+ return 0;+}+#else+static size_t charge_size(const void *block, size_t requested) {+ size_t usable = CBM_USABLE_SIZE(block);+ return usable ? usable : requested;+}++size_t cbm_mem_usable_size(const void *block) {+ if (!block) {+ return 0;+ }+ return CBM_USABLE_SIZE(block);+}+#endif++void *cbm_alloc(cbm_mem_class_t cls, size_t bytes) {+ void *block = malloc(bytes ? bytes : CBM_ALLOC_ONE);+ if (!block) {+ return NULL;+ }+ class_add(cls, charge_size(block, bytes), CBM_ALLOC_ONE);+ return block;+}++void *cbm_calloc(cbm_mem_class_t cls, size_t bytes) {+ void *block = calloc(CBM_ALLOC_ONE, bytes ? bytes : CBM_ALLOC_ONE);+ if (!block) {+ return NULL;+ }+ class_add(cls, charge_size(block, bytes), CBM_ALLOC_ONE);+ return block;+}++void *cbm_realloc(cbm_mem_class_t cls, void *block, size_t bytes) {+ if (!block) {+ return cbm_alloc(cls, bytes);+ }+ /* Measure BEFORE: after realloc the old block is gone and its size is+ * unknowable, so the decrement has to be computed first. */+ size_t old = charge_size(block, 0);+ void *next = realloc(block, bytes ? bytes : CBM_ALLOC_ONE);+ if (!next) {+ return NULL; /* original intact and still charged - correct */+ }+ class_sub(cls, old, 0);+ class_add(cls, charge_size(next, bytes), 0);+ return next;+}++char *cbm_mem_strdup(cbm_mem_class_t cls, const char *s) {+ if (!s) {+ return NULL;+ }+ size_t len = strlen(s) + CBM_ALLOC_ONE;+ char *copy = (char *)cbm_alloc(cls, len);+ if (!copy) {+ return NULL;+ }+ memcpy(copy, s, len);+ return copy;+}++void cbm_free(cbm_mem_class_t cls, void *block) {+ if (!block) {+ return;+ }+ class_sub(cls, charge_size(block, 0), CBM_ALLOC_ONE);+ free(block);+}++void cbm_mem_class_add_external(cbm_mem_class_t cls, size_t bytes) {+ class_add(cls, bytes, 0);+}++void cbm_mem_class_remove_external(cbm_mem_class_t cls, size_t bytes) {+ class_sub(cls, bytes, 0);+}++size_t cbm_mem_class_live_bytes(cbm_mem_class_t cls) {+ return atomic_load_explicit(&class_slot(cls)->live_bytes, memory_order_relaxed);+}++size_t cbm_mem_class_live_blocks(cbm_mem_class_t cls) {+ return atomic_load_explicit(&class_slot(cls)->live_blocks, memory_order_relaxed);+}++size_t cbm_mem_class_peak_bytes(cbm_mem_class_t cls) {+ return atomic_load_explicit(&class_slot(cls)->peak_bytes, memory_order_relaxed);+}++size_t cbm_mem_tracked_live_bytes(void) {+ size_t total = 0;+ for (int i = 0; i < CBM_MEM_CLASS_COUNT; i++) {+ total += atomic_load_explicit(&g_classes[i].live_bytes, memory_order_relaxed);+ }+ return total;+}++void cbm_mem_class_reset_peaks(void) {+ for (int i = 0; i < CBM_MEM_CLASS_COUNT; i++) {+ size_t live = atomic_load_explicit(&g_classes[i].live_bytes, memory_order_relaxed);+ atomic_store_explicit(&g_classes[i].peak_bytes, live, memory_order_relaxed);+ }+}++int cbm_mem_class_report_json(char *out, size_t size) {+ if (!out || size < MEM_CORE_REPORT_MIN) {+ return 0;+ }+ int order[CBM_MEM_CLASS_COUNT];+ int n = 0;+ for (int i = 0; i < CBM_MEM_CLASS_COUNT; i++) {+ if (atomic_load_explicit(&g_classes[i].peak_bytes, memory_order_relaxed) > 0) {+ order[n++] = i;+ }+ }+ if (n == 0) {+ return 0;+ }+ /* Insertion sort: n is at most CBM_MEM_CLASS_COUNT. */+ for (int i = 1; i < n; i++) {+ int key = order[i];+ size_t kv = atomic_load_explicit(&g_classes[key].live_bytes, memory_order_relaxed);+ int j = i - 1;+ while (j >= 0 &&+ atomic_load_explicit(&g_classes[order[j]].live_bytes, memory_order_relaxed) < kv) {+ order[j + 1] = order[j];+ j--;+ }+ order[j + 1] = key;+ }++ int written = snprintf(out, size, "[");+ for (int i = 0; i < n && written > 0 && (size_t)written < size; i++) {+ int idx = order[i];+ int add = snprintf(out + written, size - (size_t)written,+ "%s{\"class\":\"%s\",\"live_bytes\":%zu,\"live_blocks\":%zu,"+ "\"peak_bytes\":%zu}",+ i ? "," : "", g_class_names[idx],+ atomic_load_explicit(&g_classes[idx].live_bytes, memory_order_relaxed),+ atomic_load_explicit(&g_classes[idx].live_blocks, memory_order_relaxed),+ atomic_load_explicit(&g_classes[idx].peak_bytes, memory_order_relaxed));+ if (add < 0 || (size_t)(written + add) >= size) {+ return 0; /* truncated: a partial JSON array is worse than none */+ }+ written += add;+ }+ if ((size_t)written + CBM_ALLOC_ONE >= size) {+ return 0;+ }+ out[written++] = ']';+ out[written] = '\0';+ return written;+}++void cbm_mem_class_log(const char *tag) {+ char report[CBM_SZ_1K];+ if (cbm_mem_class_report_json(report, sizeof(report)) <= 0) {+ return;+ }+ cbm_log_info("mem.classes", "tag", tag ? tag : "-", "classes", report);+}diff --git a/src/foundation/mem_core.h b/src/foundation/mem_core.hnew file mode 100644index 000000000..042184ba8--- /dev/null+++ b/src/foundation/mem_core.h@@ -0,0 +1,140 @@+/*+ * mem_core.h — THE allocation route.+ *+ * Memory in this project is allocated through one core, not through ~800+ * scattered malloc/calloc/strdup sites. This header is that core;+ * foundation/mem.h remains policy and measurement (budget, RSS, pressure,+ * phase marks) and deliberately owns no allocation.+ *+ * WHY THIS EXISTS (audited 2026-09-13)+ *+ * The budget could only ever be OBSERVED, never enforced. cbm_mem_over_budget()+ * reads process RSS *after* the allocation that crossed the line already+ * succeeded, and the only available response was to nap. A thermostat wired to+ * a thermometer with no cooler attached.+ *+ * Worse, we could not even say WHERE the memory was. Two independent reasons:+ *+ * 1. cbm_mem_map_collect()'s live_bytes walks mi_theap_get_default() — THIS+ * thread's mimalloc heap only. Walking the process-wide mi_heap_main() is+ * a data race that TSan caught on macOS, so it is deliberately not done.+ * An 18-worker index therefore attributes almost nothing; the rest lands+ * in `residual`.+ * 2. The mimalloc global override is ON for Linux/MinGW and permanently OFF+ * for macOS (the two-level namespace turns this binary's free into mi_free+ * while system libraries keep allocating from the system zone, and a+ * pointer crossing that boundary aborts). On macOS ordinary malloc is+ * served by the SYSTEM allocator: the startup audit reports+ * owned_classes=0/6. Any accounting that assumes mimalloc owns the pointer+ * is blind on an entire platform.+ *+ * So the core keeps its OWN counters. Atomic, per class, incremented at+ * allocation and decremented at free. Thread-safe by construction, identical on+ * every platform, and independent of which allocator actually serves malloc.+ *+ * NO PER-ALLOCATION HEADER. The obvious design — a {class,size} prefix — costs+ * 16 bytes on every block, and the graph buffer makes ~100M of them at kernel+ * scale: ~1.6 GB of pure overhead to measure a memory problem. Sizes come from+ * the platform's usable-size query instead, which is exact-to-the-bucket, free,+ * and correct under either allocator.+ */+#ifndef CBM_MEM_CORE_H+#define CBM_MEM_CORE_H++#include <stdbool.h>+#include <stddef.h>++/* Allocation classes.+ *+ * A class is a BUDGETING bucket, not a type taxonomy: split only where the+ * split would change a decision. These follow the measured phase profile of a+ * kernel index (2026-09-13), where the peak was 35.20 GB and the two consumers+ * behaved differently — extraction transients scale with worker count, the+ * semantic plateau does not. Attribution that cannot separate those two cannot+ * choose between "park workers" and "stream the vectors". */+typedef enum {+ CBM_MEM_CLASS_OTHER = 0, /* unclassified; the residual to drive down */+ CBM_MEM_CLASS_GBUF_NODE, /* node records (~64 B each) */+ CBM_MEM_CLASS_GBUF_EDGE, /* edge records (~48 B each) */+ CBM_MEM_CLASS_GBUF_STRING, /* name / qualified_name / properties_json */+ CBM_MEM_CLASS_GBUF_INDEX, /* the 8 lookup indexes (~3.77 GB at kernel scale) */+ CBM_MEM_CLASS_EXTRACT, /* per-file working set: arena, tree, source */+ CBM_MEM_CLASS_SEMANTIC, /* embedding vectors (~9.4 GB at kernel scale) */+ CBM_MEM_CLASS_DUMP, /* dump-time transients */+ CBM_MEM_CLASS_STORE, /* store batches and row buffers */+ CBM_MEM_CLASS_COUNT+} cbm_mem_class_t;++/* Stable lowercase name, for logs and JSON. Never NULL, even out of range. */+const char *cbm_mem_class_name(cbm_mem_class_t cls);++/* ── The allocation route ──────────────────────────────────────────────+ *+ * Semantics match the C library exactly, so adoption is a mechanical rename and+ * never a behaviour change:+ * - cbm_alloc(cls, 0) returns a non-NULL pointer that is valid to free+ * - cbm_free(cls, NULL) is a no-op+ * - cbm_realloc(cls, NULL, n) behaves as cbm_alloc+ * - a failed realloc leaves the original block intact and returns NULL+ *+ * The class passed to free/realloc MUST be the class the block was allocated+ * with, or the counters drift. Pass the class through alongside the pointer,+ * the same way a custom deleter would. */+void *cbm_alloc(cbm_mem_class_t cls, size_t bytes);+void *cbm_calloc(cbm_mem_class_t cls, size_t bytes);+void *cbm_realloc(cbm_mem_class_t cls, void *block, size_t bytes);+char *cbm_mem_strdup(cbm_mem_class_t cls, const char *s);+void cbm_free(cbm_mem_class_t cls, void *block);++/* ── Accounting ────────────────────────────────────────────────────────+ *+ * live_bytes is what the ALLOCATOR handed us (usable size), so it exceeds the+ * bytes requested by the per-block rounding and is the honest number for a+ * memory budget: rounding is memory the process cannot use for anything else.+ *+ * These counters see only memory that went through this core. Everything still+ * on raw malloc is invisible here — which is the point of+ * cbm_mem_tracked_live_bytes() versus the process RSS in mem.h: the gap between+ * them IS the unmigrated surface, and it should shrink as adoption spreads. An+ * unmeasured allocation must never read as an absent one. */+size_t cbm_mem_class_live_bytes(cbm_mem_class_t cls);+size_t cbm_mem_class_live_blocks(cbm_mem_class_t cls);+size_t cbm_mem_tracked_live_bytes(void);++/* Peak live bytes for a class since process start (or the last reset). The+ * budget question is always about the PEAK, never the value at the moment+ * someone happened to look. */+size_t cbm_mem_class_peak_bytes(cbm_mem_class_t cls);++/* Drop all peaks to the current live values. For a measurement run that wants+ * one phase, not the whole process history. Never resets live counters --+ * those track real outstanding blocks. */+void cbm_mem_class_reset_peaks(void);++/* JSON array of {class, live_bytes, live_blocks, peak_bytes}, biggest live+ * first, classes with no activity omitted. Returns bytes written, 0 if none. */+int cbm_mem_class_report_json(char *out, size_t size);++/* Log the class table at info level under `tag`. For phase boundaries in the+ * index pipeline, where the interesting question is which class grew. */+void cbm_mem_class_log(const char *tag);++/* Usable size of a block obtained from this core, or 0 when the platform+ * cannot answer. Exposed because the same query is what makes header-free+ * accounting possible, and callers doing their own bulk accounting (arenas)+ * need the identical definition to stay consistent with these counters. */+size_t cbm_mem_usable_size(const void *block);++/* ── Bulk accounting, for allocators that are not this one ─────────────+ *+ * The extraction engine already allocates through arenas (1301 call sites) and+ * must NOT be rewritten to per-object cbm_alloc — that would undo the very+ * batching that keeps its allocation count low. Instead an arena reports its+ * block acquisitions here, so arena-backed memory appears in the same table as+ * heap memory and the totals stay comparable.+ *+ * Symmetric: every add must be matched by a remove of the same size. */+void cbm_mem_class_add_external(cbm_mem_class_t cls, size_t bytes);+void cbm_mem_class_remove_external(cbm_mem_class_t cls, size_t bytes);++#endif /* CBM_MEM_CORE_H */diff --git a/src/foundation/platform.h b/src/foundation/platform.hindex cc58e8429..7e9d3ff29 100644--- a/src/foundation/platform.h+++ b/src/foundation/platform.h@@ -116,6 +116,10 @@ typedef struct {/* Query system information. Results are cached after first call. */cbm_system_info_t cbm_system_info(void);+/* Physical memory the system could hand out right now, or 0 when the platform+ * cannot answer. NOT cached - it changes during a run, which is the point. */+size_t cbm_system_available_ram(void);+/* Recommended worker count for parallel indexing.* initial=true: all cores (user is waiting for initial index)* initial=false: max(1, perf_cores-1) (leave headroom for user apps) */diff --git a/src/foundation/system_info.c b/src/foundation/system_info.cindex 8ba237f6c..489a7dc65 100644--- a/src/foundation/system_info.c+++ b/src/foundation/system_info.c@@ -26,6 +26,7 @@ enum { DEFAULT_CORES = 1, MIN_WORKERS = 1, CBM_WORKERS_MAX = 256 };#endif#include <windows.h>#elif defined(__APPLE__)+#include <mach/mach.h> /* host_statistics64 - reclaimable page accounting */#include <sys/sysctl.h>#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)#include <unistd.h>@@ -304,3 +305,64 @@ int cbm_default_worker_count(bool initial) {int workers = info.perf_cores - SKIP_ONE;return workers > 0 ? workers : MIN_WORKERS;}++/* -- Available RAM --------------------------------------------------+ *+ * Bytes the system could hand out right now, or 0 when the platform cannot+ * answer. Deliberately NOT cached: core counts and total RAM are immutable+ * hardware facts, but this changes continuously and the entire point is to+ * observe it DURING an index.+ *+ * Why this exists: the indexer decided "out of memory" by comparing its own+ * RSS against a static fraction of TOTAL ram. That refuses work a machine can+ * plainly do -- measured 2026-09-13, the linux kernel needs 31.75 GB on a+ * 48 GB host (66 percent) against a 0.5 default budget, and the same index+ * completed on the previous release by overshooting to 33.56 GB. Whether+ * memory is actually scarce is a property of the SYSTEM, not of a constant. */+size_t cbm_system_available_ram(void) {+#ifdef _WIN32+ MEMORYSTATUSEX status;+ status.dwLength = sizeof(status);+ if (GlobalMemoryStatusEx(&status)) {+ return (size_t)status.ullAvailPhys;+ }+ return 0;+#elif defined(__APPLE__)+ /* free + inactive + purgeable. Counting only free_count would report+ * pressure on any machine that is merely warm, because inactive and+ * purgeable pages are reclaimed on demand. */+ mach_port_t host = mach_host_self();+ vm_size_t page_size = 0;+ if (host_page_size(host, &page_size) != KERN_SUCCESS || page_size == 0) {+ return 0;+ }+ vm_statistics64_data_t vm_stat = {0};+ mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;+ if (host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stat, &count) != KERN_SUCCESS) {+ return 0;+ }+ uint64_t pages = (uint64_t)vm_stat.free_count + (uint64_t)vm_stat.inactive_count ++ (uint64_t)vm_stat.purgeable_count;+ return (size_t)(pages * (uint64_t)page_size);+#elif !defined(__NetBSD__) && !defined(__FreeBSD__) && !defined(__OpenBSD__)+ /* Linux: MemAvailable is the kernel estimate and accounts for reclaimable+ * slab and page cache, which MemFree does not. */+ FILE *meminfo = fopen("/proc/meminfo", "re");+ if (!meminfo) {+ return 0;+ }+ char line[CBM_SZ_256];+ size_t available = 0;+ while (fgets(line, sizeof(line), meminfo) != NULL) {+ unsigned long long kb = 0;+ if (sscanf(line, "MemAvailable: %llu kB", &kb) == 1) {+ available = (size_t)(kb * (unsigned long long)CBM_SZ_1K);+ break;+ }+ }+ (void)fclose(meminfo);+ return available;+#else+ return 0; /* BSD: unknown rather than guessed */+#endif+}diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.cindex 25b1c2eb7..d87e5f90f 100644--- a/src/pipeline/pipeline.c+++ b/src/pipeline/pipeline.c@@ -37,6 +37,7 @@ enum { CBM_DIR_PERMS = 0755, PL_RING = 4, PL_RING_MASK = 3, PL_SEQ_PASSES = 6 };#include "foundation/compat_thread.h"#include "foundation/profile.h"#include "foundation/mem.h"+#include "foundation/mem_core.h"#include "foundation/secure_random.h"#include <ctype.h>@@ -988,6 +989,23 @@ static void predump_importance(cbm_pipeline_ctx_t *ctx) {cbm_pipeline_pass_importance(ctx);}+/* Phase boundary for memory attribution. Two instruments, both already in+ * foundation/, both previously wired ONLY into MCP request handling and never+ * into the index pipeline -- which is where the memory is (a kernel index+ * peaks at 35 GB in extraction, measured 2026-09-13 with an external sampler+ * because nothing in-process could say which phase it was in):+ * - cbm_mem_phase_mark attributes the committed-bytes delta since the last+ * mark to the phase just ended. Off unless CBM_MEM_PHASES=1.+ * - cbm_mem_class_log prints the mem_core class table, so the log answers+ * WHICH class grew in WHICH pass. Logs nothing until a class has activity,+ * so it is silent on a tree that has not migrated yet.+ * Marks must bracket the whole path with no unlabelled gaps (mem.h), hence a+ * mark at every pass.timing site plus pipeline.begin at the top. */+static void pipeline_phase_mark(const char *pass) {+ cbm_mem_phase_mark(pass);+ cbm_mem_class_log(pass);+}+static void run_predump_passes(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {static const struct {predump_pass_fn fn;@@ -1024,6 +1042,7 @@ static void run_predump_passes(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {passes[i].fn(ctx);cbm_log_info("pass.timing", "pass", passes[i].name, "elapsed_ms",itoa_buf((int)elapsed_ms(t)));+ pipeline_phase_mark(passes[i].name);}}@@ -1145,6 +1164,7 @@ static int run_sequential_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,}cbm_log_info("pass.timing", "pass", seq_passes[si].name, "elapsed_ms",itoa_buf((int)elapsed_ms(*t)));+ pipeline_phase_mark(seq_passes[si].name);if (check_cancel(p)) {rc = CBM_NOT_FOUND;}@@ -1222,6 +1242,7 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,int rc = cbm_parallel_extract(ctx, files, file_count, cache, &shared_ids, worker_count);cbm_log_info("pass.timing", "pass", "parallel_extract", "elapsed_ms",itoa_buf((int)elapsed_ms(*t)));+ pipeline_phase_mark("parallel_extract");if (rc != 0 || check_cancel(p)) {for (int i = 0; i < file_count; i++) {cbm_free_result(cache[i]);@@ -2238,6 +2259,7 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_hash_t *bas}cbm_log_info("pass.timing", "pass", "dump_and_persist", "elapsed_ms",itoa_buf((int)elapsed_ms(*t)), "files", itoa_buf(manifest_count));+ pipeline_phase_mark("dump_and_persist");if (p->ignored_total > p->ignored_count) {cbm_log_warn("index.ignored_capped", "stored", itoa_buf(p->ignored_count), "total",itoa_buf(p->ignored_total));@@ -2302,6 +2324,7 @@ static int run_tests_and_history(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,int rc = cbm_pipeline_pass_tests(ctx, files, file_count);CBM_PROF_END_N("pipeline", "pass_tests", t_tests, file_count);cbm_log_info("pass.timing", "pass", "tests", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));+ pipeline_phase_mark("tests");if (rc == 0 && !check_cancel(p)) {CBM_PROF_START(t_gh);rc = run_githistory(p, ctx);@@ -3040,6 +3063,12 @@ static void sweep_orphan_stages(const char *final_path) {}int cbm_pipeline_run(cbm_pipeline_t *p) {+ /* Per-index attribution: peaks and phase totals are about THIS index, not+ * the process history, so they start clean here. The first mark opens+ * the labelled path; every pass.timing site below closes a phase. */+ cbm_mem_class_reset_peaks();+ cbm_mem_phase_reset();+ cbm_mem_phase_mark("pipeline.begin");if (!p) {return CBM_NOT_FOUND;}diff --git a/tests/test_mem.c b/tests/test_mem.cindex 7db92b735..5ac8abe43 100644--- a/tests/test_mem.c+++ b/tests/test_mem.c@@ -6,6 +6,7 @@#include "test_framework.h"#include "test_helpers.h"#include "../src/foundation/mem.h"+#include "../src/foundation/mem_core.h"#include "../src/foundation/arena.h"#include "../src/foundation/slab_alloc.h"#include "../src/foundation/compat_thread.h"@@ -1336,6 +1337,163 @@ TEST(extract_traversal_stacks_come_from_ctx_scratch_issue2010) {PASS();}+/* ── mem_core: the central allocation route ────────────────────────────+ *+ * Every assertion below is a DELTA, never an absolute. Other code in this+ * process may allocate through the core concurrently, so a test that pinned an+ * absolute total would be measuring the rest of the suite. */++TEST(mem_core_accounts_alloc_and_free) {+ size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_NODE);+ size_t blocks_before = cbm_mem_class_live_blocks(CBM_MEM_CLASS_GBUF_NODE);++ void *p = cbm_alloc(CBM_MEM_CLASS_GBUF_NODE, 4096);+ ASSERT_TRUE(p != NULL);+ size_t during = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_NODE);+ /* Charged at least what was asked for; usable size may round UP, never+ * down, so a strict >= is the honest assertion. */+ ASSERT_TRUE(during >= before + 4096);+ ASSERT_EQ((int)(cbm_mem_class_live_blocks(CBM_MEM_CLASS_GBUF_NODE) - blocks_before), 1);++ cbm_free(CBM_MEM_CLASS_GBUF_NODE, p);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_NODE) - before), 0);+ ASSERT_EQ((int)(cbm_mem_class_live_blocks(CBM_MEM_CLASS_GBUF_NODE) - blocks_before), 0);+ PASS();+}++/* The whole point of classes: attribution. If a gbuf allocation could show up+ * under semantic, the table could not choose between "park workers" and+ * "stream the vectors" -- the decision this core exists to inform. */+TEST(mem_core_classes_do_not_bleed) {+ size_t node_before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_NODE);+ size_t sem_before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_SEMANTIC);++ void *p = cbm_alloc(CBM_MEM_CLASS_SEMANTIC, 8192);+ ASSERT_TRUE(p != NULL);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_NODE) - node_before), 0);+ ASSERT_TRUE(cbm_mem_class_live_bytes(CBM_MEM_CLASS_SEMANTIC) >= sem_before + 8192);+ cbm_free(CBM_MEM_CLASS_SEMANTIC, p);+ PASS();+}++TEST(mem_core_realloc_replaces_the_old_charge) {+ size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_DUMP);+ void *p = cbm_alloc(CBM_MEM_CLASS_DUMP, 1024);+ ASSERT_TRUE(p != NULL);+ p = cbm_realloc(CBM_MEM_CLASS_DUMP, p, 65536);+ ASSERT_TRUE(p != NULL);+ size_t grown = cbm_mem_class_live_bytes(CBM_MEM_CLASS_DUMP);+ /* The old 1024 must be gone, not stacked on top: exactly one block is live,+ * so the delta is bounded by the new size plus rounding, not by the sum. */+ ASSERT_TRUE(grown >= before + 65536);+ ASSERT_TRUE(grown < before + 65536 + 65536);+ cbm_free(CBM_MEM_CLASS_DUMP, p);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_DUMP) - before), 0);+ PASS();+}++/* realloc(NULL) is alloc, and free(NULL) is a no-op: the core must match the C+ * library exactly or adoption stops being a mechanical rename. */+TEST(mem_core_matches_libc_null_semantics) {+ size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_STORE);+ cbm_free(CBM_MEM_CLASS_STORE, NULL);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_STORE) - before), 0);++ void *p = cbm_realloc(CBM_MEM_CLASS_STORE, NULL, 2048);+ ASSERT_TRUE(p != NULL);+ ASSERT_TRUE(cbm_mem_class_live_bytes(CBM_MEM_CLASS_STORE) >= before + 2048);+ cbm_free(CBM_MEM_CLASS_STORE, p);++ /* A zero-size request still yields a freeable pointer. */+ void *z = cbm_alloc(CBM_MEM_CLASS_STORE, 0);+ ASSERT_TRUE(z != NULL);+ cbm_free(CBM_MEM_CLASS_STORE, z);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_STORE) - before), 0);+ PASS();+}++TEST(mem_core_strdup_copies_and_accounts) {+ size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_STRING);+ ASSERT_TRUE(cbm_mem_strdup(CBM_MEM_CLASS_GBUF_STRING, NULL) == NULL);++ const char *src = "qualified::name::example";+ char *copy = cbm_mem_strdup(CBM_MEM_CLASS_GBUF_STRING, src);+ ASSERT_TRUE(copy != NULL);+ ASSERT_TRUE(strcmp(copy, src) == 0);+ ASSERT_TRUE(copy != src);+ ASSERT_TRUE(cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_STRING) > before);+ cbm_free(CBM_MEM_CLASS_GBUF_STRING, copy);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_GBUF_STRING) - before), 0);+ PASS();+}++/* A budget decision is about the PEAK, not about whatever was live when+ * someone looked. Peak must survive the free that follows it. */+TEST(mem_core_peak_survives_the_free) {+ cbm_mem_class_reset_peaks();+ size_t base = cbm_mem_class_peak_bytes(CBM_MEM_CLASS_EXTRACT);+ void *p = cbm_alloc(CBM_MEM_CLASS_EXTRACT, 32768);+ ASSERT_TRUE(p != NULL);+ size_t peak_live = cbm_mem_class_peak_bytes(CBM_MEM_CLASS_EXTRACT);+ ASSERT_TRUE(peak_live >= base + 32768);+ cbm_free(CBM_MEM_CLASS_EXTRACT, p);+ ASSERT_EQ((int)(cbm_mem_class_peak_bytes(CBM_MEM_CLASS_EXTRACT) - peak_live), 0);+ PASS();+}++/* Arena-backed memory reports in bulk rather than per object: the extraction+ * engine has 1301 arena call sites and rewriting them to per-object cbm_alloc+ * would undo the batching that keeps its allocation count low. */+TEST(mem_core_external_bulk_accounting_is_symmetric) {+ size_t before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_EXTRACT);+ cbm_mem_class_add_external(CBM_MEM_CLASS_EXTRACT, 1024 * 1024);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_EXTRACT) - before), 1024 * 1024);+ cbm_mem_class_remove_external(CBM_MEM_CLASS_EXTRACT, 1024 * 1024);+ ASSERT_EQ((int)(cbm_mem_class_live_bytes(CBM_MEM_CLASS_EXTRACT) - before), 0);+ PASS();+}++/* The one mistake a caller can actually make is freeing with the wrong class.+ * That must never underflow the counter: an unsigned wrap would turn a small+ * drift into a colossal bogus total that reads as a catastrophic leak and+ * sends someone hunting a phantom. Clamp at zero instead. */+TEST(mem_core_mismatched_class_never_wraps) {+ void *p = cbm_alloc(CBM_MEM_CLASS_GBUF_EDGE, 4096);+ ASSERT_TRUE(p != NULL);+ /* Free against a class that was never charged for it. */+ size_t other_before = cbm_mem_class_live_bytes(CBM_MEM_CLASS_DUMP);+ cbm_free(CBM_MEM_CLASS_DUMP, p);+ size_t other_after = cbm_mem_class_live_bytes(CBM_MEM_CLASS_DUMP);+ ASSERT_TRUE(other_after <= other_before); /* clamped, never wrapped */+ ASSERT_TRUE(other_after < (size_t)-1 / 2);+ PASS();+}++TEST(mem_core_report_json_is_wellformed_or_empty) {+ void *p = cbm_alloc(CBM_MEM_CLASS_GBUF_INDEX, 4096);+ ASSERT_TRUE(p != NULL);+ char buf[CBM_SZ_1K];+ int n = cbm_mem_class_report_json(buf, sizeof(buf));+ ASSERT_TRUE(n > 0);+ ASSERT_TRUE(buf[0] == '[');+ ASSERT_TRUE(buf[n - 1] == ']');+ ASSERT_TRUE(strstr(buf, "gbuf_index") != NULL);+ /* A buffer too small must yield NOTHING, never a truncated array that a+ * JSON reader would reject or, worse, silently mis-parse. */+ char tiny[8];+ ASSERT_EQ(cbm_mem_class_report_json(tiny, sizeof(tiny)), 0);+ cbm_free(CBM_MEM_CLASS_GBUF_INDEX, p);+ PASS();+}++TEST(mem_core_class_names_are_total) {+ ASSERT_TRUE(strcmp(cbm_mem_class_name(CBM_MEM_CLASS_SEMANTIC), "semantic") == 0);+ /* Out of range must still answer, so a log line never takes a NULL. */+ ASSERT_TRUE(cbm_mem_class_name((cbm_mem_class_t)(CBM_MEM_CLASS_COUNT + 5)) != NULL);+ ASSERT_TRUE(cbm_mem_class_name((cbm_mem_class_t)-1) != NULL);+ PASS();+}+SUITE(mem) {/* mem API */RUN_TEST(mem_arena_eager_commit_follows_platform_commit_cost);@@ -1402,4 +1560,14 @@ SUITE(mem) {/* extraction scratch arena (#2010) */RUN_TEST(extract_traversal_stacks_come_from_ctx_scratch_issue2010);+ RUN_TEST(mem_core_accounts_alloc_and_free);+ RUN_TEST(mem_core_classes_do_not_bleed);+ RUN_TEST(mem_core_realloc_replaces_the_old_charge);+ RUN_TEST(mem_core_matches_libc_null_semantics);+ RUN_TEST(mem_core_strdup_copies_and_accounts);+ RUN_TEST(mem_core_peak_survives_the_free);+ RUN_TEST(mem_core_external_bulk_accounting_is_symmetric);+ RUN_TEST(mem_core_mismatched_class_never_wraps);+ RUN_TEST(mem_core_report_json_is_wellformed_or_empty);+ RUN_TEST(mem_core_class_names_are_total);}