diff options
Diffstat (limited to 'src/allocators.zig')
| -rw-r--r-- | src/allocators.zig | 50 |
1 files changed, 9 insertions, 41 deletions
diff --git a/src/allocators.zig b/src/allocators.zig index cf024a0b..61c55f39 100644 --- a/src/allocators.zig +++ b/src/allocators.zig @@ -1,43 +1,9 @@ const std = @import("std"); const builtin = @import("builtin"); -const config = @import("pardes_config"); +const limits = @import("limits.zig"); const Allocator = std.mem.Allocator; const debug_enabled = builtin.mode == .Debug; -/// Three tiers, because the address space differs by four orders of magnitude. -/// `reduced_target` is the browser: a wasm linear memory it grows on demand, so -/// the static reservations are megabytes rather than tens. -/// -/// `p4` is ESP32-P4 firmware, and its tier is deliberately ALL FALLBACK. Every -/// capacity here is a `StackFallbackAllocator`'s buffer, which is a static and -/// therefore lands in `.bss` — and on the P4 `.bss`, `.data` and the stack all -/// share ONE 240 KiB chunk of L2MEM at 0x4FF03000, while the heap the fallback -/// allocator hands out is the separate 384 KiB chunk at 0x4FF40000 - the 128 KiB -/// above that measured as L2 cache rather than memory. A -/// megabyte-shaped reservation here would not fit, and every byte that did fit -/// would be taken from the stack's neighbourhood to duplicate memory the heap -/// already has. So the buffers exist only because the type requires one: 4 KiB -/// absorbs the small churn, and everything else spills to the real heap on the -/// first allocation. -const p4 = config.platform == .p4; -const reduced_target = config.platform == .web or builtin.os.tag == .freestanding; -const KiB = 1024; -const MiB = 1024 * KiB; - -const capacities = struct { - const pardes = if (p4) 4 * KiB else if (reduced_target) 8 * MiB else 32 * MiB; - const frame = if (p4) 4 * KiB else if (reduced_target) 4 * MiB else 16 * MiB; - // Zero is legal and always spills, which is exactly what an arena for a - // compiled-out subsystem should do. `StackFallbackAllocator(0).buffer` is - // `[0]u8`; `get()` inits the FixedBufferAllocator over an empty slice, so - // `FixedBufferAllocator.alloc` fails every nonzero request and `alloc` - // falls through to `self.fallback_allocator.rawAlloc`, while `ownsPtr` over - // an empty range is false for every pointer so `resize`/`remap`/`free` - // route to the fallback too. See lib/std/heap.zig, StackFallbackAllocator. - const tree_sitter = if (p4) 0 else if (reduced_target) 4 * MiB else 16 * MiB; - const image = if (p4) 0 else if (reduced_target) 64 * KiB else 32 * MiB; - const pdf = if (p4) 0 else if (reduced_target or !config.mupdf) 64 * KiB else 64 * MiB; -}; pub const Allocators = struct { pardes: Allocator, @@ -48,11 +14,13 @@ pub const Allocators = struct { pdf: Allocator, }; -var pardes_fallback: std.heap.StackFallbackAllocator(capacities.pardes) = undefined; -var frame_fallback: std.heap.StackFallbackAllocator(capacities.frame) = undefined; -var tree_sitter_fallback: std.heap.StackFallbackAllocator(capacities.tree_sitter) = undefined; -var image_fallback: std.heap.StackFallbackAllocator(capacities.image) = undefined; -var pdf_fallback: std.heap.StackFallbackAllocator(capacities.pdf) = undefined; +/// The static reservations, one per profile tier. See `limits.arena` for why +/// each number is what it is, and why the board's are 4 KiB and zero. +var pardes_fallback: std.heap.StackFallbackAllocator(limits.arena.pardes) = undefined; +var frame_fallback: std.heap.StackFallbackAllocator(limits.arena.frame) = undefined; +var tree_sitter_fallback: std.heap.StackFallbackAllocator(limits.arena.tree_sitter) = undefined; +var image_fallback: std.heap.StackFallbackAllocator(limits.arena.image) = undefined; +var pdf_fallback: std.heap.StackFallbackAllocator(limits.arena.pdf) = undefined; const Debug = std.heap.DebugAllocator(.{}); var pardes_debug: Debug = .init; @@ -121,7 +89,7 @@ test "fixed allocators are separate, spill, and restart" { try std.testing.expect(frame_fallback.fixed_buffer_allocator.ownsPtr(frame.ptr)); try std.testing.expect(core.ptr != frame.ptr); - const spill = try allocs.pardes.alloc(u8, capacities.pardes + 1); + const spill = try allocs.pardes.alloc(u8, limits.arena.pardes + 1); try std.testing.expect(!pardes_fallback.fixed_buffer_allocator.ownsPtr(spill.ptr)); allocs.pardes.free(spill); allocs.frame.free(frame); |
