nstd_alloc_reallocate

Function nstd_alloc_reallocate 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn nstd_alloc_reallocate( ptr: &mut NSTDAnyMut, old_layout: NSTDAllocLayout, new_layout: NSTDAllocLayout, ) -> NSTDAllocError
Available on crate feature alloc only.
Expand description

Reallocates memory that was previously allocated by this allocator.

On successful reallocation, ptr will point to the new memory location and NSTD_ALLOC_ERROR_NONE will be returned. If this is not the case and reallocation fails, the pointer will remain untouched and the appropriate error is returned.

§Parameters:

  • NSTDAnyMut *ptr - A pointer to the allocated memory.

  • NSTDAllocLayout old_layout - Describes the previous memory layout.

  • NSTDAllocLayout new_layout - Describes the new memory layout to allocate for.

§Returns

NSTDAllocError errc - The allocation operation error code.

§Safety

  • Behavior is undefined if new_layout’s size is zero.

  • Behavior is undefined if ptr is not a pointer to memory allocated by this allocator.

  • old_layout must 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},
    core::alloc::{nstd_core_alloc_layout_new, NSTDAllocError::NSTD_ALLOC_ERROR_NONE},
};


unsafe {
    let mut size = core::mem::size_of::<[u64; 64]>();
    let mut align = core::mem::align_of::<[u64; 64]>();
    let layout = nstd_core_alloc_layout_new(size, align).unwrap();
    let mut mem = nstd_alloc_allocate_zeroed(layout);
    assert!(!mem.is_null());
    assert!(*mem.cast::<[u64; 64]>() == [0u64; 64]);

    size = core::mem::size_of::<[u64; 32]>();
    align = core::mem::align_of::<[u64; 32]>();
    let new_layout = nstd_core_alloc_layout_new(size, align).unwrap();
    assert!(nstd_alloc_reallocate(&mut mem, layout, new_layout) == NSTD_ALLOC_ERROR_NONE);
    assert!(*mem.cast::<[u64; 32]>() == [0u64; 32]);

    assert!(nstd_alloc_deallocate(mem, new_layout) == NSTD_ALLOC_ERROR_NONE);
}