Skip to content

Commit 16af5c8

Browse files
committed
fallible allocator experiment
1 parent adc719d commit 16af5c8

File tree

12 files changed

+663
-575
lines changed

12 files changed

+663
-575
lines changed

library/alloc/src/boxed.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ use core::task::{Context, Poll};
167167

168168
#[cfg(not(no_global_oom_handling))]
169169
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
170-
use crate::alloc::{AllocError, Allocator, Global, Layout};
171170
#[cfg(not(no_global_oom_handling))]
172171
use crate::borrow::Cow;
172+
use crate::falloc::{AllocError, Allocator, Global, Layout};
173173
use crate::raw_vec::RawVec;
174174
#[cfg(not(no_global_oom_handling))]
175175
use crate::str::from_boxed_utf8_unchecked;
@@ -624,7 +624,9 @@ impl<T> Box<[T]> {
624624
#[unstable(feature = "new_uninit", issue = "63291")]
625625
#[must_use]
626626
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
627-
unsafe { RawVec::with_capacity(len).into_box(len) }
627+
unsafe {
628+
<Global as Allocator>::map_result(RawVec::with_capacity_in(len, Global)).into_box(len)
629+
}
628630
}
629631

630632
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -649,7 +651,10 @@ impl<T> Box<[T]> {
649651
#[unstable(feature = "new_uninit", issue = "63291")]
650652
#[must_use]
651653
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
652-
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
654+
unsafe {
655+
<Global as Allocator>::map_result(RawVec::with_capacity_zeroed_in(len, Global))
656+
.into_box(len)
657+
}
653658
}
654659

655660
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -675,14 +680,7 @@ impl<T> Box<[T]> {
675680
#[unstable(feature = "allocator_api", issue = "32838")]
676681
#[inline]
677682
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
678-
unsafe {
679-
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
680-
Ok(l) => l,
681-
Err(_) => return Err(AllocError),
682-
};
683-
let ptr = Global.allocate(layout)?;
684-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
685-
}
683+
unsafe { Ok(RawVec::with_capacity_in(len, Global).map_err(|_| AllocError)?.into_box(len)) }
686684
}
687685

688686
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -708,12 +706,7 @@ impl<T> Box<[T]> {
708706
#[inline]
709707
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
710708
unsafe {
711-
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
712-
Ok(l) => l,
713-
Err(_) => return Err(AllocError),
714-
};
715-
let ptr = Global.allocate_zeroed(layout)?;
716-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
709+
Ok(RawVec::with_capacity_zeroed_in(len, Global).map_err(|_| AllocError)?.into_box(len))
717710
}
718711
}
719712
}
@@ -741,12 +734,11 @@ impl<T, A: Allocator> Box<[T], A> {
741734
///
742735
/// assert_eq!(*values, [1, 2, 3])
743736
/// ```
744-
#[cfg(not(no_global_oom_handling))]
745737
#[unstable(feature = "allocator_api", issue = "32838")]
746738
// #[unstable(feature = "new_uninit", issue = "63291")]
747739
#[must_use]
748-
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
749-
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
740+
pub fn new_uninit_slice_in(len: usize, alloc: A) -> A::Result<Box<[mem::MaybeUninit<T>], A>> {
741+
unsafe { A::map_result(RawVec::with_capacity_in(len, alloc).map(|r| r.into_box(len))) }
750742
}
751743

752744
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -769,12 +761,13 @@ impl<T, A: Allocator> Box<[T], A> {
769761
/// ```
770762
///
771763
/// [zeroed]: mem::MaybeUninit::zeroed
772-
#[cfg(not(no_global_oom_handling))]
773764
#[unstable(feature = "allocator_api", issue = "32838")]
774765
// #[unstable(feature = "new_uninit", issue = "63291")]
775766
#[must_use]
776-
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
777-
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
767+
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> A::Result<Box<[mem::MaybeUninit<T>], A>> {
768+
unsafe {
769+
A::map_result(RawVec::with_capacity_zeroed_in(len, alloc).map(|r| r.into_box(len)))
770+
}
778771
}
779772
}
780773

@@ -1474,7 +1467,7 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
14741467
#[inline]
14751468
fn from_slice(slice: &[T]) -> Self {
14761469
let len = slice.len();
1477-
let buf = RawVec::with_capacity(len);
1470+
let buf = <Global as Allocator>::map_result(RawVec::with_capacity_in(len, Global));
14781471
unsafe {
14791472
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
14801473
buf.into_box(slice.len()).assume_init()
@@ -2014,9 +2007,8 @@ impl<I> FromIterator<I> for Box<[I]> {
20142007
}
20152008
}
20162009

2017-
#[cfg(not(no_global_oom_handling))]
20182010
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2019-
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
2011+
impl<T: Clone, A: Allocator<Result<Self> = Self> + Clone> Clone for Box<[T], A> {
20202012
fn clone(&self) -> Self {
20212013
let alloc = Box::allocator(self).clone();
20222014
self.to_vec_in(alloc).into_boxed_slice()

0 commit comments

Comments
 (0)