pub unsafe fn realloc(
ptr: *mut u8,
new_size: usize,
) -> Result<*mut u8, ReallocationError>Expand description
Reallocates memory allocated by crate::alloc.
Mirrors C’s realloc:
realloc(null, new_size)is equivalent tocrate::alloc(new_size).realloc(ptr, 0)freesptrand returns a null pointer.- Otherwise, the allocation is resized, in place when possible, and its contents are
preserved up to the lesser of the old and new sizes. A
new_sizethat rounds up to the allocation’s current total size returnsptrunchanged.
On success, the old pointer must be considered invalid: it must not be dereferenced
or passed to crate::free or this function again. Only the returned pointer may be
used.
§Errors
The old pointer remains valid whenever an error is returned.
AllocationErroris returned ifptris null and the allocation fails, or ifnew_sizeoverflows the size computation or does not form a valid layout.DeallocationErroris returned if the size stored in the header does not form a valid layout, which signals memory corruption.ImproperAlignmentis returned if the pointer provided was not aligned properly.MarkerFreeis returned ifptrwas previously freed or reallocated.MarkerCorruptedis returned if the marker is neither free nor used, which generally signals memory corruption, or the passing of a pointer not allocated bycrate::alloc.NewAllocationFailedis returned if the allocator failed to resize the allocation.
§Safety
See crate::free for safety information. Additionally, best-effort detection of
stale pointers after a successful realloc is only possible when the block has
moved; after an in-place resize, the old pointer aliases the live allocation and
misuse cannot be detected.