Skip to content

Commit

Permalink
debloat: Debloat pointer inbounds assert
Browse files Browse the repository at this point in the history
Debloat by pushing the code down into the DataReprs, this avoids
instantiating the pointer check code for the views (where it can't do
anything anyway).
  • Loading branch information
bluss committed May 17, 2021
1 parent 2a8b58a commit 2ddc9db
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
53 changes: 53 additions & 0 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ pub unsafe trait RawData: Sized {

#[doc(hidden)]
// This method is only used for debugging
#[deprecated(note="Unused", since="0.15.2")]
fn _data_slice(&self) -> Option<&[Self::Elem]>;

#[doc(hidden)]
fn _is_pointer_inbounds(&self, ptr: *const Self::Elem) -> bool;

private_decl! {}
}

Expand Down Expand Up @@ -146,9 +150,15 @@ pub unsafe trait DataMut: Data + RawDataMut {

unsafe impl<A> RawData for RawViewRepr<*const A> {
type Elem = A;

#[inline]
fn _data_slice(&self) -> Option<&[A]> {
None
}

#[inline(always)]
fn _is_pointer_inbounds(&self, _ptr: *const Self::Elem) -> bool { true }

private_impl! {}
}

Expand All @@ -160,9 +170,15 @@ unsafe impl<A> RawDataClone for RawViewRepr<*const A> {

unsafe impl<A> RawData for RawViewRepr<*mut A> {
type Elem = A;

#[inline]
fn _data_slice(&self) -> Option<&[A]> {
None
}

#[inline(always)]
fn _is_pointer_inbounds(&self, _ptr: *const Self::Elem) -> bool { true }

private_impl! {}
}

Expand Down Expand Up @@ -192,6 +208,11 @@ unsafe impl<A> RawData for OwnedArcRepr<A> {
fn _data_slice(&self) -> Option<&[A]> {
Some(self.0.as_slice())
}

fn _is_pointer_inbounds(&self, self_ptr: *const Self::Elem) -> bool {
self.0._is_pointer_inbounds(self_ptr)
}

private_impl! {}
}

Expand Down Expand Up @@ -274,9 +295,18 @@ unsafe impl<A> RawDataClone for OwnedArcRepr<A> {

unsafe impl<A> RawData for OwnedRepr<A> {
type Elem = A;

fn _data_slice(&self) -> Option<&[A]> {
Some(self.as_slice())
}

fn _is_pointer_inbounds(&self, self_ptr: *const Self::Elem) -> bool {
let slc = self.as_slice();
let ptr = slc.as_ptr() as *mut A;
let end = unsafe { ptr.add(slc.len()) };
self_ptr >= ptr && self_ptr <= end
}

private_impl! {}
}

Expand Down Expand Up @@ -340,9 +370,15 @@ where

unsafe impl<'a, A> RawData for ViewRepr<&'a A> {
type Elem = A;

#[inline]
fn _data_slice(&self) -> Option<&[A]> {
None
}

#[inline(always)]
fn _is_pointer_inbounds(&self, _ptr: *const Self::Elem) -> bool { true }

private_impl! {}
}

Expand All @@ -364,9 +400,15 @@ unsafe impl<'a, A> RawDataClone for ViewRepr<&'a A> {

unsafe impl<'a, A> RawData for ViewRepr<&'a mut A> {
type Elem = A;

#[inline]
fn _data_slice(&self) -> Option<&[A]> {
None
}

#[inline(always)]
fn _is_pointer_inbounds(&self, _ptr: *const Self::Elem) -> bool { true }

private_impl! {}
}

Expand Down Expand Up @@ -458,12 +500,23 @@ unsafe impl<A> DataOwned for OwnedArcRepr<A> {

unsafe impl<'a, A> RawData for CowRepr<'a, A> {
type Elem = A;

fn _data_slice(&self) -> Option<&[A]> {
#[allow(deprecated)]
match self {
CowRepr::View(view) => view._data_slice(),
CowRepr::Owned(data) => data._data_slice(),
}
}

#[inline]
fn _is_pointer_inbounds(&self, ptr: *const Self::Elem) -> bool {
match self {
CowRepr::View(view) => view._is_pointer_inbounds(ptr),
CowRepr::Owned(data) => data._is_pointer_inbounds(ptr),
}
}

private_impl! {}
}

Expand Down
12 changes: 1 addition & 11 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,17 +2135,7 @@ where
}

pub(crate) fn pointer_is_inbounds(&self) -> bool {
match self.data._data_slice() {
None => {
// special case for non-owned views
true
}
Some(slc) => {
let ptr = slc.as_ptr() as *mut A;
let end = unsafe { ptr.add(slc.len()) };
self.ptr.as_ptr() >= ptr && self.ptr.as_ptr() <= end
}
}
self.data._is_pointer_inbounds(self.as_ptr())
}

/// Perform an elementwise assigment to `self` from `rhs`.
Expand Down

0 comments on commit 2ddc9db

Please sign in to comment.