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

View file

@ -10,14 +10,14 @@
use crate::gecko::url::CssUrlData;
use crate::gecko_bindings::structs::{
RawServoAnimationValue, RawServoContainerRule, RawServoCounterStyleRule, RawServoCssUrlData,
RawServoAnimationValue, RawServoContainerRule, RawServoCounterStyleRule,
RawServoDeclarationBlock, RawServoFontFaceRule, RawServoFontFeatureValuesRule,
RawServoFontPaletteValuesRule, RawServoImportRule, RawServoKeyframe, RawServoKeyframesRule,
RawServoLayerBlockRule, RawServoLayerStatementRule, RawServoMediaList, RawServoMediaRule,
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::properties::animated_properties::AnimationValue;
use crate::properties::{ComputedValues, PropertyDeclarationBlock};
@ -28,8 +28,7 @@ use crate::stylesheets::{
FontPaletteValuesRule, ImportRule, KeyframesRule, LayerBlockRule, LayerStatementRule,
MediaRule, NamespaceRule, PageRule, StyleRule, StylesheetContents, SupportsRule,
};
use servo_arc::{Arc, ArcBorrow};
use std::{mem, ptr};
use servo_arc::Arc;
macro_rules! impl_arc_ffi {
($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
[Servo_CssRules_AddRef, Servo_CssRules_Release]);
impl_arc_ffi!(StylesheetContents => RawServoStyleSheetContents
[Servo_StyleSheetContents_AddRef, Servo_StyleSheetContents_Release]);
impl_arc_ffi!(Locked<PropertyDeclarationBlock> => RawServoDeclarationBlock
[Servo_DeclarationBlock_AddRef, Servo_DeclarationBlock_Release]);
@ -112,29 +108,32 @@ impl_arc_ffi!(Locked<FontFaceRule> => RawServoFontFaceRule
impl_arc_ffi!(Locked<CounterStyleRule> => RawServoCounterStyleRule
[Servo_CounterStyleRule_AddRef, Servo_CounterStyleRule_Release]);
impl_arc_ffi!(CssUrlData => RawServoCssUrlData
[Servo_CssUrlData_AddRef, Servo_CssUrlData_Release]);
macro_rules! impl_simple_arc_ffi {
($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.
// This means that doing the HasArcFFI type trick is actually unsound,
// since it gives us a way to construct an Arc<ComputedStyle> from
// 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]
pub unsafe extern "C" fn $release(obj: &$ty) {
let _ = Arc::from_raw(obj);
}
};
}
#[no_mangle]
pub unsafe extern "C" fn Servo_ComputedStyle_Release(obj: &ComputedValues) {
ArcBorrow::from_ref(obj).with_arc(|a: &Arc<ComputedValues>| {
let _: Arc<ComputedValues> = ptr::read(a);
});
}
impl From<Arc<ComputedValues>> for Strong<ComputedValues> {
fn from(arc: Arc<ComputedValues>) -> Self {
unsafe { mem::transmute(Arc::into_raw_offset(arc)) }
}
}
impl_simple_arc_ffi!(
StylesheetContents,
Servo_StyleSheetContents_AddRef,
Servo_StyleSheetContents_Release
);
impl_simple_arc_ffi!(
CssUrlData,
Servo_CssUrlData_AddRef,
Servo_CssUrlData_Release
);
impl_simple_arc_ffi!(
ComputedValues,
Servo_ComputedStyle_AddRef,
Servo_ComputedStyle_Release
);

View file

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

View file

@ -95,6 +95,15 @@ pub struct Strong<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> {
#[inline]
/// Returns whether this reference is null.