From 94ce251b4b195eb4a0acf556056d8fab96ca1e60 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Fri, 16 Feb 2018 13:24:16 -0800 Subject: [PATCH] object-alloc: Switch from *mut to NonNull - Switch from *mut u8 and *mut T to NonNull and NonNull in trait methods - Upgrade slab-alloc, mmap-alloc, and object-alloc-test accordingly - While upgrading slab-alloc to use the latest mmap-alloc, remove support for huge pages (since mmap-alloc no longer supports them) - Closes #140 --- mmap-alloc/CHANGELOG.md | 4 + mmap-alloc/Cargo.toml | 2 +- mmap-alloc/src/lib.rs | 16 +-- object-alloc-test/CHANGELOG.md | 6 +- object-alloc-test/Cargo.toml | 8 +- object-alloc-test/src/corruption.rs | 159 ++++++++++++++---------- object-alloc-test/src/leaky_alloc.rs | 39 ++---- object-alloc-test/src/lib.rs | 3 +- object-alloc/CHANGELOG.md | 5 +- object-alloc/src/lib.rs | 20 +-- slab-alloc/CHANGELOG.md | 10 +- slab-alloc/Cargo.toml | 4 +- slab-alloc/src/aligned.rs | 18 +-- slab-alloc/src/backing.rs | 28 ++--- slab-alloc/src/init.rs | 63 +++++----- slab-alloc/src/large.rs | 71 ++++++----- slab-alloc/src/lib.rs | 109 ++++++++-------- slab-alloc/src/ptr_map.rs | 60 ++++----- slab-alloc/src/stack.rs | 113 ++++++++++------- slab-alloc/src/tests.rs | 72 ++++++----- slab-alloc/src/util.rs | 179 ++++++++++++++------------- slab-alloc/travis.sh | 4 +- 22 files changed, 539 insertions(+), 454 deletions(-) diff --git a/mmap-alloc/CHANGELOG.md b/mmap-alloc/CHANGELOG.md index a04b671..8559599 100644 --- a/mmap-alloc/CHANGELOG.md +++ b/mmap-alloc/CHANGELOG.md @@ -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` instead + of `*mut u8` + ## 0.2.0 ### Added diff --git a/mmap-alloc/Cargo.toml b/mmap-alloc/Cargo.toml index fc2f01a..23425d8 100644 --- a/mmap-alloc/Cargo.toml +++ b/mmap-alloc/Cargo.toml @@ -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" diff --git a/mmap-alloc/src/lib.rs b/mmap-alloc/src/lib.rs index 2bb86dc..f8e9268 100644 --- a/mmap-alloc/src/lib.rs +++ b/mmap-alloc/src/lib.rs @@ -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 @@ -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; @@ -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, 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) { + unmap(ptr.as_ptr(), self.obj_size); } } @@ -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, Exhausted> { <&MapAlloc as UntypedObjectAlloc>::alloc(&mut (&*self)) } - unsafe fn dealloc(&mut self, ptr: *mut u8) { + unsafe fn dealloc(&mut self, ptr: NonNull) { <&MapAlloc as UntypedObjectAlloc>::dealloc(&mut (&*self), ptr); } } diff --git a/object-alloc-test/CHANGELOG.md b/object-alloc-test/CHANGELOG.md index 22d4873..abff5d3 100644 --- a/object-alloc-test/CHANGELOG.md +++ b/object-alloc-test/CHANGELOG.md @@ -1,4 +1,4 @@ -