Skip to content

Commit

Permalink
Associate an allocator to boxes
Browse files Browse the repository at this point in the history
This turns `Box<T>` into `Box<T, A = Global>`, with a `A: Alloc` bound
for impls.

Ideally, inherent methods like `Box::new` would be applied to
`Box<T, A: Alloc + Default>`, but as of now, that would be backwards
incompatible because it would require type annotations in places where
they currently aren't required.

`impl FromIterator` is not covered because it relies on `Vec`, which
would need allocator awareness.

`DispatchFromDyn` is left out or being generic over `A` because there
is no bound that would make it work currently. `FnBox` is left out
because it's related to `DispatchFromDyn`.
  • Loading branch information
glandium committed Jun 11, 2019
1 parent 98ebb0c commit 3cbdf18
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 119 deletions.
24 changes: 13 additions & 11 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,7 @@ 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")]
unsafe impl Alloc for Global {
#[inline]
Expand Down Expand Up @@ -201,25 +206,22 @@ 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);
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
pub(crate) unsafe fn box_free<T: ?Sized, A: Alloc>(ptr: Unique<T>, mut a: 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, A> 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

0 comments on commit 3cbdf18

Please sign in to comment.