diff options
Diffstat (limited to 'src/memory.zig')
| -rw-r--r-- | src/memory.zig | 104 |
1 files changed, 19 insertions, 85 deletions
diff --git a/src/memory.zig b/src/memory.zig index c4e0e6fd..6f6c6191 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -3,7 +3,6 @@ const builtin = @import("builtin"); const walk = @import("walk.zig"); const config = @import("pardes_config"); -const reduced_target = builtin.os.tag == .freestanding; const KiB = 1024; const MiB = 1024 * KiB; @@ -47,19 +46,19 @@ pub const limits = struct { /// 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 (reduced_target) 8 * MiB else 1024 * MiB; - pub const frame = if (reduced_target) 4 * MiB else 64 * MiB; - pub const lsp = if (reduced_target) 0 else 512 * MiB; + pub const pardes = 1024 * MiB; + pub const frame = 64 * MiB; + pub const lsp = 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 (reduced_target) 0 else 512 * MiB; - pub const tree_sitter = if (reduced_target) 4 * MiB else 256 * MiB; - pub const image = if (reduced_target) 64 * KiB else 256 * MiB; - pub const pdf = if (reduced_target or !config.mupdf) 64 * KiB else 512 * MiB; + pub const host = 512 * MiB; + pub const tree_sitter = 256 * MiB; + pub const image = 256 * MiB; + pub const pdf = if (!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. - pub const lsp_query = if (reduced_target) 0 else 256 * MiB; + pub const lsp_query = 256 * MiB; }; }; @@ -75,50 +74,6 @@ pub const Allocators = struct { pdf: Allocator, }; -/// A freestanding target's arena (the web's): `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 -/// written into a Debug binary as 0xAA filler, 160 MB of file for the five -/// arenas, where a zeroed one is .bss and costs nothing. -const SharedStackFallback = struct { - buffer: []u8, - fallback_allocator: Allocator = undefined, - fixed: std.heap.FixedBufferAllocator = undefined, - - fn get(self: *@This()) Allocator { - self.fixed = .init(self.buffer); - return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; - } - - fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ra: usize) ?[*]u8 { - const self: *@This() = @ptrCast(@alignCast(ctx)); - return self.fixed.threadSafeAllocator().rawAlloc(len, alignment, ra) orelse - self.fallback_allocator.rawAlloc(len, alignment, ra); - } - - fn resize(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ra: usize) bool { - const self: *@This() = @ptrCast(@alignCast(ctx)); - if (self.fixed.ownsPtr(buf.ptr)) return self.fixed.threadSafeAllocator().rawResize(buf, alignment, new_len, ra); - return self.fallback_allocator.rawResize(buf, alignment, new_len, ra); - } - - fn remap(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, new_len: usize, ra: usize) ?[*]u8 { - const self: *@This() = @ptrCast(@alignCast(ctx)); - if (self.fixed.ownsPtr(buf.ptr)) return self.fixed.threadSafeAllocator().rawRemap(buf, alignment, new_len, ra); - return self.fallback_allocator.rawRemap(buf, alignment, new_len, ra); - } - - fn free(ctx: *anyopaque, buf: []u8, alignment: std.mem.Alignment, ra: usize) void { - const self: *@This() = @ptrCast(@alignCast(ctx)); - if (self.fixed.ownsPtr(buf.ptr)) return self.fixed.threadSafeAllocator().rawFree(buf, alignment, ra); - return self.fallback_allocator.rawFree(buf, alignment, ra); - } -}; - /// 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` @@ -285,20 +240,14 @@ const General = std.heap.DebugAllocator(.{ /// 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`. +/// x86-64's 32-bit offsets. 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); @@ -309,7 +258,6 @@ const Region = struct { } 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(); @@ -318,43 +266,31 @@ const Region = struct { /// 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. +/// Each arena's bitmap, 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; + return size / Pages.granule / 64; } 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 pardes_region: Region = .{ .size = limits.arena.pardes, .bits = &pardes_bits }; 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 frame_region: Region = .{ .size = limits.arena.frame, .bits = &frame_bits }; 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 lsp_region: Region = .{ .size = limits.arena.lsp, .bits = &lsp_bits }; 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 tree_sitter_region: Region = .{ .size = limits.arena.tree_sitter, .bits = &tree_sitter_bits }; 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 image_region: Region = .{ .size = limits.arena.image, .bits = &image_bits }; 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 pdf_region: Region = .{ .size = limits.arena.pdf, .bits = &pdf_bits }; 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 }; +var host_region: Region = .{ .size = limits.arena.host, .bits = &host_bits }; /// 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 @@ -546,7 +482,6 @@ pub fn deinit() void { } 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); @@ -594,7 +529,6 @@ test "a region's runs: aligned first fit, growth in place, release" { } test "memory limits preserve desktop capacities" { - if (reduced_target) return error.SkipZigTest; try std.testing.expectEqual(4096, limits.effect_cap); try std.testing.expectEqual(@as(usize, 4 * MiB), limits.pending_write_cap); try std.testing.expectEqual(256, limits.wrap_rows); |
