Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › mem › Allocator › resize Public Function resize Doc Source Public Function std.mem.Allocator.resize
pub fn resize (self : Allocator , allocation : anytype , new_len : usize ) bool Params File mem/Allocator.zig: 319
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 pub fn resize (self : Allocator , allocation : anytype , new_len : usize ) bool {
const slice_info = @typeInfo (@TypeOf (allocation )).pointer ;
const T = if (slice_info .size != .slice ) comptime T : {
assert (slice_info .size == .one );
break :T @typeInfo (slice_info .child ).array .child ;
} else slice_info .child ;
if (new_len == 0 ) {
self .free (allocation );
return true ;
}
if (allocation .len == 0 ) {
return false ;
}
const old_memory : []u8 = @ptrCast (@constCast (mem .absorbSentinel (allocation )));
const new_len_bytes = math .mul (usize , @sizeOf (T ), new_len ) catch return false ;
return self .rawResize (
old_memory ,
.fromByteUnits (slice_info .attrs .@"align" orelse @alignOf (T )),
new_len_bytes ,
@returnAddress (),
);
}
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.allocationmay be an empty slice, in which casefalseis returned, unlessnew_lenis also 0, in which casetrueis returned.new_lenmay be zero, in which case the allocation is freed.