rt_malloc was a bare mmap-per-call wrapper: every allocation, even a 32-72B AST/symbol node, consumed a page-rounded 4KB region and was never freed. w6a/w6c emit ~4 such nodes per .s line, so assembling a 145K-line file burned ~625K pages (~2.5GB); test 995's 5 concurrent self-rebuilds then OOM'd. The defect is linear and str-independent -- the str->24B codegen merely enlarged .s files past the cliff. Replace it with the no-free SUBSET of Hare's allocator (ref/hare/rt/malloc.ha): 2MiB chunk-bump (CHUNKSZ malloc.ha:24, ALIGN malloc.ha:14), oversized (>CHUNKSZ) requests direct-mmap'd. The bin/freelist/META machinery exists only to support free, which ww does not have, so it is omitted. Policy lives in rt/malloc.ww; the raw mmap primitive stays in rt/alloc.s as rt_segmalloc -- Hare's malloc/segmalloc split. rt_free becomes a documented no-op (os.free re-exports it for the public API, so the symbol must stay); rt/ensure.ww drops its now-impossible reclaim. Zero-init is preserved: the bump never reuses memory, so every byte is fresh MAP_ANONYMOUS-zeroed. w6a_ww on a 145K-line .s: 2165MB -> 28MB (~glibc parity, C w6a 24MB). .o output byte-identical; both stages emit identical asm. make test 134/134.
53 lines
1.7 KiB
Plaintext
53 lines
1.7 KiB
Plaintext
// rt/ensure.ww — slice growth helper, archived into libwwrt.a.
|
|
//
|
|
// Companion to the `append(s, v)` builtin. The compiler lowers
|
|
// `append(s, v)` to:
|
|
//
|
|
// ; push v
|
|
// ; s.len += 1
|
|
// ; CALL rt_ensure(&s, sizeof(elem))
|
|
// ; ; ensure may have realloc'd, so re-read s.ptr
|
|
// ; pop v
|
|
// ; *(s.ptr + (s.len - 1) * elem_size) = v
|
|
//
|
|
// One helper handles every element width via the membsz parameter —
|
|
// no per-type wrapper functions (appendu8 / appendi64) needed.
|
|
//
|
|
// User code never `use`s this — the symbol is resolved at link time
|
|
// from libwwrt.a, like rt_malloc and rt_streq. No `module` declaration:
|
|
// rt/ensure.ww is compiled standalone via `w6c rt/ensure.ww` (not
|
|
// through the driver), and its `export fn rt_ensure` must keep its
|
|
// bare symbol name so the linker resolves it.
|
|
|
|
@symbol("rt_malloc") fn malloc(n: u64) *void;
|
|
|
|
// Mirrors ww's []T header layout: 24 bytes with 8-byte slots.
|
|
// ww's source uses i32 for len/cap but the compiler stores them in
|
|
// 8-byte slots; declaring as i64 here keeps the field offsets right
|
|
// for this polymorphic alias.
|
|
type slice = struct {
|
|
ptr: *u8,
|
|
len: i64,
|
|
cap: i64,
|
|
};
|
|
|
|
export fn rt_ensure(s: *slice, membsz: u64) void = {
|
|
if (s.cap >= s.len) { return; };
|
|
let nc: i64 = s.cap * 2i64;
|
|
if (nc < 8i64) { nc = 8i64; };
|
|
for (nc < s.len) { nc *= 2i64; };
|
|
// Task #30 (commit 3) upgrades to nullable *void + null-check.
|
|
// For now, rt_malloc returns plain *void; OOM faults on deref.
|
|
let np: *u8 = malloc((nc: u64) * membsz): *u8;
|
|
let n: u64 = (s.cap: u64) * membsz;
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
np[i] = s.ptr[i];
|
|
i += 1u64;
|
|
};
|
|
// No free: the bump allocator (rt/malloc.ww) can't reclaim a
|
|
// mid-chunk region; the old buffer leaks until process exit. (#8)
|
|
s.ptr = np;
|
|
s.cap = nc;
|
|
};
|