diff --git a/src/global.rs b/src/global.rs index 556286b..5882015 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,5 +1,6 @@ use alloc::heap::{Alloc, Layout, Excess, CannotReallocInPlace, AllocErr}; use core::ops::{Deref, DerefMut}; +use core::ptr::NonNull; use Dlmalloc; @@ -7,27 +8,27 @@ pub struct GlobalDlmalloc; unsafe impl Alloc for GlobalDlmalloc { #[inline] - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { + unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { (&*self).alloc(layout) } #[inline] unsafe fn alloc_zeroed(&mut self, layout: Layout) - -> Result<*mut u8, AllocErr> + -> Result, AllocErr> { (&*self).alloc_zeroed(layout) } #[inline] - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { + unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { (&*self).dealloc(ptr, layout) } #[inline] unsafe fn realloc(&mut self, - ptr: *mut u8, + ptr: NonNull, old_layout: Layout, - new_layout: Layout) -> Result<*mut u8, AllocErr> { + new_layout: Layout) -> Result, AllocErr> { (&*self).realloc(ptr, old_layout, new_layout) } @@ -47,7 +48,7 @@ unsafe impl Alloc for GlobalDlmalloc { #[inline] unsafe fn realloc_excess(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result { (&*self).realloc_excess(ptr, layout, new_layout) @@ -55,7 +56,7 @@ unsafe impl Alloc for GlobalDlmalloc { #[inline] unsafe fn grow_in_place(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> { (&*self).grow_in_place(ptr, layout, new_layout) @@ -63,7 +64,7 @@ unsafe impl Alloc for GlobalDlmalloc { #[inline] unsafe fn shrink_in_place(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> { (&*self).shrink_in_place(ptr, layout, new_layout) @@ -99,24 +100,24 @@ impl Drop for Instance { } unsafe impl<'a> Alloc for &'a GlobalDlmalloc { - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { + unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { get().alloc(layout) } unsafe fn alloc_zeroed(&mut self, layout: Layout) - -> Result<*mut u8, AllocErr> + -> Result, AllocErr> { get().alloc_zeroed(layout) } - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { + unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { get().dealloc(ptr, layout) } unsafe fn realloc(&mut self, - ptr: *mut u8, + ptr: NonNull, old_layout: Layout, - new_layout: Layout) -> Result<*mut u8, AllocErr> { + new_layout: Layout) -> Result, AllocErr> { get().realloc(ptr, old_layout, new_layout) } @@ -135,7 +136,7 @@ unsafe impl<'a> Alloc for &'a GlobalDlmalloc { #[inline] unsafe fn realloc_excess(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result { get().realloc_excess(ptr, layout, new_layout) @@ -143,7 +144,7 @@ unsafe impl<'a> Alloc for &'a GlobalDlmalloc { #[inline] unsafe fn grow_in_place(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> { get().grow_in_place(ptr, layout, new_layout) @@ -151,7 +152,7 @@ unsafe impl<'a> Alloc for &'a GlobalDlmalloc { #[inline] unsafe fn shrink_in_place(&mut self, - ptr: *mut u8, + ptr: NonNull, layout: Layout, new_layout: Layout) -> Result<(), CannotReallocInPlace> { get().shrink_in_place(ptr, layout, new_layout) diff --git a/src/lib.rs b/src/lib.rs index c130d9e..96d0bc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ extern crate alloc; use alloc::heap::{Alloc, Layout, AllocErr}; use core::cmp; -use core::ptr; +use core::ptr::{self, NonNull}; pub use self::global::GlobalDlmalloc; @@ -35,42 +35,38 @@ impl Dlmalloc { unsafe impl Alloc for Dlmalloc { #[inline] - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { + unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { let ptr = if layout.align() <= self.0.malloc_alignment() { self.0.malloc(layout.size()) } else { self.0.memalign(layout.align(), layout.size()) }; - if ptr.is_null() { - Err(AllocErr::Exhausted { request: layout }) - } else { - Ok(ptr) - } + NonNull::new(ptr).ok_or_else(|| AllocErr::Exhausted { request: layout }) } #[inline] unsafe fn alloc_zeroed(&mut self, layout: Layout) - -> Result<*mut u8, AllocErr> + -> Result, AllocErr> { let size = layout.size(); let ptr = self.alloc(layout)?; - if self.0.calloc_must_clear(ptr) { - ptr::write_bytes(ptr, 0, size); + if self.0.calloc_must_clear(ptr.as_ptr()) { + ptr::write_bytes(ptr.as_ptr(), 0, size); } Ok(ptr) } #[inline] - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { + unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { drop(layout); - self.0.free(ptr) + self.0.free(ptr.as_ptr()) } #[inline] unsafe fn realloc(&mut self, - ptr: *mut u8, + ptr: NonNull, old_layout: Layout, - new_layout: Layout) -> Result<*mut u8, AllocErr> { + new_layout: Layout) -> Result, AllocErr> { if old_layout.align() != new_layout.align() { return Err(AllocErr::Unsupported { details: "cannot change alignment on `realloc`", @@ -78,17 +74,13 @@ unsafe impl Alloc for Dlmalloc { } if new_layout.align() <= self.0.malloc_alignment() { - let ptr = self.0.realloc(ptr, new_layout.size()); - if !ptr.is_null() { - Ok(ptr as *mut u8) - } else { - Err(AllocErr::Exhausted { request: new_layout }) - } + let ptr = self.0.realloc(ptr.as_ptr(), new_layout.size()); + NonNull::new(ptr).ok_or_else(|| AllocErr::Exhausted { request: new_layout }) } else { let res = self.alloc(new_layout.clone()); if let Ok(new_ptr) = res { let size = cmp::min(old_layout.size(), new_layout.size()); - ptr::copy_nonoverlapping(ptr, new_ptr, size); + ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr(), size); self.dealloc(ptr, old_layout); } res @@ -111,7 +103,7 @@ unsafe impl Alloc for Dlmalloc { // // #[inline] // unsafe fn realloc_excess(&mut self, - // ptr: *mut u8, + // ptr: NonNull, // layout: Layout, // new_layout: Layout) -> Result { // (&*self).realloc_excess(ptr, layout, new_layout) @@ -119,7 +111,7 @@ unsafe impl Alloc for Dlmalloc { // // #[inline] // unsafe fn grow_in_place(&mut self, - // ptr: *mut u8, + // ptr: NonNull, // layout: Layout, // new_layout: Layout) -> Result<(), CannotReallocInPlace> { // (&*self).grow_in_place(ptr, layout, new_layout) @@ -127,7 +119,7 @@ unsafe impl Alloc for Dlmalloc { // // #[inline] // unsafe fn shrink_in_place(&mut self, - // ptr: *mut u8, + // ptr: NonNull, // layout: Layout, // new_layout: Layout) -> Result<(), CannotReallocInPlace> { // (&*self).shrink_in_place(ptr, layout, new_layout) @@ -136,27 +128,27 @@ unsafe impl Alloc for Dlmalloc { // unsafe impl<'a> Alloc for &'a Dlmalloc { // #[inline] -// unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { +// unsafe fn alloc(&mut self, layout: Layout) -> Result, AllocErr> { // panic!() // } // // // #[inline] // // unsafe fn alloc_zeroed(&mut self, layout: Layout) -// // -> Result<*mut u8, AllocErr> +// // -> Result, AllocErr> // // { // // panic!() // // } // // #[inline] -// unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { +// unsafe fn dealloc(&mut self, ptr: NonNull, layout: Layout) { // panic!() // } // // // #[inline] // // unsafe fn realloc(&mut self, -// // ptr: *mut u8, +// // ptr: NonNull, // // old_layout: Layout, -// // new_layout: Layout) -> Result<*mut u8, AllocErr> { +// // new_layout: Layout) -> Result, AllocErr> { // // panic!() // // } //