fixes
This commit is contained in:
parent
709f755da6
commit
9aae18d764
1 changed files with 23 additions and 23 deletions
|
@ -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 }
|
||||
}
|
||||
|
|
Reference in a new issue