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

Associate an allocator to boxes #58418

Closed
wants to merge 1 commit 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
5 changes: 3 additions & 2 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![stable(feature = "alloc_module", since = "1.28.0")]

use core::intrinsics::{min_align_of_val, size_of_val};
use core::marker::PhantomData;
use core::ptr::{NonNull, Unique};
use core::usize;

Expand Down Expand Up @@ -190,14 +191,14 @@ 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 + Default>(ptr: Unique<T>, _a: PhantomData<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::default().dealloc(NonNull::new_unchecked(ptr).cast(), layout);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use core::fmt;
use core::future::Future;
use core::hash::{Hash, Hasher};
use core::iter::{Iterator, FromIterator, FusedIterator};
use core::marker::{Unpin, Unsize};
use core::marker::{PhantomData, Unpin, Unsize};
use core::mem;
use core::pin::Pin;
use core::ops::{
Expand All @@ -73,6 +73,7 @@ use core::ops::{
use core::ptr::{self, NonNull, Unique};
use core::task::{LocalWaker, Poll};

use crate::alloc::{Alloc, Global};
use crate::vec::Vec;
use crate::raw_vec::RawVec;
use crate::str::from_boxed_utf8_unchecked;
Expand All @@ -83,7 +84,7 @@ use crate::str::from_boxed_utf8_unchecked;
#[lang = "owned_box"]
#[fundamental]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Box<T: ?Sized>(Unique<T>);
pub struct Box<T: ?Sized, A: Alloc + Default = Global>(Unique<T>, PhantomData<A>);

impl<T> Box<T> {
/// Allocates memory on the heap and then places `x` into it.
Expand Down Expand Up @@ -136,7 +137,7 @@ impl<T: ?Sized> Box<T> {
#[stable(feature = "box_raw", since = "1.4.0")]
#[inline]
pub unsafe fn from_raw(raw: *mut T) -> Self {
Box(Unique::new_unchecked(raw))
Box(Unique::new_unchecked(raw), PhantomData)
}

/// Consumes the `Box`, returning a wrapped raw pointer.
Expand Down Expand Up @@ -273,7 +274,7 @@ impl<T: ?Sized> Box<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl<#[may_dangle] T: ?Sized> Drop for Box<T> {
unsafe impl<#[may_dangle] T: ?Sized, A: Alloc + Default> Drop for Box<T, A> {
fn drop(&mut self) {
// FIXME: Do nothing, drop is currently performed by compiler.
}
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ impl<T: ?Sized> Rc<T> {
value_size);

// Free the allocation without dropping its contents
box_free(box_unique);
box_free::<_, Global>(box_unique, PhantomData);

Rc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
}
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl<T: ?Sized> Arc<T> {
value_size);

// Free the allocation without dropping its contents
box_free(box_unique);
box_free::<_, Global>(box_unique, PhantomData);

Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
}
Expand Down