MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / try_new_uninit_box

Function try_new_uninit_box

crates/core/src/error/boxed.rs:28–43  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26/// Create a `Box<T>`, or return an `OutOfMemory` error.
27#[inline]
28pub(crate) fn try_new_uninit_box<T>() -> Result<Box<MaybeUninit<T>>, OutOfMemory> {
29 let layout = std_alloc::alloc::Layout::new::<MaybeUninit<T>>();
30
31 if layout.size() == 0 {
32 // NB: no actual allocation takes place when boxing zero-sized types.
33 return Ok(Box::new(MaybeUninit::uninit()));
34 }
35
36 // Safety: layout size is non-zero.
37 let ptr = unsafe { try_alloc(layout)? };
38
39 let ptr = ptr.cast::<MaybeUninit<T>>();
40
41 // Safety: The pointer's memory block was allocated by the global allocator.
42 Ok(unsafe { Box::from_raw(ptr.as_ptr()) })
43}

Callers 3

newMethod · 0.85

Calls 5

OkFunction · 0.85
try_allocFunction · 0.70
newFunction · 0.50
sizeMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected