about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/alloc.rs58
-rw-r--r--src/libcore/ptr.rs8
2 files changed, 33 insertions, 33 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs
index 7334f986f2b..632eed96049 100644
--- a/src/libcore/alloc.rs
+++ b/src/libcore/alloc.rs
@@ -42,21 +42,17 @@ impl Void {
 }
 
 /// Convert from a return value of GlobalAlloc::alloc to that of Alloc::alloc
-impl From<*mut Void> for Result<*mut u8, AllocErr> {
+impl From<*mut Void> for Result<NonNull<Void>, AllocErr> {
     fn from(ptr: *mut Void) -> Self {
-        if !ptr.is_null() {
-            Ok(ptr as *mut u8)
-        } else {
-            Err(AllocErr)
-        }
+        NonNull::new(ptr).ok_or(AllocErr)
     }
 }
 
 /// Convert from a return value of Alloc::alloc to that of GlobalAlloc::alloc
-impl From<Result<*mut u8, AllocErr>> for *mut Void {
-    fn from(result: Result<*mut u8, AllocErr>) -> Self {
+impl From<Result<NonNull<Void>, AllocErr>> for *mut Void {
+    fn from(result: Result<NonNull<Void>, AllocErr>) -> Self {
         match result {
-            Ok(ptr) => ptr as *mut Void,
+            Ok(ptr) => ptr.as_ptr(),
             Err(_) => Void::null_mut(),
         }
     }
@@ -65,7 +61,7 @@ impl From<Result<*mut u8, AllocErr>> for *mut Void {
 /// Represents the combination of a starting address and
 /// a total capacity of the returned block.
 #[derive(Debug)]
-pub struct Excess(pub *mut u8, pub usize);
+pub struct Excess(pub NonNull<Void>, pub usize);
 
 fn size_align<T>() -> (usize, usize) {
     (mem::size_of::<T>(), mem::align_of::<T>())
@@ -575,7 +571,7 @@ pub unsafe trait Alloc {
     /// Clients wishing to abort computation in response to an
     /// allocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
-    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
+    unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr>;
 
     /// Deallocate the memory referenced by `ptr`.
     ///
@@ -592,7 +588,7 @@ pub unsafe trait Alloc {
     /// * In addition to fitting the block of memory `layout`, the
     ///   alignment of the `layout` must match the alignment used
     ///   to allocate that block of memory.
-    unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout);
+    unsafe fn dealloc(&mut self, ptr: NonNull<Void>, layout: Layout);
 
     /// Allocator-specific method for signaling an out-of-memory
     /// condition.
@@ -710,9 +706,9 @@ pub unsafe trait Alloc {
     /// reallocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
     unsafe fn realloc(&mut self,
-                      ptr: *mut u8,
+                      ptr: NonNull<Void>,
                       layout: Layout,
-                      new_size: usize) -> Result<*mut u8, AllocErr> {
+                      new_size: usize) -> Result<NonNull<Void>, AllocErr> {
         let old_size = layout.size();
 
         if new_size >= old_size {
@@ -729,7 +725,9 @@ pub unsafe trait Alloc {
         let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
         let result = self.alloc(new_layout);
         if let Ok(new_ptr) = result {
-            ptr::copy_nonoverlapping(ptr as *const u8, new_ptr, cmp::min(old_size, new_size));
+            ptr::copy_nonoverlapping(ptr.as_ptr() as *const u8,
+                                     new_ptr.as_ptr() as *mut u8,
+                                     cmp::min(old_size, new_size));
             self.dealloc(ptr, layout);
         }
         result
@@ -751,11 +749,11 @@ pub unsafe trait Alloc {
     /// Clients wishing to abort computation in response to an
     /// allocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
-    unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+    unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
         let size = layout.size();
         let p = self.alloc(layout);
         if let Ok(p) = p {
-            ptr::write_bytes(p, 0, size);
+            ptr::write_bytes(p.as_ptr() as *mut u8, 0, size);
         }
         p
     }
@@ -800,7 +798,7 @@ pub unsafe trait Alloc {
     /// reallocation error are encouraged to call the allocator's `oom`
     /// method, rather than directly invoking `panic!` or similar.
     unsafe fn realloc_excess(&mut self,
-                             ptr: *mut u8,
+                             ptr: NonNull<Void>,
                              layout: Layout,
                              new_size: usize) -> Result<Excess, AllocErr> {
         let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
@@ -845,7 +843,7 @@ pub unsafe trait Alloc {
     /// `grow_in_place` failures without aborting, or to fall back on
     /// another reallocation method before resorting to an abort.
     unsafe fn grow_in_place(&mut self,
-                            ptr: *mut u8,
+                            ptr: NonNull<Void>,
                             layout: Layout,
                             new_size: usize) -> Result<(), CannotReallocInPlace> {
         let _ = ptr; // this default implementation doesn't care about the actual address.
@@ -900,7 +898,7 @@ pub unsafe trait Alloc {
     /// `shrink_in_place` failures without aborting, or to fall back
     /// on another reallocation method before resorting to an abort.
     unsafe fn shrink_in_place(&mut self,
-                              ptr: *mut u8,
+                              ptr: NonNull<Void>,
                               layout: Layout,
                               new_size: usize) -> Result<(), CannotReallocInPlace> {
         let _ = ptr; // this default implementation doesn't care about the actual address.
@@ -951,7 +949,7 @@ pub unsafe trait Alloc {
     {
         let k = Layout::new::<T>();
         if k.size() > 0 {
-            unsafe { self.alloc(k).map(|p| NonNull::new_unchecked(p as *mut T)) }
+            unsafe { self.alloc(k).map(|p| p.cast()) }
         } else {
             Err(AllocErr)
         }
@@ -977,10 +975,9 @@ pub unsafe trait Alloc {
     unsafe fn dealloc_one<T>(&mut self, ptr: NonNull<T>)
         where Self: Sized
     {
-        let raw_ptr = ptr.as_ptr() as *mut u8;
         let k = Layout::new::<T>();
         if k.size() > 0 {
-            self.dealloc(raw_ptr, k);
+            self.dealloc(ptr.as_void(), k);
         }
     }
 
@@ -1020,10 +1017,7 @@ pub unsafe trait Alloc {
         match Layout::array::<T>(n) {
             Ok(ref layout) if layout.size() > 0 => {
                 unsafe {
-                    self.alloc(layout.clone())
-                        .map(|p| {
-                            NonNull::new_unchecked(p as *mut T)
-                        })
+                    self.alloc(layout.clone()).map(|p| p.cast())
                 }
             }
             _ => Err(AllocErr),
@@ -1068,11 +1062,10 @@ pub unsafe trait Alloc {
                                n_new: usize) -> Result<NonNull<T>, AllocErr>
         where Self: Sized
     {
-        match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
-            (Ok(ref k_old), Ok(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
+        match (Layout::array::<T>(n_old), Layout::array::<T>(n_new)) {
+            (Ok(ref k_old), Ok(ref k_new)) if k_old.size() > 0 && k_new.size() > 0 => {
                 debug_assert!(k_old.align() == k_new.align());
-                self.realloc(ptr as *mut u8, k_old.clone(), k_new.size())
-                    .map(|p| NonNull::new_unchecked(p as *mut T))
+                self.realloc(ptr.as_void(), k_old.clone(), k_new.size()).map(NonNull::cast)
             }
             _ => {
                 Err(AllocErr)
@@ -1103,10 +1096,9 @@ pub unsafe trait Alloc {
     unsafe fn dealloc_array<T>(&mut self, ptr: NonNull<T>, n: usize) -> Result<(), AllocErr>
         where Self: Sized
     {
-        let raw_ptr = ptr.as_ptr() as *mut u8;
         match Layout::array::<T>(n) {
             Ok(ref k) if k.size() > 0 => {
-                Ok(self.dealloc(raw_ptr, k.clone()))
+                Ok(self.dealloc(ptr.as_void(), k.clone()))
             }
             _ => {
                 Err(AllocErr)
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index c1e150e9fb9..f4e668328ce 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -2750,6 +2750,14 @@ impl<T: ?Sized> NonNull<T> {
             NonNull::new_unchecked(self.as_ptr() as *mut U)
         }
     }
+
+    /// Cast to a `Void` pointer
+    #[unstable(feature = "allocator_api", issue = "32838")]
+    pub fn as_void(self) -> NonNull<::alloc::Void> {
+        unsafe {
+            NonNull::new_unchecked(self.as_ptr() as _)
+        }
+    }
 }
 
 #[stable(feature = "nonnull", since = "1.25.0")]