|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Extensions to [`Box`] for fallible allocations. |
| 4 | +
|
| 5 | +use super::Flags; |
| 6 | +use alloc::boxed::Box; |
| 7 | +use core::alloc::AllocError; |
| 8 | +use core::mem::MaybeUninit; |
| 9 | +use core::result::Result; |
| 10 | + |
| 11 | +/// Extensions to [`Box`]. |
| 12 | +pub trait BoxExt<T>: Sized { |
| 13 | + /// Allocates a new box. |
| 14 | + /// |
| 15 | + /// The allocation may fail, in which case an error is returned. |
| 16 | + fn new(x: T, flags: Flags) -> Result<Self, AllocError>; |
| 17 | + |
| 18 | + /// Allocates a new uninitialised box. |
| 19 | + /// |
| 20 | + /// The allocation may fail, in which case an error is returned. |
| 21 | + fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError>; |
| 22 | +} |
| 23 | + |
| 24 | +impl<T> BoxExt<T> for Box<T> { |
| 25 | + fn new(x: T, flags: Flags) -> Result<Self, AllocError> { |
| 26 | + let mut b = <Self as BoxExt<_>>::new_uninit(flags)?; |
| 27 | + b.write(x); |
| 28 | + // SAFETY: The contents were just initialised in the line above. |
| 29 | + Ok(unsafe { b.assume_init() }) |
| 30 | + } |
| 31 | + |
| 32 | + #[cfg(any(test, testlib))] |
| 33 | + fn new_uninit(_flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> { |
| 34 | + Ok(Box::new_uninit()) |
| 35 | + } |
| 36 | + |
| 37 | + #[cfg(not(any(test, testlib)))] |
| 38 | + fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>>, AllocError> { |
| 39 | + let ptr = if core::mem::size_of::<MaybeUninit<T>>() == 0 { |
| 40 | + core::ptr::NonNull::<_>::dangling().as_ptr() |
| 41 | + } else { |
| 42 | + let layout = core::alloc::Layout::new::<MaybeUninit<T>>(); |
| 43 | + |
| 44 | + // SAFETY: Memory is being allocated (first arg is null). The only other source of |
| 45 | + // safety issues is sleeping on atomic context, which is addressed by klint. Lastly, |
| 46 | + // the type is not a SZT (checked above). |
| 47 | + let ptr = |
| 48 | + unsafe { super::allocator::krealloc_aligned(core::ptr::null_mut(), layout, flags) }; |
| 49 | + if ptr.is_null() { |
| 50 | + return Err(AllocError); |
| 51 | + } |
| 52 | + |
| 53 | + ptr.cast::<MaybeUninit<T>>() |
| 54 | + }; |
| 55 | + |
| 56 | + // SAFETY: For non-zero-sized types, we allocate above using the global allocator. For |
| 57 | + // zero-sized types, we use `NonNull::dangling`. |
| 58 | + Ok(unsafe { Box::from_raw(ptr) }) |
| 59 | + } |
| 60 | +} |
0 commit comments