commit 32a77a6047d378e3bf5b9e8d071f182dc64823f8 (tree)
parent 41bbadbb9a27107da539e27e175f5bdb52656bd5
Author: Andrew Kelley <andrew@ziglang.org>
Date: Sun, 6 Sep 2020 01:16:33 -0400
Merge pull request #6239 from pfgithub/patch-1
Support allocating 0 bit types
Diffstat:
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/std/heap.zig b/lib/std/heap.zig
@@ -915,6 +915,10 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void {
testing.expect(slice.len == 10);
allocator.free(slice);
+
+ const zero_bit_ptr = try allocator.create(u0);
+ zero_bit_ptr.* = 0;
+ allocator.destroy(zero_bit_ptr);
}
pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {
diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig
@@ -159,7 +159,7 @@ fn moveBytes(
/// Returns a pointer to undefined memory.
/// Call `destroy` with the result to free the memory.
pub fn create(self: *Allocator, comptime T: type) Error!*T {
- if (@sizeOf(T) == 0) return &(T{});
+ if (@sizeOf(T) == 0) return @as(*T, undefined);
const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress());
return &slice[0];
}