mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
style: Make Rust static atoms able to be used in const contexts.
I see atom dropping code generated in release builds for stuff like dropping the "class" atom here: https://searchfox.org/mozilla-central/rev/4df8821c1b824db5f40f381f48432f219d99ae36/servo/components/style/gecko/wrapper.rs#592 That is silly, and I hope making Atom be able to be used in const context will help the compiler see that yeah, we're not doing anything interesting and the atom shouldn't get dropped. It also allows us to get rid of a few lazy_static!s, so we should do it anyway. In order to accomplish this, compute the offset into gGkAtoms manually instead of going through the static_atoms() array and then back to the byte offset. Differential Revision: https://phabricator.services.mozilla.com/D55039
This commit is contained in:
parent
001c511f9c
commit
31837a1efa
3 changed files with 45 additions and 46 deletions
|
@ -49,10 +49,15 @@ macro_rules! local_name {
|
|||
};
|
||||
}
|
||||
|
||||
/// A handle to a Gecko atom.
|
||||
/// A handle to a Gecko atom. This is a type that can represent either:
|
||||
///
|
||||
/// * A strong reference to a dynamic atom (an `nsAtom` pointer), in which case
|
||||
/// the `usize` just holds the pointer value.
|
||||
///
|
||||
/// * A byte offset from `gGkAtoms` to the `nsStaticAtom` object (shifted to
|
||||
/// the left one bit, and with the lower bit set to `1` to differentiate it
|
||||
/// from the above), so `(offset << 1 | 1)`.
|
||||
///
|
||||
/// 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)]
|
||||
#[repr(C)]
|
||||
pub struct Atom(NonZeroUsize);
|
||||
|
@ -87,7 +92,7 @@ fn static_atoms() -> &'static [nsStaticAtom; STATIC_ATOM_COUNT] {
|
|||
fn valid_static_atom_addr(addr: usize) -> bool {
|
||||
unsafe {
|
||||
let atoms = static_atoms();
|
||||
let start = atoms.get_unchecked(0) as *const _;
|
||||
let start = atoms.as_ptr();
|
||||
let end = atoms.get_unchecked(STATIC_ATOM_COUNT) as *const _;
|
||||
let in_range = addr >= start as usize && addr < end as usize;
|
||||
let aligned = addr % mem::align_of::<nsStaticAtom>() == 0;
|
||||
|
@ -379,17 +384,16 @@ impl Atom {
|
|||
}
|
||||
|
||||
/// Creates a static atom from its index in the static atom table, without
|
||||
/// checking in release builds.
|
||||
/// checking.
|
||||
#[inline]
|
||||
pub unsafe fn from_index(index: u16) -> Self {
|
||||
let ptr = static_atoms().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
|
||||
pub const unsafe fn from_index_unchecked(index: u16) -> Self {
|
||||
// FIXME(emilio): No support for debug_assert! in const fn for now. Note
|
||||
// that violating this invariant will debug-assert in the `Deref` impl
|
||||
// though.
|
||||
//
|
||||
// debug_assert!((index as usize) < STATIC_ATOM_COUNT);
|
||||
let offset = (index as usize) * std::mem::size_of::<nsStaticAtom>() + kGkAtomsArrayOffset as usize;
|
||||
Atom(NonZeroUsize::new_unchecked((offset << 1) | 1))
|
||||
}
|
||||
|
||||
/// Creates an atom from an atom pointer.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue