From 51ce21083347ca16fe27111d2ee1bb63561762ca Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 29 Jan 2026 18:23:54 +0000 Subject: [PATCH] terminal: add helpers to BitmapAllocator for sizing --- src/terminal/bitmap_allocator.zig | 14 ++++++++++++++ src/inspector/widgets/page.zig | 27 +++++++-------------------- 2 file(s) changed, 21 insertion(s)(+), 20 deletion(s)(-) diff --git a/src/terminal/bitmap_allocator.zig b/src/terminal/bitmap_allocator.zig --- a/src/terminal/bitmap_allocator.zig +++ b/src/terminal/bitmap_allocator.zig @@ -147,6 +147,20 @@ } } + /// Returns the total capacity in bytes. + pub fn capacityBytes(self: Self) usize { + return self.bitmap_count * bitmap_bit_size * chunk_size; + } + + /// Returns the number of bytes currently in use. + pub fn usedBytes(self: Self, base: anytype) usize { + const bitmaps = self.bitmap.ptr(base); + var free_chunks: usize = 0; + for (bitmaps[0..self.bitmap_count]) |bitmap| free_chunks += @popCount(bitmap); + const total_chunks = self.bitmap_count * bitmap_bit_size; + return (total_chunks - free_chunks) * chunk_size; + } + /// For testing only. fn isAllocated(self: *Self, base: anytype, slice: anytype) bool { comptime assert(@import("builtin").is_test); diff --git a/src/inspector/widgets/page.zig b/src/inspector/widgets/page.zig --- a/src/inspector/widgets/page.zig +++ b/src/inspector/widgets/page.zig @@ -149,26 +149,13 @@ _ = cimgui.c.ImGui_TableSetColumnIndex(2); cimgui.c.ImGui_Text("%d", page.graphemeCapacity()); - { - const StringAlloc = @TypeOf(page.string_alloc); - const string_chunk = StringAlloc.bytesRequired(u8, 1); - const string_total_chunks = page.string_alloc.bitmap_count * StringAlloc.bitmap_bit_size; - var string_free_chunks: usize = 0; - const string_bitmaps = page.string_alloc.bitmap.ptr(page.memory); - for (string_bitmaps[0..page.string_alloc.bitmap_count]) |bitmap| { - string_free_chunks += @popCount(bitmap); - } - const string_used_chunks = string_total_chunks - string_free_chunks; - const string_used_bytes = string_used_chunks * string_chunk; - const string_capacity_bytes = string_total_chunks * string_chunk; - cimgui.c.ImGui_TableNextRow(); - _ = cimgui.c.ImGui_TableSetColumnIndex(0); - cimgui.c.ImGui_Text("Strings (bytes)"); - _ = cimgui.c.ImGui_TableSetColumnIndex(1); - cimgui.c.ImGui_Text("%d", string_used_bytes); - _ = cimgui.c.ImGui_TableSetColumnIndex(2); - cimgui.c.ImGui_Text("%d", string_capacity_bytes); - } + cimgui.c.ImGui_TableNextRow(); + _ = cimgui.c.ImGui_TableSetColumnIndex(0); + cimgui.c.ImGui_Text("Strings (bytes)"); + _ = cimgui.c.ImGui_TableSetColumnIndex(1); + cimgui.c.ImGui_Text("%d", page.string_alloc.usedBytes(page.memory)); + _ = cimgui.c.ImGui_TableSetColumnIndex(2); + cimgui.c.ImGui_Text("%d", page.string_alloc.capacityBytes()); { const hyperlink_map = page.hyperlink_map.map(page.memory); -- tangled.sh