summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.zig13
-rw-r--r--src/memory.zig425
2 files changed, 344 insertions, 94 deletions
diff --git a/src/main.zig b/src/main.zig
index 52093b9a..9086fa90 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -317,16 +317,13 @@ fn webMain() !void {
try @import("gui/gui.zig").run(.{}, .{}, null);
}
-// init.gpa, what the tty, gui and detached shells allocate from, is
-// std.start's: a DebugAllocator in Debug and, since we link libc, libc's
-// malloc otherwise. Kept on purpose: glibc's malloc was measured at least as
-// fast as std.heap.smp_allocator on every scenario and faster on MuPDF's
-// page-sized rasters. macos.zig makes the same choice for itself.
+// init.gpa, what the tty, gui and detached shells allocate from, is their
+// own fixed arena (memory.host) in a release build, spilling to libc's
+// malloc, and std.start's DebugAllocator in Debug. Zig 0.17's std.start
+// gives a safe build a SafeAllocator over the page allocator instead.
fn nativeMain(std_init: std.process.Init) !void {
var init = std_init;
- // Zig 0.17's std.start gives a safe build a SafeAllocator over the page
- // allocator, not libc's malloc: hand the shells malloc, as above.
- if (builtin.mode != .debug) init.gpa = std.heap.c_allocator;
+ if (builtin.mode != .debug) init.gpa = pardes.memory.host(std.heap.c_allocator);
var stderr_probe: [64]u8 = undefined;
_ = std.debug.lockStderr(&stderr_probe);
std.debug.unlockStderr();
diff --git a/src/memory.zig b/src/memory.zig
index 853d641a..cb75ee0e 100644
--- a/src/memory.zig
+++ b/src/memory.zig
@@ -45,12 +45,20 @@ pub const limits = struct {
pub const embedded_sources = config.embed_sources;
pub const hexdump_row_bytes: u32 = if (board) 8 else 16;
+ /// Each arena's fixed region. On a native target these are address
+ /// space reserved at startup, resident only where used (`Pages`), so
+ /// they are sized for the worst case: the core holds a 256 MiB file, its
+ /// edit's copy and its undo.
pub const arena = struct {
- pub const pardes = if (board) 4 * KiB else if (reduced_target) 8 * MiB else 32 * MiB;
- pub const frame = if (board) 4 * KiB else if (reduced_target) 4 * MiB else 16 * MiB;
- pub const tree_sitter = if (board) 0 else if (reduced_target) 4 * MiB else 16 * MiB;
- pub const image = if (board) 0 else if (reduced_target) 64 * KiB else 32 * MiB;
- pub const pdf = if (board) 0 else if (reduced_target or !config.mupdf) 64 * KiB else 64 * MiB;
+ pub const pardes = if (board) 4 * KiB else if (reduced_target) 8 * MiB else 1024 * MiB;
+ pub const frame = if (board) 4 * KiB else if (reduced_target) 4 * MiB else 64 * MiB;
+ pub const lsp = if (board or reduced_target) 0 else 512 * MiB;
+ /// What a native shell allocates itself (`host`): its screen, the
+ /// bytes its ptys read, its 9P connections, its pipe jobs.
+ pub const host = if (board or reduced_target) 0 else 512 * MiB;
+ pub const tree_sitter = if (board) 0 else if (reduced_target) 4 * MiB else 256 * MiB;
+ pub const image = if (board) 0 else if (reduced_target) 64 * KiB else 256 * MiB;
+ pub const pdf = if (board) 0 else if (reduced_target or !config.mupdf) 64 * KiB else 512 * MiB;
/// One language query's working memory (its parsed answers, the rows
/// it writes): a hard bound, nothing spills. Callstack walks until it
/// would leave less than its rendering reserve.
@@ -70,8 +78,10 @@ pub const Allocators = struct {
pdf: Allocator,
};
-/// `std.heap.StackFallbackAllocator` with the buffer taken through its
-/// lock-free interface. The core's allocator is used from two threads: a 9P
+/// A freestanding target's arena (web, board): `std.heap.StackFallbackAllocator`
+/// with the buffer taken through its lock-free interface. A
+/// FixedBufferAllocator frees only its last block, so once a big operation
+/// fills the buffer everything after spills; a native target uses `Pages`. The core's allocator is used from two threads: a 9P
/// connection task answers a request while the editor's own thread is out
/// in a syscall that allocates (`readFileLimit`, `grep`; see `pardes.turn`).
/// The buffer is a separate zeroed global, not a field: an `undefined` one is
@@ -112,11 +122,256 @@ const SharedStackFallback = struct {
}
};
-var pardes_buffer: [limits.arena.pardes]u8 = @splat(0);
-var frame_buffer: [limits.arena.frame]u8 = @splat(0);
-var tree_sitter_buffer: [limits.arena.tree_sitter]u8 = @splat(0);
-var image_buffer: [limits.arena.image]u8 = @splat(0);
-var pdf_buffer: [limits.arena.pdf]u8 = @splat(0);
+/// One arena's fixed region on a native target, handed out in runs of 4 KiB
+/// granules to the general allocator above it (`Region.general`). A freed run
+/// is used again, and big ones go back to the system (`release`; `trim`
+/// gives back the rest), so what is resident follows what is in use. When no
+/// run fits, the block spills to the heap (`spill`) and is counted in
+/// `spilled`. Called only under the general allocator's mutex.
+const Pages = struct {
+ region: []u8 = @constCast(&[_]u8{}),
+ used: []u64 = &.{},
+ spill: Allocator = undefined,
+ spilled: usize = 0,
+ /// Past the highest granule used yet: where `trim` stops.
+ top: usize = 0,
+
+ const granule = 4 * KiB;
+ /// A freed run from `release_min` up to `keep_min` goes back at once:
+ /// kept until `trim`, a V8 Callstack page's emptied buckets and middling
+ /// blocks took its peak from 160 to 219 MiB. A bigger one stays resident
+ /// until `trim`: the next block of its size takes it without faulting it
+ /// in again, which each keystroke in a 7 MB file does (its edit's copy of
+ /// the text frees the one before; given back at once, 2.0 -> 3.2 ms a key).
+ const release_min = 64 * KiB;
+ const keep_min = 1 * MiB;
+
+ fn allocator(self: *Pages) Allocator {
+ return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } };
+ }
+
+ fn owns(self: *const Pages, ptr: [*]u8) bool {
+ const at = @intFromPtr(ptr) -% @intFromPtr(self.region.ptr);
+ return at < self.region.len;
+ }
+
+ fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ra: usize) ?[*]u8 {
+ const self: *Pages = @ptrCast(@alignCast(ctx));
+ const n = std.math.divCeil(usize, len, granule) catch unreachable;
+ const step = @max(1, alignment.toByteUnits() / granule);
+ if (find(self.used, n, step)) |start| {
+ mark(self.used, start, n, true);
+ self.top = @max(self.top, start + n);
+ return self.region.ptr + start * granule;
+ }
+ const ptr = self.spill.rawAlloc(len, alignment, ra) orelse return null;
+ self.spilled += len;
+ return ptr;
+ }
+
+ fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ra: usize) bool {
+ const self: *Pages = @ptrCast(@alignCast(ctx));
+ if (!self.owns(buf.ptr)) {
+ if (!self.spill.rawResize(buf, alignment, new_len, ra)) return false;
+ self.spilled = self.spilled - buf.len + new_len;
+ return true;
+ }
+ const start = (@intFromPtr(buf.ptr) - @intFromPtr(self.region.ptr)) / granule;
+ const n = std.math.divCeil(usize, buf.len, granule) catch unreachable;
+ const want = std.math.divCeil(usize, new_len, granule) catch unreachable;
+ if (want <= n) {
+ self.release(start + want, n - want);
+ return true;
+ }
+ if (start + want > self.used.len * 64 or freeRun(self.used, start + n, want - n) < want - n) return false;
+ mark(self.used, start + n, want - n, true);
+ self.top = @max(self.top, start + want);
+ return true;
+ }
+
+ fn remap(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ra: usize) ?[*]u8 {
+ const self: *Pages = @ptrCast(@alignCast(ctx));
+ if (!self.owns(buf.ptr)) {
+ const ptr = self.spill.rawRemap(buf, alignment, new_len, ra) orelse return null;
+ self.spilled = self.spilled - buf.len + new_len;
+ return ptr;
+ }
+ return if (resize(ctx, buf, alignment, new_len, ra)) buf.ptr else null;
+ }
+
+ fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ra: usize) void {
+ const self: *Pages = @ptrCast(@alignCast(ctx));
+ if (!self.owns(buf.ptr)) {
+ self.spilled -= buf.len;
+ return self.spill.rawFree(buf, alignment, ra);
+ }
+ const start = (@intFromPtr(buf.ptr) - @intFromPtr(self.region.ptr)) / granule;
+ self.release(start, std.math.divCeil(usize, buf.len, granule) catch unreachable);
+ }
+
+ fn release(self: *Pages, start: usize, n: usize) void {
+ mark(self.used, start, n, false);
+ if (n * granule >= release_min and n * granule < keep_min) advise(self.region[start * granule ..][0 .. n * granule], std.posix.MADV.DONTNEED);
+ }
+
+ /// Gives back the pages of every free run in `n` granules from `start`.
+ fn giveBack(self: *Pages, start: usize, n: usize) void {
+ var i = start;
+ while (i < start + n) {
+ if (i % 64 == 0 and self.used[i / 64] == ~@as(u64, 0)) {
+ i += 64;
+ continue;
+ }
+ const run = freeRun(self.used, i, start + n - i);
+ if (run > 0) advise(self.region[i * granule ..][0 .. run * granule], std.posix.MADV.DONTNEED);
+ i += run + 1;
+ }
+ }
+
+ /// Gives back every free run below `top`: the small ones `release`
+ /// keeps resident, small and big.
+ fn trim(self: *Pages) void {
+ self.giveBack(0, self.top);
+ }
+
+ /// The first free run of `n` granules starting at a multiple of `step`.
+ // ponytail: first fit, a scan of the region's bitmap (32 KiB for 1 GiB)
+ // per page-sized block; a free-run index if a profile ever shows it.
+ fn find(used: []const u64, n: usize, step: usize) ?usize {
+ const total = used.len * 64;
+ var start: usize = 0;
+ while (start + n <= total) {
+ if (start % 64 == 0 and used[start / 64] == ~@as(u64, 0)) {
+ start = std.mem.alignForward(usize, start + 64, step);
+ continue;
+ }
+ const got = freeRun(used, start, n);
+ if (got == n) return start;
+ start = std.mem.alignForward(usize, start + got + 1, step);
+ }
+ return null;
+ }
+
+ /// How many of the `n` granules from `start` are free before a used one.
+ fn freeRun(used: []const u64, start: usize, n: usize) usize {
+ var i = start;
+ while (i < start + n) {
+ if (i % 64 == 0 and start + n - i >= 64 and used[i / 64] == 0) {
+ i += 64;
+ continue;
+ }
+ if (used[i / 64] >> @intCast(i % 64) & 1 != 0) break;
+ i += 1;
+ }
+ return i - start;
+ }
+
+ fn mark(used: []u64, start: usize, n: usize, set: bool) void {
+ var i = start;
+ while (i < start + n) {
+ if (i % 64 == 0 and start + n - i >= 64) {
+ used[i / 64] = if (set) ~@as(u64, 0) else 0;
+ i += 64;
+ continue;
+ }
+ const bit = @as(u64, 1) << @intCast(i % 64);
+ if (set) used[i / 64] |= bit else used[i / 64] &= ~bit;
+ i += 1;
+ }
+ }
+};
+
+/// The general allocator over one arena's region: std's DebugAllocator for
+/// its size classes, its checks and leak report only in a Debug build.
+const General = std.heap.DebugAllocator(.{
+ .safety = debug_enabled,
+ .thread_safe = true,
+ .backing_allocator_zeroes = false,
+ .page_size = 64 * KiB,
+});
+
+/// One arena: its region, the bitmap of its used granules, and the
+/// allocator over them. A native region is address space mapped once, at the
+/// first `init`, not .bss: gigabytes of .bss put static data out of reach of
+/// x86-64's 32-bit offsets. A freestanding one is its .bss `buffer`.
+const Region = struct {
+ size: usize,
+ bits: []u64,
+ buffer: []u8,
+ pages: Pages = .{},
+ general: General = .init,
+ fallback: SharedStackFallback = undefined,
+
+ fn init(r: *Region, spill: Allocator) Allocator {
+ if (reduced_target) {
+ r.fallback = .{ .buffer = r.buffer, .fallback_allocator = spill };
+ return r.fallback.get();
+ }
+ // Without the mapping (an address-space limit) every block spills.
+ const region = if (r.pages.region.len > 0) r.pages.region else if (std.heap.page_allocator.rawAlloc(r.size, .fromByteUnits(64 * KiB), @returnAddress())) |ptr| ptr[0..r.size] else @constCast(&[_]u8{});
+ sparse(region);
+ @memset(r.bits, 0);
+ r.pages = .{ .region = region, .used = r.bits[0 .. region.len / Pages.granule / 64], .spill = spill };
+ r.general = .{ .backing_allocator = r.pages.allocator() };
+ return r.general.allocator();
+ }
+
+ fn trim(r: *Region) void {
+ if (reduced_target) return;
+ std.Io.Threaded.mutexLockUncancelable(&r.general.mutex);
+ defer std.Io.Threaded.mutexUnlock(&r.general.mutex);
+ r.pages.trim();
+ }
+
+ /// A fresh allocator in its place: a Debug deinit leaves 0xAA behind,
+ /// its mutex too, which a later `trim` would wait on for good.
+ fn deinit(r: *Region) bool {
+ if (reduced_target) return false;
+ const leaked = r.general.deinit() == .leak;
+ r.general = .{ .backing_allocator = r.pages.allocator() };
+ return leaked;
+ }
+};
+
+/// Each arena's bitmap on a native target, its buffer on a freestanding one;
+/// spelled out per arena: a generic returning the globals would share them
+/// between two arenas of one size.
+fn bitsFor(size: usize) usize {
+ return if (reduced_target) 0 else size / Pages.granule / 64;
+}
+fn bufferFor(size: usize) usize {
+ return if (reduced_target) size else 0;
+}
+var pardes_bits: [bitsFor(limits.arena.pardes)]u64 = @splat(0);
+var pardes_buffer: [bufferFor(limits.arena.pardes)]u8 = @splat(0);
+var pardes_region: Region = .{ .size = limits.arena.pardes, .bits = &pardes_bits, .buffer = &pardes_buffer };
+var frame_bits: [bitsFor(limits.arena.frame)]u64 = @splat(0);
+var frame_buffer: [bufferFor(limits.arena.frame)]u8 = @splat(0);
+var frame_region: Region = .{ .size = limits.arena.frame, .bits = &frame_bits, .buffer = &frame_buffer };
+var lsp_bits: [bitsFor(limits.arena.lsp)]u64 = @splat(0);
+var lsp_buffer: [bufferFor(limits.arena.lsp)]u8 = @splat(0);
+var lsp_region: Region = .{ .size = limits.arena.lsp, .bits = &lsp_bits, .buffer = &lsp_buffer };
+var tree_sitter_bits: [bitsFor(limits.arena.tree_sitter)]u64 = @splat(0);
+var tree_sitter_buffer: [bufferFor(limits.arena.tree_sitter)]u8 = @splat(0);
+var tree_sitter_region: Region = .{ .size = limits.arena.tree_sitter, .bits = &tree_sitter_bits, .buffer = &tree_sitter_buffer };
+var image_bits: [bitsFor(limits.arena.image)]u64 = @splat(0);
+var image_buffer: [bufferFor(limits.arena.image)]u8 = @splat(0);
+var image_region: Region = .{ .size = limits.arena.image, .bits = &image_bits, .buffer = &image_buffer };
+var pdf_bits: [bitsFor(limits.arena.pdf)]u64 = @splat(0);
+var pdf_buffer: [bufferFor(limits.arena.pdf)]u8 = @splat(0);
+var pdf_region: Region = .{ .size = limits.arena.pdf, .bits = &pdf_bits, .buffer = &pdf_buffer };
+var host_bits: [bitsFor(limits.arena.host)]u64 = @splat(0);
+var host_buffer: [bufferFor(limits.arena.host)]u8 = @splat(0);
+var host_region: Region = .{ .size = limits.arena.host, .bits = &host_bits, .buffer = &host_buffer };
+
+/// The allocator a native shell allocates itself from, over its own arena,
+/// for the life of the process (no `deinit`); what does not fit spills to
+/// `spill`. A release build's: a Debug build keeps std.start's allocator
+/// for its leak report.
+pub fn host(spill: Allocator) Allocator {
+ return host_region.init(spill);
+}
+
var lsp_query_buffer: [limits.arena.lsp_query]u8 align(std.heap.page_size_max) = @splat(0);
var lsp_query_busy: std.atomic.Value(bool) = .init(false);
/// What a query may leave resident of the region (giveLspQuery).
@@ -166,11 +421,13 @@ pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void {
if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress());
}
-/// Gives libc's free memory back to the system, inside its heap too: after a
-/// big result is formatted or a big pane torn down, glibc keeps what was freed
-/// (a V8 Callstack page left 21 MiB free in the heap; this returned 20).
-/// Walks the heap, so for rare, large events only.
+/// Gives free memory back to the system, the arenas' and libc's, inside its
+/// heap too: after a big result is formatted or a big pane torn down, the
+/// tree-sitter arena kept 73 MiB of emptied buckets resident and glibc what
+/// was freed (a V8 Callstack page left 21 MiB free in its heap; this returned
+/// 20). Walks every arena and the heap, so for rare, large events only.
pub fn trim() void {
+ inline for (.{ &pardes_region, &frame_region, &lsp_region, &tree_sitter_region, &image_region, &pdf_region, &host_region }) |r| r.trim();
if (comptime builtin.os.tag != .linux or builtin.abi.isMusl() or !builtin.link_libc) return;
const glibc = struct {
extern "c" fn malloc_trim(pad: usize) c_int;
@@ -185,91 +442,72 @@ pub fn trim() void {
/// 71 MiB that way. Only the region given, never the whole process (a prctl
/// would reach every shell and server pardes starts).
pub fn sparse(region: []u8) void {
+ advise(region, std.posix.MADV.NOHUGEPAGE);
+}
+
+/// madvise over the whole pages inside `region`.
+fn advise(region: []u8, advice: u32) void {
if (comptime builtin.os.tag != .linux) return;
const page = std.heap.pageSize();
const start = std.mem.alignForward(usize, @intFromPtr(region.ptr), page);
const end = std.mem.alignBackward(usize, @intFromPtr(region.ptr) + region.len, page);
if (end <= start) return;
- std.posix.madvise(@ptrFromInt(start), end - start, std.posix.MADV.NOHUGEPAGE) catch {};
+ std.posix.madvise(@ptrFromInt(start), end - start, advice) catch {};
}
-var pardes_fallback: SharedStackFallback = .{ .buffer = &pardes_buffer };
-var frame_fallback: SharedStackFallback = .{ .buffer = &frame_buffer };
-var tree_sitter_fallback: SharedStackFallback = .{ .buffer = &tree_sitter_buffer };
-var image_fallback: SharedStackFallback = .{ .buffer = &image_buffer };
-var pdf_fallback: SharedStackFallback = .{ .buffer = &pdf_buffer };
-
-const Debug = std.heap.DebugAllocator(.{});
-var pardes_debug: Debug = .init;
-var frame_debug: Debug = .init;
-var lsp_debug: Debug = .init;
-var tree_sitter_debug: Debug = .init;
-var image_debug: Debug = .init;
-var pdf_debug: Debug = .init;
-
-// One session at a time; free its allocations before deinit. Concurrent LSP workers use the caller's allocator.
+// One session at a time; free its allocations before deinit. The language
+// workers allocate their answers from `lsp` on their own threads.
pub fn init(fallback: Allocator) Allocators {
- // A Debug build puts each allocator under a DebugAllocator of its own,
- // which reports that subsystem's leaks and bad frees (deinit panics on a
- // leak). Beneath that goes the page allocator rather than `fallback`: in
- // a Debug build `fallback` is std.process.Init's gpa, a DebugAllocator
- // already, and a second one checked every block twice.
- const backing = if (debug_enabled) std.heap.page_allocator else fallback;
- inline for (.{ &pardes_buffer, &frame_buffer, &tree_sitter_buffer, &image_buffer, &pdf_buffer, &lsp_query_buffer }) |buffer| sparse(buffer);
- pardes_fallback.fallback_allocator = backing;
- frame_fallback.fallback_allocator = backing;
- tree_sitter_fallback.fallback_allocator = backing;
- image_fallback.fallback_allocator = backing;
- pdf_fallback.fallback_allocator = backing;
-
- const raw: Allocators = .{
- .pardes = pardes_fallback.get(),
- .frame = frame_fallback.get(),
- .lsp = backing,
- .tree_sitter = tree_sitter_fallback.get(),
- .image = image_fallback.get(),
- .pdf = pdf_fallback.get(),
- };
- if (!debug_enabled) return raw;
-
- pardes_debug = .{ .backing_allocator = raw.pardes };
- frame_debug = .{ .backing_allocator = raw.frame };
- lsp_debug = .{ .backing_allocator = raw.lsp };
- tree_sitter_debug = .{ .backing_allocator = raw.tree_sitter };
- image_debug = .{ .backing_allocator = raw.image };
- pdf_debug = .{ .backing_allocator = raw.pdf };
+ // Beneath a Debug build's arenas goes the page allocator rather than
+ // `fallback`: in a Debug build `fallback` is std.process.Init's gpa, a
+ // DebugAllocator already, and the arenas' own check every block.
+ const spill = if (debug_enabled) std.heap.page_allocator else fallback;
+ sparse(&lsp_query_buffer);
return .{
- .pardes = pardes_debug.allocator(),
- .frame = frame_debug.allocator(),
- .lsp = lsp_debug.allocator(),
- .tree_sitter = tree_sitter_debug.allocator(),
- .image = image_debug.allocator(),
- .pdf = pdf_debug.allocator(),
+ .pardes = pardes_region.init(spill),
+ .frame = frame_region.init(spill),
+ .lsp = lsp_region.init(spill),
+ .tree_sitter = tree_sitter_region.init(spill),
+ .image = image_region.init(spill),
+ .pdf = pdf_region.init(spill),
};
}
+/// A Debug build's leak report, each arena's own (it panics on a leak).
pub fn deinit() void {
if (!debug_enabled) return;
- var leaked = pardes_debug.deinit() == .leak;
- leaked = (frame_debug.deinit() == .leak) or leaked;
- leaked = (lsp_debug.deinit() == .leak) or leaked;
- leaked = (tree_sitter_debug.deinit() == .leak) or leaked;
- leaked = (image_debug.deinit() == .leak) or leaked;
- leaked = (pdf_debug.deinit() == .leak) or leaked;
+ var leaked = pardes_region.deinit();
+ leaked = frame_region.deinit() or leaked;
+ leaked = lsp_region.deinit() or leaked;
+ leaked = tree_sitter_region.deinit() or leaked;
+ leaked = image_region.deinit() or leaked;
+ leaked = pdf_region.deinit() or leaked;
if (leaked) @panic("allocator leaks detected");
}
-test "fixed allocators are separate, spill, and restart" {
+test "arenas are separate, reuse what is freed, spill, and restart" {
+ if (reduced_target) return error.SkipZigTest;
var allocs = init(std.testing.allocator);
const core = try allocs.pardes.alloc(u8, 32);
const frame = try allocs.frame.alloc(u8, 32);
- try std.testing.expect(pardes_fallback.fixed.ownsPtr(core.ptr));
- try std.testing.expect(frame_fallback.fixed.ownsPtr(frame.ptr));
- try std.testing.expect(core.ptr != frame.ptr);
+ try std.testing.expect(pardes_region.pages.owns(core.ptr));
+ try std.testing.expect(frame_region.pages.owns(frame.ptr));
- const spill = try allocs.pardes.alloc(u8, limits.arena.pardes + 1);
- try std.testing.expect(!pardes_fallback.fixed.ownsPtr(spill.ptr));
- allocs.pardes.free(spill);
+ // A freed block is used again: a FixedBufferAllocator's was not, unless
+ // it was the last one, so an arena filled once and spilled after.
+ const big = try unfilled(allocs.pardes, 8 * MiB);
+ const after = try allocs.pardes.alloc(u8, 32);
+ freeUnfilled(allocs.pardes, big);
+ const again = try unfilled(allocs.pardes, 8 * MiB);
+ try std.testing.expectEqual(big.ptr, again.ptr);
+
+ const spill = try unfilled(allocs.pardes, limits.arena.pardes + 1);
+ try std.testing.expect(!pardes_region.pages.owns(spill.ptr));
+ try std.testing.expectEqual(limits.arena.pardes + 1, pardes_region.pages.spilled);
+ freeUnfilled(allocs.pardes, spill);
+ try std.testing.expectEqual(0, pardes_region.pages.spilled);
+ freeUnfilled(allocs.pardes, again);
+ allocs.pardes.free(after);
allocs.frame.free(frame);
allocs.pardes.free(core);
deinit();
@@ -278,7 +516,21 @@ test "fixed allocators are separate, spill, and restart" {
defer deinit();
const restarted = try allocs.pardes.alloc(u8, 32);
defer allocs.pardes.free(restarted);
- try std.testing.expect(pardes_fallback.fixed.ownsPtr(restarted.ptr));
+ try std.testing.expect(pardes_region.pages.owns(restarted.ptr));
+}
+
+test "a region's runs: aligned first fit, growth in place, release" {
+ var used: [2]u64 = @splat(0);
+ Pages.mark(&used, 0, 3, true);
+ try std.testing.expectEqual(@as(?usize, 4), Pages.find(&used, 2, 4));
+ try std.testing.expectEqual(@as(?usize, 3), Pages.find(&used, 70, 1));
+ try std.testing.expectEqual(@as(?usize, null), Pages.find(&used, 126, 1));
+ try std.testing.expectEqual(@as(?usize, 3), Pages.find(&used, 125, 1));
+ Pages.mark(&used, 3, 125, true);
+ try std.testing.expectEqual(~@as(u64, 0), used[1]);
+ Pages.mark(&used, 10, 100, false);
+ try std.testing.expectEqual(@as(usize, 100), Pages.freeRun(&used, 10, 100));
+ try std.testing.expectEqual(@as(usize, 100), Pages.freeRun(&used, 10, 101));
}
test "memory limits preserve desktop capacities" {
@@ -293,9 +545,10 @@ test "memory limits preserve desktop capacities" {
try std.testing.expectEqual(@as(usize, 4095), limits.host_path_cap);
try std.testing.expectEqual(config.embed_sources, limits.embedded_sources);
try std.testing.expectEqual(@as(u32, 16), limits.hexdump_row_bytes);
- try std.testing.expectEqual(32 * MiB, limits.arena.pardes);
- try std.testing.expectEqual(16 * MiB, limits.arena.frame);
- try std.testing.expectEqual(16 * MiB, limits.arena.tree_sitter);
- try std.testing.expectEqual(32 * MiB, limits.arena.image);
- try std.testing.expectEqual(if (config.mupdf) 64 * MiB else 64 * KiB, limits.arena.pdf);
+ try std.testing.expectEqual(1024 * MiB, limits.arena.pardes);
+ try std.testing.expectEqual(64 * MiB, limits.arena.frame);
+ try std.testing.expectEqual(512 * MiB, limits.arena.lsp);
+ try std.testing.expectEqual(256 * MiB, limits.arena.tree_sitter);
+ try std.testing.expectEqual(256 * MiB, limits.arena.image);
+ try std.testing.expectEqual(if (config.mupdf) 512 * MiB else 64 * KiB, limits.arena.pdf);
}