style: Remove HasArcFFI for some types

See blocked bug.

For non-Locked types we can just use the same underlying type at the FFI
boundary. Port StylesheetContents and CssUrlData to this set-up.
CssUrlData is already generated by cbindgen anyways.

Differential Revision: https://phabricator.services.mozilla.com/D177559
This commit is contained in:
Emilio Cobos Álvarez 2023-05-11 00:36:24 +00:00 committed by Martin Robinson
parent d9d865e8c9
commit 779aa9d30e
4 changed files with 42 additions and 40 deletions

View file

@ -221,7 +221,7 @@ impl<T> Arc<T> {
/// ///
/// It is recommended to use RawOffsetArc for this. /// It is recommended to use RawOffsetArc for this.
#[inline] #[inline]
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
@ -231,10 +231,8 @@ impl<T> Arc<T> {
/// ///
/// Note: This raw pointer will be offset in the allocation and must be preceded /// Note: This raw pointer will be offset in the allocation and must be preceded
/// by the atomic count. /// by the atomic count.
///
/// It is recommended to use RawOffsetArc for this
#[inline] #[inline]
unsafe fn from_raw(ptr: *const T) -> Self { pub 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).sub(data_offset::<T>()); let ptr = (ptr as *const u8).sub(data_offset::<T>());

View file

@ -10,14 +10,14 @@
use crate::gecko::url::CssUrlData; use crate::gecko::url::CssUrlData;
use crate::gecko_bindings::structs::{ use crate::gecko_bindings::structs::{
RawServoAnimationValue, RawServoContainerRule, RawServoCounterStyleRule, RawServoCssUrlData, RawServoAnimationValue, RawServoContainerRule, RawServoCounterStyleRule,
RawServoDeclarationBlock, RawServoFontFaceRule, RawServoFontFeatureValuesRule, RawServoDeclarationBlock, RawServoFontFaceRule, RawServoFontFeatureValuesRule,
RawServoFontPaletteValuesRule, RawServoImportRule, RawServoKeyframe, RawServoKeyframesRule, RawServoFontPaletteValuesRule, RawServoImportRule, RawServoKeyframe, RawServoKeyframesRule,
RawServoLayerBlockRule, RawServoLayerStatementRule, RawServoMediaList, RawServoMediaRule, RawServoLayerBlockRule, RawServoLayerStatementRule, RawServoMediaList, RawServoMediaRule,
RawServoMozDocumentRule, RawServoNamespaceRule, RawServoPageRule, RawServoStyleRule, RawServoMozDocumentRule, RawServoNamespaceRule, RawServoPageRule, RawServoStyleRule,
RawServoStyleSheetContents, RawServoSupportsRule, ServoCssRules, RawServoSupportsRule, ServoCssRules,
}; };
use crate::gecko_bindings::sugar::ownership::{HasArcFFI, Strong}; use crate::gecko_bindings::sugar::ownership::HasArcFFI;
use crate::media_queries::MediaList; use crate::media_queries::MediaList;
use crate::properties::animated_properties::AnimationValue; use crate::properties::animated_properties::AnimationValue;
use crate::properties::{ComputedValues, PropertyDeclarationBlock}; use crate::properties::{ComputedValues, PropertyDeclarationBlock};
@ -28,8 +28,7 @@ use crate::stylesheets::{
FontPaletteValuesRule, ImportRule, KeyframesRule, LayerBlockRule, LayerStatementRule, FontPaletteValuesRule, ImportRule, KeyframesRule, LayerBlockRule, LayerStatementRule,
MediaRule, NamespaceRule, PageRule, StyleRule, StylesheetContents, SupportsRule, MediaRule, NamespaceRule, PageRule, StyleRule, StylesheetContents, SupportsRule,
}; };
use servo_arc::{Arc, ArcBorrow}; use servo_arc::Arc;
use std::{mem, ptr};
macro_rules! impl_arc_ffi { macro_rules! impl_arc_ffi {
($servo_type:ty => $gecko_type:ty[$addref:ident, $release:ident]) => { ($servo_type:ty => $gecko_type:ty[$addref:ident, $release:ident]) => {
@ -52,9 +51,6 @@ macro_rules! impl_arc_ffi {
impl_arc_ffi!(Locked<CssRules> => ServoCssRules impl_arc_ffi!(Locked<CssRules> => ServoCssRules
[Servo_CssRules_AddRef, Servo_CssRules_Release]); [Servo_CssRules_AddRef, Servo_CssRules_Release]);
impl_arc_ffi!(StylesheetContents => RawServoStyleSheetContents
[Servo_StyleSheetContents_AddRef, Servo_StyleSheetContents_Release]);
impl_arc_ffi!(Locked<PropertyDeclarationBlock> => RawServoDeclarationBlock impl_arc_ffi!(Locked<PropertyDeclarationBlock> => RawServoDeclarationBlock
[Servo_DeclarationBlock_AddRef, Servo_DeclarationBlock_Release]); [Servo_DeclarationBlock_AddRef, Servo_DeclarationBlock_Release]);
@ -112,29 +108,32 @@ impl_arc_ffi!(Locked<FontFaceRule> => RawServoFontFaceRule
impl_arc_ffi!(Locked<CounterStyleRule> => RawServoCounterStyleRule impl_arc_ffi!(Locked<CounterStyleRule> => RawServoCounterStyleRule
[Servo_CounterStyleRule_AddRef, Servo_CounterStyleRule_Release]); [Servo_CounterStyleRule_AddRef, Servo_CounterStyleRule_Release]);
impl_arc_ffi!(CssUrlData => RawServoCssUrlData macro_rules! impl_simple_arc_ffi {
[Servo_CssUrlData_AddRef, Servo_CssUrlData_Release]); ($ty:ty, $addref:ident, $release:ident) => {
#[no_mangle]
pub unsafe extern "C" fn $addref(obj: &$ty) {
std::mem::forget(Arc::from_raw_addrefed(obj));
}
// ComputedStyle is not an opaque type on any side of FFI. #[no_mangle]
// This means that doing the HasArcFFI type trick is actually unsound, pub unsafe extern "C" fn $release(obj: &$ty) {
// since it gives us a way to construct an Arc<ComputedStyle> from let _ = Arc::from_raw(obj);
// an &ComputedStyle, which in general is not allowed. So we }
// implement the restricted set of arc type functionality we need. };
#[no_mangle]
pub unsafe extern "C" fn Servo_ComputedStyle_AddRef(obj: &ComputedValues) {
mem::forget(ArcBorrow::from_ref(obj).clone_arc());
} }
#[no_mangle] impl_simple_arc_ffi!(
pub unsafe extern "C" fn Servo_ComputedStyle_Release(obj: &ComputedValues) { StylesheetContents,
ArcBorrow::from_ref(obj).with_arc(|a: &Arc<ComputedValues>| { Servo_StyleSheetContents_AddRef,
let _: Arc<ComputedValues> = ptr::read(a); Servo_StyleSheetContents_Release
}); );
} impl_simple_arc_ffi!(
CssUrlData,
impl From<Arc<ComputedValues>> for Strong<ComputedValues> { Servo_CssUrlData_AddRef,
fn from(arc: Arc<ComputedValues>) -> Self { Servo_CssUrlData_Release
unsafe { mem::transmute(Arc::into_raw_offset(arc)) } );
} impl_simple_arc_ffi!(
} ComputedValues,
Servo_ComputedStyle_AddRef,
Servo_ComputedStyle_Release
);

View file

@ -125,11 +125,7 @@ impl StylesheetInDocument for GeckoStyleSheet {
#[inline] #[inline]
fn contents(&self) -> &StylesheetContents { fn contents(&self) -> &StylesheetContents {
debug_assert!(!self.inner().mContents.mRawPtr.is_null()); debug_assert!(!self.inner().mContents.mRawPtr.is_null());
unsafe { unsafe { &*self.inner().mContents.mRawPtr }
let contents =
(&**StylesheetContents::as_arc(&&*self.inner().mContents.mRawPtr)) as *const _;
&*contents
}
} }
} }

View file

@ -95,6 +95,15 @@ pub struct Strong<GeckoType> {
_marker: PhantomData<GeckoType>, _marker: PhantomData<GeckoType>,
} }
impl<T> From<Arc<T>> for Strong<T> {
fn from(arc: Arc<T>) -> Self {
Self {
ptr: Arc::into_raw(arc),
_marker: PhantomData,
}
}
}
impl<GeckoType> Strong<GeckoType> { impl<GeckoType> Strong<GeckoType> {
#[inline] #[inline]
/// Returns whether this reference is null. /// Returns whether this reference is null.