Skip to content

Replaced *mut T by Option<NonNull<T>> in box.rs #1

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 2 commits into from
Jun 24, 2019
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## v0.1.4 (24-06-2019)
- `BoxHelper`: replaced nullable pointer with NonNull

## v0.1.3 (31-05-2019)
- fixed zero-sized box allocations
- fixed file permissions in the package
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "copyless"
description = "Ways to eliminate memcpy calls when using the standard library."
version = "0.1.3"
version = "0.1.4"
authors = ["Dzmitry Malyshau <[email protected]>"]
license = "MPL-2.0"
edition = "2018"
Expand Down
40 changes: 26 additions & 14 deletions src/boxed.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use std::{alloc, mem, ptr};
use std::{
alloc, mem,
ptr::{self, NonNull},
};

/// A typesafe helper that stores the allocated pointer without the data initialized.
pub struct BoxAllocation<T>(*mut T);
pub struct BoxAllocation<T>(
// ptr cannot be null since it would mean the allocation failed.
// Note: covariance is acceptable since this eventually becomes a `Box<T>`,
// which is covariant too.
NonNull<T>,
);

impl<T> BoxAllocation<T> {
/// Consumes self and writes the given value into the allocation.
pub fn init(mut self, value: T) -> Box<T> {
#[inline(always)] // if this does not get inlined then copying happens
pub fn init(self, value: T) -> Box<T> {
if mem::size_of::<T>() == 0 {
return Box::new(value);
}

let ptr = mem::replace(&mut self.0, ptr::null_mut());
unsafe {
let ptr = self.0.as_ptr();
mem::forget(self);
ptr::write(ptr, value);
Box::from_raw(ptr)
}
Expand All @@ -20,16 +30,17 @@ impl<T> BoxAllocation<T> {

impl<T> Drop for BoxAllocation<T> {
fn drop(&mut self) {
if !self.0.is_null() {
let layout = alloc::Layout::new::<T>();
unsafe {
alloc::dealloc(self.0 as *mut u8, layout);
}
if mem::size_of::<T>() == 0 {
return;
}

let layout = alloc::Layout::new::<T>();
unsafe {
alloc::dealloc(self.0.as_ptr() as *mut u8, layout);
}
}
}


/// Helper trait for a `Box` type that allocates up-front.
pub trait BoxHelper<T> {
/// Allocates the storage without providing any data.
Expand All @@ -39,12 +50,13 @@ pub trait BoxHelper<T> {
impl<T> BoxHelper<T> for Box<T> {
fn alloc() -> BoxAllocation<T> {
if mem::size_of::<T>() == 0 {
return BoxAllocation(ptr::null_mut());
return BoxAllocation(NonNull::dangling());
}

let layout = alloc::Layout::new::<T>();
BoxAllocation(unsafe {
alloc::alloc(layout) as *mut T
})
BoxAllocation(
NonNull::new(unsafe { alloc::alloc(layout) as *mut T })
.unwrap_or_else(|| alloc::handle_alloc_error(layout)), // oom
)
}
}