summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2019-08-09 22:41:26 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2019-08-09 22:41:26 +0200
commit9aae18d76454b01f07b177efafcc9867dfa7dff1 (patch)
tree9fdd6dfbb7825a23393eba197394abc8c1d9d24e
parent709f755da61a02cbdb9586c677a0fa67c25536d0 (diff)
downloadneco-9aae18d76454b01f07b177efafcc9867dfa7dff1.tar
neco-9aae18d76454b01f07b177efafcc9867dfa7dff1.zip
fixes
-rw-r--r--src/c/string.rs46
1 files changed, 23 insertions, 23 deletions
diff --git a/src/c/string.rs b/src/c/string.rs
index d0e620c..eef6371 100644
--- a/src/c/string.rs
+++ b/src/c/string.rs
@@ -2,8 +2,6 @@ use super::posix;
use core::{mem, ptr, slice};
use core::ops::{Deref, DerefMut};
-const SENTINEL: *mut libc::c_void = 1 as *mut libc::c_void;
-
fn must_succeed<T>(p: *mut T) -> *mut T {
if p.is_null() {
panic!("allocation failure");
@@ -13,17 +11,28 @@ fn must_succeed<T>(p: *mut T) -> *mut T {
fn malloc<T>() -> *mut T {
let size = mem::size_of::<T>();
+ let align = mem::align_of::<T>();
if size == 0 {
- return SENTINEL as *mut T;
+ return align as *mut T;
}
must_succeed(
- unsafe {libc::memalign(mem::align_of::<T>() as libc::size_t, size as libc::size_t) }
- ) as *mut T
+ unsafe {
+ libc::memalign(align as libc::size_t, size as libc::size_t) as *mut T
+ }
+ )
}
-pub struct CBox<T: ?Sized>(*mut T);
+pub struct CBox<T>(*mut T);
+
+impl<T> CBox<T> {
+ pub fn new(value: T) -> CBox<T> {
+ let p = malloc();
+ unsafe {
+ ptr::write(p, value);
+ CBox::from_raw(p)
+ }
+ }
-impl<T: ?Sized> CBox<T> {
pub const unsafe fn from_raw(p: *mut T) -> CBox<T> {
CBox(p)
}
@@ -43,27 +52,18 @@ impl<T: ?Sized> CBox<T> {
}
}
-impl<T> CBox<T> {
- pub fn new(value: T) -> CBox<T> {
- let p = malloc();
- unsafe {
- ptr::write(p, value);
- CBox::from_raw(p)
- }
- }
-}
-
-impl<T: ?Sized> Drop for CBox<T> {
+impl<T> Drop for CBox<T> {
fn drop(&mut self) {
unsafe { ptr::drop_in_place(self.0); }
- let p = self.0 as *mut libc::c_void;
- if p != SENTINEL {
- unsafe { libc::free(p); }
+ if mem::size_of::<T>() != 0 {
+ unsafe {
+ libc::free(self.0 as *mut libc::c_void);
+ }
}
}
}
-impl<T: ?Sized> Deref for CBox<T> {
+impl<T> Deref for CBox<T> {
type Target = T;
fn deref(&self) -> &T {
@@ -71,7 +71,7 @@ impl<T: ?Sized> Deref for CBox<T> {
}
}
-impl<T: ?Sized> DerefMut for CBox<T> {
+impl<T> DerefMut for CBox<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.0 }
}