diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2017-06-03 14:54:08 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2017-07-05 14:37:01 -0700 |
| commit | 695dee063bcd40f154bb27b7beafcb3d4dd775ac (patch) | |
| tree | 8ac64f40091434e679b4221343dce7447c4f1236 /src/liballoc/boxed.rs | |
| parent | 4c225c4d1732537aff63dd97c2b7ac681fd3d188 (diff) | |
| download | rust-695dee063bcd40f154bb27b7beafcb3d4dd775ac.tar.gz rust-695dee063bcd40f154bb27b7beafcb3d4dd775ac.zip | |
rustc: Implement the #[global_allocator] attribute
This PR is an implementation of [RFC 1974] which specifies a new method of defining a global allocator for a program. This obsoletes the old `#![allocator]` attribute and also removes support for it. [RFC 1974]: https://github.com/rust-lang/rfcs/pull/197 The new `#[global_allocator]` attribute solves many issues encountered with the `#![allocator]` attribute such as composition and restrictions on the crate graph itself. The compiler now has much more control over the ABI of the allocator and how it's implemented, allowing much more freedom in terms of how this feature is implemented. cc #27389
Diffstat (limited to 'src/liballoc/boxed.rs')
| -rw-r--r-- | src/liballoc/boxed.rs | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 4a43018e973..76cf10f0d55 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -55,7 +55,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use heap; +use heap::{Heap, Layout, Alloc}; use raw_vec::RawVec; use core::any::Any; @@ -135,8 +135,7 @@ pub struct Box<T: ?Sized>(Unique<T>); #[allow(missing_debug_implementations)] pub struct IntermediateBox<T: ?Sized> { ptr: *mut u8, - size: usize, - align: usize, + layout: Layout, marker: marker::PhantomData<*mut T>, } @@ -156,23 +155,21 @@ unsafe fn finalize<T>(b: IntermediateBox<T>) -> Box<T> { } fn make_place<T>() -> IntermediateBox<T> { - let size = mem::size_of::<T>(); - let align = mem::align_of::<T>(); + let layout = Layout::new::<T>(); - let p = if size == 0 { + let p = if layout.size() == 0 { mem::align_of::<T>() as *mut u8 } else { - let p = unsafe { heap::allocate(size, align) }; - if p.is_null() { - panic!("Box make_place allocation failure."); + unsafe { + Heap.alloc(layout.clone()).unwrap_or_else(|err| { + Heap.oom(err) + }) } - p }; IntermediateBox { ptr: p, - size: size, - align: align, + layout: layout, marker: marker::PhantomData, } } @@ -221,8 +218,10 @@ impl<T> Placer<T> for ExchangeHeapSingleton { issue = "27779")] impl<T: ?Sized> Drop for IntermediateBox<T> { fn drop(&mut self) { - if self.size > 0 { - unsafe { heap::deallocate(self.ptr, self.size, self.align) } + if self.layout.size() > 0 { + unsafe { + Heap.dealloc(self.ptr, self.layout.clone()) + } } } } |
