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 #52420

Closed
wants to merge 8 commits into from
118 changes: 118 additions & 0 deletions src/liballoc/abort_adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![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 `Alloc::oom` 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.
#[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))
}

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>()))
}
}
20 changes: 18 additions & 2 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ extern "Rust" {
/// This type implements the [`Alloc`] trait by forwarding calls
/// to the allocator registered with the `#[global_allocator]` attribute
/// if there is one, or the `std` crate’s default.
#[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 @@ -116,6 +120,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 @@ -165,14 +176,19 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {

#[cfg_attr(not(test), lang = "box_free")]
#[inline]
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
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 ptr = ptr.as_ptr();
let size = size_of_val(&*ptr);
let align = min_align_of_val(&*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::new_unchecked(ptr).cast(), layout);
}
}

Expand Down
Loading