summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-24 12:52:49 -0300
committerGabriel Schneider <[email protected]>2026-10-01 00:12:14 -0300
commit8de5bf6fecc19cf2be6093b4338691fe4af84815 (patch)
treef64a62c4a720150574d796ebc2d62c0f21cbff02 /src/gui
parent40197552bed794b92410bdf803532162c8f5e27d (diff)
downloadpardes-8de5bf6fecc19cf2be6093b4338691fe4af84815.tar.gz
pardes-8de5bf6fecc19cf2be6093b4338691fe4af84815.zip
Hand every C library's allocator hook the same C heap
C's malloc, calloc, realloc and free over a Zig allocator were written three times: syntax.zig's for tree-sitter, pdf.zig's for MuPDF, and gui.zig's for FreeType and HarfBuzz. They are now one, src/c_heap.zig's Heap. Each hook names the allocator variable its heap reads; gui.zig exports its heap under the ui_malloc names font.c and HarfBuzz call. It is a module of its own because pdf.zig is the MuPDF module, and a file cannot belong to that module and the core's at once. A block starts with a 16-byte header holding its size and the allocator that made it, so it goes back where it came from even after the heap is repointed, as the fonts' copy did. The fonts' copy carried the 16-byte Allocator itself in a 32-byte header; here the header holds the allocator's index in a table of every allocator a heap has used (entries are published once and never change, and a heap remembers where its current one was last found). The difference is measurable: with the 32-byte header, pardes-pdf-bench's filtered page render at 96 dpi took 1.1% longer than before this change, slower in 15 of 16 pinned rounds in two separate runs, and highlighting all of src/pardes.zig (305k tree-sitter blocks) up to 0.6% longer, against an A/A pair within 0.7%. With the 16-byte header both are back within the A/A pair's noise. Two other things change. For tree-sitter and MuPDF, a block made before the allocator was repointed now goes back to the one that made it rather than through the current one. For the fonts, realloc(block, 0) now frees and returns null, as glibc's does and as the other two copies already did; neither FreeType nor HarfBuzz asks for it. The heap's test replaces the tests of the old copies. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/gui.zig56
1 files changed, 12 insertions, 44 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index dd74ae58..73f17a22 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -15,6 +15,7 @@ const crt = @import("crt.zig");
const pet = @import("pet.zig");
const fonts = @import("../fonts.zig"); // the Font builtin's half of the seam
const selection_pipe = @import("../selection_pipe.zig");
+const c_heap = @import("c_heap");
const ninep_io = @import("../9p_io.zig");
@@ -133,53 +134,20 @@ fn taglineRaster(font: *c.UIFont, body_px: f32, body_cell_w: u32, body_cell_h: u
.height = @intCast(band_h),
};
}
-/// What FreeType and HarfBuzz allocate from, through ui_malloc and friends
-/// below. runNative points it at the gui's gpa once HarfBuzz's process-wide
-/// objects exist (ui_font_prime), so a Debug build's leak report is about
-/// fonts; a test that counts what fonts leak points it at
-/// std.testing.allocator the same way. Only the render thread calls into
+/// What FreeType and HarfBuzz allocate from, through ui_malloc and friends:
+/// the shared C heap, exported under the names font.c and HarfBuzz's
+/// hb_malloc_impl expect. runNative points it at the gui's gpa once
+/// HarfBuzz's process-wide objects exist (ui_font_prime), so a Debug build's
+/// leak report is about fonts; a test that counts what fonts leak points it
+/// at std.testing.allocator the same way. Only the render thread calls into
/// fonts, and every allocator it is pointed at is thread-safe anyway.
var font_allocator: std.mem.Allocator = std.heap.smp_allocator;
-
-/// C's free and realloc pass no size, so a block starts with its size and the
-/// allocator that made it: a block made before font_allocator was repointed
-/// goes back where it came from. 32 bytes keep the caller's pointer 16-byte
-/// (max_align_t) aligned.
-const FontBlock = struct { size: usize, allocator: std.mem.Allocator };
-const font_block_header = 32;
+const font_heap = c_heap.Heap(&font_allocator);
comptime {
- std.debug.assert(@sizeOf(FontBlock) <= font_block_header);
-}
-
-export fn ui_malloc(size: usize) ?*anyopaque {
- const total = std.math.add(usize, font_block_header, size) catch return null;
- const bytes = font_allocator.alignedAlloc(u8, .@"16", total) catch return null;
- @as(*FontBlock, @ptrCast(bytes.ptr)).* = .{ .size = size, .allocator = font_allocator };
- return bytes.ptr + font_block_header;
-}
-
-export fn ui_calloc(count: usize, size: usize) ?*anyopaque {
- const total = std.math.mul(usize, count, size) catch return null;
- const block: [*]u8 = @ptrCast(ui_malloc(total) orelse return null);
- @memset(block[0..total], 0);
- return block;
-}
-
-export fn ui_realloc(block: ?*anyopaque, size: usize) ?*anyopaque {
- const old = block orelse return ui_malloc(size);
- const base: [*]align(16) u8 = @alignCast(@as([*]u8, @ptrCast(old)) - font_block_header);
- const header: *FontBlock = @ptrCast(base);
- const total = std.math.add(usize, font_block_header, size) catch return null;
- const bytes = header.allocator.realloc(base[0 .. font_block_header + header.size], total) catch return null;
- @as(*FontBlock, @ptrCast(bytes.ptr)).size = size;
- return bytes.ptr + font_block_header;
-}
-
-export fn ui_free(block: ?*anyopaque) void {
- const old = block orelse return;
- const base: [*]align(16) u8 = @alignCast(@as([*]u8, @ptrCast(old)) - font_block_header);
- const header: *FontBlock = @ptrCast(base);
- header.allocator.free(base[0 .. font_block_header + header.size]);
+ @export(&font_heap.malloc, .{ .name = "ui_malloc" });
+ @export(&font_heap.calloc, .{ .name = "ui_calloc" });
+ @export(&font_heap.realloc, .{ .name = "ui_realloc" });
+ @export(&font_heap.free, .{ .name = "ui_free" });
}
test "FreeType and HarfBuzz allocate from the gui allocator and give it all back" {