mem: List(T) mutations gain optional alloc: Allocator = context.allocator

The chess panel-text regression (text vanished after the first move on
macOS) had a single root cause: GlyphCache's entries List, hash table,
and shaped_buf grew through `context.allocator` — which during render
is the per-frame arena. On the next arena reset the backing died, and
subsequent glyph lookups read garbage / wrote into freshly-allocated
view-tree memory.

Fix is shaped as the user proposed: `List(T)`'s mutations take an
optional trailing `alloc: Allocator = context.allocator` argument. No
allocator stored on the container, no init ceremony, every existing
`list.append(item)` callsite keeps working unchanged. Long-lived
owners now write `list.append(item, self.parent_allocator)` and the
arena-leak bug becomes impossible to write accidentally.

Default-arg substitution previously only fired for identifier callees
(`expandCallDefaults` at lower.zig:7978). Extended to the generic
struct-method dispatch path (`list.append(...)` lands here) via a new
`appendDefaultArgs` helper that lowers fd.params[i].default_expr in
the caller's scope and appends to the lowered args slice.

Long-lived owners updated to capture `parent_allocator: Allocator` at
init and use it for every internal growth:

- GlyphCache (the chess bug) — entries, shaped_buf, hash_keys,
  hash_vals, atlas bitmap.
- DockInteraction — drops the existing `push Context` workaround in
  `ensure_capacity` for the explicit-arg form.
- StateStore — entries list + per-entry data buffer.
- Gles3Gpu, MetalGPU — shaders, buffers, textures (atlas-grow during
  render would otherwise leak resources into the frame arena).

Also kept: an operator-precedence fix in pipeline.sx
(`(self.frame_index & 1) == 0` instead of
`self.frame_index & 1 == 0`, which parses as
`self.frame_index & (1 == 0)` = always 0). That was a stealth
single-arena-only bug that masked the GlyphCache one for a long time.

Docs:
- specs.md §11 documents `param: T = expr` default parameter values.
  The parser already supported it — formalised in the spec now.
- current/CHECKPOINT-MEM.md logs the change.
- CLAUDE.md REJECTED PATTERNS gains a "Long-lived containers growing
  through context.allocator" section with the `parent_allocator`
  capture template and the list of existing examples to mirror.

155/155 example tests pass — zero-diff against snapshots since every
existing callsite still resolves to `context.allocator`.
This commit is contained in:
agra
2026-05-25 14:41:17 +03:00
parent b263704664
commit 72593db953
11 changed files with 258 additions and 63 deletions

View File

@@ -65,6 +65,12 @@ Gles3Gpu :: struct {
shaders: List(Gles3ShaderSlot) = .{};
buffers: List(u32) = .{};
textures: List(Gles3TextureSlot) = .{};
// Captured at init() time so resource creation always grows the cache
// lists through the long-lived allocator, even when a caller (e.g.
// glyph_cache atlas-grow during render) is currently inside a transient
// arena context.
parent_allocator: Allocator = .{};
}
// ── GPU impl ───────────────────────────────────────────────────────────
@@ -79,6 +85,7 @@ impl GPU for Gles3Gpu {
inline if OS != .android { return false; }
self.pixel_w = pixel_w;
self.pixel_h = pixel_h;
self.parent_allocator = context.allocator;
if !self.gl_loaded {
load_gl(@eglGetProcAddress);
@@ -133,7 +140,7 @@ impl GPU for Gles3Gpu {
proj_loc = glGetUniformLocation(prog, "uProj".ptr),
tex_loc = glGetUniformLocation(prog, "uTex".ptr),
};
self.shaders.append(slot);
self.shaders.append(slot, self.parent_allocator);
xx self.shaders.len;
}
@@ -144,7 +151,7 @@ impl GPU for Gles3Gpu {
glGenBuffers(1, @b);
glBindBuffer(GL_ARRAY_BUFFER, b);
glBufferData(GL_ARRAY_BUFFER, xx size_bytes, null, GL_DYNAMIC_DRAW);
self.buffers.append(b);
self.buffers.append(b, self.parent_allocator);
xx self.buffers.len;
}
@@ -198,7 +205,7 @@ impl GPU for Gles3Gpu {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, xx GL_CLAMP_TO_EDGE);
slot : Gles3TextureSlot = .{ tex = t, bytes_per_pixel = bpp };
self.textures.append(slot);
self.textures.append(slot, self.parent_allocator);
xx self.textures.len;
}

View File

@@ -86,6 +86,11 @@ MetalGPU :: struct {
shaders: List(*void) = .{}; // MTLRenderPipelineState*
buffers: List(*void) = .{}; // MTLBuffer*
textures: List(TextureSlot) = .{};
// Captured at init() so resource creation always grows the cache lists
// through the long-lived allocator, even when the caller is currently
// inside a transient arena context (e.g. glyph atlas grow during render).
parent_allocator: Allocator = .{};
}
impl GPU for MetalGPU {
@@ -101,6 +106,7 @@ impl GPU for MetalGPU {
self.pixel_w = pixel_w;
self.pixel_h = pixel_h;
}
self.parent_allocator = context.allocator;
metal_init_ios(self);
}
@@ -432,7 +438,7 @@ metal_create_shader_ios :: (self: *MetalGPU, src: string) -> u32 {
return 0;
}
self.shaders.append(state);
self.shaders.append(state, self.parent_allocator);
xx self.shaders.len;
}
@@ -452,7 +458,7 @@ metal_create_buffer_ios :: (self: *MetalGPU, size_bytes: s64) -> u32 {
xx size_bytes, 0);
if buf == null { return 0; }
self.buffers.append(buf);
self.buffers.append(buf, self.parent_allocator);
xx self.buffers.len;
}
@@ -541,7 +547,7 @@ metal_create_texture_ios :: (self: *MetalGPU, w: s32, h: s32, format: TextureFor
if tex == null { return 0; }
slot : TextureSlot = .{ tex = tex, bytes_per_pixel = bytes_per_pixel };
self.textures.append(slot);
self.textures.append(slot, self.parent_allocator);
if pixels != null {
handle : u32 = xx self.textures.len;