servo_arc: Add some #[inline] and repr(C) annotations.

inline is probably unnecessary, but anyway... We rely on those being repr(C), so
they rather have some.
This commit is contained in:
Emilio Cobos Álvarez 2017-12-01 14:33:22 +01:00
parent b7bb6ffbb8
commit a1521d9d4d
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C

View file

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