diff --git a/components/servo_arc/lib.rs b/components/servo_arc/lib.rs index cc378e237d3..0e77abd6e4b 100644 --- a/components/servo_arc/lib.rs +++ b/components/servo_arc/lib.rs @@ -127,6 +127,7 @@ impl Hash for NonZeroPtrMut { } } +#[repr(C)] pub struct Arc { p: NonZeroPtrMut>, } @@ -168,6 +169,7 @@ impl DerefMut for UniqueArc { unsafe impl Send for Arc {} unsafe impl Sync for Arc {} +#[repr(C)] struct ArcInner { count: atomic::AtomicUsize, data: T, @@ -186,13 +188,15 @@ impl Arc { Arc { p: NonZeroPtrMut::new(Box::into_raw(x)) } } + #[inline] pub fn into_raw(this: Self) -> *const T { let ptr = unsafe { &((*this.ptr()).data) as *const _ }; mem::forget(this); ptr } - pub unsafe fn from_raw(ptr: *const T) -> Self { + #[inline] + unsafe fn from_raw(ptr: *const T) -> Self { // To find the corresponding pointer to the `ArcInner` we need // to subtract the offset of the `data` field from the pointer. let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner, data)); @@ -203,6 +207,7 @@ impl Arc { /// Produce a pointer to the data that can be converted back /// to an arc + #[inline] pub fn borrow_arc<'a>(&'a self) -> ArcBorrow<'a, T> { ArcBorrow(&**self) } @@ -454,18 +459,21 @@ impl Hash for Arc { } impl From for Arc { + #[inline] fn from(t: T) -> Self { Arc::new(t) } } impl borrow::Borrow for Arc { + #[inline] fn borrow(&self) -> &T { &**self } } impl AsRef for Arc { + #[inline] fn as_ref(&self) -> &T { &**self } @@ -652,7 +660,7 @@ fn thin_to_thick(thin: *mut ArcInner>) impl ThinArc { /// Temporarily converts |self| into a bonafide Arc and exposes it to the /// provided callback. The refcount is not modified. - #[inline(always)] + #[inline] pub fn with_arc(&self, f: F) -> U where F: FnOnce(&Arc>) -> U { @@ -675,6 +683,7 @@ impl ThinArc { /// Returns the address on the heap of the ThinArc itself -- not the T /// within it -- for memory reporting. + #[inline] pub fn heap_ptr(&self) -> *const c_void { self.ptr as *const ArcInner as *const c_void } @@ -682,18 +691,22 @@ impl ThinArc { impl Deref for ThinArc { type Target = HeaderSliceWithLength; + + #[inline] fn deref(&self) -> &Self::Target { unsafe { &(*thin_to_thick(self.ptr)).data } } } impl Clone for ThinArc { + #[inline] fn clone(&self) -> Self { ThinArc::with_arc(self, |a| Arc::into_thin(a.clone())) } } impl Drop for ThinArc { + #[inline] fn drop(&mut self) { let _ = Arc::from_thin(ThinArc { ptr: self.ptr }); } @@ -702,6 +715,7 @@ impl Drop for ThinArc { impl Arc> { /// Converts an Arc into a ThinArc. This consumes the Arc, so the refcount /// is not modified. + #[inline] pub fn into_thin(a: Self) -> ThinArc { assert!(a.header.length == a.slice.len(), "Length needs to be correct for ThinArc to work"); @@ -715,6 +729,7 @@ impl Arc> { /// Converts a ThinArc into an Arc. This consumes the ThinArc, so the refcount /// is not modified. + #[inline] pub fn from_thin(a: ThinArc) -> Self { let ptr = thin_to_thick(a.ptr); mem::forget(a); @@ -725,6 +740,7 @@ impl Arc> { } impl PartialEq for ThinArc { + #[inline] fn eq(&self, other: &ThinArc) -> bool { ThinArc::with_arc(self, |a| { ThinArc::with_arc(other, |b| { @@ -752,6 +768,7 @@ impl Eq for ThinArc {} /// its contained data (and can be read from by both C++ and Rust), /// but we can also convert it to a "regular" Arc by removing the offset #[derive(Eq)] +#[repr(C)] pub struct RawOffsetArc { ptr: NonZeroPtrMut, } @@ -767,6 +784,7 @@ impl Deref for RawOffsetArc { } impl Clone for RawOffsetArc { + #[inline] fn clone(&self) -> Self { Arc::into_raw_offset(self.clone_arc()) } @@ -798,7 +816,7 @@ impl PartialEq for RawOffsetArc { impl RawOffsetArc { /// Temporarily converts |self| into a bonafide Arc and exposes it to the /// provided callback. The refcount is not modified. - #[inline(always)] + #[inline] pub fn with_arc(&self, f: F) -> U where F: FnOnce(&Arc) -> U { @@ -819,6 +837,7 @@ impl RawOffsetArc { /// If uniquely owned, provide a mutable reference /// Else create a copy, and mutate that + #[inline] pub fn make_mut(&mut self) -> &mut T where T: Clone { unsafe { // extract the RawOffsetArc as an owned variable @@ -836,12 +855,14 @@ impl RawOffsetArc { } /// Clone it as an Arc + #[inline] pub fn clone_arc(&self) -> Arc { RawOffsetArc::with_arc(self, |a| a.clone()) } /// Produce a pointer to the data that can be converted back /// to an arc + #[inline] pub fn borrow_arc<'a>(&'a self) -> ArcBorrow<'a, T> { ArcBorrow(&**self) } @@ -886,12 +907,14 @@ pub struct ArcBorrow<'a, T: 'a>(&'a T); impl<'a, T> Copy for ArcBorrow<'a, T> {} impl<'a, T> Clone for ArcBorrow<'a, T> { + #[inline] fn clone(&self) -> Self { *self } } impl<'a, T> ArcBorrow<'a, T> { + #[inline] pub fn clone_arc(&self) -> Arc { let arc = unsafe { Arc::from_raw(self.0) }; // addref it! @@ -901,10 +924,12 @@ impl<'a, T> ArcBorrow<'a, T> { /// For constructing from a reference known to be Arc-backed, /// e.g. if we obtain such a reference over FFI + #[inline] pub unsafe fn from_ref(r: &'a T) -> Self { ArcBorrow(r) } + #[inline] pub fn with_arc(&self, f: F) -> U where F: FnOnce(&Arc) -> U, T: 'static { // Synthesize transient Arc, which never touches the refcount. let transient = unsafe { NoDrop::new(Arc::from_raw(self.0)) }; @@ -924,6 +949,8 @@ impl<'a, T> ArcBorrow<'a, T> { impl<'a, T> Deref for ArcBorrow<'a, T> { type Target = T; + + #[inline] fn deref(&self) -> &T { &*self.0 }