Skip to content

Commit

Permalink
object-alloc: Switch from *mut to NonNull
Browse files Browse the repository at this point in the history
- Switch from *mut u8 and *mut T to NonNull<u8> and NonNull<T>
  in trait methods
- Upgrade slab-alloc, mmap-alloc, and object-alloc-test accordingly
- Closes #140
  • Loading branch information
joshlf committed Feb 16, 2018
1 parent 2b4b5d5 commit f7f1f08
Show file tree
Hide file tree
Showing 22 changed files with 524 additions and 421 deletions.
4 changes: 4 additions & 0 deletions mmap-alloc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Changed
- Upgraded to new `UntypedObjectAlloc` trait that uses `NonNull<u8>` instead
of `*mut u8`

## 0.2.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion mmap-alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ errno = "0.2"
kernel32-sys = "0.2"
# use no_std libc
libc = { version = "0.2", default-features = false }
object-alloc = "0.1.0"
object-alloc = { path = "../object-alloc" }
sysconf = "0.3.1"
winapi = "0.2"
14 changes: 7 additions & 7 deletions mmap-alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extern crate winapi;

use self::alloc::allocator::{Alloc, AllocErr, CannotReallocInPlace, Excess, Layout};
use self::object_alloc::{Exhausted, UntypedObjectAlloc};
use core::ptr;
use core::ptr::{self, NonNull};

#[cfg(any(target_os = "linux", target_os = "macos"))]
use errno::errno;
Expand Down Expand Up @@ -533,17 +533,17 @@ unsafe impl<'a> UntypedObjectAlloc for &'a MapAlloc {
}
}

unsafe fn alloc(&mut self) -> Result<*mut u8, Exhausted> {
unsafe fn alloc(&mut self) -> Result<NonNull<u8>, Exhausted> {
// TODO: There's probably a method that does this more cleanly.
match self.alloc_excess(self.layout()) {
Ok(Excess(ptr, _)) => Ok(ptr),
Ok(Excess(ptr, _)) => Ok(NonNull::new_unchecked(ptr)),
Err(AllocErr::Exhausted { .. }) => Err(Exhausted),
Err(AllocErr::Unsupported { .. }) => unreachable!(),
}
}

unsafe fn dealloc(&mut self, ptr: *mut u8) {
unmap(ptr, self.obj_size);
unsafe fn dealloc(&mut self, ptr: NonNull<u8>) {
unmap(ptr.as_ptr(), self.obj_size);
}
}

Expand Down Expand Up @@ -601,11 +601,11 @@ unsafe impl UntypedObjectAlloc for MapAlloc {
<&MapAlloc as UntypedObjectAlloc>::layout(&(&*self))
}

unsafe fn alloc(&mut self) -> Result<*mut u8, Exhausted> {
unsafe fn alloc(&mut self) -> Result<NonNull<u8>, Exhausted> {
<&MapAlloc as UntypedObjectAlloc>::alloc(&mut (&*self))
}

unsafe fn dealloc(&mut self, ptr: *mut u8) {
unsafe fn dealloc(&mut self, ptr: NonNull<u8>) {
<&MapAlloc as UntypedObjectAlloc>::dealloc(&mut (&*self), ptr);
}
}
Expand Down
4 changes: 4 additions & 0 deletions object-alloc-test/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added
- Added this changelog

### Changed
- Upgraded to new `ObjectAlloc` trait that uses `NonNull<u8>` instead
of `*mut u8`
8 changes: 6 additions & 2 deletions object-alloc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ exclude = ["appveyor.sh", "travis.sh"]

[dependencies]
interpolate_idents = "0.2.2"
kernel32-sys = "0.2"
lazy_static = "1.0.0"
libc = "0.2"
object-alloc = "0.1.0"
object-alloc = { path = "../object-alloc" }
quickcheck = "0.4"
rand = "0.3"
twox-hash = "1.1"
winapi = "0.2"

[dependencies.winapi]
version = "0.3"
features = ["basetsd", "psapi"]
Loading

0 comments on commit f7f1f08

Please sign in to comment.