Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NonNull in the allocator API #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/global.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
use alloc::heap::{Alloc, Layout, Excess, CannotReallocInPlace, AllocErr};
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;

use Dlmalloc;

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<NonNull<u8>, AllocErr> {
(&*self).alloc(layout)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout)
-> Result<*mut u8, AllocErr>
-> Result<NonNull<u8>, AllocErr>
{
(&*self).alloc_zeroed(layout)
}

#[inline]
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
(&*self).dealloc(ptr, layout)
}

#[inline]
unsafe fn realloc(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout) -> Result<*mut u8, AllocErr> {
new_layout: Layout) -> Result<NonNull<u8>, AllocErr> {
(&*self).realloc(ptr, old_layout, new_layout)
}

Expand All @@ -47,23 +48,23 @@ unsafe impl Alloc for GlobalDlmalloc {

#[inline]
unsafe fn realloc_excess(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<Excess, AllocErr> {
(&*self).realloc_excess(ptr, layout, new_layout)
}

#[inline]
unsafe fn grow_in_place(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
(&*self).grow_in_place(ptr, layout, new_layout)
}

#[inline]
unsafe fn shrink_in_place(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
(&*self).shrink_in_place(ptr, layout, new_layout)
Expand Down Expand Up @@ -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<NonNull<u8>, AllocErr> {
get().alloc(layout)
}

unsafe fn alloc_zeroed(&mut self, layout: Layout)
-> Result<*mut u8, AllocErr>
-> Result<NonNull<u8>, AllocErr>
{
get().alloc_zeroed(layout)
}

unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
get().dealloc(ptr, layout)
}

unsafe fn realloc(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout) -> Result<*mut u8, AllocErr> {
new_layout: Layout) -> Result<NonNull<u8>, AllocErr> {
get().realloc(ptr, old_layout, new_layout)
}

Expand All @@ -135,23 +136,23 @@ unsafe impl<'a> Alloc for &'a GlobalDlmalloc {

#[inline]
unsafe fn realloc_excess(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<Excess, AllocErr> {
get().realloc_excess(ptr, layout, new_layout)
}

#[inline]
unsafe fn grow_in_place(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
get().grow_in_place(ptr, layout, new_layout)
}

#[inline]
unsafe fn shrink_in_place(&mut self,
ptr: *mut u8,
ptr: NonNull<u8>,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
get().shrink_in_place(ptr, layout, new_layout)
Expand Down
50 changes: 21 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,60 +35,52 @@ 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<NonNull<u8>, 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<NonNull<u8>, 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<u8>, 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<u8>,
old_layout: Layout,
new_layout: Layout) -> Result<*mut u8, AllocErr> {
new_layout: Layout) -> Result<NonNull<u8>, AllocErr> {
if old_layout.align() != new_layout.align() {
return Err(AllocErr::Unsupported {
details: "cannot change alignment on `realloc`",
})
}

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
Expand All @@ -111,23 +103,23 @@ unsafe impl Alloc for Dlmalloc {
//
// #[inline]
// unsafe fn realloc_excess(&mut self,
// ptr: *mut u8,
// ptr: NonNull<u8>,
// layout: Layout,
// new_layout: Layout) -> Result<Excess, AllocErr> {
// (&*self).realloc_excess(ptr, layout, new_layout)
// }
//
// #[inline]
// unsafe fn grow_in_place(&mut self,
// ptr: *mut u8,
// ptr: NonNull<u8>,
// layout: Layout,
// new_layout: Layout) -> Result<(), CannotReallocInPlace> {
// (&*self).grow_in_place(ptr, layout, new_layout)
// }
//
// #[inline]
// unsafe fn shrink_in_place(&mut self,
// ptr: *mut u8,
// ptr: NonNull<u8>,
// layout: Layout,
// new_layout: Layout) -> Result<(), CannotReallocInPlace> {
// (&*self).shrink_in_place(ptr, layout, new_layout)
Expand All @@ -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<NonNull<u8>, AllocErr> {
// panic!()
// }
//
// // #[inline]
// // unsafe fn alloc_zeroed(&mut self, layout: Layout)
// // -> Result<*mut u8, AllocErr>
// // -> Result<NonNull<u8>, AllocErr>
// // {
// // panic!()
// // }
//
// #[inline]
// unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
// unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
// panic!()
// }
//
// // #[inline]
// // unsafe fn realloc(&mut self,
// // ptr: *mut u8,
// // ptr: NonNull<u8>,
// // old_layout: Layout,
// // new_layout: Layout) -> Result<*mut u8, AllocErr> {
// // new_layout: Layout) -> Result<NonNull<u8>, AllocErr> {
// // panic!()
// // }
//
Expand Down