]> git.lizzy.rs Git - rust.git/commitdiff
impl GlobalAlloc for Global
authorSimon Sapin <simon.sapin@exyr.org>
Wed, 4 Apr 2018 17:15:22 +0000 (19:15 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Thu, 12 Apr 2018 20:53:22 +0000 (22:53 +0200)
src/liballoc/alloc.rs

index beae52726a6ecf1a0f51428e58aab2c52aa3dcaf..063f0543ec4a6e9b6b039511e90e9d2d7ccd684a 100644 (file)
@@ -73,62 +73,42 @@ fn __rust_realloc(ptr: *mut u8,
 #[allow(non_upper_case_globals)]
 pub const Heap: Global = Global;
 
-unsafe impl Alloc for Global {
+unsafe impl GlobalAlloc for Global {
     #[inline]
-    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+    unsafe fn alloc(&self, layout: Layout) -> *mut Void {
         #[cfg(not(stage0))]
         let ptr = __rust_alloc(layout.size(), layout.align());
         #[cfg(stage0)]
         let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0);
-
-        if !ptr.is_null() {
-            Ok(ptr)
-        } else {
-            Err(AllocErr)
-        }
+        ptr as *mut Void
     }
 
     #[inline]
-    unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
-        __rust_dealloc(ptr, layout.size(), layout.align())
+    unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout) {
+        __rust_dealloc(ptr as *mut u8, layout.size(), layout.align())
     }
 
     #[inline]
-    unsafe fn realloc(&mut self,
-                      ptr: *mut u8,
-                      layout: Layout,
-                      new_size: usize)
-                      -> Result<*mut u8, AllocErr>
-    {
+    unsafe fn realloc(&self, ptr: *mut Void, layout: Layout, new_size: usize) -> *mut Void {
         #[cfg(not(stage0))]
-        let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_size);
+        let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), new_size);
         #[cfg(stage0)]
-        let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
+        let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(),
                                  new_size, layout.align(), &mut 0);
-
-        if !ptr.is_null() {
-            Ok(ptr)
-        } else {
-            Err(AllocErr)
-        }
+        ptr as *mut Void
     }
 
     #[inline]
-    unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Void {
         #[cfg(not(stage0))]
         let ptr = __rust_alloc_zeroed(layout.size(), layout.align());
         #[cfg(stage0)]
         let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
-
-        if !ptr.is_null() {
-            Ok(ptr)
-        } else {
-            Err(AllocErr)
-        }
+        ptr as *mut Void
     }
 
     #[inline]
-    fn oom(&mut self) -> ! {
+    fn oom(&self) -> ! {
         unsafe {
             #[cfg(not(stage0))]
             __rust_oom();
@@ -138,6 +118,38 @@ fn oom(&mut self) -> ! {
     }
 }
 
+unsafe impl Alloc for Global {
+    #[inline]
+    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+        GlobalAlloc::alloc(self, layout).into()
+    }
+
+    #[inline]
+    unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
+        GlobalAlloc::dealloc(self, ptr as *mut Void, layout)
+    }
+
+    #[inline]
+    unsafe fn realloc(&mut self,
+                      ptr: *mut u8,
+                      layout: Layout,
+                      new_size: usize)
+                      -> Result<*mut u8, AllocErr>
+    {
+        GlobalAlloc::realloc(self, ptr as *mut Void, layout, new_size).into()
+    }
+
+    #[inline]
+    unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+        GlobalAlloc::alloc_zeroed(self, layout).into()
+    }
+
+    #[inline]
+    fn oom(&mut self) -> ! {
+        GlobalAlloc::oom(self)
+    }
+}
+
 /// The allocator for unique pointers.
 // This function must not unwind. If it does, MIR trans will fail.
 #[cfg(not(test))]
@@ -148,9 +160,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
         align as *mut u8
     } else {
         let layout = Layout::from_size_align_unchecked(size, align);
-        Global.alloc(layout).unwrap_or_else(|_| {
+        let ptr = Global.alloc(layout);
+        if !ptr.is_null() {
+            ptr as *mut u8
+        } else {
             Global.oom()
-        })
+        }
     }
 }
 
@@ -162,7 +177,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
     // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
     if size != 0 {
         let layout = Layout::from_size_align_unchecked(size, align);
-        Global.dealloc(ptr as *mut u8, layout);
+        Global.dealloc(ptr as *mut Void, layout);
     }
 }