diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2018-04-04 19:15:22 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2018-04-12 22:53:22 +0200 |
| commit | fd242ee64c5488e64e2bb677d90f2460e017b7cb (patch) | |
| tree | 81f47548b1a0b4e1afafdc7a9a43245e3f30b096 /src/liballoc | |
| parent | eae0d468932660ca383e35bb9d8b0cb4943a82ae (diff) | |
| download | rust-fd242ee64c5488e64e2bb677d90f2460e017b7cb.tar.gz rust-fd242ee64c5488e64e2bb677d90f2460e017b7cb.zip | |
impl GlobalAlloc for Global
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/alloc.rs | 85 |
1 files changed, 50 insertions, 35 deletions
diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index beae52726a6..063f0543ec4 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -73,62 +73,42 @@ pub type Heap = Global; #[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 @@ unsafe impl Alloc for Global { } } +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); } } |
