diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 56 |
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" { |
