Function resize [src]
Request to modify the size of an allocation.
It is guaranteed to not move the pointer, however the allocator
implementation may refuse the resize request by returning false.
allocation may be an empty slice, in which case a new allocation is made.
new_len may be zero, in which case the allocation is freed.
Prototype
pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool
Parameters
self: Allocator
new_len: usize
Source
pub fn resize(self: Allocator, allocation: anytype, new_len: usize) bool {
const Slice = @typeInfo(@TypeOf(allocation)).pointer;
const T = Slice.child;
const alignment = Slice.alignment;
if (new_len == 0) {
self.free(allocation);
return true;
}
if (allocation.len == 0) {
return false;
}
const old_memory = mem.sliceAsBytes(allocation);
// I would like to use saturating multiplication here, but LLVM cannot lower it
// on WebAssembly: https://github.com/ziglang/zig/issues/9660
//const new_len_bytes = new_len *| @sizeOf(T);
const new_len_bytes = math.mul(usize, @sizeOf(T), new_len) catch return false;
return self.rawResize(old_memory, .fromByteUnits(alignment), new_len_bytes, @returnAddress());
}