authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-06 01:16:33-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-06 01:16:33-04:00
log32a77a6047d378e3bf5b9e8d071f182dc64823f8
tree4a99d0c2b2599095c1af48365d5e445cbefe66ed
parent41bbadbb9a27107da539e27e175f5bdb52656bd5
parentabe672956ed037077b23ac6aa29df3dd99795539
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6239 from pfgithub/patch-1

Support allocating 0 bit types

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

lib/std/heap.zig+4
......@@ -915,6 +915,10 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void {
915915 testing.expect(slice.len == 10);
916916
917917 allocator.free(slice);
918
919 const zero_bit_ptr = try allocator.create(u0);
920 zero_bit_ptr.* = 0;
921 allocator.destroy(zero_bit_ptr);
918922}
919923
920924pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {
lib/std/mem/Allocator.zig+1-1
......@@ -159,7 +159,7 @@ fn moveBytes(
159159/// Returns a pointer to undefined memory.
160160/// Call `destroy` with the result to free the memory.
161161pub fn create(self: *Allocator, comptime T: type) Error!*T {
162 if (@sizeOf(T) == 0) return &(T{});
162 if (@sizeOf(T) == 0) return @as(*T, undefined);
163163 const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress());
164164 return &slice[0];
165165}