|
2 | 2 |
|
3 | 3 | #![stable(feature = "alloc_module", since = "1.28.0")] |
4 | 4 |
|
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}; |
7 | 12 |
|
8 | 13 | #[stable(feature = "alloc_module", since = "1.28.0")] |
9 | 14 | #[doc(inline)] |
@@ -39,8 +44,12 @@ extern "Rust" { |
39 | 44 | /// accessed through the [free functions in `alloc`](self#functions). |
40 | 45 | #[unstable(feature = "allocator_api", issue = "32838")] |
41 | 46 | #[derive(Copy, Clone, Default, Debug)] |
| 47 | +#[cfg(not(test))] |
42 | 48 | pub struct Global; |
43 | 49 |
|
| 50 | +#[cfg(test)] |
| 51 | +pub use std::alloc::Global; |
| 52 | + |
44 | 53 | /// Allocate memory with the global allocator. |
45 | 54 | /// |
46 | 55 | /// This function forwards calls to the [`GlobalAlloc::alloc`] method |
@@ -144,6 +153,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 { |
144 | 153 | unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) } |
145 | 154 | } |
146 | 155 |
|
| 156 | +#[cfg(not(test))] |
147 | 157 | impl Global { |
148 | 158 | #[inline] |
149 | 159 | fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> { |
@@ -207,6 +217,7 @@ impl Global { |
207 | 217 | } |
208 | 218 |
|
209 | 219 | #[unstable(feature = "allocator_api", issue = "32838")] |
| 220 | +#[cfg(not(test))] |
210 | 221 | unsafe impl AllocRef for Global { |
211 | 222 | #[inline] |
212 | 223 | fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { |
@@ -313,12 +324,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 { |
313 | 324 | // well. |
314 | 325 | // For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`, |
315 | 326 | // 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) { |
317 | 328 | unsafe { |
318 | 329 | let size = size_of_val(ptr.as_ref()); |
319 | 330 | let align = min_align_of_val(ptr.as_ref()); |
320 | 331 | let layout = Layout::from_size_align_unchecked(size, align); |
321 | | - Global.dealloc(ptr.cast().into(), layout) |
| 332 | + alloc.dealloc(ptr.cast().into(), layout) |
322 | 333 | } |
323 | 334 | } |
324 | 335 |
|
|
0 commit comments