terminal/new: fix allocation

This commit is contained in:
Mitchell Hashimoto
2024-02-22 21:52:20 -08:00
parent 396cf5eb7a
commit f929c86d18
5 changed files with 18 additions and 9 deletions

View File

@ -210,22 +210,20 @@ fn ensureRows(self: *PageList, row: RowOffset, n: usize) !void {
self.pages.insertAfter(page, next_page);
page = next_page;
// we expect the pages at this point to be full capacity. we
// shrink them if we have to since they've never been used.
assert(page.data.size.rows == page.data.capacity.rows);
// If we have enough space, use it.
if (n_rem <= page.data.size.rows) {
if (n_rem <= page.data.capacity.rows) {
page.data.size.rows = @intCast(n_rem);
return;
}
// created pages are always empty so fill it with blanks
page.data.size.rows = page.data.capacity.rows;
// Continue
n_rem -= page.data.size.rows;
}
}
// TODO: test, refine
pub fn grow(self: *PageList) !*List.Node {
const next_page = try self.createPage();
// we don't errdefer this because we've added it to the linked
@ -246,6 +244,7 @@ fn createPage(self: *PageList) !*List.Node {
.styles = page_default_styles,
}),
};
page.data.size.rows = 0;
return page;
}

View File

@ -0,0 +1,8 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
test {
const testing = std.testing;
try testing.expect(false);
}

View File

@ -136,6 +136,8 @@ pub fn cursorDownScroll(self: *Screen) !void {
// No space, we need to allocate a new page and move the cursor to it.
const new_page = try self.pages.grow();
assert(new_page.data.size.rows == 0);
new_page.data.size.rows = 1;
const page_offset: PageList.RowOffset = .{
.page = new_page,
.row_offset = 0,

View File

@ -2,6 +2,7 @@ const builtin = @import("builtin");
const page = @import("page.zig");
pub const PageList = @import("PageList.zig");
pub const PagePool = @import("PagePool.zig");
pub const Terminal = @import("Terminal.zig");
pub const Page = page.Page;
@ -12,7 +13,6 @@ test {
_ = @import("bitmap_allocator.zig");
_ = @import("hash_map.zig");
_ = @import("page.zig");
_ = @import("PageList.zig");
_ = @import("Screen.zig");
_ = @import("point.zig");
_ = @import("size.zig");

View File

@ -95,8 +95,8 @@ pub const Page = struct {
/// requirements. This is enough to support a very large number of cells.
/// The standard capacity is chosen as the fast-path for allocation.
pub const std_capacity: Capacity = .{
.cols = 250,
.rows = 250,
.cols = 120,
.rows = 520,
.styles = 128,
.grapheme_bytes = 1024,
};