about summary refs log tree commit diff
path: root/src/liballoc/heap.rs
diff options
context:
space:
mode:
authorMike Hommey <mh@glandium.org>2018-04-03 08:51:02 +0900
committerSimon Sapin <simon.sapin@exyr.org>2018-04-12 22:53:22 +0200
commitfddf51ee0b9765484fc316dbf3d4feb8ceea715d (patch)
tree52814590ab7288801f78e8ee5493e156f3181017 /src/liballoc/heap.rs
parentfd242ee64c5488e64e2bb677d90f2460e017b7cb (diff)
downloadrust-fddf51ee0b9765484fc316dbf3d4feb8ceea715d.tar.gz
rust-fddf51ee0b9765484fc316dbf3d4feb8ceea715d.zip
Use NonNull<Void> instead of *mut u8 in the Alloc trait
Fixes #49608
Diffstat (limited to 'src/liballoc/heap.rs')
-rw-r--r--src/liballoc/heap.rs22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs
index e79383331e1..cfb6504e743 100644
--- a/src/liballoc/heap.rs
+++ b/src/liballoc/heap.rs
@@ -8,14 +8,20 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-pub use alloc::{Excess, Layout, AllocErr, CannotReallocInPlace};
+#![allow(deprecated)]
+
+pub use alloc::{Layout, AllocErr, CannotReallocInPlace, Void};
 use core::alloc::Alloc as CoreAlloc;
+use core::ptr::NonNull;
 
 #[doc(hidden)]
 pub mod __core {
     pub use core::*;
 }
 
+#[derive(Debug)]
+pub struct Excess(pub *mut u8, pub usize);
+
 /// Compatibility with older versions of #[global_allocator] during bootstrap
 pub unsafe trait Alloc {
     unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
@@ -42,13 +48,13 @@ pub unsafe trait Alloc {
                               new_layout: Layout) -> Result<(), CannotReallocInPlace>;
 }
 
-#[allow(deprecated)]
 unsafe impl<T> Alloc for T where T: CoreAlloc {
     unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
-        CoreAlloc::alloc(self, layout)
+        CoreAlloc::alloc(self, layout).map(|ptr| ptr.cast().as_ptr())
     }
 
     unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
+        let ptr = NonNull::new_unchecked(ptr as *mut Void);
         CoreAlloc::dealloc(self, ptr, layout)
     }
 
@@ -64,28 +70,33 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
                       ptr: *mut u8,
                       layout: Layout,
                       new_layout: Layout) -> Result<*mut u8, AllocErr> {
-        CoreAlloc::realloc(self, ptr, layout, new_layout.size())
+        let ptr = NonNull::new_unchecked(ptr as *mut Void);
+        CoreAlloc::realloc(self, ptr, layout, new_layout.size()).map(|ptr| ptr.cast().as_ptr())
     }
 
     unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
-        CoreAlloc::alloc_zeroed(self, layout)
+        CoreAlloc::alloc_zeroed(self, layout).map(|ptr| ptr.cast().as_ptr())
     }
 
     unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
         CoreAlloc::alloc_excess(self, layout)
+            .map(|e| Excess(e.0 .cast().as_ptr(), e.1))
     }
 
     unsafe fn realloc_excess(&mut self,
                              ptr: *mut u8,
                              layout: Layout,
                              new_layout: Layout) -> Result<Excess, AllocErr> {
+        let ptr = NonNull::new_unchecked(ptr as *mut Void);
         CoreAlloc::realloc_excess(self, ptr, layout, new_layout.size())
+            .map(|e| Excess(e.0 .cast().as_ptr(), e.1))
     }
 
     unsafe fn grow_in_place(&mut self,
                             ptr: *mut u8,
                             layout: Layout,
                             new_layout: Layout) -> Result<(), CannotReallocInPlace> {
+        let ptr = NonNull::new_unchecked(ptr as *mut Void);
         CoreAlloc::grow_in_place(self, ptr, layout, new_layout.size())
     }
 
@@ -93,6 +104,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
                               ptr: *mut u8,
                               layout: Layout,
                               new_layout: Layout) -> Result<(), CannotReallocInPlace> {
+        let ptr = NonNull::new_unchecked(ptr as *mut Void);
         CoreAlloc::shrink_in_place(self, ptr, layout, new_layout.size())
     }
 }