mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
style: Use atom handles in favour of atom pointers in style system code.
Differential Revision: https://phabricator.services.mozilla.com/D15078
This commit is contained in:
parent
160842b8c1
commit
63fd707bd3
4 changed files with 82 additions and 94 deletions
|
@ -15,6 +15,7 @@ use crate::gecko_bindings::bindings::Gecko_Atomize;
|
|||
use crate::gecko_bindings::bindings::Gecko_Atomize16;
|
||||
use crate::gecko_bindings::bindings::Gecko_ReleaseAtom;
|
||||
use crate::gecko_bindings::structs::{nsAtom, nsDynamicAtom, nsStaticAtom};
|
||||
use crate::gecko_bindings::structs::root::mozilla::detail::gGkAtoms;
|
||||
use nsstring::{nsAString, nsStr};
|
||||
use precomputed_hash::PrecomputedHash;
|
||||
use std::borrow::{Borrow, Cow};
|
||||
|
@ -43,9 +44,12 @@ macro_rules! local_name {
|
|||
};
|
||||
}
|
||||
|
||||
/// A strong reference to a Gecko atom.
|
||||
/// A handle to a Gecko atom.
|
||||
///
|
||||
/// This is either a strong reference to a dynamic atom (an nsAtom pointer),
|
||||
/// or an offset from gGkAtoms to the nsStaticAtom object.
|
||||
#[derive(Eq, PartialEq)]
|
||||
pub struct Atom(*mut WeakAtom);
|
||||
pub struct Atom(usize);
|
||||
|
||||
/// An atom *without* a strong reference.
|
||||
///
|
||||
|
@ -53,12 +57,31 @@ pub struct Atom(*mut WeakAtom);
|
|||
/// where `'a` is the lifetime of something that holds a strong reference to that atom.
|
||||
pub struct WeakAtom(nsAtom);
|
||||
|
||||
#[inline]
|
||||
fn valid_static_atom_addr(addr: usize) -> bool {
|
||||
unsafe {
|
||||
let start = gGkAtoms.mAtoms.get_unchecked(0) as *const _;
|
||||
let end = gGkAtoms.mAtoms.get_unchecked(gGkAtoms.mAtoms.len()) as *const _;
|
||||
let in_range = addr >= start as usize && addr < end as usize;
|
||||
let aligned = addr % mem::align_of::<nsStaticAtom>() == 0;
|
||||
in_range && aligned
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Atom {
|
||||
type Target = WeakAtom;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &WeakAtom {
|
||||
unsafe { &*self.0 }
|
||||
unsafe {
|
||||
let addr = if self.is_static() {
|
||||
(&gGkAtoms as *const _ as usize) + (self.0 >> 1)
|
||||
} else {
|
||||
self.0
|
||||
};
|
||||
debug_assert!(!self.is_static() || valid_static_atom_addr(addr));
|
||||
WeakAtom::new(addr as *const nsAtom)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -277,50 +300,73 @@ impl fmt::Display for WeakAtom {
|
|||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn make_handle(ptr: *const nsAtom) -> usize {
|
||||
debug_assert!(!ptr.is_null());
|
||||
if !WeakAtom::new(ptr).is_static() {
|
||||
ptr as usize
|
||||
} else {
|
||||
make_static_handle(ptr as *mut nsStaticAtom)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn make_static_handle(ptr: *const nsStaticAtom) -> usize {
|
||||
// FIXME(heycam): Use offset_from once it's stabilized.
|
||||
// https://github.com/rust-lang/rust/issues/41079
|
||||
debug_assert!(valid_static_atom_addr(ptr as usize));
|
||||
let base = &gGkAtoms as *const _;
|
||||
let offset = ptr as usize - base as usize;
|
||||
(offset << 1) | 1
|
||||
}
|
||||
|
||||
impl Atom {
|
||||
#[inline]
|
||||
fn is_static(&self) -> bool {
|
||||
self.0 & 1 == 1
|
||||
}
|
||||
|
||||
/// Execute a callback with the atom represented by `ptr`.
|
||||
pub unsafe fn with<F, R>(ptr: *const nsAtom, callback: F) -> R
|
||||
where
|
||||
F: FnOnce(&Atom) -> R,
|
||||
{
|
||||
let atom = Atom(WeakAtom::new(ptr));
|
||||
let atom = Atom(make_handle(ptr as *mut nsAtom));
|
||||
let ret = callback(&atom);
|
||||
mem::forget(atom);
|
||||
ret
|
||||
}
|
||||
|
||||
/// Creates an atom from an static atom pointer without checking in release
|
||||
/// builds.
|
||||
///
|
||||
/// Right now it's only used by the atom macro, and ideally it should keep
|
||||
/// that way, now we have sugar for is_static, creating atoms using
|
||||
/// Atom::from_raw should involve almost no overhead.
|
||||
/// Creates a static atom from its index in the static atom table, without
|
||||
/// checking in release builds.
|
||||
#[inline]
|
||||
pub unsafe fn from_static(ptr: *const nsStaticAtom) -> Self {
|
||||
let atom = Atom(ptr as *mut WeakAtom);
|
||||
debug_assert!(
|
||||
atom.is_static(),
|
||||
"Called from_static for a non-static atom!"
|
||||
);
|
||||
pub unsafe fn from_index(index: u16) -> Self {
|
||||
let ptr = gGkAtoms.mAtoms.get_unchecked(index as usize) as *const _;
|
||||
let handle = make_static_handle(ptr);
|
||||
let atom = Atom(handle);
|
||||
debug_assert!(valid_static_atom_addr(ptr as usize));
|
||||
debug_assert!(atom.is_static());
|
||||
debug_assert!((*atom).is_static());
|
||||
debug_assert!(handle == make_handle(atom.as_ptr()));
|
||||
atom
|
||||
}
|
||||
|
||||
/// Creates an atom from an atom pointer.
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_raw(ptr: *mut nsAtom) -> Self {
|
||||
let atom = Atom(ptr as *mut WeakAtom);
|
||||
let atom = Atom(make_handle(ptr));
|
||||
if !atom.is_static() {
|
||||
Gecko_AddRefAtom(ptr);
|
||||
}
|
||||
atom
|
||||
}
|
||||
|
||||
/// Creates an atom from a dynamic atom pointer that has already had AddRef
|
||||
/// called on it.
|
||||
/// Creates an atom from an atom pointer that has already had AddRef
|
||||
/// called on it. This may be a static or dynamic atom.
|
||||
#[inline]
|
||||
pub unsafe fn from_addrefed(ptr: *mut nsAtom) -> Self {
|
||||
assert!(!ptr.is_null());
|
||||
Atom(WeakAtom::new(ptr))
|
||||
Atom(make_handle(ptr))
|
||||
}
|
||||
|
||||
/// Convert this atom into an addrefed nsAtom pointer.
|
||||
|
@ -353,7 +399,13 @@ impl Hash for WeakAtom {
|
|||
impl Clone for Atom {
|
||||
#[inline(always)]
|
||||
fn clone(&self) -> Atom {
|
||||
unsafe { Atom::from_raw(self.as_ptr()) }
|
||||
unsafe {
|
||||
let atom = Atom(self.0);
|
||||
if !atom.is_static() {
|
||||
Gecko_AddRefAtom(atom.as_ptr());
|
||||
}
|
||||
atom
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -377,13 +429,13 @@ impl Default for Atom {
|
|||
|
||||
impl fmt::Debug for Atom {
|
||||
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(w, "Gecko Atom({:p}, {})", self.0, self)
|
||||
write!(w, "Atom(0x{:08x}, {})", self.0, self)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Atom {
|
||||
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
|
||||
unsafe { (&*self.0).fmt(w) }
|
||||
self.deref().fmt(w)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -392,10 +444,10 @@ impl<'a> From<&'a str> for Atom {
|
|||
fn from(string: &str) -> Atom {
|
||||
debug_assert!(string.len() <= u32::max_value() as usize);
|
||||
unsafe {
|
||||
Atom(WeakAtom::new(Gecko_Atomize(
|
||||
Atom::from_addrefed(Gecko_Atomize(
|
||||
string.as_ptr() as *const _,
|
||||
string.len() as u32,
|
||||
)))
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -410,7 +462,7 @@ impl<'a> From<&'a [u16]> for Atom {
|
|||
impl<'a> From<&'a nsAString> for Atom {
|
||||
#[inline]
|
||||
fn from(string: &nsAString) -> Atom {
|
||||
unsafe { Atom(WeakAtom::new(Gecko_Atomize16(string))) }
|
||||
unsafe { Atom::from_addrefed(Gecko_Atomize16(string)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue