From 99d812cfd203b7c61697d7e18857b4720d4aed51 Mon Sep 17 00:00:00 2001 From: Ilia Choly Date: Sun, 3 Nov 2024 09:47:57 -0500 Subject: [PATCH] Allocator.free: document zero-length behavior It wasn't immediately clear from the implementation whether passing zero-length memory to free() was undefined behavior or intentionally supported. Since ArrayList and other core data structures rely on this behavior working correctly, this should be explicitly documented as part of the public API contract. --- lib/std/mem/Allocator.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 0d4ab9141f..8aea197d6a 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -301,8 +301,9 @@ pub fn reallocAdvanced( return mem.bytesAsSlice(T, new_bytes); } -/// Free an array allocated with `alloc`. To free a single item, -/// see `destroy`. +/// Free an array allocated with `alloc`. +/// If memory has length 0, free is a no-op. +/// To free a single item, see `destroy`. pub fn free(self: Allocator, memory: anytype) void { const Slice = @typeInfo(@TypeOf(memory)).pointer; const bytes = mem.sliceAsBytes(memory);