Function nstd_heap_ptr_new

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

Creates a new initialized heap allocated object.

§Parameters:

  • const NSTDAllocator *allocator - The memory allocator.

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

  • NSTDAny init - A pointer to the object to initialize the heap object with.

§Returns

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

§Safety

init must be a pointer to a value that is valid for reads based on layout.

§Example

use core::ptr::addr_of;
use nstd_sys::{
    alloc::NSTD_ALLOCATOR, core::alloc::nstd_core_alloc_layout_new, heap_ptr::nstd_heap_ptr_new,
};

let v = '🦀';
let size = core::mem::size_of::<char>();
let align = core::mem::align_of::<char>();
let layout = nstd_core_alloc_layout_new(size, align).unwrap();
let hptr = unsafe { nstd_heap_ptr_new(&NSTD_ALLOCATOR, layout, addr_of!(v).cast()).unwrap() };