From 583b843803c5851c1d9796ba301f6a602a6da3d9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 18 Aug 2020 15:09:48 -0700 Subject: [PATCH] std.heap.GeneralPurposeAllocator: add `never_unmap` config option This is a temporary debugging trick you can use to turn segfaults into more helpful logged error messages with stack trace details. The downside is that every allocation will be leaked! --- lib/std/heap/general_purpose_allocator.zig | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig index 91a01bb837..cb53af113e 100644 --- a/lib/std/heap/general_purpose_allocator.zig +++ b/lib/std/heap/general_purpose_allocator.zig @@ -142,6 +142,11 @@ pub const Config = struct { /// Whether the allocator may be used simultaneously from multiple threads. thread_safe: bool = !std.builtin.single_threaded, + + /// This is a temporary debugging trick you can use to turn segfaults into more helpful + /// logged error messages with stack trace details. The downside is that every allocation + /// will be leaked! + never_unmap: bool = false, }; pub fn GeneralPurposeAllocator(comptime config: Config) type { @@ -416,7 +421,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { bucket.prev.next = bucket.next; self.buckets[bucket_index] = bucket.prev; } - self.backing_allocator.free(bucket.page[0..page_size]); + if (!config.never_unmap) { + self.backing_allocator.free(bucket.page[0..page_size]); + } const bucket_size = bucketSize(size_class); const bucket_slice = @ptrCast([*]align(@alignOf(BucketHeader)) u8, bucket)[0..bucket_size]; self.backing_allocator.free(bucket_slice);