nstd_alloc_allocate

Function nstd_alloc_allocate 

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

Allocates a new block of 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.

  • The new memory buffer should be considered uninitialized.

§Example

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

unsafe {
    let layout = nstd_core_alloc_layout_new(32, 1).unwrap();
    let mem = nstd_alloc_allocate(layout);
    assert!(!mem.is_null());
    assert!(nstd_alloc_deallocate(mem, layout) == NSTD_ALLOC_ERROR_NONE);
}