From 46bc0525f7cef6f48a2ff3c1f4ec4baff9876816 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sat, 29 Apr 2017 22:03:41 -0700 Subject: [PATCH 1/6] Fork std::arc. This is a verbatim copy of the source at [1]. It won't compile on stable rust, but I'm including it here to make the changes clear. [1] https://doc.rust-lang.org/src/alloc/arc.rs.html MozReview-Commit-ID: XEbOK4fXQX --- components/style/stylearc.rs | 1468 ++++++++++++++++++++++++++++++++++ 1 file changed, 1468 insertions(+) create mode 100644 components/style/stylearc.rs diff --git a/components/style/stylearc.rs b/components/style/stylearc.rs new file mode 100644 index 00000000000..8c0dd6b3795 --- /dev/null +++ b/components/style/stylearc.rs @@ -0,0 +1,1468 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![stable(feature = "rust1", since = "1.0.0")] + +//! Thread-safe reference-counting pointers. +//! +//! See the [`Arc`][arc] documentation for more details. +//! +//! [arc]: struct.Arc.html + +use boxed::Box; + +use core::sync::atomic; +use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; +use core::borrow; +use core::fmt; +use core::cmp::Ordering; +use core::mem::{align_of_val, size_of_val}; +use core::intrinsics::abort; +use core::mem; +use core::mem::uninitialized; +use core::ops::Deref; +use core::ops::CoerceUnsized; +use core::ptr::{self, Shared}; +use core::marker::Unsize; +use core::hash::{Hash, Hasher}; +use core::{isize, usize}; +use core::convert::From; +use heap::deallocate; + +/// A soft limit on the amount of references that may be made to an `Arc`. +/// +/// Going above this limit will abort your program (although not +/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references. +const MAX_REFCOUNT: usize = (isize::MAX) as usize; + +/// A thread-safe reference-counting pointer. +/// +/// The type `Arc` provides shared ownership of a value of type `T`, +/// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces +/// a new pointer to the same value in the heap. When the last `Arc` +/// pointer to a given value is destroyed, the pointed-to value is +/// also destroyed. +/// +/// Shared references in Rust disallow mutation by default, and `Arc` is no +/// exception. If you need to mutate through an `Arc`, use [`Mutex`][mutex], +/// [`RwLock`][rwlock], or one of the [`Atomic`][atomic] types. +/// +/// `Arc` uses atomic operations for reference counting, so `Arc`s can be +/// sent between threads. In other words, `Arc` implements [`Send`] +/// as long as `T` implements [`Send`] and [`Sync`][sync]. The disadvantage is +/// that atomic operations are more expensive than ordinary memory accesses. +/// If you are not sharing reference-counted values between threads, consider +/// using [`rc::Rc`][`Rc`] for lower overhead. [`Rc`] is a safe default, because +/// the compiler will catch any attempt to send an [`Rc`] between threads. +/// However, a library might choose `Arc` in order to give library consumers +/// more flexibility. +/// +/// The [`downgrade`][downgrade] method can be used to create a non-owning +/// [`Weak`][weak] pointer. A [`Weak`][weak] pointer can be [`upgrade`][upgrade]d +/// to an `Arc`, but this will return [`None`] if the value has already been +/// dropped. +/// +/// A cycle between `Arc` pointers will never be deallocated. For this reason, +/// [`Weak`][weak] is used to break cycles. For example, a tree could have +/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak] +/// pointers from children back to their parents. +/// +/// `Arc` automatically dereferences to `T` (via the [`Deref`][deref] trait), +/// so you can call `T`'s methods on a value of type `Arc`. To avoid name +/// clashes with `T`'s methods, the methods of `Arc` itself are [associated +/// functions][assoc], called using function-like syntax: +/// +/// ``` +/// use std::sync::Arc; +/// let my_arc = Arc::new(()); +/// +/// Arc::downgrade(&my_arc); +/// ``` +/// +/// [`Weak`][weak] does not auto-dereference to `T`, because the value may have +/// already been destroyed. +/// +/// [arc]: struct.Arc.html +/// [weak]: struct.Weak.html +/// [`Rc`]: ../../std/rc/struct.Rc.html +/// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone +/// [mutex]: ../../std/sync/struct.Mutex.html +/// [rwlock]: ../../std/sync/struct.RwLock.html +/// [atomic]: ../../std/sync/atomic/ +/// [`Send`]: ../../std/marker/trait.Send.html +/// [sync]: ../../std/marker/trait.Sync.html +/// [deref]: ../../std/ops/trait.Deref.html +/// [downgrade]: struct.Arc.html#method.downgrade +/// [upgrade]: struct.Weak.html#method.upgrade +/// [`None`]: ../../std/option/enum.Option.html#variant.None +/// [assoc]: ../../book/method-syntax.html#associated-functions +/// +/// # Examples +/// +/// Sharing some immutable data between threads: +/// +// Note that we **do not** run these tests here. The windows builders get super +// unhappy if a thread outlives the main thread and then exits at the same time +// (something deadlocks) so we just avoid this entirely by not running these +// tests. +/// ```no_run +/// use std::sync::Arc; +/// use std::thread; +/// +/// let five = Arc::new(5); +/// +/// for _ in 0..10 { +/// let five = five.clone(); +/// +/// thread::spawn(move || { +/// println!("{:?}", five); +/// }); +/// } +/// ``` +/// +/// Sharing a mutable [`AtomicUsize`]: +/// +/// [`AtomicUsize`]: ../../std/sync/atomic/struct.AtomicUsize.html +/// +/// ```no_run +/// use std::sync::Arc; +/// use std::sync::atomic::{AtomicUsize, Ordering}; +/// use std::thread; +/// +/// let val = Arc::new(AtomicUsize::new(5)); +/// +/// for _ in 0..10 { +/// let val = val.clone(); +/// +/// thread::spawn(move || { +/// let v = val.fetch_add(1, Ordering::SeqCst); +/// println!("{:?}", v); +/// }); +/// } +/// ``` +/// +/// See the [`rc` documentation][rc_examples] for more examples of reference +/// counting in general. +/// +/// [rc_examples]: ../../std/rc/#examples +#[stable(feature = "rust1", since = "1.0.0")] +pub struct Arc { + ptr: Shared>, +} + +#[stable(feature = "rust1", since = "1.0.0")] +unsafe impl Send for Arc {} +#[stable(feature = "rust1", since = "1.0.0")] +unsafe impl Sync for Arc {} + +#[unstable(feature = "coerce_unsized", issue = "27732")] +impl, U: ?Sized> CoerceUnsized> for Arc {} + +/// A weak version of [`Arc`][arc]. +/// +/// `Weak` pointers do not count towards determining if the inner value +/// should be dropped. +/// +/// The typical way to obtain a `Weak` pointer is to call +/// [`Arc::downgrade`][downgrade]. +/// +/// See the [`Arc`][arc] documentation for more details. +/// +/// [arc]: struct.Arc.html +/// [downgrade]: struct.Arc.html#method.downgrade +#[stable(feature = "arc_weak", since = "1.4.0")] +pub struct Weak { + ptr: Shared>, +} + +#[stable(feature = "arc_weak", since = "1.4.0")] +unsafe impl Send for Weak {} +#[stable(feature = "arc_weak", since = "1.4.0")] +unsafe impl Sync for Weak {} + +#[unstable(feature = "coerce_unsized", issue = "27732")] +impl, U: ?Sized> CoerceUnsized> for Weak {} + +#[stable(feature = "arc_weak", since = "1.4.0")] +impl fmt::Debug for Weak { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "(Weak)") + } +} + +struct ArcInner { + strong: atomic::AtomicUsize, + + // the value usize::MAX acts as a sentinel for temporarily "locking" the + // ability to upgrade weak pointers or downgrade strong ones; this is used + // to avoid races in `make_mut` and `get_mut`. + weak: atomic::AtomicUsize, + + data: T, +} + +unsafe impl Send for ArcInner {} +unsafe impl Sync for ArcInner {} + +impl Arc { + /// Constructs a new `Arc`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// ``` + #[inline] + #[stable(feature = "rust1", since = "1.0.0")] + pub fn new(data: T) -> Arc { + // Start the weak pointer count as 1 which is the weak pointer that's + // held by all the strong pointers (kinda), see std/rc.rs for more info + let x: Box<_> = box ArcInner { + strong: atomic::AtomicUsize::new(1), + weak: atomic::AtomicUsize::new(1), + data: data, + }; + Arc { ptr: unsafe { Shared::new(Box::into_raw(x)) } } + } + + /// Returns the contained value, if the `Arc` has exactly one strong reference. + /// + /// Otherwise, an [`Err`][result] is returned with the same `Arc` that was + /// passed in. + /// + /// This will succeed even if there are outstanding weak references. + /// + /// [result]: ../../std/result/enum.Result.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let x = Arc::new(3); + /// assert_eq!(Arc::try_unwrap(x), Ok(3)); + /// + /// let x = Arc::new(4); + /// let _y = x.clone(); + /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4); + /// ``` + #[inline] + #[stable(feature = "arc_unique", since = "1.4.0")] + pub fn try_unwrap(this: Self) -> Result { + // See `drop` for why all these atomics are like this + if this.inner().strong.compare_exchange(1, 0, Release, Relaxed).is_err() { + return Err(this); + } + + atomic::fence(Acquire); + + unsafe { + let ptr = *this.ptr; + let elem = ptr::read(&(*ptr).data); + + // Make a weak pointer to clean up the implicit strong-weak reference + let _weak = Weak { ptr: this.ptr }; + mem::forget(this); + + Ok(elem) + } + } + + /// Consumes the `Arc`, returning the wrapped pointer. + /// + /// To avoid a memory leak the pointer must be converted back to an `Arc` using + /// [`Arc::from_raw`][from_raw]. + /// + /// [from_raw]: struct.Arc.html#method.from_raw + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let x = Arc::new(10); + /// let x_ptr = Arc::into_raw(x); + /// assert_eq!(unsafe { *x_ptr }, 10); + /// ``` + #[stable(feature = "rc_raw", since = "1.17.0")] + pub fn into_raw(this: Self) -> *const T { + let ptr = unsafe { &(**this.ptr).data as *const _ }; + mem::forget(this); + ptr + } + + /// Constructs an `Arc` from a raw pointer. + /// + /// The raw pointer must have been previously returned by a call to a + /// [`Arc::into_raw`][into_raw]. + /// + /// This function is unsafe because improper use may lead to memory problems. For example, a + /// double-free may occur if the function is called twice on the same raw pointer. + /// + /// [into_raw]: struct.Arc.html#method.into_raw + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let x = Arc::new(10); + /// let x_ptr = Arc::into_raw(x); + /// + /// unsafe { + /// // Convert back to an `Arc` to prevent leak. + /// let x = Arc::from_raw(x_ptr); + /// assert_eq!(*x, 10); + /// + /// // Further calls to `Arc::from_raw(x_ptr)` would be memory unsafe. + /// } + /// + /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! + /// ``` + #[stable(feature = "rc_raw", since = "1.17.0")] + 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).offset(-offset_of!(ArcInner, data)); + Arc { + ptr: Shared::new(ptr as *const _), + } + } +} + +impl Arc { + /// Creates a new [`Weak`][weak] pointer to this value. + /// + /// [weak]: struct.Weak.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// let weak_five = Arc::downgrade(&five); + /// ``` + #[stable(feature = "arc_weak", since = "1.4.0")] + pub fn downgrade(this: &Self) -> Weak { + // This Relaxed is OK because we're checking the value in the CAS + // below. + let mut cur = this.inner().weak.load(Relaxed); + + loop { + // check if the weak counter is currently "locked"; if so, spin. + if cur == usize::MAX { + cur = this.inner().weak.load(Relaxed); + continue; + } + + // NOTE: this code currently ignores the possibility of overflow + // into usize::MAX; in general both Rc and Arc need to be adjusted + // to deal with overflow. + + // Unlike with Clone(), we need this to be an Acquire read to + // synchronize with the write coming from `is_unique`, so that the + // events prior to that write happen before this read. + match this.inner().weak.compare_exchange_weak(cur, cur + 1, Acquire, Relaxed) { + Ok(_) => return Weak { ptr: this.ptr }, + Err(old) => cur = old, + } + } + } + + /// Gets the number of [`Weak`][weak] pointers to this value. + /// + /// [weak]: struct.Weak.html + /// + /// # Safety + /// + /// This method by itself is safe, but using it correctly requires extra care. + /// Another thread can change the weak count at any time, + /// including potentially between calling this method and acting on the result. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// let _weak_five = Arc::downgrade(&five); + /// + /// // This assertion is deterministic because we haven't shared + /// // the `Arc` or `Weak` between threads. + /// assert_eq!(1, Arc::weak_count(&five)); + /// ``` + #[inline] + #[stable(feature = "arc_counts", since = "1.15.0")] + pub fn weak_count(this: &Self) -> usize { + this.inner().weak.load(SeqCst) - 1 + } + + /// Gets the number of strong (`Arc`) pointers to this value. + /// + /// # Safety + /// + /// This method by itself is safe, but using it correctly requires extra care. + /// Another thread can change the strong count at any time, + /// including potentially between calling this method and acting on the result. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// let _also_five = five.clone(); + /// + /// // This assertion is deterministic because we haven't shared + /// // the `Arc` between threads. + /// assert_eq!(2, Arc::strong_count(&five)); + /// ``` + #[inline] + #[stable(feature = "arc_counts", since = "1.15.0")] + pub fn strong_count(this: &Self) -> usize { + this.inner().strong.load(SeqCst) + } + + #[inline] + fn inner(&self) -> &ArcInner { + // This unsafety is ok because while this arc is alive we're guaranteed + // that the inner pointer is valid. Furthermore, we know that the + // `ArcInner` structure itself is `Sync` because the inner data is + // `Sync` as well, so we're ok loaning out an immutable pointer to these + // contents. + unsafe { &**self.ptr } + } + + // Non-inlined part of `drop`. + #[inline(never)] + unsafe fn drop_slow(&mut self) { + let ptr = self.ptr.as_mut_ptr(); + + // Destroy the data at this time, even though we may not free the box + // allocation itself (there may still be weak pointers lying around). + ptr::drop_in_place(&mut (*ptr).data); + + if self.inner().weak.fetch_sub(1, Release) == 1 { + atomic::fence(Acquire); + deallocate(ptr as *mut u8, size_of_val(&*ptr), align_of_val(&*ptr)) + } + } + + #[inline] + #[stable(feature = "ptr_eq", since = "1.17.0")] + /// Returns true if the two `Arc`s point to the same value (not + /// just values that compare as equal). + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// let same_five = five.clone(); + /// let other_five = Arc::new(5); + /// + /// assert!(Arc::ptr_eq(&five, &same_five)); + /// assert!(!Arc::ptr_eq(&five, &other_five)); + /// ``` + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + let this_ptr: *const ArcInner = *this.ptr; + let other_ptr: *const ArcInner = *other.ptr; + this_ptr == other_ptr + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Clone for Arc { + /// Makes a clone of the `Arc` pointer. + /// + /// This creates another pointer to the same inner value, increasing the + /// strong reference count. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// five.clone(); + /// ``` + #[inline] + fn clone(&self) -> Arc { + // Using a relaxed ordering is alright here, as knowledge of the + // original reference prevents other threads from erroneously deleting + // the object. + // + // As explained in the [Boost documentation][1], Increasing the + // reference counter can always be done with memory_order_relaxed: New + // references to an object can only be formed from an existing + // reference, and passing an existing reference from one thread to + // another must already provide any required synchronization. + // + // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) + let old_size = self.inner().strong.fetch_add(1, Relaxed); + + // However we need to guard against massive refcounts in case someone + // is `mem::forget`ing Arcs. If we don't do this the count can overflow + // and users will use-after free. We racily saturate to `isize::MAX` on + // the assumption that there aren't ~2 billion threads incrementing + // the reference count at once. This branch will never be taken in + // any realistic program. + // + // We abort because such a program is incredibly degenerate, and we + // don't care to support it. + if old_size > MAX_REFCOUNT { + unsafe { + abort(); + } + } + + Arc { ptr: self.ptr } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Deref for Arc { + type Target = T; + + #[inline] + fn deref(&self) -> &T { + &self.inner().data + } +} + +impl Arc { + /// Makes a mutable reference into the given `Arc`. + /// + /// If there are other `Arc` or [`Weak`][weak] pointers to the same value, + /// then `make_mut` will invoke [`clone`][clone] on the inner value to + /// ensure unique ownership. This is also referred to as clone-on-write. + /// + /// See also [`get_mut`][get_mut], which will fail rather than cloning. + /// + /// [weak]: struct.Weak.html + /// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone + /// [get_mut]: struct.Arc.html#method.get_mut + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let mut data = Arc::new(5); + /// + /// *Arc::make_mut(&mut data) += 1; // Won't clone anything + /// let mut other_data = data.clone(); // Won't clone inner data + /// *Arc::make_mut(&mut data) += 1; // Clones inner data + /// *Arc::make_mut(&mut data) += 1; // Won't clone anything + /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything + /// + /// // Now `data` and `other_data` point to different values. + /// assert_eq!(*data, 8); + /// assert_eq!(*other_data, 12); + /// ``` + #[inline] + #[stable(feature = "arc_unique", since = "1.4.0")] + pub fn make_mut(this: &mut Self) -> &mut T { + // Note that we hold both a strong reference and a weak reference. + // Thus, releasing our strong reference only will not, by itself, cause + // the memory to be deallocated. + // + // Use Acquire to ensure that we see any writes to `weak` that happen + // before release writes (i.e., decrements) to `strong`. Since we hold a + // weak count, there's no chance the ArcInner itself could be + // deallocated. + if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err() { + // Another strong pointer exists; clone + *this = Arc::new((**this).clone()); + } else if this.inner().weak.load(Relaxed) != 1 { + // Relaxed suffices in the above because this is fundamentally an + // optimization: we are always racing with weak pointers being + // dropped. Worst case, we end up allocated a new Arc unnecessarily. + + // We removed the last strong ref, but there are additional weak + // refs remaining. We'll move the contents to a new Arc, and + // invalidate the other weak refs. + + // Note that it is not possible for the read of `weak` to yield + // usize::MAX (i.e., locked), since the weak count can only be + // locked by a thread with a strong reference. + + // Materialize our own implicit weak pointer, so that it can clean + // up the ArcInner as needed. + let weak = Weak { ptr: this.ptr }; + + // mark the data itself as already deallocated + unsafe { + // there is no data race in the implicit write caused by `read` + // here (due to zeroing) because data is no longer accessed by + // other threads (due to there being no more strong refs at this + // point). + let mut swap = Arc::new(ptr::read(&(**weak.ptr).data)); + mem::swap(this, &mut swap); + mem::forget(swap); + } + } else { + // We were the sole reference of either kind; bump back up the + // strong ref count. + this.inner().strong.store(1, Release); + } + + // As with `get_mut()`, the unsafety is ok because our reference was + // either unique to begin with, or became one upon cloning the contents. + unsafe { + let inner = &mut *this.ptr.as_mut_ptr(); + &mut inner.data + } + } +} + +impl Arc { + /// Returns a mutable reference to the inner value, if there are + /// no other `Arc` or [`Weak`][weak] pointers to the same value. + /// + /// Returns [`None`][option] otherwise, because it is not safe to + /// mutate a shared value. + /// + /// See also [`make_mut`][make_mut], which will [`clone`][clone] + /// the inner value when it's shared. + /// + /// [weak]: struct.Weak.html + /// [option]: ../../std/option/enum.Option.html + /// [make_mut]: struct.Arc.html#method.make_mut + /// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let mut x = Arc::new(3); + /// *Arc::get_mut(&mut x).unwrap() = 4; + /// assert_eq!(*x, 4); + /// + /// let _y = x.clone(); + /// assert!(Arc::get_mut(&mut x).is_none()); + /// ``` + #[inline] + #[stable(feature = "arc_unique", since = "1.4.0")] + pub fn get_mut(this: &mut Self) -> Option<&mut T> { + if this.is_unique() { + // This unsafety is ok because we're guaranteed that the pointer + // returned is the *only* pointer that will ever be returned to T. Our + // reference count is guaranteed to be 1 at this point, and we required + // the Arc itself to be `mut`, so we're returning the only possible + // reference to the inner data. + unsafe { + let inner = &mut *this.ptr.as_mut_ptr(); + Some(&mut inner.data) + } + } else { + None + } + } + + /// Determine whether this is the unique reference (including weak refs) to + /// the underlying data. + /// + /// Note that this requires locking the weak ref count. + fn is_unique(&mut self) -> bool { + // lock the weak pointer count if we appear to be the sole weak pointer + // holder. + // + // The acquire label here ensures a happens-before relationship with any + // writes to `strong` prior to decrements of the `weak` count (via drop, + // which uses Release). + if self.inner().weak.compare_exchange(1, usize::MAX, Acquire, Relaxed).is_ok() { + // Due to the previous acquire read, this will observe any writes to + // `strong` that were due to upgrading weak pointers; only strong + // clones remain, which require that the strong count is > 1 anyway. + let unique = self.inner().strong.load(Relaxed) == 1; + + // The release write here synchronizes with a read in `downgrade`, + // effectively preventing the above read of `strong` from happening + // after the write. + self.inner().weak.store(1, Release); // release the lock + unique + } else { + false + } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { + /// Drops the `Arc`. + /// + /// This will decrement the strong reference count. If the strong reference + /// count reaches zero then the only other references (if any) are + /// [`Weak`][weak], so we `drop` the inner value. + /// + /// [weak]: struct.Weak.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// struct Foo; + /// + /// impl Drop for Foo { + /// fn drop(&mut self) { + /// println!("dropped!"); + /// } + /// } + /// + /// let foo = Arc::new(Foo); + /// let foo2 = foo.clone(); + /// + /// drop(foo); // Doesn't print anything + /// drop(foo2); // Prints "dropped!" + /// ``` + #[inline] + fn drop(&mut self) { + // Because `fetch_sub` is already atomic, we do not need to synchronize + // with other threads unless we are going to delete the object. This + // same logic applies to the below `fetch_sub` to the `weak` count. + if self.inner().strong.fetch_sub(1, Release) != 1 { + return; + } + + // This fence is needed to prevent reordering of use of the data and + // deletion of the data. Because it is marked `Release`, the decreasing + // of the reference count synchronizes with this `Acquire` fence. This + // means that use of the data happens before decreasing the reference + // count, which happens before this fence, which happens before the + // deletion of the data. + // + // As explained in the [Boost documentation][1], + // + // > It is important to enforce any possible access to the object in one + // > thread (through an existing reference) to *happen before* deleting + // > the object in a different thread. This is achieved by a "release" + // > operation after dropping a reference (any access to the object + // > through this reference must obviously happened before), and an + // > "acquire" operation before deleting the object. + // + // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) + atomic::fence(Acquire); + + unsafe { + self.drop_slow(); + } + } +} + +impl Weak { + /// Constructs a new `Weak`, without an accompanying instance of `T`. + /// + /// This allocates memory for `T`, but does not initialize it. Calling + /// [`upgrade`][upgrade] on the return value always gives + /// [`None`][option]. + /// + /// [upgrade]: struct.Weak.html#method.upgrade + /// [option]: ../../std/option/enum.Option.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Weak; + /// + /// let empty: Weak = Weak::new(); + /// assert!(empty.upgrade().is_none()); + /// ``` + #[stable(feature = "downgraded_weak", since = "1.10.0")] + pub fn new() -> Weak { + unsafe { + Weak { + ptr: Shared::new(Box::into_raw(box ArcInner { + strong: atomic::AtomicUsize::new(0), + weak: atomic::AtomicUsize::new(1), + data: uninitialized(), + })), + } + } + } +} + +impl Weak { + /// Upgrades the `Weak` pointer to an [`Arc`][arc], if possible. + /// + /// Returns [`None`][option] if the strong count has reached zero and the + /// inner value was destroyed. + /// + /// [arc]: struct.Arc.html + /// [option]: ../../std/option/enum.Option.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// let weak_five = Arc::downgrade(&five); + /// + /// let strong_five: Option> = weak_five.upgrade(); + /// assert!(strong_five.is_some()); + /// + /// // Destroy all strong pointers. + /// drop(strong_five); + /// drop(five); + /// + /// assert!(weak_five.upgrade().is_none()); + /// ``` + #[stable(feature = "arc_weak", since = "1.4.0")] + pub fn upgrade(&self) -> Option> { + // We use a CAS loop to increment the strong count instead of a + // fetch_add because once the count hits 0 it must never be above 0. + let inner = self.inner(); + + // Relaxed load because any write of 0 that we can observe + // leaves the field in a permanently zero state (so a + // "stale" read of 0 is fine), and any other value is + // confirmed via the CAS below. + let mut n = inner.strong.load(Relaxed); + + loop { + if n == 0 { + return None; + } + + // See comments in `Arc::clone` for why we do this (for `mem::forget`). + if n > MAX_REFCOUNT { + unsafe { + abort(); + } + } + + // Relaxed is valid for the same reason it is on Arc's Clone impl + match inner.strong.compare_exchange_weak(n, n + 1, Relaxed, Relaxed) { + Ok(_) => return Some(Arc { ptr: self.ptr }), + Err(old) => n = old, + } + } + } + + #[inline] + fn inner(&self) -> &ArcInner { + // See comments above for why this is "safe" + unsafe { &**self.ptr } + } +} + +#[stable(feature = "arc_weak", since = "1.4.0")] +impl Clone for Weak { + /// Makes a clone of the `Weak` pointer. + /// + /// This creates another pointer to the same inner value, increasing the + /// weak reference count. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let weak_five = Arc::downgrade(&Arc::new(5)); + /// + /// weak_five.clone(); + /// ``` + #[inline] + fn clone(&self) -> Weak { + // See comments in Arc::clone() for why this is relaxed. This can use a + // fetch_add (ignoring the lock) because the weak count is only locked + // where are *no other* weak pointers in existence. (So we can't be + // running this code in that case). + let old_size = self.inner().weak.fetch_add(1, Relaxed); + + // See comments in Arc::clone() for why we do this (for mem::forget). + if old_size > MAX_REFCOUNT { + unsafe { + abort(); + } + } + + return Weak { ptr: self.ptr }; + } +} + +#[stable(feature = "downgraded_weak", since = "1.10.0")] +impl Default for Weak { + /// Constructs a new `Weak`, without an accompanying instance of `T`. + /// + /// This allocates memory for `T`, but does not initialize it. Calling + /// [`upgrade`][upgrade] on the return value always gives + /// [`None`][option]. + /// + /// [upgrade]: struct.Weak.html#method.upgrade + /// [option]: ../../std/option/enum.Option.html + /// + /// # Examples + /// + /// ``` + /// use std::sync::Weak; + /// + /// let empty: Weak = Default::default(); + /// assert!(empty.upgrade().is_none()); + /// ``` + fn default() -> Weak { + Weak::new() + } +} + +#[stable(feature = "arc_weak", since = "1.4.0")] +impl Drop for Weak { + /// Drops the `Weak` pointer. + /// + /// This will decrement the weak reference count. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// struct Foo; + /// + /// impl Drop for Foo { + /// fn drop(&mut self) { + /// println!("dropped!"); + /// } + /// } + /// + /// let foo = Arc::new(Foo); + /// let weak_foo = Arc::downgrade(&foo); + /// let other_weak_foo = weak_foo.clone(); + /// + /// drop(weak_foo); // Doesn't print anything + /// drop(foo); // Prints "dropped!" + /// + /// assert!(other_weak_foo.upgrade().is_none()); + /// ``` + fn drop(&mut self) { + let ptr = *self.ptr; + + // If we find out that we were the last weak pointer, then its time to + // deallocate the data entirely. See the discussion in Arc::drop() about + // the memory orderings + // + // It's not necessary to check for the locked state here, because the + // weak count can only be locked if there was precisely one weak ref, + // meaning that drop could only subsequently run ON that remaining weak + // ref, which can only happen after the lock is released. + if self.inner().weak.fetch_sub(1, Release) == 1 { + atomic::fence(Acquire); + unsafe { deallocate(ptr as *mut u8, size_of_val(&*ptr), align_of_val(&*ptr)) } + } + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl PartialEq for Arc { + /// Equality for two `Arc`s. + /// + /// Two `Arc`s are equal if their inner values are equal. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five == Arc::new(5)); + /// ``` + fn eq(&self, other: &Arc) -> bool { + *(*self) == *(*other) + } + + /// Inequality for two `Arc`s. + /// + /// Two `Arc`s are unequal if their inner values are unequal. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five != Arc::new(6)); + /// ``` + fn ne(&self, other: &Arc) -> bool { + *(*self) != *(*other) + } +} +#[stable(feature = "rust1", since = "1.0.0")] +impl PartialOrd for Arc { + /// Partial comparison for two `Arc`s. + /// + /// The two are compared by calling `partial_cmp()` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use std::cmp::Ordering; + /// + /// let five = Arc::new(5); + /// + /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6))); + /// ``` + fn partial_cmp(&self, other: &Arc) -> Option { + (**self).partial_cmp(&**other) + } + + /// Less-than comparison for two `Arc`s. + /// + /// The two are compared by calling `<` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five < Arc::new(6)); + /// ``` + fn lt(&self, other: &Arc) -> bool { + *(*self) < *(*other) + } + + /// 'Less than or equal to' comparison for two `Arc`s. + /// + /// The two are compared by calling `<=` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five <= Arc::new(5)); + /// ``` + fn le(&self, other: &Arc) -> bool { + *(*self) <= *(*other) + } + + /// Greater-than comparison for two `Arc`s. + /// + /// The two are compared by calling `>` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five > Arc::new(4)); + /// ``` + fn gt(&self, other: &Arc) -> bool { + *(*self) > *(*other) + } + + /// 'Greater than or equal to' comparison for two `Arc`s. + /// + /// The two are compared by calling `>=` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// + /// assert!(five >= Arc::new(5)); + /// ``` + fn ge(&self, other: &Arc) -> bool { + *(*self) >= *(*other) + } +} +#[stable(feature = "rust1", since = "1.0.0")] +impl Ord for Arc { + /// Comparison for two `Arc`s. + /// + /// The two are compared by calling `cmp()` on their inner values. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// use std::cmp::Ordering; + /// + /// let five = Arc::new(5); + /// + /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6))); + /// ``` + fn cmp(&self, other: &Arc) -> Ordering { + (**self).cmp(&**other) + } +} +#[stable(feature = "rust1", since = "1.0.0")] +impl Eq for Arc {} + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Display for Arc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&**self, f) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Debug for Arc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl fmt::Pointer for Arc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Pointer::fmt(&*self.ptr, f) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Default for Arc { + /// Creates a new `Arc`, with the `Default` value for `T`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let x: Arc = Default::default(); + /// assert_eq!(*x, 0); + /// ``` + fn default() -> Arc { + Arc::new(Default::default()) + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl Hash for Arc { + fn hash(&self, state: &mut H) { + (**self).hash(state) + } +} + +#[stable(feature = "from_for_ptrs", since = "1.6.0")] +impl From for Arc { + fn from(t: T) -> Self { + Arc::new(t) + } +} + +#[cfg(test)] +mod tests { + use std::clone::Clone; + use std::sync::mpsc::channel; + use std::mem::drop; + use std::ops::Drop; + use std::option::Option; + use std::option::Option::{None, Some}; + use std::sync::atomic; + use std::sync::atomic::Ordering::{Acquire, SeqCst}; + use std::thread; + use std::vec::Vec; + use super::{Arc, Weak}; + use std::sync::Mutex; + use std::convert::From; + + struct Canary(*mut atomic::AtomicUsize); + + impl Drop for Canary { + fn drop(&mut self) { + unsafe { + match *self { + Canary(c) => { + (*c).fetch_add(1, SeqCst); + } + } + } + } + } + + #[test] + #[cfg_attr(target_os = "emscripten", ignore)] + fn manually_share_arc() { + let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let arc_v = Arc::new(v); + + let (tx, rx) = channel(); + + let _t = thread::spawn(move || { + let arc_v: Arc> = rx.recv().unwrap(); + assert_eq!((*arc_v)[3], 4); + }); + + tx.send(arc_v.clone()).unwrap(); + + assert_eq!((*arc_v)[2], 3); + assert_eq!((*arc_v)[4], 5); + } + + #[test] + fn test_arc_get_mut() { + let mut x = Arc::new(3); + *Arc::get_mut(&mut x).unwrap() = 4; + assert_eq!(*x, 4); + let y = x.clone(); + assert!(Arc::get_mut(&mut x).is_none()); + drop(y); + assert!(Arc::get_mut(&mut x).is_some()); + let _w = Arc::downgrade(&x); + assert!(Arc::get_mut(&mut x).is_none()); + } + + #[test] + fn try_unwrap() { + let x = Arc::new(3); + assert_eq!(Arc::try_unwrap(x), Ok(3)); + let x = Arc::new(4); + let _y = x.clone(); + assert_eq!(Arc::try_unwrap(x), Err(Arc::new(4))); + let x = Arc::new(5); + let _w = Arc::downgrade(&x); + assert_eq!(Arc::try_unwrap(x), Ok(5)); + } + + #[test] + fn into_from_raw() { + let x = Arc::new(box "hello"); + let y = x.clone(); + + let x_ptr = Arc::into_raw(x); + drop(y); + unsafe { + assert_eq!(**x_ptr, "hello"); + + let x = Arc::from_raw(x_ptr); + assert_eq!(**x, "hello"); + + assert_eq!(Arc::try_unwrap(x).map(|x| *x), Ok("hello")); + } + } + + #[test] + fn test_cowarc_clone_make_mut() { + let mut cow0 = Arc::new(75); + let mut cow1 = cow0.clone(); + let mut cow2 = cow1.clone(); + + assert!(75 == *Arc::make_mut(&mut cow0)); + assert!(75 == *Arc::make_mut(&mut cow1)); + assert!(75 == *Arc::make_mut(&mut cow2)); + + *Arc::make_mut(&mut cow0) += 1; + *Arc::make_mut(&mut cow1) += 2; + *Arc::make_mut(&mut cow2) += 3; + + assert!(76 == *cow0); + assert!(77 == *cow1); + assert!(78 == *cow2); + + // none should point to the same backing memory + assert!(*cow0 != *cow1); + assert!(*cow0 != *cow2); + assert!(*cow1 != *cow2); + } + + #[test] + fn test_cowarc_clone_unique2() { + let mut cow0 = Arc::new(75); + let cow1 = cow0.clone(); + let cow2 = cow1.clone(); + + assert!(75 == *cow0); + assert!(75 == *cow1); + assert!(75 == *cow2); + + *Arc::make_mut(&mut cow0) += 1; + assert!(76 == *cow0); + assert!(75 == *cow1); + assert!(75 == *cow2); + + // cow1 and cow2 should share the same contents + // cow0 should have a unique reference + assert!(*cow0 != *cow1); + assert!(*cow0 != *cow2); + assert!(*cow1 == *cow2); + } + + #[test] + fn test_cowarc_clone_weak() { + let mut cow0 = Arc::new(75); + let cow1_weak = Arc::downgrade(&cow0); + + assert!(75 == *cow0); + assert!(75 == *cow1_weak.upgrade().unwrap()); + + *Arc::make_mut(&mut cow0) += 1; + + assert!(76 == *cow0); + assert!(cow1_weak.upgrade().is_none()); + } + + #[test] + fn test_live() { + let x = Arc::new(5); + let y = Arc::downgrade(&x); + assert!(y.upgrade().is_some()); + } + + #[test] + fn test_dead() { + let x = Arc::new(5); + let y = Arc::downgrade(&x); + drop(x); + assert!(y.upgrade().is_none()); + } + + #[test] + fn weak_self_cyclic() { + struct Cycle { + x: Mutex>>, + } + + let a = Arc::new(Cycle { x: Mutex::new(None) }); + let b = Arc::downgrade(&a.clone()); + *a.x.lock().unwrap() = Some(b); + + // hopefully we don't double-free (or leak)... + } + + #[test] + fn drop_arc() { + let mut canary = atomic::AtomicUsize::new(0); + let x = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize)); + drop(x); + assert!(canary.load(Acquire) == 1); + } + + #[test] + fn drop_arc_weak() { + let mut canary = atomic::AtomicUsize::new(0); + let arc = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize)); + let arc_weak = Arc::downgrade(&arc); + assert!(canary.load(Acquire) == 0); + drop(arc); + assert!(canary.load(Acquire) == 1); + drop(arc_weak); + } + + #[test] + fn test_strong_count() { + let a = Arc::new(0); + assert!(Arc::strong_count(&a) == 1); + let w = Arc::downgrade(&a); + assert!(Arc::strong_count(&a) == 1); + let b = w.upgrade().expect(""); + assert!(Arc::strong_count(&b) == 2); + assert!(Arc::strong_count(&a) == 2); + drop(w); + drop(a); + assert!(Arc::strong_count(&b) == 1); + let c = b.clone(); + assert!(Arc::strong_count(&b) == 2); + assert!(Arc::strong_count(&c) == 2); + } + + #[test] + fn test_weak_count() { + let a = Arc::new(0); + assert!(Arc::strong_count(&a) == 1); + assert!(Arc::weak_count(&a) == 0); + let w = Arc::downgrade(&a); + assert!(Arc::strong_count(&a) == 1); + assert!(Arc::weak_count(&a) == 1); + let x = w.clone(); + assert!(Arc::weak_count(&a) == 2); + drop(w); + drop(x); + assert!(Arc::strong_count(&a) == 1); + assert!(Arc::weak_count(&a) == 0); + let c = a.clone(); + assert!(Arc::strong_count(&a) == 2); + assert!(Arc::weak_count(&a) == 0); + let d = Arc::downgrade(&c); + assert!(Arc::weak_count(&c) == 1); + assert!(Arc::strong_count(&c) == 2); + + drop(a); + drop(c); + drop(d); + } + + #[test] + fn show_arc() { + let a = Arc::new(5); + assert_eq!(format!("{:?}", a), "5"); + } + + // Make sure deriving works with Arc + #[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Debug, Default)] + struct Foo { + inner: Arc, + } + + #[test] + fn test_unsized() { + let x: Arc<[i32]> = Arc::new([1, 2, 3]); + assert_eq!(format!("{:?}", x), "[1, 2, 3]"); + let y = Arc::downgrade(&x.clone()); + drop(x); + assert!(y.upgrade().is_none()); + } + + #[test] + fn test_from_owned() { + let foo = 123; + let foo_arc = Arc::from(foo); + assert!(123 == *foo_arc); + } + + #[test] + fn test_new_weak() { + let foo: Weak = Weak::new(); + assert!(foo.upgrade().is_none()); + } + + #[test] + fn test_ptr_eq() { + let five = Arc::new(5); + let same_five = five.clone(); + let other_five = Arc::new(5); + + assert!(Arc::ptr_eq(&five, &same_five)); + assert!(!Arc::ptr_eq(&five, &other_five)); + } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl borrow::Borrow for Arc { + fn borrow(&self) -> &T { + &**self + } +} + +#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] +impl AsRef for Arc { + fn as_ref(&self) -> &T { + &**self + } +} From 6d8fc600b88dc2f393d7ff3e57d140891343c988 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sun, 30 Apr 2017 11:39:47 -0700 Subject: [PATCH 2/6] Strip down StyleArc to what we need. We remove most of the doc comments to minimize the number of lines of forked code. MozReview-Commit-ID: LehEisKxkJW --- components/style/lib.rs | 2 + components/style/stylearc.rs | 1366 ++++------------------------------ 2 files changed, 141 insertions(+), 1227 deletions(-) diff --git a/components/style/lib.rs b/components/style/lib.rs index 7479279418d..99ded2b9fe8 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -70,6 +70,7 @@ extern crate pdqsort; #[cfg(feature = "gecko")] extern crate precomputed_hash; extern crate rayon; extern crate selectors; +#[cfg(feature = "servo")] extern crate serde; #[cfg(feature = "servo")] #[macro_use] extern crate serde_derive; #[cfg(feature = "servo")] #[macro_use] extern crate servo_atoms; #[cfg(feature = "servo")] extern crate servo_config; @@ -118,6 +119,7 @@ pub mod sequential; pub mod sink; pub mod str; pub mod style_adjuster; +pub mod stylearc; pub mod stylesheet_set; pub mod stylesheets; pub mod supports; diff --git a/components/style/stylearc.rs b/components/style/stylearc.rs index 8c0dd6b3795..01885dc481b 100644 --- a/components/style/stylearc.rs +++ b/components/style/stylearc.rs @@ -8,33 +8,51 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![stable(feature = "rust1", since = "1.0.0")] - -//! Thread-safe reference-counting pointers. +//! Fork of Arc for the style system. This has the following advantages over std::Arc: +//! * We don't waste storage on the weak reference count. +//! * We don't do extra RMU operations to handle the possibility of weak references. +//! * We can experiment with arena allocation (todo). +//! * We can add methods to support our custom use cases [1]. //! -//! See the [`Arc`][arc] documentation for more details. -//! -//! [arc]: struct.Arc.html +//! [1] https://bugzilla.mozilla.org/show_bug.cgi?id=1360883 -use boxed::Box; +// The semantics of Arc are alread documented in the Rust docs, so we don't +// duplicate those here. +#![allow(missing_docs)] -use core::sync::atomic; -use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; -use core::borrow; -use core::fmt; -use core::cmp::Ordering; -use core::mem::{align_of_val, size_of_val}; -use core::intrinsics::abort; -use core::mem; -use core::mem::uninitialized; -use core::ops::Deref; -use core::ops::CoerceUnsized; -use core::ptr::{self, Shared}; -use core::marker::Unsize; -use core::hash::{Hash, Hasher}; -use core::{isize, usize}; -use core::convert::From; -use heap::deallocate; +#[cfg(feature = "servo")] +use heapsize::HeapSizeOf; +#[cfg(feature = "servo")] +use serde::{Deserialize, Serialize}; +use std::{isize, usize}; +use std::borrow; +use std::cmp::Ordering; +use std::convert::From; +use std::fmt; +use std::hash::{Hash, Hasher}; +use std::mem; +use std::ops::Deref; +use std::sync::atomic; +use std::sync::atomic::Ordering::{Acquire, Relaxed, Release}; + +// Private macro to get the offset of a struct field in bytes from the address of the struct. +macro_rules! offset_of { + ($container:path, $field:ident) => {{ + // Make sure the field actually exists. This line ensures that a compile-time error is + // generated if $field is accessed through a Deref impl. + let $container { $field: _, .. }; + + // Create an (invalid) instance of the container and calculate the offset to its + // field. Using a null pointer might be UB if `&(*(0 as *const T)).field` is interpreted to + // be nullptr deref. + let invalid: $container = ::std::mem::uninitialized(); + let offset = &invalid.$field as *const _ as usize - &invalid as *const _ as usize; + + // Do not run destructors on the made up invalid instance. + ::std::mem::forget(invalid); + offset as isize + }}; +} /// A soft limit on the amount of references that may be made to an `Arc`. /// @@ -42,169 +60,22 @@ use heap::deallocate; /// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references. const MAX_REFCOUNT: usize = (isize::MAX) as usize; -/// A thread-safe reference-counting pointer. -/// -/// The type `Arc` provides shared ownership of a value of type `T`, -/// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces -/// a new pointer to the same value in the heap. When the last `Arc` -/// pointer to a given value is destroyed, the pointed-to value is -/// also destroyed. -/// -/// Shared references in Rust disallow mutation by default, and `Arc` is no -/// exception. If you need to mutate through an `Arc`, use [`Mutex`][mutex], -/// [`RwLock`][rwlock], or one of the [`Atomic`][atomic] types. -/// -/// `Arc` uses atomic operations for reference counting, so `Arc`s can be -/// sent between threads. In other words, `Arc` implements [`Send`] -/// as long as `T` implements [`Send`] and [`Sync`][sync]. The disadvantage is -/// that atomic operations are more expensive than ordinary memory accesses. -/// If you are not sharing reference-counted values between threads, consider -/// using [`rc::Rc`][`Rc`] for lower overhead. [`Rc`] is a safe default, because -/// the compiler will catch any attempt to send an [`Rc`] between threads. -/// However, a library might choose `Arc` in order to give library consumers -/// more flexibility. -/// -/// The [`downgrade`][downgrade] method can be used to create a non-owning -/// [`Weak`][weak] pointer. A [`Weak`][weak] pointer can be [`upgrade`][upgrade]d -/// to an `Arc`, but this will return [`None`] if the value has already been -/// dropped. -/// -/// A cycle between `Arc` pointers will never be deallocated. For this reason, -/// [`Weak`][weak] is used to break cycles. For example, a tree could have -/// strong `Arc` pointers from parent nodes to children, and [`Weak`][weak] -/// pointers from children back to their parents. -/// -/// `Arc` automatically dereferences to `T` (via the [`Deref`][deref] trait), -/// so you can call `T`'s methods on a value of type `Arc`. To avoid name -/// clashes with `T`'s methods, the methods of `Arc` itself are [associated -/// functions][assoc], called using function-like syntax: -/// -/// ``` -/// use std::sync::Arc; -/// let my_arc = Arc::new(()); -/// -/// Arc::downgrade(&my_arc); -/// ``` -/// -/// [`Weak`][weak] does not auto-dereference to `T`, because the value may have -/// already been destroyed. -/// -/// [arc]: struct.Arc.html -/// [weak]: struct.Weak.html -/// [`Rc`]: ../../std/rc/struct.Rc.html -/// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone -/// [mutex]: ../../std/sync/struct.Mutex.html -/// [rwlock]: ../../std/sync/struct.RwLock.html -/// [atomic]: ../../std/sync/atomic/ -/// [`Send`]: ../../std/marker/trait.Send.html -/// [sync]: ../../std/marker/trait.Sync.html -/// [deref]: ../../std/ops/trait.Deref.html -/// [downgrade]: struct.Arc.html#method.downgrade -/// [upgrade]: struct.Weak.html#method.upgrade -/// [`None`]: ../../std/option/enum.Option.html#variant.None -/// [assoc]: ../../book/method-syntax.html#associated-functions -/// -/// # Examples -/// -/// Sharing some immutable data between threads: -/// -// Note that we **do not** run these tests here. The windows builders get super -// unhappy if a thread outlives the main thread and then exits at the same time -// (something deadlocks) so we just avoid this entirely by not running these -// tests. -/// ```no_run -/// use std::sync::Arc; -/// use std::thread; -/// -/// let five = Arc::new(5); -/// -/// for _ in 0..10 { -/// let five = five.clone(); -/// -/// thread::spawn(move || { -/// println!("{:?}", five); -/// }); -/// } -/// ``` -/// -/// Sharing a mutable [`AtomicUsize`]: -/// -/// [`AtomicUsize`]: ../../std/sync/atomic/struct.AtomicUsize.html -/// -/// ```no_run -/// use std::sync::Arc; -/// use std::sync::atomic::{AtomicUsize, Ordering}; -/// use std::thread; -/// -/// let val = Arc::new(AtomicUsize::new(5)); -/// -/// for _ in 0..10 { -/// let val = val.clone(); -/// -/// thread::spawn(move || { -/// let v = val.fetch_add(1, Ordering::SeqCst); -/// println!("{:?}", v); -/// }); -/// } -/// ``` -/// -/// See the [`rc` documentation][rc_examples] for more examples of reference -/// counting in general. -/// -/// [rc_examples]: ../../std/rc/#examples -#[stable(feature = "rust1", since = "1.0.0")] pub struct Arc { - ptr: Shared>, + // FIXME(bholley): When NonZero/Shared/Unique are stabilized, we should use + // Shared here to get the NonZero optimization. Gankro is working on this. + // + // If we need a compact Option> beforehand, we can make a helper + // class that wraps the result of Arc::into_raw. + // + // https://github.com/rust-lang/rust/issues/27730 + ptr: *mut ArcInner, } -#[stable(feature = "rust1", since = "1.0.0")] unsafe impl Send for Arc {} -#[stable(feature = "rust1", since = "1.0.0")] unsafe impl Sync for Arc {} -#[unstable(feature = "coerce_unsized", issue = "27732")] -impl, U: ?Sized> CoerceUnsized> for Arc {} - -/// A weak version of [`Arc`][arc]. -/// -/// `Weak` pointers do not count towards determining if the inner value -/// should be dropped. -/// -/// The typical way to obtain a `Weak` pointer is to call -/// [`Arc::downgrade`][downgrade]. -/// -/// See the [`Arc`][arc] documentation for more details. -/// -/// [arc]: struct.Arc.html -/// [downgrade]: struct.Arc.html#method.downgrade -#[stable(feature = "arc_weak", since = "1.4.0")] -pub struct Weak { - ptr: Shared>, -} - -#[stable(feature = "arc_weak", since = "1.4.0")] -unsafe impl Send for Weak {} -#[stable(feature = "arc_weak", since = "1.4.0")] -unsafe impl Sync for Weak {} - -#[unstable(feature = "coerce_unsized", issue = "27732")] -impl, U: ?Sized> CoerceUnsized> for Weak {} - -#[stable(feature = "arc_weak", since = "1.4.0")] -impl fmt::Debug for Weak { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "(Weak)") - } -} - struct ArcInner { - strong: atomic::AtomicUsize, - - // the value usize::MAX acts as a sentinel for temporarily "locking" the - // ability to upgrade weak pointers or downgrade strong ones; this is used - // to avoid races in `make_mut` and `get_mut`. - weak: atomic::AtomicUsize, - + count: atomic::AtomicUsize, data: T, } @@ -212,228 +83,32 @@ unsafe impl Send for ArcInner {} unsafe impl Sync for ArcInner {} impl Arc { - /// Constructs a new `Arc`. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// ``` #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(data: T) -> Arc { - // Start the weak pointer count as 1 which is the weak pointer that's - // held by all the strong pointers (kinda), see std/rc.rs for more info - let x: Box<_> = box ArcInner { - strong: atomic::AtomicUsize::new(1), - weak: atomic::AtomicUsize::new(1), + pub fn new(data: T) -> Self { + let x = Box::new(ArcInner { + count: atomic::AtomicUsize::new(1), data: data, - }; - Arc { ptr: unsafe { Shared::new(Box::into_raw(x)) } } + }); + Arc { ptr: Box::into_raw(x) } } - /// Returns the contained value, if the `Arc` has exactly one strong reference. - /// - /// Otherwise, an [`Err`][result] is returned with the same `Arc` that was - /// passed in. - /// - /// This will succeed even if there are outstanding weak references. - /// - /// [result]: ../../std/result/enum.Result.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let x = Arc::new(3); - /// assert_eq!(Arc::try_unwrap(x), Ok(3)); - /// - /// let x = Arc::new(4); - /// let _y = x.clone(); - /// assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4); - /// ``` - #[inline] - #[stable(feature = "arc_unique", since = "1.4.0")] - pub fn try_unwrap(this: Self) -> Result { - // See `drop` for why all these atomics are like this - if this.inner().strong.compare_exchange(1, 0, Release, Relaxed).is_err() { - return Err(this); - } - - atomic::fence(Acquire); - - unsafe { - let ptr = *this.ptr; - let elem = ptr::read(&(*ptr).data); - - // Make a weak pointer to clean up the implicit strong-weak reference - let _weak = Weak { ptr: this.ptr }; - mem::forget(this); - - Ok(elem) - } - } - - /// Consumes the `Arc`, returning the wrapped pointer. - /// - /// To avoid a memory leak the pointer must be converted back to an `Arc` using - /// [`Arc::from_raw`][from_raw]. - /// - /// [from_raw]: struct.Arc.html#method.from_raw - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let x = Arc::new(10); - /// let x_ptr = Arc::into_raw(x); - /// assert_eq!(unsafe { *x_ptr }, 10); - /// ``` - #[stable(feature = "rc_raw", since = "1.17.0")] 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); ptr } - /// Constructs an `Arc` from a raw pointer. - /// - /// The raw pointer must have been previously returned by a call to a - /// [`Arc::into_raw`][into_raw]. - /// - /// This function is unsafe because improper use may lead to memory problems. For example, a - /// double-free may occur if the function is called twice on the same raw pointer. - /// - /// [into_raw]: struct.Arc.html#method.into_raw - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let x = Arc::new(10); - /// let x_ptr = Arc::into_raw(x); - /// - /// unsafe { - /// // Convert back to an `Arc` to prevent leak. - /// let x = Arc::from_raw(x_ptr); - /// assert_eq!(*x, 10); - /// - /// // Further calls to `Arc::from_raw(x_ptr)` would be memory unsafe. - /// } - /// - /// // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! - /// ``` - #[stable(feature = "rc_raw", since = "1.17.0")] 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. + // 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)); Arc { - ptr: Shared::new(ptr as *const _), + ptr: ptr as *mut ArcInner, } } } impl Arc { - /// Creates a new [`Weak`][weak] pointer to this value. - /// - /// [weak]: struct.Weak.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// let weak_five = Arc::downgrade(&five); - /// ``` - #[stable(feature = "arc_weak", since = "1.4.0")] - pub fn downgrade(this: &Self) -> Weak { - // This Relaxed is OK because we're checking the value in the CAS - // below. - let mut cur = this.inner().weak.load(Relaxed); - - loop { - // check if the weak counter is currently "locked"; if so, spin. - if cur == usize::MAX { - cur = this.inner().weak.load(Relaxed); - continue; - } - - // NOTE: this code currently ignores the possibility of overflow - // into usize::MAX; in general both Rc and Arc need to be adjusted - // to deal with overflow. - - // Unlike with Clone(), we need this to be an Acquire read to - // synchronize with the write coming from `is_unique`, so that the - // events prior to that write happen before this read. - match this.inner().weak.compare_exchange_weak(cur, cur + 1, Acquire, Relaxed) { - Ok(_) => return Weak { ptr: this.ptr }, - Err(old) => cur = old, - } - } - } - - /// Gets the number of [`Weak`][weak] pointers to this value. - /// - /// [weak]: struct.Weak.html - /// - /// # Safety - /// - /// This method by itself is safe, but using it correctly requires extra care. - /// Another thread can change the weak count at any time, - /// including potentially between calling this method and acting on the result. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// let _weak_five = Arc::downgrade(&five); - /// - /// // This assertion is deterministic because we haven't shared - /// // the `Arc` or `Weak` between threads. - /// assert_eq!(1, Arc::weak_count(&five)); - /// ``` - #[inline] - #[stable(feature = "arc_counts", since = "1.15.0")] - pub fn weak_count(this: &Self) -> usize { - this.inner().weak.load(SeqCst) - 1 - } - - /// Gets the number of strong (`Arc`) pointers to this value. - /// - /// # Safety - /// - /// This method by itself is safe, but using it correctly requires extra care. - /// Another thread can change the strong count at any time, - /// including potentially between calling this method and acting on the result. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// let _also_five = five.clone(); - /// - /// // This assertion is deterministic because we haven't shared - /// // the `Arc` between threads. - /// assert_eq!(2, Arc::strong_count(&five)); - /// ``` - #[inline] - #[stable(feature = "arc_counts", since = "1.15.0")] - pub fn strong_count(this: &Self) -> usize { - this.inner().strong.load(SeqCst) - } - #[inline] fn inner(&self) -> &ArcInner { // This unsafety is ok because while this arc is alive we're guaranteed @@ -441,66 +116,25 @@ impl Arc { // `ArcInner` structure itself is `Sync` because the inner data is // `Sync` as well, so we're ok loaning out an immutable pointer to these // contents. - unsafe { &**self.ptr } + unsafe { &*self.ptr } } - // Non-inlined part of `drop`. + // Non-inlined part of `drop`. Just invokes the destructor. #[inline(never)] unsafe fn drop_slow(&mut self) { - let ptr = self.ptr.as_mut_ptr(); - - // Destroy the data at this time, even though we may not free the box - // allocation itself (there may still be weak pointers lying around). - ptr::drop_in_place(&mut (*ptr).data); - - if self.inner().weak.fetch_sub(1, Release) == 1 { - atomic::fence(Acquire); - deallocate(ptr as *mut u8, size_of_val(&*ptr), align_of_val(&*ptr)) - } + let _ = Box::from_raw(self.ptr); } + #[inline] - #[stable(feature = "ptr_eq", since = "1.17.0")] - /// Returns true if the two `Arc`s point to the same value (not - /// just values that compare as equal). - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// let same_five = five.clone(); - /// let other_five = Arc::new(5); - /// - /// assert!(Arc::ptr_eq(&five, &same_five)); - /// assert!(!Arc::ptr_eq(&five, &other_five)); - /// ``` pub fn ptr_eq(this: &Self, other: &Self) -> bool { - let this_ptr: *const ArcInner = *this.ptr; - let other_ptr: *const ArcInner = *other.ptr; - this_ptr == other_ptr + this.ptr == other.ptr } } -#[stable(feature = "rust1", since = "1.0.0")] impl Clone for Arc { - /// Makes a clone of the `Arc` pointer. - /// - /// This creates another pointer to the same inner value, increasing the - /// strong reference count. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// five.clone(); - /// ``` #[inline] - fn clone(&self) -> Arc { + fn clone(&self) -> Self { // Using a relaxed ordering is alright here, as knowledge of the // original reference prevents other threads from erroneously deleting // the object. @@ -512,7 +146,7 @@ impl Clone for Arc { // another must already provide any required synchronization. // // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) - let old_size = self.inner().strong.fetch_add(1, Relaxed); + let old_size = self.inner().count.fetch_add(1, Relaxed); // However we need to guard against massive refcounts in case someone // is `mem::forget`ing Arcs. If we don't do this the count can overflow @@ -524,16 +158,19 @@ impl Clone for Arc { // We abort because such a program is incredibly degenerate, and we // don't care to support it. if old_size > MAX_REFCOUNT { - unsafe { - abort(); - } + // Note: std::process::abort is stable in 1.17, which we don't yet + // require for Gecko. Panic is good enough in practice here (it will + // trigger an abort at least in Gecko, and this case is degenerate + // enough that Servo shouldn't have code that triggers it). + // + // We should fix this when we require 1.17. + panic!(); } Arc { ptr: self.ptr } } } -#[stable(feature = "rust1", since = "1.0.0")] impl Deref for Arc { type Target = T; @@ -544,207 +181,67 @@ impl Deref for Arc { } impl Arc { - /// Makes a mutable reference into the given `Arc`. - /// - /// If there are other `Arc` or [`Weak`][weak] pointers to the same value, - /// then `make_mut` will invoke [`clone`][clone] on the inner value to - /// ensure unique ownership. This is also referred to as clone-on-write. - /// - /// See also [`get_mut`][get_mut], which will fail rather than cloning. - /// - /// [weak]: struct.Weak.html - /// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone - /// [get_mut]: struct.Arc.html#method.get_mut - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let mut data = Arc::new(5); - /// - /// *Arc::make_mut(&mut data) += 1; // Won't clone anything - /// let mut other_data = data.clone(); // Won't clone inner data - /// *Arc::make_mut(&mut data) += 1; // Clones inner data - /// *Arc::make_mut(&mut data) += 1; // Won't clone anything - /// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything - /// - /// // Now `data` and `other_data` point to different values. - /// assert_eq!(*data, 8); - /// assert_eq!(*other_data, 12); - /// ``` #[inline] - #[stable(feature = "arc_unique", since = "1.4.0")] pub fn make_mut(this: &mut Self) -> &mut T { - // Note that we hold both a strong reference and a weak reference. - // Thus, releasing our strong reference only will not, by itself, cause - // the memory to be deallocated. - // - // Use Acquire to ensure that we see any writes to `weak` that happen - // before release writes (i.e., decrements) to `strong`. Since we hold a - // weak count, there's no chance the ArcInner itself could be - // deallocated. - if this.inner().strong.compare_exchange(1, 0, Acquire, Relaxed).is_err() { - // Another strong pointer exists; clone + if !this.is_unique() { + // Another pointer exists; clone *this = Arc::new((**this).clone()); - } else if this.inner().weak.load(Relaxed) != 1 { - // Relaxed suffices in the above because this is fundamentally an - // optimization: we are always racing with weak pointers being - // dropped. Worst case, we end up allocated a new Arc unnecessarily. - - // We removed the last strong ref, but there are additional weak - // refs remaining. We'll move the contents to a new Arc, and - // invalidate the other weak refs. - - // Note that it is not possible for the read of `weak` to yield - // usize::MAX (i.e., locked), since the weak count can only be - // locked by a thread with a strong reference. - - // Materialize our own implicit weak pointer, so that it can clean - // up the ArcInner as needed. - let weak = Weak { ptr: this.ptr }; - - // mark the data itself as already deallocated - unsafe { - // there is no data race in the implicit write caused by `read` - // here (due to zeroing) because data is no longer accessed by - // other threads (due to there being no more strong refs at this - // point). - let mut swap = Arc::new(ptr::read(&(**weak.ptr).data)); - mem::swap(this, &mut swap); - mem::forget(swap); - } - } else { - // We were the sole reference of either kind; bump back up the - // strong ref count. - this.inner().strong.store(1, Release); } - // As with `get_mut()`, the unsafety is ok because our reference was - // either unique to begin with, or became one upon cloning the contents. unsafe { - let inner = &mut *this.ptr.as_mut_ptr(); - &mut inner.data - } - } -} - -impl Arc { - /// Returns a mutable reference to the inner value, if there are - /// no other `Arc` or [`Weak`][weak] pointers to the same value. - /// - /// Returns [`None`][option] otherwise, because it is not safe to - /// mutate a shared value. - /// - /// See also [`make_mut`][make_mut], which will [`clone`][clone] - /// the inner value when it's shared. - /// - /// [weak]: struct.Weak.html - /// [option]: ../../std/option/enum.Option.html - /// [make_mut]: struct.Arc.html#method.make_mut - /// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let mut x = Arc::new(3); - /// *Arc::get_mut(&mut x).unwrap() = 4; - /// assert_eq!(*x, 4); - /// - /// let _y = x.clone(); - /// assert!(Arc::get_mut(&mut x).is_none()); - /// ``` - #[inline] - #[stable(feature = "arc_unique", since = "1.4.0")] - pub fn get_mut(this: &mut Self) -> Option<&mut T> { - if this.is_unique() { // This unsafety is ok because we're guaranteed that the pointer // returned is the *only* pointer that will ever be returned to T. Our // reference count is guaranteed to be 1 at this point, and we required // the Arc itself to be `mut`, so we're returning the only possible // reference to the inner data. + &mut (*this.ptr).data + } + } +} + +impl Arc { + #[inline] + pub fn get_mut(this: &mut Self) -> Option<&mut T> { + if this.is_unique() { unsafe { - let inner = &mut *this.ptr.as_mut_ptr(); - Some(&mut inner.data) + // See make_mut() for documentation of the threadsafety here. + Some(&mut (*this.ptr).data) } } else { None } } - /// Determine whether this is the unique reference (including weak refs) to - /// the underlying data. - /// - /// Note that this requires locking the weak ref count. - fn is_unique(&mut self) -> bool { - // lock the weak pointer count if we appear to be the sole weak pointer - // holder. + #[inline] + fn is_unique(&self) -> bool { + // We can use Relaxed here, but the justification is a bit subtle. // - // The acquire label here ensures a happens-before relationship with any - // writes to `strong` prior to decrements of the `weak` count (via drop, - // which uses Release). - if self.inner().weak.compare_exchange(1, usize::MAX, Acquire, Relaxed).is_ok() { - // Due to the previous acquire read, this will observe any writes to - // `strong` that were due to upgrading weak pointers; only strong - // clones remain, which require that the strong count is > 1 anyway. - let unique = self.inner().strong.load(Relaxed) == 1; - - // The release write here synchronizes with a read in `downgrade`, - // effectively preventing the above read of `strong` from happening - // after the write. - self.inner().weak.store(1, Release); // release the lock - unique - } else { - false - } + // The reason to use Acquire would be to synchronize with other threads + // that are modifying the refcount with Release, i.e. to ensure that + // their writes to memory guarded by this refcount are flushed. However, + // we know that threads only modify the contents of the Arc when they + // observe the refcount to be 1, and no other thread could observe that + // because we're holding one strong reference here. + self.inner().count.load(Relaxed) == 1 } } -#[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { - /// Drops the `Arc`. - /// - /// This will decrement the strong reference count. If the strong reference - /// count reaches zero then the only other references (if any) are - /// [`Weak`][weak], so we `drop` the inner value. - /// - /// [weak]: struct.Weak.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// struct Foo; - /// - /// impl Drop for Foo { - /// fn drop(&mut self) { - /// println!("dropped!"); - /// } - /// } - /// - /// let foo = Arc::new(Foo); - /// let foo2 = foo.clone(); - /// - /// drop(foo); // Doesn't print anything - /// drop(foo2); // Prints "dropped!" - /// ``` +impl Drop for Arc { #[inline] fn drop(&mut self) { // Because `fetch_sub` is already atomic, we do not need to synchronize - // with other threads unless we are going to delete the object. This - // same logic applies to the below `fetch_sub` to the `weak` count. - if self.inner().strong.fetch_sub(1, Release) != 1 { + // with other threads unless we are going to delete the object. + if self.inner().count.fetch_sub(1, Release) != 1 { return; } - // This fence is needed to prevent reordering of use of the data and + // FIXME(bholley): Use the updated comment when [2] is merged. + // + // This load is needed to prevent reordering of use of the data and // deletion of the data. Because it is marked `Release`, the decreasing - // of the reference count synchronizes with this `Acquire` fence. This + // of the reference count synchronizes with this `Acquire` load. This // means that use of the data happens before decreasing the reference - // count, which happens before this fence, which happens before the + // count, which happens before this load, which happens before the // deletion of the data. // // As explained in the [Boost documentation][1], @@ -757,7 +254,8 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { // > "acquire" operation before deleting the object. // // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html) - atomic::fence(Acquire); + // [2]: https://github.com/rust-lang/rust/pull/41714 + self.inner().count.load(Acquire); unsafe { self.drop_slow(); @@ -765,704 +263,118 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc { } } -impl Weak { - /// Constructs a new `Weak`, without an accompanying instance of `T`. - /// - /// This allocates memory for `T`, but does not initialize it. Calling - /// [`upgrade`][upgrade] on the return value always gives - /// [`None`][option]. - /// - /// [upgrade]: struct.Weak.html#method.upgrade - /// [option]: ../../std/option/enum.Option.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Weak; - /// - /// let empty: Weak = Weak::new(); - /// assert!(empty.upgrade().is_none()); - /// ``` - #[stable(feature = "downgraded_weak", since = "1.10.0")] - pub fn new() -> Weak { - unsafe { - Weak { - ptr: Shared::new(Box::into_raw(box ArcInner { - strong: atomic::AtomicUsize::new(0), - weak: atomic::AtomicUsize::new(1), - data: uninitialized(), - })), - } - } - } -} - -impl Weak { - /// Upgrades the `Weak` pointer to an [`Arc`][arc], if possible. - /// - /// Returns [`None`][option] if the strong count has reached zero and the - /// inner value was destroyed. - /// - /// [arc]: struct.Arc.html - /// [option]: ../../std/option/enum.Option.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// let weak_five = Arc::downgrade(&five); - /// - /// let strong_five: Option> = weak_five.upgrade(); - /// assert!(strong_five.is_some()); - /// - /// // Destroy all strong pointers. - /// drop(strong_five); - /// drop(five); - /// - /// assert!(weak_five.upgrade().is_none()); - /// ``` - #[stable(feature = "arc_weak", since = "1.4.0")] - pub fn upgrade(&self) -> Option> { - // We use a CAS loop to increment the strong count instead of a - // fetch_add because once the count hits 0 it must never be above 0. - let inner = self.inner(); - - // Relaxed load because any write of 0 that we can observe - // leaves the field in a permanently zero state (so a - // "stale" read of 0 is fine), and any other value is - // confirmed via the CAS below. - let mut n = inner.strong.load(Relaxed); - - loop { - if n == 0 { - return None; - } - - // See comments in `Arc::clone` for why we do this (for `mem::forget`). - if n > MAX_REFCOUNT { - unsafe { - abort(); - } - } - - // Relaxed is valid for the same reason it is on Arc's Clone impl - match inner.strong.compare_exchange_weak(n, n + 1, Relaxed, Relaxed) { - Ok(_) => return Some(Arc { ptr: self.ptr }), - Err(old) => n = old, - } - } - } - - #[inline] - fn inner(&self) -> &ArcInner { - // See comments above for why this is "safe" - unsafe { &**self.ptr } - } -} - -#[stable(feature = "arc_weak", since = "1.4.0")] -impl Clone for Weak { - /// Makes a clone of the `Weak` pointer. - /// - /// This creates another pointer to the same inner value, increasing the - /// weak reference count. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let weak_five = Arc::downgrade(&Arc::new(5)); - /// - /// weak_five.clone(); - /// ``` - #[inline] - fn clone(&self) -> Weak { - // See comments in Arc::clone() for why this is relaxed. This can use a - // fetch_add (ignoring the lock) because the weak count is only locked - // where are *no other* weak pointers in existence. (So we can't be - // running this code in that case). - let old_size = self.inner().weak.fetch_add(1, Relaxed); - - // See comments in Arc::clone() for why we do this (for mem::forget). - if old_size > MAX_REFCOUNT { - unsafe { - abort(); - } - } - - return Weak { ptr: self.ptr }; - } -} - -#[stable(feature = "downgraded_weak", since = "1.10.0")] -impl Default for Weak { - /// Constructs a new `Weak`, without an accompanying instance of `T`. - /// - /// This allocates memory for `T`, but does not initialize it. Calling - /// [`upgrade`][upgrade] on the return value always gives - /// [`None`][option]. - /// - /// [upgrade]: struct.Weak.html#method.upgrade - /// [option]: ../../std/option/enum.Option.html - /// - /// # Examples - /// - /// ``` - /// use std::sync::Weak; - /// - /// let empty: Weak = Default::default(); - /// assert!(empty.upgrade().is_none()); - /// ``` - fn default() -> Weak { - Weak::new() - } -} - -#[stable(feature = "arc_weak", since = "1.4.0")] -impl Drop for Weak { - /// Drops the `Weak` pointer. - /// - /// This will decrement the weak reference count. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// struct Foo; - /// - /// impl Drop for Foo { - /// fn drop(&mut self) { - /// println!("dropped!"); - /// } - /// } - /// - /// let foo = Arc::new(Foo); - /// let weak_foo = Arc::downgrade(&foo); - /// let other_weak_foo = weak_foo.clone(); - /// - /// drop(weak_foo); // Doesn't print anything - /// drop(foo); // Prints "dropped!" - /// - /// assert!(other_weak_foo.upgrade().is_none()); - /// ``` - fn drop(&mut self) { - let ptr = *self.ptr; - - // If we find out that we were the last weak pointer, then its time to - // deallocate the data entirely. See the discussion in Arc::drop() about - // the memory orderings - // - // It's not necessary to check for the locked state here, because the - // weak count can only be locked if there was precisely one weak ref, - // meaning that drop could only subsequently run ON that remaining weak - // ref, which can only happen after the lock is released. - if self.inner().weak.fetch_sub(1, Release) == 1 { - atomic::fence(Acquire); - unsafe { deallocate(ptr as *mut u8, size_of_val(&*ptr), align_of_val(&*ptr)) } - } - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl PartialEq for Arc { - /// Equality for two `Arc`s. - /// - /// Two `Arc`s are equal if their inner values are equal. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five == Arc::new(5)); - /// ``` fn eq(&self, other: &Arc) -> bool { *(*self) == *(*other) } - /// Inequality for two `Arc`s. - /// - /// Two `Arc`s are unequal if their inner values are unequal. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five != Arc::new(6)); - /// ``` fn ne(&self, other: &Arc) -> bool { *(*self) != *(*other) } } -#[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for Arc { - /// Partial comparison for two `Arc`s. - /// - /// The two are compared by calling `partial_cmp()` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// use std::cmp::Ordering; - /// - /// let five = Arc::new(5); - /// - /// assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6))); - /// ``` fn partial_cmp(&self, other: &Arc) -> Option { (**self).partial_cmp(&**other) } - /// Less-than comparison for two `Arc`s. - /// - /// The two are compared by calling `<` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five < Arc::new(6)); - /// ``` fn lt(&self, other: &Arc) -> bool { *(*self) < *(*other) } - /// 'Less than or equal to' comparison for two `Arc`s. - /// - /// The two are compared by calling `<=` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five <= Arc::new(5)); - /// ``` fn le(&self, other: &Arc) -> bool { *(*self) <= *(*other) } - /// Greater-than comparison for two `Arc`s. - /// - /// The two are compared by calling `>` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five > Arc::new(4)); - /// ``` fn gt(&self, other: &Arc) -> bool { *(*self) > *(*other) } - /// 'Greater than or equal to' comparison for two `Arc`s. - /// - /// The two are compared by calling `>=` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let five = Arc::new(5); - /// - /// assert!(five >= Arc::new(5)); - /// ``` fn ge(&self, other: &Arc) -> bool { *(*self) >= *(*other) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Ord for Arc { - /// Comparison for two `Arc`s. - /// - /// The two are compared by calling `cmp()` on their inner values. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// use std::cmp::Ordering; - /// - /// let five = Arc::new(5); - /// - /// assert_eq!(Ordering::Less, five.cmp(&Arc::new(6))); - /// ``` fn cmp(&self, other: &Arc) -> Ordering { (**self).cmp(&**other) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Eq for Arc {} -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for Arc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&**self, f) } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Arc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -#[stable(feature = "rust1", since = "1.0.0")] impl fmt::Pointer for Arc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Pointer::fmt(&*self.ptr, f) + fmt::Pointer::fmt(&self.ptr, f) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Default for Arc { - /// Creates a new `Arc`, with the `Default` value for `T`. - /// - /// # Examples - /// - /// ``` - /// use std::sync::Arc; - /// - /// let x: Arc = Default::default(); - /// assert_eq!(*x, 0); - /// ``` fn default() -> Arc { Arc::new(Default::default()) } } -#[stable(feature = "rust1", since = "1.0.0")] impl Hash for Arc { fn hash(&self, state: &mut H) { (**self).hash(state) } } -#[stable(feature = "from_for_ptrs", since = "1.6.0")] impl From for Arc { fn from(t: T) -> Self { Arc::new(t) } } -#[cfg(test)] -mod tests { - use std::clone::Clone; - use std::sync::mpsc::channel; - use std::mem::drop; - use std::ops::Drop; - use std::option::Option; - use std::option::Option::{None, Some}; - use std::sync::atomic; - use std::sync::atomic::Ordering::{Acquire, SeqCst}; - use std::thread; - use std::vec::Vec; - use super::{Arc, Weak}; - use std::sync::Mutex; - use std::convert::From; - - struct Canary(*mut atomic::AtomicUsize); - - impl Drop for Canary { - fn drop(&mut self) { - unsafe { - match *self { - Canary(c) => { - (*c).fetch_add(1, SeqCst); - } - } - } - } - } - - #[test] - #[cfg_attr(target_os = "emscripten", ignore)] - fn manually_share_arc() { - let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let arc_v = Arc::new(v); - - let (tx, rx) = channel(); - - let _t = thread::spawn(move || { - let arc_v: Arc> = rx.recv().unwrap(); - assert_eq!((*arc_v)[3], 4); - }); - - tx.send(arc_v.clone()).unwrap(); - - assert_eq!((*arc_v)[2], 3); - assert_eq!((*arc_v)[4], 5); - } - - #[test] - fn test_arc_get_mut() { - let mut x = Arc::new(3); - *Arc::get_mut(&mut x).unwrap() = 4; - assert_eq!(*x, 4); - let y = x.clone(); - assert!(Arc::get_mut(&mut x).is_none()); - drop(y); - assert!(Arc::get_mut(&mut x).is_some()); - let _w = Arc::downgrade(&x); - assert!(Arc::get_mut(&mut x).is_none()); - } - - #[test] - fn try_unwrap() { - let x = Arc::new(3); - assert_eq!(Arc::try_unwrap(x), Ok(3)); - let x = Arc::new(4); - let _y = x.clone(); - assert_eq!(Arc::try_unwrap(x), Err(Arc::new(4))); - let x = Arc::new(5); - let _w = Arc::downgrade(&x); - assert_eq!(Arc::try_unwrap(x), Ok(5)); - } - - #[test] - fn into_from_raw() { - let x = Arc::new(box "hello"); - let y = x.clone(); - - let x_ptr = Arc::into_raw(x); - drop(y); - unsafe { - assert_eq!(**x_ptr, "hello"); - - let x = Arc::from_raw(x_ptr); - assert_eq!(**x, "hello"); - - assert_eq!(Arc::try_unwrap(x).map(|x| *x), Ok("hello")); - } - } - - #[test] - fn test_cowarc_clone_make_mut() { - let mut cow0 = Arc::new(75); - let mut cow1 = cow0.clone(); - let mut cow2 = cow1.clone(); - - assert!(75 == *Arc::make_mut(&mut cow0)); - assert!(75 == *Arc::make_mut(&mut cow1)); - assert!(75 == *Arc::make_mut(&mut cow2)); - - *Arc::make_mut(&mut cow0) += 1; - *Arc::make_mut(&mut cow1) += 2; - *Arc::make_mut(&mut cow2) += 3; - - assert!(76 == *cow0); - assert!(77 == *cow1); - assert!(78 == *cow2); - - // none should point to the same backing memory - assert!(*cow0 != *cow1); - assert!(*cow0 != *cow2); - assert!(*cow1 != *cow2); - } - - #[test] - fn test_cowarc_clone_unique2() { - let mut cow0 = Arc::new(75); - let cow1 = cow0.clone(); - let cow2 = cow1.clone(); - - assert!(75 == *cow0); - assert!(75 == *cow1); - assert!(75 == *cow2); - - *Arc::make_mut(&mut cow0) += 1; - assert!(76 == *cow0); - assert!(75 == *cow1); - assert!(75 == *cow2); - - // cow1 and cow2 should share the same contents - // cow0 should have a unique reference - assert!(*cow0 != *cow1); - assert!(*cow0 != *cow2); - assert!(*cow1 == *cow2); - } - - #[test] - fn test_cowarc_clone_weak() { - let mut cow0 = Arc::new(75); - let cow1_weak = Arc::downgrade(&cow0); - - assert!(75 == *cow0); - assert!(75 == *cow1_weak.upgrade().unwrap()); - - *Arc::make_mut(&mut cow0) += 1; - - assert!(76 == *cow0); - assert!(cow1_weak.upgrade().is_none()); - } - - #[test] - fn test_live() { - let x = Arc::new(5); - let y = Arc::downgrade(&x); - assert!(y.upgrade().is_some()); - } - - #[test] - fn test_dead() { - let x = Arc::new(5); - let y = Arc::downgrade(&x); - drop(x); - assert!(y.upgrade().is_none()); - } - - #[test] - fn weak_self_cyclic() { - struct Cycle { - x: Mutex>>, - } - - let a = Arc::new(Cycle { x: Mutex::new(None) }); - let b = Arc::downgrade(&a.clone()); - *a.x.lock().unwrap() = Some(b); - - // hopefully we don't double-free (or leak)... - } - - #[test] - fn drop_arc() { - let mut canary = atomic::AtomicUsize::new(0); - let x = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize)); - drop(x); - assert!(canary.load(Acquire) == 1); - } - - #[test] - fn drop_arc_weak() { - let mut canary = atomic::AtomicUsize::new(0); - let arc = Arc::new(Canary(&mut canary as *mut atomic::AtomicUsize)); - let arc_weak = Arc::downgrade(&arc); - assert!(canary.load(Acquire) == 0); - drop(arc); - assert!(canary.load(Acquire) == 1); - drop(arc_weak); - } - - #[test] - fn test_strong_count() { - let a = Arc::new(0); - assert!(Arc::strong_count(&a) == 1); - let w = Arc::downgrade(&a); - assert!(Arc::strong_count(&a) == 1); - let b = w.upgrade().expect(""); - assert!(Arc::strong_count(&b) == 2); - assert!(Arc::strong_count(&a) == 2); - drop(w); - drop(a); - assert!(Arc::strong_count(&b) == 1); - let c = b.clone(); - assert!(Arc::strong_count(&b) == 2); - assert!(Arc::strong_count(&c) == 2); - } - - #[test] - fn test_weak_count() { - let a = Arc::new(0); - assert!(Arc::strong_count(&a) == 1); - assert!(Arc::weak_count(&a) == 0); - let w = Arc::downgrade(&a); - assert!(Arc::strong_count(&a) == 1); - assert!(Arc::weak_count(&a) == 1); - let x = w.clone(); - assert!(Arc::weak_count(&a) == 2); - drop(w); - drop(x); - assert!(Arc::strong_count(&a) == 1); - assert!(Arc::weak_count(&a) == 0); - let c = a.clone(); - assert!(Arc::strong_count(&a) == 2); - assert!(Arc::weak_count(&a) == 0); - let d = Arc::downgrade(&c); - assert!(Arc::weak_count(&c) == 1); - assert!(Arc::strong_count(&c) == 2); - - drop(a); - drop(c); - drop(d); - } - - #[test] - fn show_arc() { - let a = Arc::new(5); - assert_eq!(format!("{:?}", a), "5"); - } - - // Make sure deriving works with Arc - #[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Debug, Default)] - struct Foo { - inner: Arc, - } - - #[test] - fn test_unsized() { - let x: Arc<[i32]> = Arc::new([1, 2, 3]); - assert_eq!(format!("{:?}", x), "[1, 2, 3]"); - let y = Arc::downgrade(&x.clone()); - drop(x); - assert!(y.upgrade().is_none()); - } - - #[test] - fn test_from_owned() { - let foo = 123; - let foo_arc = Arc::from(foo); - assert!(123 == *foo_arc); - } - - #[test] - fn test_new_weak() { - let foo: Weak = Weak::new(); - assert!(foo.upgrade().is_none()); - } - - #[test] - fn test_ptr_eq() { - let five = Arc::new(5); - let same_five = five.clone(); - let other_five = Arc::new(5); - - assert!(Arc::ptr_eq(&five, &same_five)); - assert!(!Arc::ptr_eq(&five, &other_five)); - } -} - -#[stable(feature = "rust1", since = "1.0.0")] impl borrow::Borrow for Arc { fn borrow(&self) -> &T { &**self } } -#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")] impl AsRef for Arc { fn as_ref(&self) -> &T { &**self } } + +// This is what the HeapSize crate does for regular arc, but is questionably +// sound. See https://github.com/servo/heapsize/issues/37 +#[cfg(feature = "servo")] +impl HeapSizeOf for Arc { + fn heap_size_of_children(&self) -> usize { + (**self).heap_size_of_children() + } +} + +#[cfg(feature = "servo")] +impl Deserialize for Arc +{ + fn deserialize(deserializer: D) -> Result, D::Error> + where + D: ::serde::de::Deserializer, + { + T::deserialize(deserializer).map(Arc::new) + } +} + +#[cfg(feature = "servo")] +impl Serialize for Arc +{ + fn serialize(&self, serializer: S) -> Result + where + S: ::serde::ser::Serializer, + { + (**self).serialize(serializer) + } +} From d78ca4c4f9f1866204cbb420b284be1cbc137712 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sun, 30 Apr 2017 15:18:01 -0700 Subject: [PATCH 3/6] Use StyleArc in the style system. MozReview-Commit-ID: flF0fv9E9M --- components/style/animation.rs | 2 +- components/style/attr.rs | 2 +- components/style/context.rs | 3 ++- components/style/custom_properties.rs | 2 +- components/style/data.rs | 2 +- components/style/dom.rs | 2 +- components/style/encoding_support.rs | 2 +- components/style/gecko/data.rs | 2 +- components/style/gecko/media_queries.rs | 2 +- components/style/gecko/restyle_damage.rs | 2 +- components/style/gecko/url.rs | 2 +- components/style/gecko/wrapper.rs | 2 +- components/style/gecko_bindings/sugar/ownership.rs | 2 +- components/style/gecko_bindings/sugar/refptr.rs | 2 +- components/style/keyframes.rs | 2 +- components/style/lib.rs | 9 --------- components/style/matching.rs | 6 +++--- components/style/properties/gecko.mako.rs | 2 +- components/style/properties/helpers.mako.rs | 4 ++-- components/style/properties/longhand/svg.mako.rs | 2 +- components/style/properties/properties.mako.rs | 6 +++--- components/style/rule_tree/mod.rs | 9 ++++----- components/style/selector_parser.rs | 2 +- components/style/servo/restyle_damage.rs | 2 +- components/style/servo/url.rs | 3 +++ components/style/shared_lock.rs | 4 ++-- components/style/stylesheet_set.rs | 2 +- components/style/stylesheets.rs | 2 +- components/style/stylist.rs | 2 +- ports/geckolib/glue.rs | 3 ++- ports/geckolib/stylesheet_loader.rs | 2 +- tests/unit/style/keyframes.rs | 2 +- tests/unit/style/media_queries.rs | 2 +- tests/unit/style/rule_tree/bench.rs | 2 +- tests/unit/style/stylesheets.rs | 2 +- tests/unit/style/stylist.rs | 2 +- tests/unit/style/viewport.rs | 2 +- 37 files changed, 49 insertions(+), 54 deletions(-) diff --git a/components/style/animation.rs b/components/style/animation.rs index 941db59cc85..0a4c7cab345 100644 --- a/components/style/animation.rs +++ b/components/style/animation.rs @@ -19,8 +19,8 @@ use properties::longhands::animation_iteration_count::single_value::computed_val use properties::longhands::animation_play_state::computed_value::single_value::T as AnimationPlayState; use properties::longhands::transition_timing_function::single_value::computed_value::StartEnd; use properties::longhands::transition_timing_function::single_value::computed_value::T as TransitionTimingFunction; -use std::sync::Arc; use std::sync::mpsc::Sender; +use stylearc::Arc; use timer::Timer; use values::computed::Time; diff --git a/components/style/attr.rs b/components/style/attr.rs index 1989816329d..101810212b7 100644 --- a/components/style/attr.rs +++ b/components/style/attr.rs @@ -16,10 +16,10 @@ use servo_url::ServoUrl; use shared_lock::Locked; use std::ascii::AsciiExt; use std::str::FromStr; -use std::sync::Arc; use str::{HTML_SPACE_CHARACTERS, read_exponent, read_fraction}; use str::{read_numbers, split_commas, split_html_space_chars}; use str::str_join; +use stylearc::Arc; use values::specified::Length; // Duplicated from script::dom::values. diff --git a/components/style/context.rs b/components/style/context.rs index 581a7c12cd3..1cdd8c00636 100644 --- a/components/style/context.rs +++ b/components/style/context.rs @@ -27,8 +27,9 @@ use std::collections::HashMap; #[cfg(not(feature = "servo"))] use std::env; use std::fmt; use std::ops::Add; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use std::sync::mpsc::Sender; +use stylearc::Arc; use stylist::Stylist; use thread_state; use time; diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index 95e43b46fdf..071efade790 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -14,8 +14,8 @@ use std::ascii::AsciiExt; use std::borrow::Cow; use std::collections::{HashMap, HashSet}; use std::fmt; -use std::sync::Arc; use style_traits::ToCss; +use stylearc::Arc; /// A custom property name is just an `Atom`. /// diff --git a/components/style/data.rs b/components/style/data.rs index 4052f0b30a3..651f003e4e9 100644 --- a/components/style/data.rs +++ b/components/style/data.rs @@ -16,7 +16,7 @@ use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, RestyleDamage, Snapshot use std::fmt; #[cfg(feature = "servo")] use std::hash::BuildHasherDefault; use std::ops::Deref; -use std::sync::Arc; +use stylearc::Arc; use stylist::Stylist; use thread_state; use traversal::TraversalFlags; diff --git a/components/style/dom.rs b/components/style/dom.rs index ffc46b0cc94..7a4fe5d1003 100644 --- a/components/style/dom.rs +++ b/components/style/dom.rs @@ -26,7 +26,7 @@ use std::fmt; use std::fmt::Debug; use std::hash::Hash; use std::ops::Deref; -use std::sync::Arc; +use stylearc::Arc; use stylist::ApplicableDeclarationBlock; use thread_state; diff --git a/components/style/encoding_support.rs b/components/style/encoding_support.rs index 2775de54893..92620378b30 100644 --- a/components/style/encoding_support.rs +++ b/components/style/encoding_support.rs @@ -13,7 +13,7 @@ use media_queries::MediaList; use self::encoding::{EncodingRef, DecoderTrap}; use shared_lock::SharedRwLock; use std::str; -use std::sync::Arc; +use stylearc::Arc; use stylesheets::{Stylesheet, StylesheetLoader, Origin, UrlExtraData}; struct RustEncoding; diff --git a/components/style/gecko/data.rs b/components/style/gecko/data.rs index 942aaf4f7b5..2e1e17a2ebc 100644 --- a/components/style/gecko/data.rs +++ b/components/style/gecko/data.rs @@ -15,8 +15,8 @@ use parking_lot::RwLock; use properties::ComputedValues; use shared_lock::{Locked, StylesheetGuards, SharedRwLockReadGuard}; use std::collections::HashMap; -use std::sync::Arc; use std::sync::mpsc::{Receiver, Sender, channel}; +use stylearc::Arc; use stylesheet_set::StylesheetSet; use stylesheets::{FontFaceRule, Origin, Stylesheet}; use stylist::{ExtraStyleData, Stylist}; diff --git a/components/style/gecko/media_queries.rs b/components/style/gecko/media_queries.rs index a0aeaec2e5c..2444338f174 100644 --- a/components/style/gecko/media_queries.rs +++ b/components/style/gecko/media_queries.rs @@ -18,11 +18,11 @@ use media_queries::MediaType; use parser::ParserContext; use properties::{ComputedValues, StyleBuilder}; use std::fmt::{self, Write}; -use std::sync::Arc; use str::starts_with_ignore_ascii_case; use string_cache::Atom; use style_traits::ToCss; use style_traits::viewport::ViewportConstraints; +use stylearc::Arc; use values::{CSSFloat, specified}; use values::computed::{self, ToComputedValue}; diff --git a/components/style/gecko/restyle_damage.rs b/components/style/gecko/restyle_damage.rs index 4d6c1269eb8..39fdafebd5d 100644 --- a/components/style/gecko/restyle_damage.rs +++ b/components/style/gecko/restyle_damage.rs @@ -10,7 +10,7 @@ use gecko_bindings::structs::{nsChangeHint, nsStyleContext}; use gecko_bindings::sugar::ownership::FFIArcHelpers; use properties::ComputedValues; use std::ops::{BitAnd, BitOr, BitOrAssign, Not}; -use std::sync::Arc; +use stylearc::Arc; /// The representation of Gecko's restyle damage is just a wrapper over /// `nsChangeHint`. diff --git a/components/style/gecko/url.rs b/components/style/gecko/url.rs index 286094bcb0e..191ae082726 100644 --- a/components/style/gecko/url.rs +++ b/components/style/gecko/url.rs @@ -10,8 +10,8 @@ use gecko_bindings::sugar::refptr::RefPtr; use parser::ParserContext; use std::borrow::Cow; use std::fmt::{self, Write}; -use std::sync::Arc; use style_traits::ToCss; +use stylearc::Arc; /// A specified url() value for gecko. Gecko does not eagerly resolve SpecifiedUrls. #[derive(Clone, Debug, PartialEq)] diff --git a/components/style/gecko/wrapper.rs b/components/style/gecko/wrapper.rs index 100830c0e0d..f71ef0a6c9c 100644 --- a/components/style/gecko/wrapper.rs +++ b/components/style/gecko/wrapper.rs @@ -71,8 +71,8 @@ use std::collections::HashMap; use std::fmt; use std::hash::{Hash, Hasher}; use std::ptr; -use std::sync::Arc; use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace}; +use stylearc::Arc; use stylesheets::UrlExtraData; use stylist::ApplicableDeclarationBlock; diff --git a/components/style/gecko_bindings/sugar/ownership.rs b/components/style/gecko_bindings/sugar/ownership.rs index abcf5c65a8a..6c4746013b3 100644 --- a/components/style/gecko_bindings/sugar/ownership.rs +++ b/components/style/gecko_bindings/sugar/ownership.rs @@ -8,7 +8,7 @@ use std::marker::PhantomData; use std::mem::{forget, transmute}; use std::ops::{Deref, DerefMut}; use std::ptr; -use std::sync::Arc; +use stylearc::Arc; /// Indicates that a given Servo type has a corresponding Gecko FFI type. pub unsafe trait HasFFI : Sized { diff --git a/components/style/gecko_bindings/sugar/refptr.rs b/components/style/gecko_bindings/sugar/refptr.rs index 3c3dec9aee3..9ca1d3dc7b6 100644 --- a/components/style/gecko_bindings/sugar/refptr.rs +++ b/components/style/gecko_bindings/sugar/refptr.rs @@ -9,7 +9,7 @@ use gecko_bindings::sugar::ownership::HasArcFFI; use std::{mem, ptr}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; -use std::sync::Arc; +use stylearc::Arc; /// Trait for all objects that have Addref() and Release /// methods and can be placed inside RefPtr diff --git a/components/style/keyframes.rs b/components/style/keyframes.rs index 8113c06bb6c..3a80320bb52 100644 --- a/components/style/keyframes.rs +++ b/components/style/keyframes.rs @@ -17,8 +17,8 @@ use properties::animated_properties::TransitionProperty; use properties::longhands::transition_timing_function::single_value::SpecifiedValue as SpecifiedTimingFunction; use shared_lock::{SharedRwLock, SharedRwLockReadGuard, Locked, ToCssWithGuard}; use std::fmt; -use std::sync::Arc; use style_traits::ToCss; +use stylearc::Arc; use stylesheets::{CssRuleType, Stylesheet, VendorPrefix}; /// A number from 0 to 1, indicating the percentage of the animation when this diff --git a/components/style/lib.rs b/components/style/lib.rs index 99ded2b9fe8..8902ab4efc6 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -132,7 +132,6 @@ pub mod values; pub mod viewport; use std::fmt; -use std::sync::Arc; use style_traits::ToCss; #[cfg(feature = "gecko")] pub use gecko_string_cache as string_cache; @@ -177,14 +176,6 @@ macro_rules! reexport_computed_values { } longhand_properties_idents!(reexport_computed_values); -/// Returns whether the two arguments point to the same value. -/// -/// FIXME: Remove this and use Arc::ptr_eq once we require Rust 1.17 -#[inline] -pub fn arc_ptr_eq(a: &Arc, b: &Arc) -> bool { - ptr_eq::(&**a, &**b) -} - /// Pointer equality /// /// FIXME: Remove this and use std::ptr::eq once we require Rust 1.17 diff --git a/components/style/matching.rs b/components/style/matching.rs index ca2db43c4b9..8da1192d4d3 100644 --- a/components/style/matching.rs +++ b/components/style/matching.rs @@ -27,7 +27,7 @@ use selectors::bloom::BloomFilter; use selectors::matching::{ElementSelectorFlags, StyleRelations}; use selectors::matching::AFFECTED_BY_PSEUDO_ELEMENTS; use sink::ForgetfulSink; -use std::sync::Arc; +use stylearc::Arc; use stylist::ApplicableDeclarationBlock; /// The way a style should be inherited. @@ -120,8 +120,8 @@ fn same_computed_values(first: Option, second: Option) -> boo _ => return false, }; - let eq = ::arc_ptr_eq(a.borrow_data().unwrap().styles().primary.values(), - b.borrow_data().unwrap().styles().primary.values()); + let eq = Arc::ptr_eq(a.borrow_data().unwrap().styles().primary.values(), + b.borrow_data().unwrap().styles().primary.values()); eq } diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 4643d176949..b428ec3e198 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -61,7 +61,7 @@ use properties::{PropertyDeclaration, PropertyDeclarationBlock, PropertyDeclarat use std::fmt::{self, Debug}; use std::mem::{forget, transmute, zeroed}; use std::ptr; -use std::sync::Arc; +use stylearc::Arc; use std::cmp; use values::computed::ToComputedValue; use values::{Either, Auto, KeyframesName}; diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 4db19381e1f..3c3bb537320 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -252,7 +252,7 @@ use properties::{DeclaredValue, LonghandId, LonghandIdSet}; use properties::{CSSWideKeyword, ComputedValues, PropertyDeclaration}; use properties::style_structs; - use std::sync::Arc; + use stylearc::Arc; use values::computed::{Context, ToComputedValue}; use values::{computed, generics, specified}; use Atom; @@ -716,7 +716,7 @@ use properties::{PropertyDeclaration, ParsedDeclaration}; use properties::{ShorthandId, UnparsedValue, longhands}; use std::fmt; - use std::sync::Arc; + use stylearc::Arc; use style_traits::ToCss; pub struct Longhands { diff --git a/components/style/properties/longhand/svg.mako.rs b/components/style/properties/longhand/svg.mako.rs index 7a764fbe9f5..45cb125bf33 100644 --- a/components/style/properties/longhand/svg.mako.rs +++ b/components/style/properties/longhand/svg.mako.rs @@ -197,7 +197,7 @@ ${helpers.single_keyword("mask-composite", spec="https://drafts.fxtf.org/css-masking/#propdef-mask-image"> use std::fmt; use style_traits::ToCss; - use std::sync::Arc; + use stylearc::Arc; use values::specified::Image; use values::specified::url::SpecifiedUrl; use values::HasViewportPercentage; diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index aba24623855..83ca3e3d4fb 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -14,7 +14,7 @@ use std::borrow::Cow; use std::collections::HashSet; use std::fmt; use std::ops::Deref; -use std::sync::Arc; +use stylearc::Arc; use app_units::Au; #[cfg(feature = "servo")] use cssparser::{Color as CSSParserColor, RGBA}; @@ -178,7 +178,7 @@ pub mod shorthands { use cssparser::Parser; use parser::ParserContext; use properties::{ParsedDeclaration, ShorthandId, UnparsedValue}; - use std::sync::Arc; + use stylearc::Arc; pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { // This function is like the parse() that is generated by @@ -2246,7 +2246,7 @@ pub use self::lazy_static_module::INITIAL_SERVO_VALUES; #[allow(missing_docs)] mod lazy_static_module { use logical_geometry::WritingMode; - use std::sync::Arc; + use stylearc::Arc; use super::{ComputedValues, longhands, style_structs}; /// The initial values for all style structs as defined by the specification. diff --git a/components/style/rule_tree/mod.rs b/components/style/rule_tree/mod.rs index fa6acaa92b5..0947198e2da 100644 --- a/components/style/rule_tree/mod.rs +++ b/components/style/rule_tree/mod.rs @@ -6,15 +6,14 @@ //! The rule tree. -use arc_ptr_eq; #[cfg(feature = "servo")] use heapsize::HeapSizeOf; use properties::{Importance, PropertyDeclarationBlock}; use shared_lock::{Locked, StylesheetGuards, SharedRwLockReadGuard}; use std::io::{self, Write}; use std::ptr; -use std::sync::Arc; use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; +use stylearc::Arc; use stylesheets::StyleRule; use thread_state; @@ -65,8 +64,8 @@ impl StyleSource { fn ptr_equals(&self, other: &Self) -> bool { use self::StyleSource::*; match (self, other) { - (&Style(ref one), &Style(ref other)) => arc_ptr_eq(one, other), - (&Declarations(ref one), &Declarations(ref other)) => arc_ptr_eq(one, other), + (&Style(ref one), &Style(ref other)) => Arc::ptr_eq(one, other), + (&Declarations(ref one), &Declarations(ref other)) => Arc::ptr_eq(one, other), _ => false, } } @@ -203,7 +202,7 @@ impl RuleTree { // so let's skip it for now. let is_here_already = match current.get().source.as_ref() { Some(&StyleSource::Declarations(ref already_here)) => { - arc_ptr_eq(pdb, already_here) + Arc::ptr_eq(pdb, already_here) }, _ => unreachable!("Replacing non-declarations style?"), }; diff --git a/components/style/selector_parser.rs b/components/style/selector_parser.rs index 0664483cbe7..0af60279af8 100644 --- a/components/style/selector_parser.rs +++ b/components/style/selector_parser.rs @@ -37,7 +37,7 @@ pub use gecko::restyle_damage::GeckoRestyleDamage as RestyleDamage; /// A type that represents the previous computed values needed for restyle /// damage calculation. #[cfg(feature = "servo")] -pub type PreExistingComputedValues = ::std::sync::Arc<::properties::ServoComputedValues>; +pub type PreExistingComputedValues = ::stylearc::Arc<::properties::ServoComputedValues>; /// A type that represents the previous computed values needed for restyle /// damage calculation. diff --git a/components/style/servo/restyle_damage.rs b/components/style/servo/restyle_damage.rs index 0ad934278c1..1fd7b38cae2 100644 --- a/components/style/servo/restyle_damage.rs +++ b/components/style/servo/restyle_damage.rs @@ -11,7 +11,7 @@ use computed_values::display; use heapsize::HeapSizeOf; use properties::ServoComputedValues; use std::fmt; -use std::sync::Arc; +use stylearc::Arc; bitflags! { #[doc = "Individual layout actions that may be necessary after restyling."] diff --git a/components/style/servo/url.rs b/components/style/servo/url.rs index c59c6bcb0b7..467f7a45715 100644 --- a/components/style/servo/url.rs +++ b/components/style/servo/url.rs @@ -9,6 +9,9 @@ use parser::ParserContext; use servo_url::ServoUrl; use std::borrow::Cow; use std::fmt::{self, Write}; +// Note: We use std::sync::Arc rather than stylearc::Arc here because the +// nonzero optimization is important in keeping the size of SpecifiedUrl below +// the threshold. use std::sync::Arc; use style_traits::ToCss; diff --git a/components/style/shared_lock.rs b/components/style/shared_lock.rs index 2dec153f342..360118d472e 100644 --- a/components/style/shared_lock.rs +++ b/components/style/shared_lock.rs @@ -10,7 +10,7 @@ use atomic_refcell::{AtomicRefCell, AtomicRef, AtomicRefMut}; use parking_lot::RwLock; use std::cell::UnsafeCell; use std::fmt; -use std::sync::Arc; +use stylearc::Arc; /// A shared read/write lock that can protect multiple objects. /// @@ -149,7 +149,7 @@ impl fmt::Debug for Locked { impl Locked { #[cfg(feature = "servo")] fn same_lock_as(&self, lock: &SharedRwLock) -> bool { - ::arc_ptr_eq(&self.shared_lock.arc, &lock.arc) + Arc::ptr_eq(&self.shared_lock.arc, &lock.arc) } #[cfg(feature = "gecko")] diff --git a/components/style/stylesheet_set.rs b/components/style/stylesheet_set.rs index 78e0c9cd558..0cc6f4fbf51 100644 --- a/components/style/stylesheet_set.rs +++ b/components/style/stylesheet_set.rs @@ -4,7 +4,7 @@ //! A centralized set of stylesheets for a document. -use std::sync::Arc; +use stylearc::Arc; use stylesheets::Stylesheet; /// Entry for a StylesheetSet. We don't bother creating a constructor, because diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index 97dfce4c51f..36e5a2a936e 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -36,10 +36,10 @@ use servo_url::ServoUrl; use shared_lock::{SharedRwLock, Locked, ToCssWithGuard, SharedRwLockReadGuard}; use std::cell::Cell; use std::fmt; -use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use str::starts_with_ignore_ascii_case; use style_traits::ToCss; +use stylearc::Arc; use stylist::FnvHashMap; use supports::SupportsCondition; use values::{CustomIdent, KeyframesName}; diff --git a/components/style/stylist.rs b/components/style/stylist.rs index cda795d559c..5dc3e807374 100644 --- a/components/style/stylist.rs +++ b/components/style/stylist.rs @@ -39,8 +39,8 @@ use std::fmt; use std::hash::Hash; #[cfg(feature = "servo")] use std::marker::PhantomData; -use std::sync::Arc; use style_traits::viewport::ViewportConstraints; +use stylearc::Arc; use stylesheets::{CssRule, FontFaceRule, Origin, StyleRule, Stylesheet, UserAgentStylesheets}; use thread_state; use viewport::{self, MaybeNew, ViewportRule}; diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index dff56ce8b0f..f53a47f0af7 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -11,7 +11,7 @@ use std::borrow::Cow; use std::env; use std::fmt::Write; use std::ptr; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use style::context::{QuirksMode, SharedStyleContext, StyleContext}; use style::context::{ThreadLocalStyleContext, ThreadLocalStyleContextCreationInfo}; use style::data::{ElementData, ElementStyles, RestyleData}; @@ -83,6 +83,7 @@ use style::sequential; use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard, Locked}; use style::string_cache::Atom; use style::style_adjuster::StyleAdjuster; +use style::stylearc::Arc; use style::stylesheets::{CssRule, CssRules, CssRuleType, CssRulesHelpers}; use style::stylesheets::{ImportRule, MediaRule, NamespaceRule, Origin}; use style::stylesheets::{PageRule, Stylesheet, StyleRule, SupportsRule}; diff --git a/ports/geckolib/stylesheet_loader.rs b/ports/geckolib/stylesheet_loader.rs index a79aa6e192f..87fdc4d39f3 100644 --- a/ports/geckolib/stylesheet_loader.rs +++ b/ports/geckolib/stylesheet_loader.rs @@ -2,12 +2,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::sync::Arc; use style::gecko_bindings::bindings::Gecko_LoadStyleSheet; use style::gecko_bindings::structs::{Loader, ServoStyleSheet}; use style::gecko_bindings::sugar::ownership::{HasArcFFI, FFIArcHelpers}; use style::media_queries::MediaList; use style::shared_lock::Locked; +use style::stylearc::Arc; use style::stylesheets::{ImportRule, Stylesheet, StylesheetLoader as StyleStylesheetLoader}; pub struct StylesheetLoader(*mut Loader, *mut ServoStyleSheet); diff --git a/tests/unit/style/keyframes.rs b/tests/unit/style/keyframes.rs index 4f96b5f88a9..1f3947c5815 100644 --- a/tests/unit/style/keyframes.rs +++ b/tests/unit/style/keyframes.rs @@ -2,12 +2,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::sync::Arc; use style::keyframes::{Keyframe, KeyframesAnimation, KeyframePercentage, KeyframeSelector}; use style::keyframes::{KeyframesStep, KeyframesStepValue}; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, Importance}; use style::properties::animated_properties::TransitionProperty; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::values::specified::{LengthOrPercentageOrAuto, NoCalcLength}; #[test] diff --git a/tests/unit/style/media_queries.rs b/tests/unit/style/media_queries.rs index 10634a6f134..d538225d9d8 100644 --- a/tests/unit/style/media_queries.rs +++ b/tests/unit/style/media_queries.rs @@ -6,13 +6,13 @@ use cssparser::{Parser, SourcePosition}; use euclid::size::TypedSize2D; use servo_url::ServoUrl; use std::borrow::ToOwned; -use std::sync::Arc; use style::Atom; use style::context::QuirksMode; use style::error_reporting::ParseErrorReporter; use style::media_queries::*; use style::servo::media_queries::*; use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard}; +use style::stylearc::Arc; use style::stylesheets::{Stylesheet, Origin, CssRule}; use style::values::specified; use style_traits::ToCss; diff --git a/tests/unit/style/rule_tree/bench.rs b/tests/unit/style/rule_tree/bench.rs index ed73bd11d6f..68d253b2965 100644 --- a/tests/unit/style/rule_tree/bench.rs +++ b/tests/unit/style/rule_tree/bench.rs @@ -5,13 +5,13 @@ use cssparser::{Parser, SourcePosition}; use rayon; use servo_url::ServoUrl; -use std::sync::Arc; use style::context::QuirksMode; use style::error_reporting::ParseErrorReporter; use style::media_queries::MediaList; use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock}; use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource}; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::stylesheets::{Origin, Stylesheet, CssRule}; use test::{self, Bencher}; diff --git a/tests/unit/style/stylesheets.rs b/tests/unit/style/stylesheets.rs index 47aa1ea9730..e599e9687ae 100644 --- a/tests/unit/style/stylesheets.rs +++ b/tests/unit/style/stylesheets.rs @@ -10,7 +10,6 @@ use selectors::parser::*; use servo_atoms::Atom; use servo_url::ServoUrl; use std::borrow::ToOwned; -use std::sync::Arc; use std::sync::Mutex; use std::sync::atomic::AtomicBool; use style::context::QuirksMode; @@ -22,6 +21,7 @@ use style::properties::{CSSWideKeyword, DeclaredValueOwned, PropertyDeclaration, use style::properties::longhands; use style::properties::longhands::animation_play_state; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::stylesheets::{Origin, Namespaces}; use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, CssRules, StyleRule, KeyframesRule}; use style::values::{KeyframesName, CustomIdent}; diff --git a/tests/unit/style/stylist.rs b/tests/unit/style/stylist.rs index ea89dfde06b..096af2d0006 100644 --- a/tests/unit/style/stylist.rs +++ b/tests/unit/style/stylist.rs @@ -6,12 +6,12 @@ use html5ever_atoms::LocalName; use selectors::parser::LocalName as LocalNameSelector; use selectors::parser::Selector; use servo_atoms::Atom; -use std::sync::Arc; use style::properties::{PropertyDeclarationBlock, PropertyDeclaration}; use style::properties::{longhands, Importance}; use style::rule_tree::CascadeLevel; use style::selector_parser::{SelectorImpl, SelectorParser}; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::stylesheets::StyleRule; use style::stylist::{Rule, SelectorMap}; use style::stylist::needs_revalidation; diff --git a/tests/unit/style/viewport.rs b/tests/unit/style/viewport.rs index fb29e87debf..ed8b9b527ee 100644 --- a/tests/unit/style/viewport.rs +++ b/tests/unit/style/viewport.rs @@ -7,11 +7,11 @@ use euclid::size::TypedSize2D; use media_queries::CSSErrorReporterTest; use servo_config::prefs::{PREFS, PrefValue}; use servo_url::ServoUrl; -use std::sync::Arc; use style::context::QuirksMode; use style::media_queries::{Device, MediaList, MediaType}; use style::parser::{LengthParsingMode, Parse, ParserContext}; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::stylesheets::{CssRuleType, Stylesheet, Origin}; use style::values::specified::LengthOrPercentageOrAuto::{self, Auto}; use style::values::specified::NoCalcLength::{self, ViewportPercentage}; From 7b0679848b177ead3a87171d0224e05ecadd8006 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 2 May 2017 16:22:10 -0700 Subject: [PATCH 4/6] Fix up script and layout. --- components/gfx/font_context.rs | 5 +++-- components/layout/block.rs | 2 +- components/layout/construct.rs | 7 ++++--- components/layout/display_list_builder.rs | 2 +- components/layout/flex.rs | 3 +-- components/layout/flow.rs | 6 +++--- components/layout/fragment.rs | 16 ++++++++-------- components/layout/generated_content.rs | 5 ++--- components/layout/inline.rs | 10 +++++----- components/layout/lib.rs | 12 ++++++++++++ components/layout/list_item.rs | 3 +-- components/layout/multicol.rs | 5 +++-- components/layout/query.rs | 2 +- components/layout/table.rs | 3 +-- components/layout/table_caption.rs | 3 +-- components/layout/table_cell.rs | 3 +-- components/layout/table_colgroup.rs | 3 +-- components/layout/table_row.rs | 3 +-- components/layout/table_rowgroup.rs | 3 +-- components/layout/table_wrapper.rs | 3 +-- components/layout/text.rs | 2 +- components/layout_thread/lib.rs | 19 ++++++++++--------- components/script/dom/bindings/trace.rs | 7 +++++++ components/script/dom/cssconditionrule.rs | 2 +- components/script/dom/cssfontfacerule.rs | 2 +- components/script/dom/cssgroupingrule.rs | 2 +- components/script/dom/cssimportrule.rs | 2 +- components/script/dom/csskeyframerule.rs | 2 +- components/script/dom/csskeyframesrule.rs | 2 +- components/script/dom/cssmediarule.rs | 2 +- components/script/dom/cssnamespacerule.rs | 2 +- components/script/dom/cssrulelist.rs | 2 +- components/script/dom/cssstyledeclaration.rs | 2 +- components/script/dom/cssstylerule.rs | 2 +- components/script/dom/cssstylesheet.rs | 2 +- components/script/dom/csssupportsrule.rs | 2 +- components/script/dom/cssviewportrule.rs | 2 +- components/script/dom/document.rs | 2 +- components/script/dom/element.rs | 2 +- components/script/dom/htmllinkelement.rs | 2 +- components/script/dom/htmlmetaelement.rs | 2 +- components/script/dom/htmlobjectelement.rs | 2 +- components/script/dom/htmlstyleelement.rs | 2 +- components/script/dom/medialist.rs | 2 +- components/script/dom/node.rs | 2 +- components/script/layout_wrapper.rs | 2 +- components/script/stylesheet_loader.rs | 5 +++-- components/script_layout_interface/message.rs | 4 ++-- .../script_layout_interface/wrapper_traits.rs | 2 +- 49 files changed, 99 insertions(+), 85 deletions(-) diff --git a/components/gfx/font_context.rs b/components/gfx/font_context.rs index b1b9bbc6838..2e91e549611 100644 --- a/components/gfx/font_context.rs +++ b/components/gfx/font_context.rs @@ -21,6 +21,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; use style::computed_values::{font_style, font_variant_caps}; use style::properties::style_structs; +use style::stylearc::Arc as StyleArc; use webrender_traits; static SMALL_CAPS_SCALE_FACTOR: f32 = 0.8; // Matches FireFox (see gfxFont.h) @@ -109,7 +110,7 @@ impl FontContext { /// Create a group of fonts for use in layout calculations. May return /// a cached font if this font instance has already been used by /// this context. - pub fn layout_font_group_for_style(&mut self, style: Arc) + pub fn layout_font_group_for_style(&mut self, style: StyleArc) -> Rc { self.expire_font_caches_if_necessary(); @@ -239,7 +240,7 @@ impl HeapSizeOf for FontContext { #[derive(Debug)] struct LayoutFontGroupCacheKey { - pointer: Arc, + pointer: StyleArc, size: Au, } diff --git a/components/layout/block.rs b/components/layout/block.rs index 297bfcdc689..5e30c6f08ee 100644 --- a/components/layout/block.rs +++ b/components/layout/block.rs @@ -2134,7 +2134,7 @@ impl Flow for BlockFlow { self.build_display_list_for_block(state, BorderPaintingMode::Separate); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.fragment.repair_style(new_style) } diff --git a/components/layout/construct.rs b/components/layout/construct.rs index 089209551ac..2f24629b5e4 100644 --- a/components/layout/construct.rs +++ b/components/layout/construct.rs @@ -13,6 +13,7 @@ #![deny(unsafe_code)] +use StyleArc; use app_units::Au; use block::BlockFlow; use context::{LayoutContext, with_thread_local_font_context}; @@ -107,7 +108,7 @@ pub enum ConstructionItem { /// Inline fragments and associated {ib} splits that have not yet found flows. InlineFragments(InlineFragmentsConstructionResult), /// Potentially ignorable whitespace. - Whitespace(OpaqueNode, PseudoElementType<()>, Arc, RestyleDamage), + Whitespace(OpaqueNode, PseudoElementType<()>, StyleArc, RestyleDamage), /// TableColumn Fragment TableColumnFragment(Fragment), } @@ -675,7 +676,7 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode> fn create_fragments_for_node_text_content(&self, fragments: &mut IntermediateInlineFragments, node: &ConcreteThreadSafeLayoutNode, - style: &Arc) { + style: &StyleArc) { // Fast path: If there is no text content, return immediately. let text_content = node.text_content(); if text_content.is_empty() { @@ -1804,7 +1805,7 @@ pub fn strip_ignorable_whitespace_from_end(this: &mut LinkedList) { /// If the 'unicode-bidi' property has a value other than 'normal', return the bidi control codes /// to inject before and after the text content of the element. -fn bidi_control_chars(style: &Arc) -> Option<(&'static str, &'static str)> { +fn bidi_control_chars(style: &StyleArc) -> Option<(&'static str, &'static str)> { use style::computed_values::direction::T::*; use style::computed_values::unicode_bidi::T::*; diff --git a/components/layout/display_list_builder.rs b/components/layout/display_list_builder.rs index a2453067e1f..878b1220336 100644 --- a/components/layout/display_list_builder.rs +++ b/components/layout/display_list_builder.rs @@ -577,7 +577,7 @@ fn build_border_radius(abs_bounds: &Rect, /// Get the border radius for the rectangle inside of a rounded border. This is useful /// for building the clip for the content inside the border. fn build_border_radius_for_inner_rect(outer_rect: &Rect, - style: Arc) + style: ::StyleArc) -> BorderRadii { let mut radii = build_border_radius(&outer_rect, style.get_border()); if radii.is_square() { diff --git a/components/layout/flex.rs b/components/layout/flex.rs index cf77455afa8..dd990c61510 100644 --- a/components/layout/flex.rs +++ b/components/layout/flex.rs @@ -21,7 +21,6 @@ use model::{IntrinsicISizes, MaybeAuto, SizeConstraint}; use model::{specified, specified_or_none}; use std::cmp::{max, min}; use std::ops::Range; -use std::sync::Arc; use style::computed_values::{align_content, align_self, flex_direction, flex_wrap, justify_content}; use style::computed_values::border_collapse; use style::logical_geometry::{Direction, LogicalSize}; @@ -970,7 +969,7 @@ impl Flow for FlexFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/flow.rs b/components/layout/flow.rs index 27b5c291cc8..1944f0decbd 100644 --- a/components/layout/flow.rs +++ b/components/layout/flow.rs @@ -424,7 +424,7 @@ pub trait Flow: fmt::Debug + Sync + Send + 'static { /// Attempts to perform incremental fixup of this flow by replacing its fragment's style with /// the new style. This can only succeed if the flow has exactly one fragment. - fn repair_style(&mut self, new_style: &Arc); + fn repair_style(&mut self, new_style: &::StyleArc); /// Print any extra children (such as fragments) contained in this Flow /// for debugging purposes. Any items inserted into the tree will become @@ -561,7 +561,7 @@ pub trait MutableFlowUtils { /// Calls `repair_style` and `bubble_inline_sizes`. You should use this method instead of /// calling them individually, since there is no reason not to perform both operations. - fn repair_style_and_bubble_inline_sizes(self, style: &Arc); + fn repair_style_and_bubble_inline_sizes(self, style: &::StyleArc); } pub trait MutableOwnedFlowUtils { @@ -1361,7 +1361,7 @@ impl<'a> MutableFlowUtils for &'a mut Flow { /// Calls `repair_style` and `bubble_inline_sizes`. You should use this method instead of /// calling them individually, since there is no reason not to perform both operations. - fn repair_style_and_bubble_inline_sizes(self, style: &Arc) { + fn repair_style_and_bubble_inline_sizes(self, style: &::StyleArc) { self.repair_style(style); self.bubble_inline_sizes(); } diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index 5c311ec3311..9f98ff318ec 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use StyleArc; use app_units::Au; use canvas_traits::CanvasMsg; use context::{LayoutContext, with_thread_local_font_context}; @@ -39,7 +40,6 @@ use std::borrow::ToOwned; use std::cmp::{Ordering, max, min}; use std::collections::LinkedList; use std::sync::{Arc, Mutex}; -use style::arc_ptr_eq; use style::computed_values::{border_collapse, box_sizing, clear, color, display, mix_blend_mode}; use style::computed_values::{overflow_wrap, overflow_x, position, text_decoration_line, transform}; use style::computed_values::{transform_style, vertical_align, white_space, word_break}; @@ -95,10 +95,10 @@ pub struct Fragment { pub node: OpaqueNode, /// The CSS style of this fragment. - pub style: Arc, + pub style: StyleArc, /// The CSS style of this fragment when it's selected - pub selected_style: Arc, + pub selected_style: StyleArc, /// The position of this fragment relative to its owning flow. The size includes padding and /// border, but not margin. @@ -672,8 +672,8 @@ impl Fragment { /// Constructs a new `Fragment` instance from an opaque node. pub fn from_opaque_node_and_style(node: OpaqueNode, pseudo: PseudoElementType<()>, - style: Arc, - selected_style: Arc, + style: StyleArc, + selected_style: StyleArc, mut restyle_damage: RestyleDamage, specific: SpecificFragmentInfo) -> Fragment { @@ -702,7 +702,7 @@ impl Fragment { /// type. For the new anonymous fragment, layout-related values (border box, etc.) are reset to /// initial values. pub fn create_similar_anonymous_fragment(&self, - style: Arc, + style: StyleArc, specific: SpecificFragmentInfo) -> Fragment { let writing_mode = style.writing_mode; @@ -1846,7 +1846,7 @@ impl Fragment { match (&mut self.specific, &next_fragment.specific) { (&mut SpecificFragmentInfo::ScannedText(ref mut this_info), &SpecificFragmentInfo::ScannedText(ref other_info)) => { - debug_assert!(arc_ptr_eq(&this_info.run, &other_info.run)); + debug_assert!(::arc_ptr_eq(&this_info.run, &other_info.run)); this_info.range_end_including_stripped_whitespace = other_info.range_end_including_stripped_whitespace; if other_info.requires_line_break_afterward_if_wrapping_on_newlines() { @@ -2398,7 +2398,7 @@ impl Fragment { } } - pub fn repair_style(&mut self, new_style: &Arc) { + pub fn repair_style(&mut self, new_style: &StyleArc) { self.style = (*new_style).clone() } diff --git a/components/layout/generated_content.rs b/components/layout/generated_content.rs index a23fd165ecf..5f6f9b1e706 100644 --- a/components/layout/generated_content.rs +++ b/components/layout/generated_content.rs @@ -16,7 +16,6 @@ use gfx::display_list::OpaqueNode; use script_layout_interface::wrapper_traits::PseudoElementType; use smallvec::SmallVec; use std::collections::{HashMap, LinkedList}; -use std::sync::Arc; use style::computed_values::{display, list_style_type}; use style::computed_values::content::ContentItem; use style::properties::ServoComputedValues; @@ -369,7 +368,7 @@ impl Counter { layout_context: &LayoutContext, node: OpaqueNode, pseudo: PseudoElementType<()>, - style: Arc, + style: ::StyleArc, list_style_type: list_style_type::T, mode: RenderingMode) -> Option { @@ -432,7 +431,7 @@ struct CounterValue { fn render_text(layout_context: &LayoutContext, node: OpaqueNode, pseudo: PseudoElementType<()>, - style: Arc, + style: ::StyleArc, string: String) -> Option { let mut fragments = LinkedList::new(); diff --git a/components/layout/inline.rs b/components/layout/inline.rs index f28e583460f..d8cd4003ab3 100644 --- a/components/layout/inline.rs +++ b/components/layout/inline.rs @@ -4,6 +4,7 @@ #![deny(unsafe_code)] +use StyleArc; use app_units::Au; use block::AbsoluteAssignBSizesTraversal; use context::LayoutContext; @@ -29,7 +30,6 @@ use std::{fmt, i32, isize, mem}; use std::cmp::max; use std::collections::VecDeque; use std::sync::Arc; -use style::arc_ptr_eq; use style::computed_values::{display, overflow_x, position, text_align, text_justify}; use style::computed_values::{vertical_align, white_space}; use style::logical_geometry::{LogicalRect, LogicalSize, WritingMode}; @@ -398,7 +398,7 @@ impl LineBreaker { result.border_padding.inline_end == Au(0) && candidate.border_padding.inline_start == Au(0) && result_info.selected() == candidate_info.selected() && - arc_ptr_eq(&result_info.run, &candidate_info.run) && + ::arc_ptr_eq(&result_info.run, &candidate_info.run) && inline_contexts_are_equal(&result.inline_context, &candidate.inline_context) } @@ -1657,7 +1657,7 @@ impl Flow for InlineFlow { self.build_display_list_for_inline(state); } - fn repair_style(&mut self, _: &Arc) {} + fn repair_style(&mut self, _: &StyleArc) {} fn compute_overflow(&self) -> Overflow { let mut overflow = Overflow::new(); @@ -1746,8 +1746,8 @@ impl fmt::Debug for InlineFlow { #[derive(Clone)] pub struct InlineFragmentNodeInfo { pub address: OpaqueNode, - pub style: Arc, - pub selected_style: Arc, + pub style: StyleArc, + pub selected_style: StyleArc, pub pseudo: PseudoElementType<()>, pub flags: InlineFragmentNodeFlags, } diff --git a/components/layout/lib.rs b/components/layout/lib.rs index 4c5bd766e24..dd86ba46cde 100644 --- a/components/layout/lib.rs +++ b/components/layout/lib.rs @@ -93,3 +93,15 @@ pub mod wrapper; // For unit tests: pub use fragment::Fragment; pub use fragment::SpecificFragmentInfo; + +/// Returns whether the two arguments point to the same value. +/// +/// FIXME: Remove this and use Arc::ptr_eq once we require Rust 1.17 +#[inline] +pub fn arc_ptr_eq(a: &::std::sync::Arc, b: &::std::sync::Arc) -> bool { + ::style::ptr_eq::(&**a, &**b) +} + +// We can't use stylearc for everything in layout, because the Flow stuff uses +// weak references. +use style::stylearc::Arc as StyleArc; diff --git a/components/layout/list_item.rs b/components/layout/list_item.rs index cb260480cb9..8a79aaa40b7 100644 --- a/components/layout/list_item.rs +++ b/components/layout/list_item.rs @@ -18,7 +18,6 @@ use fragment::{CoordinateSystem, Fragment, FragmentBorderBoxIterator, GeneratedC use fragment::Overflow; use generated_content; use inline::InlineFlow; -use std::sync::Arc; use style::computed_values::{list_style_type, position}; use style::logical_geometry::LogicalSize; use style::properties::ServoComputedValues; @@ -148,7 +147,7 @@ impl Flow for ListItemFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/multicol.rs b/components/layout/multicol.rs index 18cfe6409cb..b43a6eb17ff 100644 --- a/components/layout/multicol.rs +++ b/components/layout/multicol.rs @@ -6,6 +6,7 @@ #![deny(unsafe_code)] +use StyleArc; use app_units::Au; use block::BlockFlow; use context::LayoutContext; @@ -193,7 +194,7 @@ impl Flow for MulticolFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &StyleArc) { self.block_flow.repair_style(new_style) } @@ -275,7 +276,7 @@ impl Flow for MulticolColumnFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/query.rs b/components/layout/query.rs index 92c3a1525dd..6b594066963 100644 --- a/components/layout/query.rs +++ b/components/layout/query.rs @@ -55,7 +55,7 @@ pub struct LayoutThreadData { pub display_list: Option>, /// Performs CSS selector matching and style resolution. - pub stylist: Arc, + pub stylist: ::StyleArc, /// A queued response for the union of the content boxes of a node. pub content_box_response: Option>, diff --git a/components/layout/table.rs b/components/layout/table.rs index b130b62bd04..c3ba52a305b 100644 --- a/components/layout/table.rs +++ b/components/layout/table.rs @@ -21,7 +21,6 @@ use layout_debug; use model::{IntrinsicISizes, IntrinsicISizesContribution, MaybeAuto}; use std::cmp; use std::fmt; -use std::sync::Arc; use style::computed_values::{border_collapse, border_spacing, table_layout}; use style::context::SharedStyleContext; use style::logical_geometry::LogicalSize; @@ -507,7 +506,7 @@ impl Flow for TableFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/table_caption.rs b/components/layout/table_caption.rs index fe3eb5bf692..99d95fd9587 100644 --- a/components/layout/table_caption.rs +++ b/components/layout/table_caption.rs @@ -15,7 +15,6 @@ use flow::{Flow, FlowClass, OpaqueFlow}; use fragment::{Fragment, FragmentBorderBoxIterator, Overflow}; use gfx_traits::print_tree::PrintTree; use std::fmt; -use std::sync::Arc; use style::logical_geometry::LogicalSize; use style::properties::ServoComputedValues; @@ -84,7 +83,7 @@ impl Flow for TableCaptionFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/table_cell.rs b/components/layout/table_cell.rs index b40d25e936c..8c54297d4b5 100644 --- a/components/layout/table_cell.rs +++ b/components/layout/table_cell.rs @@ -19,7 +19,6 @@ use layout_debug; use model::MaybeAuto; use script_layout_interface::wrapper_traits::ThreadSafeLayoutNode; use std::fmt; -use std::sync::Arc; use style::computed_values::{border_collapse, border_top_style, vertical_align}; use style::logical_geometry::{LogicalMargin, LogicalRect, LogicalSize, WritingMode}; use style::properties::ServoComputedValues; @@ -264,7 +263,7 @@ impl Flow for TableCellFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/table_colgroup.rs b/components/layout/table_colgroup.rs index 5603a02e3c9..85d5207646e 100644 --- a/components/layout/table_colgroup.rs +++ b/components/layout/table_colgroup.rs @@ -15,7 +15,6 @@ use fragment::{Fragment, FragmentBorderBoxIterator, Overflow, SpecificFragmentIn use layout_debug; use std::cmp::max; use std::fmt; -use std::sync::Arc; use style::logical_geometry::LogicalSize; use style::properties::ServoComputedValues; use style::values::computed::LengthOrPercentageOrAuto; @@ -95,7 +94,7 @@ impl Flow for TableColGroupFlow { fn collect_stacking_contexts(&mut self, _: &mut DisplayListBuildState) {} - fn repair_style(&mut self, _: &Arc) {} + fn repair_style(&mut self, _: &::StyleArc) {} fn compute_overflow(&self) -> Overflow { Overflow::new() diff --git a/components/layout/table_row.rs b/components/layout/table_row.rs index d1c7146d247..8d074f59c52 100644 --- a/components/layout/table_row.rs +++ b/components/layout/table_row.rs @@ -22,7 +22,6 @@ use serde::{Serialize, Serializer}; use std::cmp::max; use std::fmt; use std::iter::{Enumerate, IntoIterator, Peekable}; -use std::sync::Arc; use style::computed_values::{border_collapse, border_spacing, border_top_style}; use style::logical_geometry::{LogicalSize, PhysicalSide, WritingMode}; use style::properties::ServoComputedValues; @@ -483,7 +482,7 @@ impl Flow for TableRowFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/table_rowgroup.rs b/components/layout/table_rowgroup.rs index e4c4291bce4..7664796b082 100644 --- a/components/layout/table_rowgroup.rs +++ b/components/layout/table_rowgroup.rs @@ -18,7 +18,6 @@ use layout_debug; use serde::{Serialize, Serializer}; use std::fmt; use std::iter::{IntoIterator, Iterator, Peekable}; -use std::sync::Arc; use style::computed_values::{border_collapse, border_spacing}; use style::logical_geometry::LogicalSize; use style::properties::ServoComputedValues; @@ -186,7 +185,7 @@ impl Flow for TableRowGroupFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/table_wrapper.rs b/components/layout/table_wrapper.rs index 64799a37e42..015f32a9762 100644 --- a/components/layout/table_wrapper.rs +++ b/components/layout/table_wrapper.rs @@ -27,7 +27,6 @@ use model::MaybeAuto; use std::cmp::{max, min}; use std::fmt; use std::ops::Add; -use std::sync::Arc; use style::computed_values::{border_collapse, position, table_layout}; use style::context::SharedStyleContext; use style::logical_geometry::{LogicalRect, LogicalSize}; @@ -467,7 +466,7 @@ impl Flow for TableWrapperFlow { self.block_flow.collect_stacking_contexts(state); } - fn repair_style(&mut self, new_style: &Arc) { + fn repair_style(&mut self, new_style: &::StyleArc) { self.block_flow.repair_style(new_style) } diff --git a/components/layout/text.rs b/components/layout/text.rs index 5c9b93edee1..43b2fd086e7 100644 --- a/components/layout/text.rs +++ b/components/layout/text.rs @@ -435,7 +435,7 @@ fn bounding_box_for_run_metrics(metrics: &RunMetrics, writing_mode: WritingMode) /// /// `#[inline]` because often the caller only needs a few fields from the font metrics. #[inline] -pub fn font_metrics_for_style(font_context: &mut FontContext, font_style: Arc) +pub fn font_metrics_for_style(font_context: &mut FontContext, font_style: ::StyleArc) -> FontMetrics { let fontgroup = font_context.layout_font_group_for_style(font_style); // FIXME(https://github.com/rust-lang/rust/issues/23338) diff --git a/components/layout_thread/lib.rs b/components/layout_thread/lib.rs index 30d59180a3c..054d82397d5 100644 --- a/components/layout_thread/lib.rs +++ b/components/layout_thread/lib.rs @@ -115,6 +115,7 @@ use style::logical_geometry::LogicalPoint; use style::media_queries::{Device, MediaList, MediaType}; use style::servo::restyle_damage::{REFLOW, REFLOW_OUT_OF_FLOW, REPAINT, REPOSITION, STORE_OVERFLOW}; use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard, StylesheetGuards}; +use style::stylearc::Arc as StyleArc; use style::stylesheets::{Origin, Stylesheet, UserAgentStylesheets}; use style::stylist::{ExtraStyleData, Stylist}; use style::thread_state; @@ -192,10 +193,10 @@ pub struct LayoutThread { document_shared_lock: Option, /// The list of currently-running animations. - running_animations: Arc>>>, + running_animations: StyleArc>>>, /// The list of animations that have expired since the last style recalculation. - expired_animations: Arc>>>, + expired_animations: StyleArc>>>, /// A counter for epoch messages epoch: Epoch, @@ -412,7 +413,7 @@ impl LayoutThread { let font_cache_receiver = ROUTER.route_ipc_receiver_to_new_mpsc_receiver(ipc_font_cache_receiver); - let stylist = Arc::new(Stylist::new(device)); + let stylist = StyleArc::new(Stylist::new(device)); let outstanding_web_fonts_counter = Arc::new(AtomicUsize::new(0)); let ua_stylesheets = &*UA_STYLESHEETS; let guard = ua_stylesheets.shared_lock.read(); @@ -448,8 +449,8 @@ impl LayoutThread { outstanding_web_fonts: outstanding_web_fonts_counter, root_flow: None, document_shared_lock: None, - running_animations: Arc::new(RwLock::new(HashMap::new())), - expired_animations: Arc::new(RwLock::new(HashMap::new())), + running_animations: StyleArc::new(RwLock::new(HashMap::new())), + expired_animations: StyleArc::new(RwLock::new(HashMap::new())), epoch: Epoch(0), viewport_size: Size2D::new(Au(0), Au(0)), webrender_api: webrender_api_sender.create_api(), @@ -741,7 +742,7 @@ impl LayoutThread { } fn handle_add_stylesheet<'a, 'b>(&self, - stylesheet: Arc, + stylesheet: StyleArc, possibly_locked_rw_data: &mut RwData<'a, 'b>) { // Find all font-face rules and notify the font cache of them. // GWTODO: Need to handle unloading web fonts. @@ -776,7 +777,7 @@ impl LayoutThread { possibly_locked_rw_data: &mut RwData<'a, 'b>, quirks_mode: QuirksMode) { let mut rw_data = possibly_locked_rw_data.lock(); - Arc::get_mut(&mut rw_data.stylist).unwrap().set_quirks_mode(quirks_mode); + StyleArc::get_mut(&mut rw_data.stylist).unwrap().set_quirks_mode(quirks_mode); possibly_locked_rw_data.block(rw_data); } @@ -1034,7 +1035,7 @@ impl LayoutThread { self.document_shared_lock = Some(document_shared_lock.clone()); let author_guard = document_shared_lock.read(); let device = Device::new(MediaType::Screen, initial_viewport); - Arc::get_mut(&mut rw_data.stylist).unwrap() + StyleArc::get_mut(&mut rw_data.stylist).unwrap() .set_device(device, &author_guard, &data.document_stylesheets); self.viewport_size = @@ -1088,7 +1089,7 @@ impl LayoutThread { let mut extra_data = ExtraStyleData { marker: PhantomData, }; - let needs_dirtying = Arc::get_mut(&mut rw_data.stylist).unwrap().update( + let needs_dirtying = StyleArc::get_mut(&mut rw_data.stylist).unwrap().update( &data.document_stylesheets, &guards, Some(ua_stylesheets), diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 1bc9c165fb1..2ecae0d9d73 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -99,6 +99,7 @@ use style::media_queries::MediaList; use style::properties::PropertyDeclarationBlock; use style::selector_parser::{PseudoElement, Snapshot}; use style::shared_lock::{SharedRwLock as StyleSharedRwLock, Locked as StyleLocked}; +use style::stylearc::Arc as StyleArc; use style::stylesheets::{CssRules, FontFaceRule, KeyframesRule, MediaRule}; use style::stylesheets::{NamespaceRule, StyleRule, ImportRule, SupportsRule}; use style::values::specified::Length; @@ -166,6 +167,12 @@ unsafe impl JSTraceable for Arc { } } +unsafe impl JSTraceable for StyleArc { + unsafe fn trace(&self, trc: *mut JSTracer) { + (**self).trace(trc) + } +} + unsafe impl JSTraceable for Box { unsafe fn trace(&self, trc: *mut JSTracer) { (**self).trace(trc) diff --git a/components/script/dom/cssconditionrule.rs b/components/script/dom/cssconditionrule.rs index 8bf5dad6c93..1944c28c379 100644 --- a/components/script/dom/cssconditionrule.rs +++ b/components/script/dom/cssconditionrule.rs @@ -10,8 +10,8 @@ use dom::cssmediarule::CSSMediaRule; use dom::cssstylesheet::CSSStyleSheet; use dom::csssupportsrule::CSSSupportsRule; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{SharedRwLock, Locked}; +use style::stylearc::Arc; use style::stylesheets::CssRules as StyleCssRules; #[dom_struct] diff --git a/components/script/dom/cssfontfacerule.rs b/components/script/dom/cssfontfacerule.rs index 1f247c25fdc..560722d2452 100644 --- a/components/script/dom/cssfontfacerule.rs +++ b/components/script/dom/cssfontfacerule.rs @@ -10,8 +10,8 @@ use dom::cssrule::{CSSRule, SpecificCSSRule}; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::FontFaceRule; #[dom_struct] diff --git a/components/script/dom/cssgroupingrule.rs b/components/script/dom/cssgroupingrule.rs index 249aaccd42e..4fd77b46a7c 100644 --- a/components/script/dom/cssgroupingrule.rs +++ b/components/script/dom/cssgroupingrule.rs @@ -12,8 +12,8 @@ use dom::cssrule::CSSRule; use dom::cssrulelist::{CSSRuleList, RulesSource}; use dom::cssstylesheet::CSSStyleSheet; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{SharedRwLock, Locked}; +use style::stylearc::Arc; use style::stylesheets::CssRules as StyleCssRules; #[dom_struct] diff --git a/components/script/dom/cssimportrule.rs b/components/script/dom/cssimportrule.rs index 3c0eb7eb4aa..d9d867cbc19 100644 --- a/components/script/dom/cssimportrule.rs +++ b/components/script/dom/cssimportrule.rs @@ -10,8 +10,8 @@ use dom::cssrule::{CSSRule, SpecificCSSRule}; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::ImportRule; #[dom_struct] diff --git a/components/script/dom/csskeyframerule.rs b/components/script/dom/csskeyframerule.rs index d36e8988e04..46dac811d9f 100644 --- a/components/script/dom/csskeyframerule.rs +++ b/components/script/dom/csskeyframerule.rs @@ -12,9 +12,9 @@ use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSSt use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::keyframes::Keyframe; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; #[dom_struct] pub struct CSSKeyframeRule { diff --git a/components/script/dom/csskeyframesrule.rs b/components/script/dom/csskeyframesrule.rs index a3715e914ed..f48abe60751 100644 --- a/components/script/dom/csskeyframesrule.rs +++ b/components/script/dom/csskeyframesrule.rs @@ -16,9 +16,9 @@ use dom::cssrulelist::{CSSRuleList, RulesSource}; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::keyframes::{Keyframe, KeyframeSelector}; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::KeyframesRule; use style::values::KeyframesName; diff --git a/components/script/dom/cssmediarule.rs b/components/script/dom/cssmediarule.rs index 45bf65b1adf..afc41d31be0 100644 --- a/components/script/dom/cssmediarule.rs +++ b/components/script/dom/cssmediarule.rs @@ -15,10 +15,10 @@ use dom::cssstylesheet::CSSStyleSheet; use dom::medialist::MediaList; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::media_queries::parse_media_query_list; use style::parser::{LengthParsingMode, ParserContext}; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::{CssRuleType, MediaRule}; use style_traits::ToCss; diff --git a/components/script/dom/cssnamespacerule.rs b/components/script/dom/cssnamespacerule.rs index 744a8020667..eafabf8a4a6 100644 --- a/components/script/dom/cssnamespacerule.rs +++ b/components/script/dom/cssnamespacerule.rs @@ -11,8 +11,8 @@ use dom::cssrule::{CSSRule, SpecificCSSRule}; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::NamespaceRule; #[dom_struct] diff --git a/components/script/dom/cssrulelist.rs b/components/script/dom/cssrulelist.rs index 67eac2beda2..d0dfe120410 100644 --- a/components/script/dom/cssrulelist.rs +++ b/components/script/dom/cssrulelist.rs @@ -13,8 +13,8 @@ use dom::cssrule::CSSRule; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::Locked; +use style::stylearc::Arc; use style::stylesheets::{CssRules, CssRulesHelpers, KeyframesRule, RulesMutateError}; #[allow(unsafe_code)] diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index d0996f04567..8b81201de77 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -16,13 +16,13 @@ use dom::window::Window; use dom_struct::dom_struct; use servo_url::ServoUrl; use std::ascii::AsciiExt; -use std::sync::Arc; use style::attr::AttrValue; use style::parser::LengthParsingMode; use style::properties::{Importance, PropertyDeclarationBlock, PropertyId, LonghandId, ShorthandId}; use style::properties::{parse_one_declaration, parse_style_attribute}; use style::selector_parser::PseudoElement; use style::shared_lock::Locked; +use style::stylearc::Arc; use style_traits::ToCss; // http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface diff --git a/components/script/dom/cssstylerule.rs b/components/script/dom/cssstylerule.rs index fed2b947f90..607f2cfb816 100644 --- a/components/script/dom/cssstylerule.rs +++ b/components/script/dom/cssstylerule.rs @@ -12,8 +12,8 @@ use dom::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSSt use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::StyleRule; #[dom_struct] diff --git a/components/script/dom/cssstylesheet.rs b/components/script/dom/cssstylesheet.rs index ee097313c2e..cb378b2261a 100644 --- a/components/script/dom/cssstylesheet.rs +++ b/components/script/dom/cssstylesheet.rs @@ -15,8 +15,8 @@ use dom::stylesheet::StyleSheet; use dom::window::Window; use dom_struct::dom_struct; use std::cell::Cell; -use std::sync::Arc; use style::shared_lock::SharedRwLock; +use style::stylearc::Arc; use style::stylesheets::Stylesheet as StyleStyleSheet; #[dom_struct] diff --git a/components/script/dom/csssupportsrule.rs b/components/script/dom/csssupportsrule.rs index 86aedf10864..9a0743856d6 100644 --- a/components/script/dom/csssupportsrule.rs +++ b/components/script/dom/csssupportsrule.rs @@ -13,9 +13,9 @@ use dom::cssrule::SpecificCSSRule; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::parser::{LengthParsingMode, ParserContext}; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::stylesheets::{CssRuleType, SupportsRule}; use style::supports::SupportsCondition; use style_traits::ToCss; diff --git a/components/script/dom/cssviewportrule.rs b/components/script/dom/cssviewportrule.rs index 38abf909ff0..4438aabb53e 100644 --- a/components/script/dom/cssviewportrule.rs +++ b/components/script/dom/cssviewportrule.rs @@ -10,8 +10,8 @@ use dom::cssrule::{CSSRule, SpecificCSSRule}; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::shared_lock::{Locked, ToCssWithGuard}; +use style::stylearc::Arc; use style::viewport::ViewportRule; #[dom_struct] diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 06b8f7596cf..024c201a85c 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -128,7 +128,6 @@ use std::default::Default; use std::iter::once; use std::mem; use std::rc::Rc; -use std::sync::Arc; use std::time::{Duration, Instant}; use style::attr::AttrValue; use style::context::{QuirksMode, ReflowGoal}; @@ -136,6 +135,7 @@ use style::restyle_hints::{RestyleHint, RESTYLE_SELF, RESTYLE_STYLE_ATTRIBUTE}; use style::selector_parser::{RestyleDamage, Snapshot}; use style::shared_lock::SharedRwLock as StyleSharedRwLock; use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join}; +use style::stylearc::Arc; use style::stylesheets::Stylesheet; use task_source::TaskSource; use time; diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index d6974fb9075..a7d2d96fe73 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -96,7 +96,6 @@ use std::convert::TryFrom; use std::default::Default; use std::fmt; use std::rc::Rc; -use std::sync::Arc; use style::attr::{AttrValue, LengthOrPercentageOrAuto}; use style::context::{QuirksMode, ReflowGoal}; use style::element_state::*; @@ -107,6 +106,7 @@ use style::rule_tree::CascadeLevel; use style::selector_parser::{NonTSPseudoClass, RestyleDamage, SelectorImpl, SelectorParser}; use style::shared_lock::{SharedRwLock, Locked}; use style::sink::Push; +use style::stylearc::Arc; use style::stylist::ApplicableDeclarationBlock; use style::thread_state; use style::values::CSSFloat; diff --git a/components/script/dom/htmllinkelement.rs b/components/script/dom/htmllinkelement.rs index bf36d28b29d..f3c12a30699 100644 --- a/components/script/dom/htmllinkelement.rs +++ b/components/script/dom/htmllinkelement.rs @@ -30,11 +30,11 @@ use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::cell::Cell; use std::default::Default; -use std::sync::Arc; use style::attr::AttrValue; use style::media_queries::parse_media_query_list; use style::parser::{LengthParsingMode, ParserContext as CssParserContext}; use style::str::HTML_SPACE_CHARACTERS; +use style::stylearc::Arc; use style::stylesheets::{CssRuleType, Stylesheet}; use stylesheet_loader::{StylesheetLoader, StylesheetContextSource, StylesheetOwner}; diff --git a/components/script/dom/htmlmetaelement.rs b/components/script/dom/htmlmetaelement.rs index 62d7215e3df..0c1ac5a0229 100644 --- a/components/script/dom/htmlmetaelement.rs +++ b/components/script/dom/htmlmetaelement.rs @@ -21,11 +21,11 @@ use dom_struct::dom_struct; use html5ever_atoms::LocalName; use servo_config::prefs::PREFS; use std::ascii::AsciiExt; -use std::sync::Arc; use std::sync::atomic::AtomicBool; use style::attr::AttrValue; use style::media_queries::MediaList; use style::str::HTML_SPACE_CHARACTERS; +use style::stylearc::Arc; use style::stylesheets::{Stylesheet, CssRule, CssRules, Origin}; use style::viewport::ViewportRule; diff --git a/components/script/dom/htmlobjectelement.rs b/components/script/dom/htmlobjectelement.rs index c80a7838198..ee847610791 100755 --- a/components/script/dom/htmlobjectelement.rs +++ b/components/script/dom/htmlobjectelement.rs @@ -21,7 +21,7 @@ use dom_struct::dom_struct; use html5ever_atoms::LocalName; use net_traits::image::base::Image; use std::default::Default; -use std::sync::Arc; +use style::stylearc::Arc; #[dom_struct] pub struct HTMLObjectElement { diff --git a/components/script/dom/htmlstyleelement.rs b/components/script/dom/htmlstyleelement.rs index 8905c3baa65..8b9a2c74fcb 100644 --- a/components/script/dom/htmlstyleelement.rs +++ b/components/script/dom/htmlstyleelement.rs @@ -23,9 +23,9 @@ use html5ever_atoms::LocalName; use net_traits::ReferrerPolicy; use script_layout_interface::message::Msg; use std::cell::Cell; -use std::sync::Arc; use style::media_queries::parse_media_query_list; use style::parser::{LengthParsingMode, ParserContext as CssParserContext}; +use style::stylearc::Arc; use style::stylesheets::{CssRuleType, Stylesheet, Origin}; use stylesheet_loader::{StylesheetLoader, StylesheetOwner}; diff --git a/components/script/dom/medialist.rs b/components/script/dom/medialist.rs index f2a2299d169..514bccecc4a 100644 --- a/components/script/dom/medialist.rs +++ b/components/script/dom/medialist.rs @@ -12,11 +12,11 @@ use dom::bindings::str::DOMString; use dom::cssstylesheet::CSSStyleSheet; use dom::window::Window; use dom_struct::dom_struct; -use std::sync::Arc; use style::media_queries::{MediaQuery, parse_media_query_list}; use style::media_queries::MediaList as StyleMediaList; use style::parser::{LengthParsingMode, ParserContext}; use style::shared_lock::{SharedRwLock, Locked}; +use style::stylearc::Arc; use style::stylesheets::CssRuleType; use style_traits::ToCss; diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 0384c4ed19d..aa0fcc9a2a1 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -78,10 +78,10 @@ use std::default::Default; use std::iter; use std::mem; use std::ops::Range; -use std::sync::Arc; use style::context::QuirksMode; use style::dom::OpaqueNode; use style::selector_parser::{SelectorImpl, SelectorParser}; +use style::stylearc::Arc; use style::stylesheets::Stylesheet; use style::thread_state; use uuid::Uuid; diff --git a/components/script/layout_wrapper.rs b/components/script/layout_wrapper.rs index 3ddb1d2b5c9..5fe45595388 100644 --- a/components/script/layout_wrapper.rs +++ b/components/script/layout_wrapper.rs @@ -58,7 +58,6 @@ use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem::transmute; -use std::sync::Arc; use std::sync::atomic::Ordering; use style; use style::attr::AttrValue; @@ -74,6 +73,7 @@ use style::selector_parser::{NonTSPseudoClass, PseudoElement, SelectorImpl}; use style::shared_lock::{SharedRwLock as StyleSharedRwLock, Locked as StyleLocked}; use style::sink::Push; use style::str::is_whitespace; +use style::stylearc::Arc; use style::stylist::ApplicableDeclarationBlock; #[derive(Copy, Clone)] diff --git a/components/script/stylesheet_loader.rs b/components/script/stylesheet_loader.rs index 14b8c385e39..3143d8b34ad 100644 --- a/components/script/stylesheet_loader.rs +++ b/components/script/stylesheet_loader.rs @@ -24,9 +24,10 @@ use net_traits::request::{CorsSettings, CredentialsMode, Destination, RequestIni use network_listener::{NetworkListener, PreInvoke}; use servo_url::ServoUrl; use std::mem; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use style::media_queries::MediaList; use style::shared_lock::Locked as StyleLocked; +use style::stylearc::Arc; use style::stylesheets::{ImportRule, Stylesheet, Origin}; use style::stylesheets::StylesheetLoader as StyleStylesheetLoader; @@ -207,7 +208,7 @@ impl<'a> StylesheetLoader<'a> { let document = document_from_node(self.elem); let gen = self.elem.downcast::() .map(HTMLLinkElement::get_request_generation_id); - let context = Arc::new(Mutex::new(StylesheetContext { + let context = ::std::sync::Arc::new(Mutex::new(StylesheetContext { elem: Trusted::new(&*self.elem), source: source, url: url.clone(), diff --git a/components/script_layout_interface/message.rs b/components/script_layout_interface/message.rs index b94b6cd8d14..8b04a131404 100644 --- a/components/script_layout_interface/message.rs +++ b/components/script_layout_interface/message.rs @@ -25,7 +25,7 @@ use style::stylesheets::Stylesheet; /// Asynchronous messages that script can send to layout. pub enum Msg { /// Adds the given stylesheet to the document. - AddStylesheet(Arc), + AddStylesheet(::style::stylearc::Arc), /// Change the quirks mode. SetQuirksMode(QuirksMode), @@ -116,7 +116,7 @@ pub struct ScriptReflow { /// The document node. pub document: TrustedNodeAddress, /// The document's list of stylesheets. - pub document_stylesheets: Vec>, + pub document_stylesheets: Vec<::style::stylearc::Arc>, /// Whether the document's stylesheets have changed since the last script reflow. pub stylesheets_changed: bool, /// The current window size. diff --git a/components/script_layout_interface/wrapper_traits.rs b/components/script_layout_interface/wrapper_traits.rs index 4abc74bcac0..bb7d1e373cf 100644 --- a/components/script_layout_interface/wrapper_traits.rs +++ b/components/script_layout_interface/wrapper_traits.rs @@ -15,7 +15,6 @@ use msg::constellation_msg::PipelineId; use range::Range; use servo_url::ServoUrl; use std::fmt::Debug; -use std::sync::Arc; use style::computed_values::display; use style::context::SharedStyleContext; use style::data::ElementData; @@ -24,6 +23,7 @@ use style::dom::OpaqueNode; use style::font_metrics::ServoMetricsProvider; use style::properties::{CascadeFlags, ServoComputedValues}; use style::selector_parser::{PseudoElement, PseudoElementCascadeType, SelectorImpl}; +use style::stylearc::Arc; use webrender_traits::ClipId; #[derive(Copy, PartialEq, Clone, Debug)] From 7dba36ed73de4ea00a8bd2e7690312f3e51440a9 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 2 May 2017 11:08:31 -0700 Subject: [PATCH 5/6] Make StyleBuilder more efficient using stylearc. MozReview-Commit-ID: 8wSsYPEmYE4 --- components/style/properties/properties.mako.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 83ca3e3d4fb..ff2361fb792 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -2056,7 +2056,7 @@ pub enum StyleStructRef<'a, T: 'a> { /// A borrowed struct from the parent, for example, for inheriting style. Borrowed(&'a Arc), /// An owned struct, that we've already mutated. - Owned(T), + Owned(Arc), } impl<'a, T: 'a> StyleStructRef<'a, T> @@ -2066,11 +2066,11 @@ impl<'a, T: 'a> StyleStructRef<'a, T> /// borrowed value, or returning the owned one. pub fn mutate(&mut self) -> &mut T { if let StyleStructRef::Borrowed(v) = *self { - *self = StyleStructRef::Owned((**v).clone()); + *self = StyleStructRef::Owned(Arc::new((**v).clone())); } match *self { - StyleStructRef::Owned(ref mut v) => v, + StyleStructRef::Owned(ref mut v) => Arc::get_mut(v).unwrap(), StyleStructRef::Borrowed(..) => unreachable!(), } } @@ -2079,7 +2079,7 @@ impl<'a, T: 'a> StyleStructRef<'a, T> /// hasn't been mutated. pub fn get_if_mutated(&mut self) -> Option<<&mut T> { match *self { - StyleStructRef::Owned(ref mut v) => Some(v), + StyleStructRef::Owned(ref mut v) => Some(Arc::get_mut(v).unwrap()), StyleStructRef::Borrowed(..) => None, } } @@ -2088,7 +2088,7 @@ impl<'a, T: 'a> StyleStructRef<'a, T> /// appropriate. pub fn build(self) -> Arc { match self { - StyleStructRef::Owned(v) => Arc::new(v), + StyleStructRef::Owned(v) => v, StyleStructRef::Borrowed(v) => v.clone(), } } @@ -2099,7 +2099,7 @@ impl<'a, T: 'a> Deref for StyleStructRef<'a, T> { fn deref(&self) -> &T { match *self { - StyleStructRef::Owned(ref v) => v, + StyleStructRef::Owned(ref v) => &**v, StyleStructRef::Borrowed(v) => &**v, } } From fd1c814e7812e68e1207165c9387ddcfd8a6379b Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 2 May 2017 17:59:53 -0700 Subject: [PATCH 6/6] Bump the DOM element sizes. This will go back down soonish when NonZero stablizes and we can use it in StyleArc, which I expect will be long before the memory overhead here matters. --- tests/unit/script/size_of.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/script/size_of.rs b/tests/unit/script/size_of.rs index a328bcbf6f5..c319c0b77c5 100644 --- a/tests/unit/script/size_of.rs +++ b/tests/unit/script/size_of.rs @@ -31,10 +31,10 @@ macro_rules! sizeof_checker ( // Update the sizes here sizeof_checker!(size_event_target, EventTarget, 40); sizeof_checker!(size_node, Node, 152); -sizeof_checker!(size_element, Element, 320); -sizeof_checker!(size_htmlelement, HTMLElement, 336); -sizeof_checker!(size_div, HTMLDivElement, 336); -sizeof_checker!(size_span, HTMLSpanElement, 336); +sizeof_checker!(size_element, Element, 328); +sizeof_checker!(size_htmlelement, HTMLElement, 344); +sizeof_checker!(size_div, HTMLDivElement, 344); +sizeof_checker!(size_span, HTMLSpanElement, 344); sizeof_checker!(size_text, Text, 184); sizeof_checker!(size_characterdata, CharacterData, 184); sizeof_checker!(size_servothreadsafelayoutnode, ServoThreadSafeLayoutNode, 16);