Function nstd_heap_ptr_new_zeroed

Source
#[no_mangle]
pub unsafe extern "C" fn nstd_heap_ptr_new_zeroed(
    allocator: &NSTDAllocator,
    layout: NSTDAllocLayout,
) -> NSTDOptionalHeapPtr<'_>
Available on crate feature heap_ptr only.
Expand description

Creates a new zero-initialized heap allocated object.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

  • NSTDAllocLayout layout - The heap object’s memory layout.

§Returns

NSTDOptionalHeapPtr hptr - The new heap allocated object, or an uninitialized “none” variant if allocating fails.

§Safety

The data to be stored in the heap pointer must be safely representable by an all-zero byte pattern.

§Example

use nstd_sys::{
    alloc::NSTD_ALLOCATOR,
    core::alloc::nstd_core_alloc_layout_new,
    heap_ptr::{nstd_heap_ptr_get, nstd_heap_ptr_new_zeroed},
};

unsafe {
    let size = core::mem::size_of::<u64>();
    let align = core::mem::align_of::<u64>();
    let layout = nstd_core_alloc_layout_new(size, align).unwrap();
    let hptr = nstd_heap_ptr_new_zeroed(&NSTD_ALLOCATOR, layout).unwrap();
    assert!(*nstd_heap_ptr_get(&hptr).cast::<u64>() == 0);
}