Commit dd2fa4f75d

Andrew Kelley <andrew@ziglang.org>
2025-01-31 03:25:01
std.heap.GeneralPurposeAllocator: runtime-known page size
no longer causes compilation failure. This also addresses the problem of high map count causing OOM by choosing a page size of 2MiB for most targets when the page_size_max is smaller than this number.
1 parent b23662f
Changed files (1)
lib/std/heap/general_purpose_allocator.zig
@@ -99,7 +99,11 @@ const math = std.math;
 const assert = std.debug.assert;
 const mem = std.mem;
 const Allocator = std.mem.Allocator;
-const page_size = std.heap.pageSize(); // TODO: allow this to be runtime known
+const page_size: usize = @max(std.heap.page_size_max, switch (builtin.os.tag) {
+    .windows => 64 * 1024, // Makes `std.heap.PageAllocator` take the happy path.
+    .wasi => 64 * 1024, // Max alignment supported by `std.heap.WasmAllocator`.
+    else => 2 * 1024 * 1024, // Avoids too many active mappings when `page_size_max` is low.
+});
 const StackTrace = std.builtin.StackTrace;
 
 /// Integer type for pointing to slots in a small allocation