nstd_alloc_allocate_zeroed

Function nstd_alloc_allocate_zeroed 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn nstd_alloc_allocate_zeroed( layout: NSTDAllocLayout, ) -> NSTDAnyMut
Available on crate feature alloc only.
Expand description

Allocates a new block of zero-initialized memory.

If allocation fails, a null pointer is returned.

If allocation succeeds, this returns a pointer to the new memory that is suitably aligned for layout’s alignment and the number of bytes allocated is at least equal to layout’s size.

§Parameters:

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

§Returns

NSTDAnyMut ptr - A pointer to the allocated memory, null on error.

§Safety

Behavior is undefined if layout’s size is zero.

§Example

use nstd_sys::{
    alloc::{nstd_alloc_allocate_zeroed, nstd_alloc_deallocate},
    core::alloc::{nstd_core_alloc_layout_new, NSTDAllocError::NSTD_ALLOC_ERROR_NONE},
};

unsafe {
    let size = core::mem::size_of::<[i16; 16]>();
    let align = core::mem::align_of::<[i16; 16]>();
    let layout = nstd_core_alloc_layout_new(size, align).unwrap();
    let mem = nstd_alloc_allocate_zeroed(layout);
    assert!(!mem.is_null());
    assert!(*mem.cast::<[i16; 16]>() == [0i16; 16]);
    assert!(nstd_alloc_deallocate(mem, layout) == NSTD_ALLOC_ERROR_NONE);
}