Skip to content

Commit

Permalink
Allocator.free: document zero-length behavior
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
icholy authored and andrewrk committed Nov 24, 2024
1 parent c2db5d9 commit e2f24a2
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/std/mem/Allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e2f24a2

Please sign in to comment.