Function nstd_sys::alloc::nstd_alloc_reallocate
source · #[no_mangle]
pub unsafe extern "C" fn nstd_alloc_reallocate(
ptr: &mut NSTDAnyMut,
size: NSTDUInt,
new_size: NSTDUInt
) -> NSTDAllocErrorAvailable on crate feature
nstd_alloc only.Expand description
Reallocates a block of memory previously allocated by nstd_alloc_allocate[_zeroed].
If everything goes right, the pointer will point to the new memory location and 0 will be returned. If this is not the case and allocation fails, the pointer will remain untouched and a value of nonzero is returned.
Parameters:
-
NSTDAnyMut *ptr- A pointer to the allocated memory. -
NSTDUInt size- The number of bytes currently allocated. -
NSTDUInt new_size- The number of bytes to reallocate.
Returns
NSTDAllocError errc - The allocation operation error code.
Safety
-
Behavior is undefined if
new_sizeis zero. -
Behavior is undefined if
ptris not a value returned bynstd_alloc_allocate[_zeroed]. -
sizemust be the same value that was used to allocate the memory buffer.
Example
use nstd_sys::alloc::{
nstd_alloc_allocate_zeroed, nstd_alloc_deallocate, nstd_alloc_reallocate,
NSTDAllocError::NSTD_ALLOC_ERROR_NONE,
};
const SIZE: usize = core::mem::size_of::<[u64; 64]>();
unsafe {
let mut mem = nstd_alloc_allocate_zeroed(SIZE);
assert!(!mem.is_null());
assert!(*mem.cast::<[u64; 64]>() == [0u64; 64]);
assert!(nstd_alloc_reallocate(&mut mem, SIZE, SIZE / 2) == NSTD_ALLOC_ERROR_NONE);
assert!(*mem.cast::<[u64; 32]>() == [0u64; 32]);
nstd_alloc_deallocate(&mut mem, SIZE);
}