From fd2384b7fab14732efde99da8affd830e2334e16 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 1 May 2020 18:46:23 +0200 Subject: Nightly stuff --- safe_libc/src/boxed.rs | 150 ------------------------------------------------- 1 file changed, 150 deletions(-) delete mode 100644 safe_libc/src/boxed.rs (limited to 'safe_libc/src/boxed.rs') diff --git a/safe_libc/src/boxed.rs b/safe_libc/src/boxed.rs deleted file mode 100644 index 4a38b70..0000000 --- a/safe_libc/src/boxed.rs +++ /dev/null @@ -1,150 +0,0 @@ -use crate::util; - -use core::{fmt, mem, ptr}; -use core::ops::{Deref, DerefMut}; - -fn alloc(len: usize) -> *mut T { - if util::zst::(len) { - return ptr::null_mut(); - } - let size = len.checked_mul(mem::size_of::()).expect("allocation overflow"); - let align = mem::align_of::(); - util::must_succeed( - unsafe { - libc::memalign(align, size) - } - ).cast() -} - -#[inline] -const fn dangling() -> *mut T { - mem::align_of::() as *mut T -} - -#[inline] -fn slice_len(p: *const [T]) -> usize { - unsafe { mem::transmute::<_, [usize; 2]>(p)[1] } -} - -pub trait SafePtr { - fn safe_ptr(p: *mut Self) -> *mut Self; -} - -impl SafePtr for T { - #[inline] - fn safe_ptr(p: *mut T) -> *mut T { - if util::zst::(1) { - return dangling(); - } - - debug_assert!(!p.is_null(), "NULL ptr"); - p - } -} - -impl SafePtr for [T] { - #[inline] - fn safe_ptr(p: *mut [T]) -> *mut [T] { - let len = slice_len(p); - if util::zst::(len) { - return ptr::slice_from_raw_parts_mut(dangling(), len); - } - - debug_assert!(!p.is_null(), "NULL ptr"); - p - } -} - -pub struct CBox(*mut T); - -impl CBox { - #[inline] - pub unsafe fn from_raw_unchecked(p: *mut T) -> CBox { - CBox(p) - } - - #[inline] - pub fn into_raw(self) -> *mut T { - let p = self.0; - mem::forget(self); - p - } - - #[inline] - pub fn as_ptr(&self) -> *const T { - self.0 - } - - #[inline] - pub fn as_mut_ptr(&mut self) -> *mut T { - self.0 - } -} - -impl CBox { - #[inline] - pub fn new(value: T) -> CBox { - let p = alloc(1); - unsafe { - ptr::write(p, value); - CBox::from_raw_unchecked(p) - } - } - - #[inline] - pub unsafe fn from_raw(p: *mut T) -> CBox { - util::check_ptr(p, 1); - CBox(p) - } - - #[inline] - pub unsafe fn slice_from_raw_parts_unchecked(p: *mut T, len: usize) -> CBox<[T]> { - CBox(ptr::slice_from_raw_parts_mut(p, len)) - } - - #[inline] - pub unsafe fn slice_from_raw_parts(p: *mut T, len: usize) -> CBox<[T]> { - util::check_ptr(p, len); - CBox::slice_from_raw_parts_unchecked(p, len) - } -} - -impl Drop for CBox { - #[inline] - fn drop(&mut self) { - unsafe { - ptr::drop_in_place(self.0); - libc::free(self.0.cast()); - } - } -} - -impl Deref for CBox { - type Target = T; - - #[inline] - fn deref(&self) -> &T { - unsafe { &*T::safe_ptr(self.0) } - } -} - -impl DerefMut for CBox { - #[inline] - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *T::safe_ptr(self.0) } - } -} - -impl fmt::Debug for CBox { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&**self, f) - } -} - -impl fmt::Display for CBox { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&**self, f) - } -} -- cgit v1.2.3