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 library itself.
Zig › c/ › malloc.zig › realloc
realloc
malloc.realloc
fn realloc (opt_old_base : ?[*]align (alignment_bytes ) u8 , n : usize ) callconv (.c ) ?[*]align (alignment_bytes ) u8
File
Code
fn realloc (opt_old_base : ?[*]align (alignment_bytes ) u8 , n : usize ) callconv (.c ) ?[*]align (alignment_bytes ) u8 {
if (n == 0 ) {
free (opt_old_base );
return null ;
}
const old_base = opt_old_base orelse return malloc (n );
const new_size = std .math .cast (Header .Size , n ) orelse return nomem ();
const old_header : Header = .get (old_base );
const old_size = old_header .size ;
const old_alignment = old_header .alignment ;
const old_alignment_bytes = old_alignment .toByteUnits ();
const old_ptr = old_base - old_alignment_bytes ;
const old_slice = old_ptr [0 .. old_size + old_alignment_bytes ];
const new_base : [*]align (alignment_bytes ) u8 = if (vtable .remap (
no_context ,
old_slice ,
old_alignment ,
n + old_alignment_bytes ,
no_ra ,
)) |new_ptr | @alignCast (new_ptr + old_alignment_bytes ) else b : {
const new_ptr : [*]align (alignment_bytes ) u8 = @alignCast (
vtable .alloc (no_context , n + old_alignment_bytes , old_alignment , no_ra ) orelse
return nomem (),
);
const new_base : [*]align (alignment_bytes ) u8 = @alignCast (new_ptr + old_alignment_bytes );
const copy_len = @min (new_size , old_size );
@memcpy (new_base [0 ..copy_len ], old_base [0 ..copy_len ]);
vtable .free (no_context , old_slice , old_alignment , no_ra );
break :b new_base ;
};
return Header .set (new_base , old_alignment , new_size );
}