authorgravatar for ilia.choly@gmail.comIlia Choly <ilia.choly@gmail.com> 2024-11-03 09:47:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-24 18:19:11-05:00
loge2f24a2d7096e4a28ba74513ed9473da0b7fb372
tree0a1578713fd639963679874a900948649730a9d8
parentc2db5d9cd1c83a5aed20a7e2ec8fdf874f13e3db

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.

1 files changed, 3 insertions(+), 2 deletions(-)

lib/std/mem/Allocator.zig+3-2
...@@ -301,8 +301,9 @@ pub fn reallocAdvanced(...@@ -301,8 +301,9 @@ pub fn reallocAdvanced(
301 return mem.bytesAsSlice(T, new_bytes);301 return mem.bytesAsSlice(T, new_bytes);
302}302}
303303
304/// Free an array allocated with `alloc`. To free a single item,304/// Free an array allocated with `alloc`.
305/// see `destroy`.305/// If memory has length 0, free is a no-op.
306/// To free a single item, see `destroy`.
306pub fn free(self: Allocator, memory: anytype) void {307pub fn free(self: Allocator, memory: anytype) void {
307 const Slice = @typeInfo(@TypeOf(memory)).pointer;308 const Slice = @typeInfo(@TypeOf(memory)).pointer;
308 const bytes = mem.sliceAsBytes(memory);309 const bytes = mem.sliceAsBytes(memory);