diff options
| author | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-10-06 16:37:23 +0200 |
|---|---|---|
| committer | Tim Diekmann <tim.diekmann@3dvision.de> | 2020-10-07 03:07:02 +0200 |
| commit | f288cd2e179f600fa00c2a407206a12f6c5a91e0 (patch) | |
| tree | 17d8a9240d44b2e34d96e63c4368778748cee14b /library/alloc/src/alloc.rs | |
| parent | 59dafb876e125c49fca93820c5ef22da3fcb8644 (diff) | |
| download | rust-f288cd2e179f600fa00c2a407206a12f6c5a91e0.tar.gz rust-f288cd2e179f600fa00c2a407206a12f6c5a91e0.zip | |
Support custom allocators in `Box`
Remove `Box::leak_with_alloc` Add leak-test for box with allocator Rename `AllocErr` to `AllocError` in leak-test Add `Box::alloc` and adjust examples to use the new API
Diffstat (limited to 'library/alloc/src/alloc.rs')
| -rw-r--r-- | library/alloc/src/alloc.rs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index ce70de6ebdd..4b8300f1862 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -2,8 +2,13 @@ #![stable(feature = "alloc_module", since = "1.28.0")] -use core::intrinsics::{self, min_align_of_val, size_of_val}; -use core::ptr::{self, NonNull, Unique}; +#[cfg(not(test))] +use core::intrinsics; +use core::intrinsics::{min_align_of_val, size_of_val}; + +use core::ptr::Unique; +#[cfg(not(test))] +use core::ptr::{self, NonNull}; #[stable(feature = "alloc_module", since = "1.28.0")] #[doc(inline)] @@ -40,8 +45,12 @@ extern "Rust" { /// accessed through the [free functions in `alloc`](index.html#functions). #[unstable(feature = "allocator_api", issue = "32838")] #[derive(Copy, Clone, Default, Debug)] +#[cfg(not(test))] 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 @@ -145,6 +154,7 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 { unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) } } +#[cfg(not(test))] impl Global { #[inline] fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> { @@ -208,6 +218,7 @@ impl Global { } #[unstable(feature = "allocator_api", issue = "32838")] +#[cfg(not(test))] unsafe impl AllocRef for Global { #[inline] fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { @@ -314,12 +325,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 { // well. // For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`, // this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well. -pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) { +pub(crate) unsafe fn box_free<T: ?Sized, A: AllocRef>(ptr: Unique<T>, alloc: A) { unsafe { let size = size_of_val(ptr.as_ref()); let align = min_align_of_val(ptr.as_ref()); let layout = Layout::from_size_align_unchecked(size, align); - Global.dealloc(ptr.cast().into(), layout) + alloc.dealloc(ptr.cast().into(), layout) } } |
