Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallible allocation experiment 2 #117738

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use core::ptr::{self, NonNull};
#[doc(inline)]
pub use core::alloc::*;

#[unstable(feature = "allocator_api", issue = "32838")]
pub mod failure_handling;

#[cfg(test)]
mod tests;

Expand Down
39 changes: 39 additions & 0 deletions library/alloc/src/alloc/failure_handling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! TBD
//!

mod sealed {
pub trait Sealed {}
}

/// Describes the handling behavior in case of allocation failure.
pub trait FailureHandling: sealed::Sealed + Send + Sync + Unpin {
/// The type returned by allocating functions.
///
/// `Fallible` functions will return `Result<T, E>`,
/// but `Fatal` functions will return `T`.
type Result<T, E>;
}

/// Handle allocation failure globally by panicking / aborting.
#[derive(Debug)]
pub struct Fatal;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct Fatal;
#[non_exhaustive]
pub enum Fatal {}

Maybe?


impl sealed::Sealed for Fatal {}
impl FailureHandling for Fatal {
type Result<T, E> = T;
}

/// Handle allocation failure falliblyby returning a `Result`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Handle allocation failure falliblyby returning a `Result`.
/// Handle allocation failure fallibly by returning a `Result`.

#[derive(Debug)]
pub struct Fallible;

impl sealed::Sealed for Fallible {}
impl FailureHandling for Fallible {
type Result<T, E> = Result<T, E>;
}

/// Type parameter default `FailureHandling` for use in containers.
#[cfg(not(no_global_oom_handling))]
pub type DefaultFailureHandling = Fatal;
#[cfg(no_global_oom_handling)]
pub type DefaultFailureHandling = Fallible;
6 changes: 4 additions & 2 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ use core::pin::Pin;
use core::ptr::{self, NonNull, Unique};
use core::task::{Context, Poll};

use crate::alloc::failure_handling::Fallible;

#[cfg(not(no_global_oom_handling))]
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
use crate::alloc::{AllocError, Allocator, Global, Layout};
Expand Down Expand Up @@ -692,7 +694,7 @@ impl<T> Box<[T]> {
};
Global.allocate(layout)?.cast()
};
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
unsafe { Ok(RawVec::<_, _, Fallible>::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
}

/// Constructs a new boxed slice with uninitialized contents, with the memory
Expand Down Expand Up @@ -726,7 +728,7 @@ impl<T> Box<[T]> {
};
Global.allocate_zeroed(layout)?.cast()
};
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
unsafe { Ok(RawVec::<_, _, Fallible>::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::slice;
#[allow(unused_imports)]
use core::mem;

use crate::alloc::failure_handling::Fatal;
use crate::alloc::{Allocator, Global};
use crate::collections::TryReserveError;
use crate::collections::TryReserveErrorKind;
Expand Down Expand Up @@ -102,7 +103,7 @@ pub struct VecDeque<
// if `len == 0`, the exact value of `head` is unimportant.
// if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`.
len: usize,
buf: RawVec<T, A>,
buf: RawVec<T, A, Fatal>,
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
21 changes: 21 additions & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ pub mod __export {
pub use core::format_args;
}

// HACK: so `std::vec::Vec` doesn't inherit the third `FailureHandling` parameter
#[cfg(not(test))]
#[doc(hidden)]
#[unstable(feature = "std_internals", issue = "none", reason = "implementation detail")]
pub mod std_vec {
#[stable(feature = "rust1", since = "1.0.0")]
pub mod __export {
// FIXME: include `vec` module docs here

#[stable(feature = "rust1", since = "1.0.0")]
pub use crate::vec::*;

// FIXME: include `Vec` docs here
#[stable(feature = "rust1", since = "1.0.0")]
pub type Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A = crate::alloc::Global> = crate::vec::Vec<T, A, crate::alloc::failure_handling::DefaultFailureHandling>;
}

#[stable(feature = "rust1", since = "1.0.0")]
pub use vec as __export;
}

#[cfg(test)]
#[allow(dead_code)] // Not used in all configurations
pub(crate) mod test_helpers {
Expand Down
Loading
Loading