| ... | ... | @@ -316,18 +316,15 @@ pub fn allocBytesAligned( |
| 316 | 316 | /// |
| 317 | 317 | /// `new_len` may be zero, in which case the allocation is freed. |
| 318 | 318 | pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 319 | | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 320 | | if (slice_info.size != .slice) { |
| 321 | | const slice: Slice(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 322 | | return resize(self, slice, new_len); |
| 323 | | } |
| 324 | | comptime assert(slice_info.size == .slice); |
| 319 | const SliceType = Slice(@TypeOf(allocation)); |
| 320 | const slice: SliceType = allocation; // coerce *[len]T to []T |
| 321 | const slice_info = @typeInfo(SliceType).pointer; |
| 325 | 322 | const T = slice_info.child; |
| 326 | 323 | if (new_len == 0) { |
| 327 | | self.free(allocation); |
| 324 | self.free(slice); |
| 328 | 325 | return true; |
| 329 | 326 | } |
| 330 | | if (allocation.len == 0) { |
| 327 | if (slice.len == 0) { |
| 331 | 328 | return false; |
| 332 | 329 | } |
| 333 | 330 | const old_memory: []u8 = @ptrCast(@constCast(mem.absorbSentinel(allocation))); |
| ... | ... | @@ -360,27 +357,24 @@ pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool { |
| 360 | 357 | /// |
| 361 | 358 | /// If the allocation's elements' type is zero bytes sized, `allocation.len` is set to `new_len`. |
| 362 | 359 | pub fn remap(self: Allocator, allocation: anytype, new_len: usize) ?Slice(@TypeOf(allocation)) { |
| 363 | | const slice_info = @typeInfo(@TypeOf(allocation)).pointer; |
| 364 | | if (slice_info.size != .slice) { |
| 365 | | const slice: Slice(@TypeOf(allocation)) = allocation; // coerce *[len]T to []T |
| 366 | | return remap(self, slice, new_len); |
| 367 | | } |
| 368 | | comptime assert(slice_info.size == .slice); |
| 360 | const SliceType = Slice(@TypeOf(allocation)); |
| 361 | const slice: SliceType = allocation; // coerce *[len]T to []T |
| 362 | const slice_info = @typeInfo(SliceType).pointer; |
| 369 | 363 | const T = slice_info.child; |
| 370 | 364 | |
| 371 | 365 | if (new_len == 0) { |
| 372 | | self.free(allocation); |
| 373 | | return allocation[0..0]; |
| 366 | self.free(slice); |
| 367 | return slice[0..0]; |
| 374 | 368 | } |
| 375 | | if (allocation.len == 0) { |
| 369 | if (slice.len == 0) { |
| 376 | 370 | return null; |
| 377 | 371 | } |
| 378 | 372 | if (@sizeOf(T) == 0) { |
| 379 | | var new_memory = allocation; |
| 373 | var new_memory = slice; |
| 380 | 374 | new_memory.len = new_len; |
| 381 | 375 | return new_memory; |
| 382 | 376 | } |
| 383 | | const old_memory: []u8 = @ptrCast(@constCast(mem.absorbSentinel(allocation))); |
| 377 | const old_memory: []u8 = @ptrCast(@constCast(mem.absorbSentinel(slice))); |
| 384 | 378 | // I would like to use saturating multiplication here, but LLVM cannot lower it |
| 385 | 379 | // on WebAssembly: https://github.com/ziglang/zig/issues/9660 |
| 386 | 380 | //const new_len_bytes = new_len *| @sizeOf(T); |
| ... | ... | @@ -418,25 +412,23 @@ pub fn reallocAdvanced( |
| 418 | 412 | new_n: usize, |
| 419 | 413 | return_address: usize, |
| 420 | 414 | ) Error!Slice(@TypeOf(old_mem)) { |
| 421 | | const slice_info = @typeInfo(@TypeOf(old_mem)).pointer; |
| 422 | | if (slice_info.size != .slice) { |
| 423 | | const slice: Slice(@TypeOf(old_mem)) = old_mem; // coerce *[len]T to []T |
| 424 | | return reallocAdvanced(self, slice, new_n, return_address); |
| 425 | | } |
| 415 | const SliceType = Slice(@TypeOf(old_mem)); |
| 416 | const slice: SliceType = old_mem; // coerce *[len]T to []T |
| 417 | const slice_info = @typeInfo(SliceType).pointer; |
| 426 | 418 | comptime assert(slice_info.size == .slice); |
| 427 | 419 | const T = slice_info.child; |
| 428 | | if (old_mem.len == 0) { |
| 420 | if (slice.len == 0) { |
| 429 | 421 | return self.allocAdvancedWithRetAddr(T, .fromByteUnitsOptional(slice_info.attrs.@"align"), new_n, return_address); |
| 430 | 422 | } |
| 431 | 423 | if (new_n == 0) { |
| 432 | | self.free(old_mem); |
| 424 | self.free(slice); |
| 433 | 425 | const alignment = slice_info.attrs.@"align" orelse @alignOf(T); |
| 434 | 426 | const addr = comptime std.mem.alignBackward(usize, math.maxInt(usize), alignment); |
| 435 | 427 | const ptr: *align(alignment) [0]T = @ptrFromInt(addr); |
| 436 | 428 | return ptr; |
| 437 | 429 | } |
| 438 | 430 | |
| 439 | | const old_byte_slice: []u8 = @ptrCast(@constCast(mem.absorbSentinel(old_mem))); |
| 431 | const old_byte_slice: []u8 = @ptrCast(@constCast(mem.absorbSentinel(slice))); |
| 440 | 432 | const byte_count = math.mul(usize, @sizeOf(T), new_n) catch return error.OutOfMemory; |
| 441 | 433 | // Note: can't set shrunk memory to undefined as memory shouldn't be modified on realloc failure |
| 442 | 434 | if (self.rawRemap(old_byte_slice, .fromByteUnits(slice_info.attrs.@"align" orelse @alignOf(T)), byte_count, return_address)) |p| { |