| ... | @@ -311,12 +311,15 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { | ... | @@ -311,12 +311,15 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 311 | /// `allocation` may be an empty slice, in which case a new allocation is made. | 311 | /// `allocation` may be an empty slice, in which case a new allocation is made. |
| 312 | /// | 312 | /// |
| 313 | /// `new_len` may be zero, in which case the allocation is freed. | 313 | /// `new_len` may be zero, in which case the allocation is freed. |
| | 314 | /// |
| | 315 | /// If the allocation's elements' type is zero bytes sized, `allocation.len` is set to `new_len`. |
| 314 | pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: { | 316 | pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: { |
| 315 | const Slice = @typeInfo(@TypeOf(allocation)).pointer; | 317 | const Slice = @typeInfo(@TypeOf(allocation)).pointer; |
| 316 | break :t ?[]align(Slice.alignment) Slice.child; | 318 | break :t ?[]align(Slice.alignment) Slice.child; |
| 317 | } { | 319 | } { |
| 318 | const Slice = @typeInfo(@TypeOf(allocation)).pointer; | 320 | const Slice = @typeInfo(@TypeOf(allocation)).pointer; |
| 319 | const T = Slice.child; | 321 | const T = Slice.child; |
| | 322 | |
| 320 | const alignment = Slice.alignment; | 323 | const alignment = Slice.alignment; |
| 321 | if (new_len == 0) { | 324 | if (new_len == 0) { |
| 322 | self.free(allocation); | 325 | self.free(allocation); |
| ... | @@ -325,6 +328,11 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: { | ... | @@ -325,6 +328,11 @@ pub fn remap(self: Allocator, allocation: anytype, new_len: usize) t: { |
| 325 | if (allocation.len == 0) { | 328 | if (allocation.len == 0) { |
| 326 | return null; | 329 | return null; |
| 327 | } | 330 | } |
| | 331 | if (@sizeOf(T) == 0) { |
| | 332 | var new_memory = allocation; |
| | 333 | new_memory.len = new_len; |
| | 334 | return new_memory; |
| | 335 | } |
| 328 | const old_memory = mem.sliceAsBytes(allocation); | 336 | const old_memory = mem.sliceAsBytes(allocation); |
| 329 | // I would like to use saturating multiplication here, but LLVM cannot lower it | 337 | // I would like to use saturating multiplication here, but LLVM cannot lower it |
| 330 | // on WebAssembly: https://github.com/ziglang/zig/issues/9660 | 338 | // on WebAssembly: https://github.com/ziglang/zig/issues/9660 |