mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Abstract out Node::unique_id in its own structure
An UnsafeCell is use to lazily create the Uuid.
This commit is contained in:
parent
3c73106cbb
commit
9932a5cf82
2 changed files with 49 additions and 16 deletions
|
@ -9,7 +9,6 @@ use core::nonzero::NonZero;
|
||||||
use devtools_traits::NodeInfo;
|
use devtools_traits::NodeInfo;
|
||||||
use document_loader::DocumentLoader;
|
use document_loader::DocumentLoader;
|
||||||
use dom::attr::Attr;
|
use dom::attr::Attr;
|
||||||
use dom::bindings::cell::DOMRefCell;
|
|
||||||
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
use dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
|
||||||
use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
|
use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
|
||||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||||
|
@ -57,13 +56,14 @@ use selectors::matching::matches;
|
||||||
use selectors::parser::Selector;
|
use selectors::parser::Selector;
|
||||||
use selectors::parser::parse_author_origin_selector_list_from_str;
|
use selectors::parser::parse_author_origin_selector_list_from_str;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::Cell;
|
use std::cell::{Cell, UnsafeCell};
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::iter::{self, FilterMap, Peekable};
|
use std::iter::{self, FilterMap, Peekable};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use string_cache::{Atom, Namespace, QualName};
|
use string_cache::{Atom, Namespace, QualName};
|
||||||
use style::selector_impl::ServoSelectorImpl;
|
use style::selector_impl::ServoSelectorImpl;
|
||||||
|
use util::mem::{HeapSizeOf, heap_size_of};
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
use util::thread_state;
|
use util::thread_state;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -120,7 +120,7 @@ pub struct Node {
|
||||||
/// node is finalized.
|
/// node is finalized.
|
||||||
style_and_layout_data: Cell<Option<OpaqueStyleAndLayoutData>>,
|
style_and_layout_data: Cell<Option<OpaqueStyleAndLayoutData>>,
|
||||||
|
|
||||||
unique_id: DOMRefCell<Option<Box<Uuid>>>,
|
unique_id: UniqueId,
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
|
@ -755,11 +755,7 @@ impl Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_unique_id(&self) -> String {
|
pub fn get_unique_id(&self) -> String {
|
||||||
if self.unique_id.borrow().is_none() {
|
self.unique_id.borrow().to_simple_string()
|
||||||
let mut unique_id = self.unique_id.borrow_mut();
|
|
||||||
*unique_id = Some(Box::new(Uuid::new_v4()));
|
|
||||||
}
|
|
||||||
self.unique_id.borrow().as_ref().unwrap().to_simple_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn summarize(&self) -> NodeInfo {
|
pub fn summarize(&self) -> NodeInfo {
|
||||||
|
@ -1266,7 +1262,7 @@ impl Node {
|
||||||
|
|
||||||
style_and_layout_data: Cell::new(None),
|
style_and_layout_data: Cell::new(None),
|
||||||
|
|
||||||
unique_id: DOMRefCell::new(None),
|
unique_id: UniqueId::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2438,3 +2434,40 @@ impl<'a> UnbindContext<'a> {
|
||||||
index
|
index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A node's unique ID, for devtools.
|
||||||
|
struct UniqueId {
|
||||||
|
cell: UnsafeCell<Option<Box<Uuid>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
no_jsmanaged_fields!(UniqueId);
|
||||||
|
|
||||||
|
impl HeapSizeOf for UniqueId {
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
fn heap_size_of_children(&self) -> usize {
|
||||||
|
if let &Some(ref uuid) = unsafe { &*self.cell.get() } {
|
||||||
|
heap_size_of(&** uuid as *const Uuid as *const c_void)
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UniqueId {
|
||||||
|
/// Create a new `UniqueId` value. The underlying `Uuid` is lazily created.
|
||||||
|
fn new() -> UniqueId {
|
||||||
|
UniqueId { cell: UnsafeCell::new(None) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Uuid of that unique ID.
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
fn borrow(&self) -> &Uuid {
|
||||||
|
unsafe {
|
||||||
|
let ptr = self.cell.get();
|
||||||
|
if (*ptr).is_none() {
|
||||||
|
*ptr = Some(box Uuid::new_v4());
|
||||||
|
}
|
||||||
|
&(&*ptr).as_ref().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -38,10 +38,10 @@ macro_rules! sizeof_checker (
|
||||||
|
|
||||||
// Update the sizes here
|
// Update the sizes here
|
||||||
sizeof_checker!(size_event_target, EventTarget, 40);
|
sizeof_checker!(size_event_target, EventTarget, 40);
|
||||||
sizeof_checker!(size_node, Node, 168);
|
sizeof_checker!(size_node, Node, 160);
|
||||||
sizeof_checker!(size_element, Element, 312);
|
sizeof_checker!(size_element, Element, 304);
|
||||||
sizeof_checker!(size_htmlelement, HTMLElement, 328);
|
sizeof_checker!(size_htmlelement, HTMLElement, 320);
|
||||||
sizeof_checker!(size_div, HTMLDivElement, 328);
|
sizeof_checker!(size_div, HTMLDivElement, 320);
|
||||||
sizeof_checker!(size_span, HTMLSpanElement, 328);
|
sizeof_checker!(size_span, HTMLSpanElement, 320);
|
||||||
sizeof_checker!(size_text, Text, 200);
|
sizeof_checker!(size_text, Text, 192);
|
||||||
sizeof_checker!(size_characterdata, CharacterData, 200);
|
sizeof_checker!(size_characterdata, CharacterData, 192);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue