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