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

WIP: Allocator- and fallibility-polymorphic collections #60703

Closed
wants to merge 9 commits into from
112 changes: 112 additions & 0 deletions src/liballoc/abort_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! An allocator adapter that blows up by calling `handle_alloc_error` on all errors.
//!
//! On one hand, concrete allocator implementations should always be written
//! without panicking on user error and OOM to give users maximum
//! flexibility. On the other hand, code that depends on allocation succeeding
//! should depend on `Alloc<Err=!>` to avoid repetitively handling errors from
//! which it cannot recover.
//!
//! This adapter bridges the gap, effectively allowing `Alloc<Err=!>` to be
//! implemented by any allocator.

#![unstable(feature = "allocator_api",
reason = "the precise API and guarantees it provides may be tweaked \
slightly, especially to possibly take into account the \
types being stored to make room for a future \
tracing garbage collector",
issue = "32838")]

use core::usize;
use core::ptr::NonNull;

use crate::alloc::*;

/// An allocator adapter that blows up by calling `handle_alloc_error` on all errors.
///
/// See the [module-level documentation](../../std/abort_adapter/index.html) for more.
#[derive(Copy, Clone, Debug, Default)]
pub struct AbortAdapter<Alloc>(pub Alloc);

impl<A: AllocHelper> AllocHelper for AbortAdapter<A> {
type Err = !;
}

unsafe impl<A: Alloc> Alloc for AbortAdapter<A> {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, Self::Err> {
self.0.alloc(layout).or_else(|_| handle_alloc_error(layout))

Choose a reason for hiding this comment

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

I feel like we should handle this in a way that includes the actual error that occured. Maybe it'd be best to bound the Err associated type on Debug or Display?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but I figure we can hash out what handle_alloc_error should take later?

Choose a reason for hiding this comment

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

handle_alloc_error is stable, so it can't be changed. But yeah, this is less important for the moment.

}

unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
self.0.dealloc(ptr, layout)
}

fn usable_size(&self, layout: &Layout) -> (usize, usize) {
self.0.usable_size(layout)
}

unsafe fn realloc(&mut self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize) -> Result<NonNull<u8>, Self::Err> {
self.0.realloc(ptr, layout, new_size).or_else(|_| {
let layout = Layout::from_size_align_unchecked(new_size, layout.align());
handle_alloc_error(layout)
})
}

unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, Self::Err> {
self.0.alloc_zeroed(layout).or_else(|_| handle_alloc_error(layout))
}

unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, Self::Err> {
self.0.alloc_excess(layout).or_else(|_| handle_alloc_error(layout))
}

unsafe fn grow_in_place(&mut self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize) -> Result<(), CannotReallocInPlace> {
self.0.grow_in_place(ptr, layout, new_size)
}

unsafe fn shrink_in_place(&mut self,
ptr: NonNull<u8>,
layout: Layout,
new_size: usize) -> Result<(), CannotReallocInPlace> {
self.0.shrink_in_place(ptr, layout, new_size)
}

fn alloc_one<T>(&mut self) -> Result<NonNull<T>, Self::Err>
where Self: Sized
{
self.0.alloc_one().or_else(|_| handle_alloc_error(Layout::new::<T>()))
}

unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
where Self: Sized
{
self.0.dealloc_one(ptr)
}

fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, Self::Err>
where Self: Sized
{
self.0.alloc_array(n).or_else(|_| handle_alloc_error(Layout::new::<T>()))
}

unsafe fn realloc_array<T>(&mut self,
ptr: NonNull<T>,
n_old: usize,
n_new: usize) -> Result<NonNull<T>, Self::Err>
where Self: Sized
{
self.0.realloc_array(ptr, n_old, n_new)
.or_else(|_| handle_alloc_error(Layout::new::<T>()))
}

unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), Self::Err>
where Self: Sized
{
self.0.dealloc_array(ptr, n).or_else(|_| handle_alloc_error(Layout::new::<T>()))
}
}
33 changes: 23 additions & 10 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ extern "Rust" {
/// accessed through the [free functions in `alloc`](index.html#functions).
///
/// [`Alloc`]: trait.Alloc.html
#[cfg(not(test))]
#[unstable(feature = "allocator_api", issue = "32838")]
#[derive(Copy, Clone, Default, Debug)]
pub struct Global;

#[cfg(test)]
pub use std::alloc::Global;

/// Allocate memory with the global allocator.
///
/// This function forwards calls to the [`GlobalAlloc::alloc`] method
Expand Down Expand Up @@ -163,6 +167,13 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
__rust_alloc_zeroed(layout.size(), layout.align())
}

#[cfg(not(test))]
#[unstable(feature = "allocator_api", issue = "32838")]
impl AllocHelper for Global {
type Err = AllocErr;
}

#[cfg(not(test))]
#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl Alloc for Global {
#[inline]
Expand Down Expand Up @@ -201,25 +212,27 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
align as *mut u8
} else {
let layout = Layout::from_size_align_unchecked(size, align);
let ptr = alloc(layout);
if !ptr.is_null() {
ptr
} else {
handle_alloc_error(layout)
match Global.alloc(layout) {
Ok(ptr) => ptr.as_ptr(),
Err(_) => handle_alloc_error(layout),
}
}
}

#[cfg_attr(not(test), lang = "box_free")]
#[inline]
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
let ptr = ptr.as_ptr();
let size = size_of_val(&*ptr);
let align = min_align_of_val(&*ptr);
pub(crate) unsafe fn box_free<T: ?Sized, A: Alloc>(ptr: Unique<T>, mut a: A) {
box_free_worker(ptr, &mut a)
}

#[inline]
pub(crate) unsafe fn box_free_worker<T: ?Sized, A: Alloc>(ptr: Unique<T>, a: &mut A) {
let size = size_of_val(&*ptr.as_ptr());
let align = min_align_of_val(&*ptr.as_ptr());
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
if size != 0 {
let layout = Layout::from_size_align_unchecked(size, align);
dealloc(ptr as *mut u8, layout);
a.dealloc(NonNull::from(ptr).cast(), layout);
}
}

Expand Down
Loading