stylo: Make Servo Arc types use ptr to T instead of ptr to ArcInner<T>

This commit is contained in:
Manish Goregaokar 2017-07-17 11:41:56 -07:00 committed by Manish Goregaokar
parent 808b1f509b
commit 74519cc1a7
12 changed files with 203 additions and 81 deletions

View file

@ -30,7 +30,7 @@ use std::fmt;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::Deref;
use stylearc::Arc;
use stylearc::{Arc, ArcBorrow};
use stylist::Stylist;
use thread_state;
@ -360,7 +360,7 @@ pub trait TElement : Eq + PartialEq + Debug + Hash + Sized + Copy + Clone +
}
/// Get this element's style attribute.
fn style_attribute(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>>;
fn style_attribute(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>>;
/// Unset the style attribute's dirty bit.
/// Servo doesn't need to manage ditry bit for style attribute.
@ -368,7 +368,7 @@ pub trait TElement : Eq + PartialEq + Debug + Hash + Sized + Copy + Clone +
}
/// Get this element's SMIL override declarations.
fn get_smil_override(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>> {
fn get_smil_override(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
None
}

View file

@ -56,7 +56,7 @@ impl GeckoRestyleDamage {
let mut any_style_changed: bool = false;
let hint = unsafe {
bindings::Gecko_CalcStyleDifference(context,
new_style.as_borrowed_opt().unwrap(),
new_style.as_borrowed(),
&mut any_style_changed)
};
let change = if any_style_changed { StyleChange::Changed } else { StyleChange::Unchanged };

View file

@ -64,7 +64,7 @@ use gecko_bindings::structs::EffectCompositor_CascadeLevel as CascadeLevel;
use gecko_bindings::structs::NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE;
use gecko_bindings::structs::NODE_IS_NATIVE_ANONYMOUS;
use gecko_bindings::structs::nsIDocument_DocumentTheme as DocumentTheme;
use gecko_bindings::sugar::ownership::{HasArcFFI, HasSimpleFFI};
use gecko_bindings::sugar::ownership::{FFIArcHelpers, HasArcFFI, HasSimpleFFI};
use logical_geometry::WritingMode;
use media_queries::Device;
use properties::{ComputedValues, parse_style_attribute};
@ -88,7 +88,7 @@ use std::mem;
use std::ops::DerefMut;
use std::ptr;
use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
use stylearc::Arc;
use stylearc::{Arc, ArcBorrow, RawOffsetArc};
use stylesheets::UrlExtraData;
use stylist::Stylist;
@ -860,13 +860,15 @@ impl<'le> TElement for GeckoElement<'le> {
device.pres_context().mDocument.raw::<structs::nsIDocument>()
}
fn style_attribute(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>> {
fn style_attribute(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
if !self.may_have_style_attribute() {
return None;
}
let declarations = unsafe { Gecko_GetStyleAttrDeclarationBlock(self.0) };
declarations.map_or(None, |s| s.as_arc_opt())
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>>
= declarations.and_then(|s| s.as_arc_opt());
declarations.map(|s| s.borrow_arc())
}
fn unset_dirty_style_attribute(&self) {
@ -877,9 +879,11 @@ impl<'le> TElement for GeckoElement<'le> {
unsafe { Gecko_UnsetDirtyStyleAttr(self.0) };
}
fn get_smil_override(&self) -> Option<&Arc<Locked<PropertyDeclarationBlock>>> {
fn get_smil_override(&self) -> Option<ArcBorrow<Locked<PropertyDeclarationBlock>>> {
let declarations = unsafe { Gecko_GetSMILOverrideDeclarationBlock(self.0) };
declarations.map(|s| s.as_arc_opt()).unwrap_or(None)
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>>
= declarations.and_then(|s| s.as_arc_opt());
declarations.map(|s| s.borrow_arc())
}
fn get_animation_rule_by_cascade(&self, cascade_level: ServoCascadeLevel)
@ -1098,9 +1102,9 @@ impl<'le> TElement for GeckoElement<'le> {
let computed_values =
computed_data.as_ref().map(|d| d.styles.primary());
let computed_values_opt =
computed_values.map(|v| *HasArcFFI::arc_as_borrowed(v));
computed_values.map(|v| v.as_borrowed());
let before_change_values =
before_change_style.as_ref().map(|v| *HasArcFFI::arc_as_borrowed(v));
before_change_style.as_ref().map(|v| v.as_borrowed());
unsafe {
Gecko_UpdateAnimations(self.0,
before_change_values,
@ -1182,7 +1186,7 @@ impl<'le> TElement for GeckoElement<'le> {
};
let end_value = AnimationValue::arc_from_borrowed(&raw_end_value);
debug_assert!(end_value.is_some());
map.insert(property, end_value.unwrap().clone());
map.insert(property, end_value.unwrap().clone_arc());
}
map
}
@ -1428,17 +1432,19 @@ impl<'le> PresentationalHintsSynthesizer for GeckoElement<'le> {
}
}
let declarations = unsafe { Gecko_GetHTMLPresentationAttrDeclarationBlock(self.0) };
let declarations = declarations.and_then(|s| s.as_arc_opt());
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>>
= declarations.and_then(|s| s.as_arc_opt());
if let Some(decl) = declarations {
hints.push(
ApplicableDeclarationBlock::from_declarations(Clone::clone(decl), ServoCascadeLevel::PresHints)
ApplicableDeclarationBlock::from_declarations(decl.clone_arc(), ServoCascadeLevel::PresHints)
);
}
let declarations = unsafe { Gecko_GetExtraContentStyleDeclarations(self.0) };
let declarations = declarations.and_then(|s| s.as_arc_opt());
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>>
= declarations.and_then(|s| s.as_arc_opt());
if let Some(decl) = declarations {
hints.push(
ApplicableDeclarationBlock::from_declarations(Clone::clone(decl), ServoCascadeLevel::PresHints)
ApplicableDeclarationBlock::from_declarations(decl.clone_arc(), ServoCascadeLevel::PresHints)
);
}
@ -1458,20 +1464,22 @@ impl<'le> PresentationalHintsSynthesizer for GeckoElement<'le> {
Gecko_GetVisitedLinkAttrDeclarationBlock(self.0)
},
};
let declarations = declarations.and_then(|s| s.as_arc_opt());
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>> =
declarations.and_then(|s| s.as_arc_opt());
if let Some(decl) = declarations {
hints.push(
ApplicableDeclarationBlock::from_declarations(Clone::clone(decl), ServoCascadeLevel::PresHints)
ApplicableDeclarationBlock::from_declarations(decl.clone_arc(), ServoCascadeLevel::PresHints)
);
}
let active = self.get_state().intersects(NonTSPseudoClass::Active.state_flag());
if active {
let declarations = unsafe { Gecko_GetActiveLinkAttrDeclarationBlock(self.0) };
let declarations = declarations.and_then(|s| s.as_arc_opt());
let declarations: Option<&RawOffsetArc<Locked<PropertyDeclarationBlock>>>
= declarations.and_then(|s| s.as_arc_opt());
if let Some(decl) = declarations {
hints.push(
ApplicableDeclarationBlock::from_declarations(Clone::clone(decl), ServoCascadeLevel::PresHints)
ApplicableDeclarationBlock::from_declarations(decl.clone_arc(), ServoCascadeLevel::PresHints)
);
}
}

View file

@ -8,7 +8,7 @@ use std::marker::PhantomData;
use std::mem::{forget, transmute};
use std::ops::{Deref, DerefMut};
use std::ptr;
use stylearc::Arc;
use stylearc::{Arc, RawOffsetArc};
/// Indicates that a given Servo type has a corresponding Gecko FFI type.
pub unsafe trait HasFFI : Sized + 'static {
@ -88,7 +88,7 @@ pub unsafe trait HasArcFFI : HasFFI {
/// (usually on the C++ side) without running the Arc destructor.
unsafe fn release_opt(ptr: Option<&Self::FFIType>) {
if let Some(arc) = Self::arc_from_borrowed(&ptr) {
let _: Arc<_> = ptr::read(arc as *const Arc<_>);
let _: RawOffsetArc<_> = ptr::read(arc as *const RawOffsetArc<_>);
}
}
@ -102,16 +102,16 @@ pub unsafe trait HasArcFFI : HasFFI {
/// know that a strong reference to the backing Arc is disappearing
/// (usually on the C++ side) without running the Arc destructor.
unsafe fn release(ptr: &Self::FFIType) {
let _: Arc<_> = ptr::read(Self::as_arc(&ptr) as *const Arc<_>);
let _: RawOffsetArc<_> = ptr::read(Self::as_arc(&ptr) as *const RawOffsetArc<_>);
}
#[inline]
/// Converts a borrowed FFI reference to a borrowed Arc.
///
/// &GeckoType -> &Arc<ServoType>
fn as_arc<'a>(ptr: &'a &Self::FFIType) -> &'a Arc<Self> {
fn as_arc<'a>(ptr: &'a &Self::FFIType) -> &'a RawOffsetArc<Self> {
debug_assert!(!(ptr as *const _).is_null());
unsafe {
transmute::<&&Self::FFIType, &Arc<Self>>(ptr)
transmute::<&&Self::FFIType, &RawOffsetArc<Self>>(ptr)
}
}
@ -119,9 +119,9 @@ pub unsafe trait HasArcFFI : HasFFI {
/// Converts a borrowed Arc to a borrowed FFI reference.
///
/// &Arc<ServoType> -> &GeckoType
fn arc_as_borrowed<'a>(arc: &'a Arc<Self>) -> &'a &Self::FFIType {
fn arc_as_borrowed<'a>(arc: &'a RawOffsetArc<Self>) -> &'a &Self::FFIType {
unsafe {
transmute::<&Arc<Self>, &&Self::FFIType>(arc)
transmute::<&RawOffsetArc<Self>, &&Self::FFIType>(arc)
}
}
@ -129,10 +129,10 @@ pub unsafe trait HasArcFFI : HasFFI {
/// Converts a borrowed nullable FFI reference to a borrowed Arc.
///
/// &GeckoType -> &Arc<ServoType>
fn arc_from_borrowed<'a>(ptr: &'a Option<&Self::FFIType>) -> Option<&'a Arc<Self>> {
fn arc_from_borrowed<'a>(ptr: &'a Option<&Self::FFIType>) -> Option<&'a RawOffsetArc<Self>> {
unsafe {
if let Some(ref reference) = *ptr {
Some(transmute::<&&Self::FFIType, &Arc<_>>(reference))
Some(transmute::<&&Self::FFIType, &RawOffsetArc<_>>(reference))
} else {
None
}
@ -165,7 +165,7 @@ impl<GeckoType> Strong<GeckoType> {
/// Panics on null.
///
/// Strong<GeckoType> -> Arc<ServoType>
pub fn into_arc<ServoType>(self) -> Arc<ServoType>
pub fn into_arc<ServoType>(self) -> RawOffsetArc<ServoType>
where ServoType: HasArcFFI<FFIType = GeckoType>,
{
self.into_arc_opt().unwrap()
@ -177,7 +177,7 @@ impl<GeckoType> Strong<GeckoType> {
/// Returns None on null.
///
/// Strong<GeckoType> -> Arc<ServoType>
pub fn into_arc_opt<ServoType>(self) -> Option<Arc<ServoType>>
pub fn into_arc_opt<ServoType>(self) -> Option<RawOffsetArc<ServoType>>
where ServoType: HasArcFFI<FFIType = GeckoType>,
{
if self.is_null() {
@ -194,7 +194,7 @@ impl<GeckoType> Strong<GeckoType> {
/// Returns None on null.
///
/// Strong<GeckoType> -> Arc<ServoType>
pub fn as_arc_opt<ServoType>(&self) -> Option<&Arc<ServoType>>
pub fn as_arc_opt<ServoType>(&self) -> Option<&RawOffsetArc<ServoType>>
where ServoType: HasArcFFI<FFIType = GeckoType>,
{
if self.is_null() {
@ -222,18 +222,15 @@ pub unsafe trait FFIArcHelpers {
/// Arc<ServoType> -> Strong<GeckoType>
fn into_strong(self) -> Strong<<Self::Inner as HasFFI>::FFIType>;
/// Produces a (nullable) borrowed FFI reference by borrowing an Arc.
/// Produces a borrowed FFI reference by borrowing an Arc.
///
/// &Arc<ServoType> -> Option<&GeckoType>
///
/// FIXME(emilio): What's the point of the nullability? Arc should be
/// non-null, right?
/// &Arc<ServoType> -> &GeckoType
///
/// Then the `arc_as_borrowed` method can go away.
fn as_borrowed_opt(&self) -> Option<&<Self::Inner as HasFFI>::FFIType>;
fn as_borrowed(&self) -> &<Self::Inner as HasFFI>::FFIType;
}
unsafe impl<T: HasArcFFI> FFIArcHelpers for Arc<T> {
unsafe impl<T: HasArcFFI> FFIArcHelpers for RawOffsetArc<T> {
type Inner = T;
#[inline]
@ -242,9 +239,22 @@ unsafe impl<T: HasArcFFI> FFIArcHelpers for Arc<T> {
}
#[inline]
fn as_borrowed_opt(&self) -> Option<&T::FFIType> {
let borrowedptr = self as *const Arc<T> as *const Option<&T::FFIType>;
unsafe { ptr::read(borrowedptr) }
fn as_borrowed(&self) -> &T::FFIType {
unsafe { &*(&**self as *const T as *const T::FFIType) }
}
}
unsafe impl<T: HasArcFFI> FFIArcHelpers for Arc<T> {
type Inner = T;
#[inline]
fn into_strong(self) -> Strong<T::FFIType> {
Arc::into_raw_offset(self).into_strong()
}
#[inline]
fn as_borrowed(&self) -> &T::FFIType {
unsafe { &*(&**self as *const T as *const T::FFIType) }
}
}

View file

@ -211,7 +211,7 @@ impl<T> structs::RefPtr<T> {
/// Sets the contents to an Arc<T>
/// will leak existing contents
pub fn set_arc_leaky<U>(&mut self, other: Arc<U>) where U: HasArcFFI<FFIType = T> {
*self = unsafe { mem::transmute(other) }; // Arc::into_raw is unstable :(
*self = unsafe { mem::transmute(Arc::into_raw_offset(other)) };
}
}

View file

@ -18,7 +18,7 @@ use properties::longhands::display::computed_value as display;
use rule_tree::{CascadeLevel, StrongRuleNode};
use selector_parser::{PseudoElement, RestyleDamage};
use selectors::matching::ElementSelectorFlags;
use stylearc::Arc;
use stylearc::{Arc, ArcBorrow};
/// Represents the result of comparing an element's old and new style.
pub struct StyleDifference {
@ -690,7 +690,7 @@ pub trait MatchMethods : TElement {
};
let replace_rule_node = |level: CascadeLevel,
pdb: Option<&Arc<Locked<PropertyDeclarationBlock>>>,
pdb: Option<ArcBorrow<Locked<PropertyDeclarationBlock>>>,
path: &mut StrongRuleNode| -> bool {
let new_node = stylist.rule_tree()
.update_rule_at_level(level, pdb, path, guards);
@ -737,7 +737,7 @@ pub trait MatchMethods : TElement {
primary_rules: &mut StrongRuleNode| {
let animation_rule = self.get_animation_rule_by_cascade(level);
replace_rule_node(level,
animation_rule.as_ref(),
animation_rule.as_ref().map(|a| a.borrow_arc()),
primary_rules);
};

View file

@ -16,7 +16,7 @@ use std::io::{self, Write};
use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use stylearc::{Arc, NonZeroPtrMut};
use stylearc::{Arc, ArcBorrow, NonZeroPtrMut};
use stylesheets::StyleRule;
use thread_state;
@ -308,7 +308,7 @@ impl RuleTree {
/// the old path is still valid.
pub fn update_rule_at_level(&self,
level: CascadeLevel,
pdb: Option<&Arc<Locked<PropertyDeclarationBlock>>>,
pdb: Option<ArcBorrow<Locked<PropertyDeclarationBlock>>>,
path: &StrongRuleNode,
guards: &StylesheetGuards)
-> Option<StrongRuleNode> {
@ -347,7 +347,7 @@ impl RuleTree {
// so let's skip it for now.
let is_here_already = match &current.get().source {
&StyleSource::Declarations(ref already_here) => {
Arc::ptr_eq(pdb, already_here)
pdb.with_arc(|arc| Arc::ptr_eq(arc, already_here))
},
_ => unreachable!("Replacing non-declarations style?"),
};
@ -371,13 +371,13 @@ impl RuleTree {
if level.is_important() {
if pdb.read_with(level.guard(guards)).any_important() {
current = current.ensure_child(self.root.downgrade(),
StyleSource::Declarations(pdb.clone()),
StyleSource::Declarations(pdb.clone_arc()),
level);
}
} else {
if pdb.read_with(level.guard(guards)).any_normal() {
current = current.ensure_child(self.root.downgrade(),
StyleSource::Declarations(pdb.clone()),
StyleSource::Declarations(pdb.clone_arc()),
level);
}
}

View file

@ -43,7 +43,7 @@ pub fn have_same_style_attribute<E>(
match (target.style_attribute(), candidate.style_attribute()) {
(None, None) => true,
(Some(_), None) | (None, Some(_)) => false,
(Some(a), Some(b)) => Arc::ptr_eq(a, b)
(Some(a), Some(b)) => &*a as *const _ == &*b as *const _
}
}

View file

@ -5,7 +5,7 @@
//! A list of CSS rules.
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard};
use stylearc::Arc;
use stylearc::{Arc, RawOffsetArc};
use stylesheets::{CssRule, RulesMutateError};
use stylesheets::loader::StylesheetLoader;
use stylesheets::memory::{MallocSizeOfFn, MallocSizeOfWithGuard};
@ -109,7 +109,7 @@ pub trait CssRulesHelpers {
-> Result<CssRule, RulesMutateError>;
}
impl CssRulesHelpers for Arc<Locked<CssRules>> {
impl CssRulesHelpers for RawOffsetArc<Locked<CssRules>> {
fn insert_rule(&self,
lock: &SharedRwLock,
rule: &str,

View file

@ -37,7 +37,7 @@ use std::fmt::Debug;
#[cfg(feature = "servo")]
use std::marker::PhantomData;
use style_traits::viewport::ViewportConstraints;
use stylearc::Arc;
use stylearc::{Arc, ArcBorrow};
#[cfg(feature = "gecko")]
use stylesheets::{CounterStyleRule, FontFaceRule};
use stylesheets::{CssRule, StyleRule};
@ -1103,8 +1103,8 @@ impl Stylist {
&self,
element: &E,
pseudo_element: Option<&PseudoElement>,
style_attribute: Option<&Arc<Locked<PropertyDeclarationBlock>>>,
smil_override: Option<&Arc<Locked<PropertyDeclarationBlock>>>,
style_attribute: Option<ArcBorrow<Locked<PropertyDeclarationBlock>>>,
smil_override: Option<ArcBorrow<Locked<PropertyDeclarationBlock>>>,
animation_rules: AnimationRules,
rule_inclusion: RuleInclusion,
applicable_declarations: &mut V,
@ -1208,7 +1208,7 @@ impl Stylist {
if let Some(sa) = style_attribute {
Push::push(
applicable_declarations,
ApplicableDeclarationBlock::from_declarations(sa.clone(),
ApplicableDeclarationBlock::from_declarations(sa.clone_arc(),
CascadeLevel::StyleAttributeNormal));
}
@ -1217,7 +1217,7 @@ impl Stylist {
if let Some(so) = smil_override {
Push::push(
applicable_declarations,
ApplicableDeclarationBlock::from_declarations(so.clone(),
ApplicableDeclarationBlock::from_declarations(so.clone_arc(),
CascadeLevel::SMILOverride));
}