Skip to content

Commit fd54259

Browse files
committed
Auto merge of #77187 - TimDiekmann:box-alloc, r=Amanieu
Support custom allocators in `Box` r? `@Amanieu` This pull request requires a crater run. ### Prior work: - #71873 - #58457 - [`alloc-wg`](https://github.com/TimDiekmann/alloc-wg)-crate Currently blocked on: - ~#77118~ - ~rust-lang/chalk#615 (#77515)~
2 parents 0da6d42 + 06e4497 commit fd54259

30 files changed

+577
-215
lines changed

library/alloc/src/alloc.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
33
#![stable(feature = "alloc_module", since = "1.28.0")]
44

5-
use core::intrinsics::{self, min_align_of_val, size_of_val};
6-
use core::ptr::{self, NonNull, Unique};
5+
#[cfg(not(test))]
6+
use core::intrinsics;
7+
use core::intrinsics::{min_align_of_val, size_of_val};
8+
9+
use core::ptr::Unique;
10+
#[cfg(not(test))]
11+
use core::ptr::{self, NonNull};
712

813
#[stable(feature = "alloc_module", since = "1.28.0")]
914
#[doc(inline)]
@@ -39,8 +44,12 @@ extern "Rust" {
3944
/// accessed through the [free functions in `alloc`](self#functions).
4045
#[unstable(feature = "allocator_api", issue = "32838")]
4146
#[derive(Copy, Clone, Default, Debug)]
47+
#[cfg(not(test))]
4248
pub struct Global;
4349

50+
#[cfg(test)]
51+
pub use std::alloc::Global;
52+
4453
/// Allocate memory with the global allocator.
4554
///
4655
/// This function forwards calls to the [`GlobalAlloc::alloc`] method
@@ -144,6 +153,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
144153
unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
145154
}
146155

156+
#[cfg(not(test))]
147157
impl Global {
148158
#[inline]
149159
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
@@ -207,6 +217,7 @@ impl Global {
207217
}
208218

209219
#[unstable(feature = "allocator_api", issue = "32838")]
220+
#[cfg(not(test))]
210221
unsafe impl AllocRef for Global {
211222
#[inline]
212223
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
@@ -313,12 +324,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
313324
// well.
314325
// For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`,
315326
// this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well.
316-
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
327+
pub(crate) unsafe fn box_free<T: ?Sized, A: AllocRef>(ptr: Unique<T>, alloc: A) {
317328
unsafe {
318329
let size = size_of_val(ptr.as_ref());
319330
let align = min_align_of_val(ptr.as_ref());
320331
let layout = Layout::from_size_align_unchecked(size, align);
321-
Global.dealloc(ptr.cast().into(), layout)
332+
alloc.dealloc(ptr.cast().into(), layout)
322333
}
323334
}
324335

0 commit comments

Comments
 (0)