nstd_shared_ptr_new_zeroed

Function nstd_shared_ptr_new_zeroed 

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

Creates a new zero-initialized instance of a shared pointer.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

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

§Returns

NSTDOptionalSharedPtr shared_ptr - The yet to be shared pointer, or an uninitialized “none” variant if allocating fails.

§Safety

The data to be stored in the shared 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,
    shared_ptr::{nstd_shared_ptr_get, nstd_shared_ptr_new_zeroed},
};

unsafe {
    let size = core::mem::size_of::<u128>();
    let align = core::mem::align_of::<u128>();
    let layout = nstd_core_alloc_layout_new(size, align).unwrap();
    let shared_ptr = nstd_shared_ptr_new_zeroed(&NSTD_ALLOCATOR, layout).unwrap();
    assert!(*nstd_shared_ptr_get(&shared_ptr).cast::<u128>() == 0);
}