From 76498686639d01050eb917b016a61c58503510e5 Mon Sep 17 00:00:00 2001 From: Justus Klausecker Date: Fri, 6 Mar 2026 13:05:06 +0100 Subject: [PATCH] std.heap.ArenaAllocator/std.heap.FixedBufferAllocator: make shrinking always succeed Shrinking allocations should always succeed with these allocators, even if the allocation in question is the most recent one and `resize` didn't manage to decrement the end index of its buffer successfully. --- lib/std/heap/ArenaAllocator.zig | 3 ++- lib/std/heap/FixedBufferAllocator.zig | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/std/heap/ArenaAllocator.zig b/lib/std/heap/ArenaAllocator.zig index 3532bd07af0daec1bf166529bbb5562c5f3b1028..8888ebdbee3573b997e018f007d38693819b0a9d 100644 --- a/lib/std/heap/ArenaAllocator.zig +++ b/lib/std/heap/ArenaAllocator.zig @@ -572,7 +572,8 @@ fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, r new_end_index, .monotonic, .monotonic, - ); + ) or + new_len <= memory.len; // Shrinking allocations should always succeed. } fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { diff --git a/lib/std/heap/FixedBufferAllocator.zig b/lib/std/heap/FixedBufferAllocator.zig index 1303bd35abbfcd378eed42553f046772dd40cb9d..e15875948a977b6dff2193c4f44ec3399cc48aa1 100644 --- a/lib/std/heap/FixedBufferAllocator.zig +++ b/lib/std/heap/FixedBufferAllocator.zig @@ -172,7 +172,8 @@ fn threadSafeResize(ctx: *anyopaque, memory: []u8, alignment: mem.Alignment, new new_end_index, .monotonic, .monotonic, - ); + ) or + new_len <= memory.len; // Shrinking allocations should always succeed. } fn threadSafeRemap(ctx: *anyopaque, memory: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 { -- 2.54.0