Skip to content

object-alloc: Switch from *mut to NonNull #148

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

Merged
merged 1 commit into from
Feb 19, 2018
Merged
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
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"
16 changes: 8 additions & 8 deletions mmap-alloc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 the authors. See the 'Copyright and license' section of the
// Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
// README.md file at the top-level directory of this repository.
//
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand Down 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
6 changes: 5 additions & 1 deletion object-alloc-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Copyright 2017 the authors. See the 'Copyright and license' section of the
<!-- Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
README.md file at the top-level directory of this repository.

Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
Expand All @@ -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