Use NonZero to reduce the size of DOM smart pointers (fixes #4502).

This commit is contained in:
Ms2ger 2015-01-30 22:14:17 +01:00
parent fba6b613c2
commit 5225442dc1
2 changed files with 18 additions and 14 deletions

View file

@ -53,6 +53,8 @@ use layout_interface::TrustedNodeAddress;
use script_task::STACK_ROOTS; use script_task::STACK_ROOTS;
use util::smallvec::{SmallVec, SmallVec16}; use util::smallvec::{SmallVec, SmallVec16};
use core::nonzero::NonZero;
use std::cell::{Cell, UnsafeCell}; use std::cell::{Cell, UnsafeCell};
use std::default::Default; use std::default::Default;
use std::marker::ContravariantLifetime; use std::marker::ContravariantLifetime;
@ -124,7 +126,7 @@ impl<T: Reflectable> Temporary<T> {
/// A rooted, JS-owned value. Must only be used as a field in other JS-owned types. /// A rooted, JS-owned value. Must only be used as a field in other JS-owned types.
#[must_root] #[must_root]
pub struct JS<T> { pub struct JS<T> {
ptr: *const T ptr: NonZero<*const T>
} }
impl<T> Copy for JS<T> {} impl<T> Copy for JS<T> {}
@ -149,8 +151,9 @@ impl JS<Node> {
/// Create a new JS-owned value wrapped from an address known to be a `Node` pointer. /// Create a new JS-owned value wrapped from an address known to be a `Node` pointer.
pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> JS<Node> { pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> JS<Node> {
let TrustedNodeAddress(addr) = inner; let TrustedNodeAddress(addr) = inner;
assert!(!addr.is_null());
JS { JS {
ptr: addr as *const Node ptr: NonZero::new(addr as *const Node)
} }
} }
} }
@ -158,8 +161,9 @@ impl JS<Node> {
impl<T: Reflectable> JS<T> { impl<T: Reflectable> JS<T> {
/// Create a new JS-owned value wrapped from a raw Rust pointer. /// Create a new JS-owned value wrapped from a raw Rust pointer.
pub unsafe fn from_raw(raw: *const T) -> JS<T> { pub unsafe fn from_raw(raw: *const T) -> JS<T> {
assert!(!raw.is_null());
JS { JS {
ptr: raw ptr: NonZero::new(raw)
} }
} }
@ -313,7 +317,7 @@ impl<T: Reflectable> JS<T> {
/// only method that be safely accessed from layout. (The fact that this is /// only method that be safely accessed from layout. (The fact that this is
/// unsafe is what necessitates the layout wrappers.) /// unsafe is what necessitates the layout wrappers.)
pub unsafe fn unsafe_get(&self) -> *const T { pub unsafe fn unsafe_get(&self) -> *const T {
self.ptr *self.ptr
} }
/// Store an unrooted value in this field. This is safe under the assumption that JS<T> /// Store an unrooted value in this field. This is safe under the assumption that JS<T>
@ -577,14 +581,14 @@ impl<'a, T: Reflectable> Deref for JSRef<'a, T> {
type Target = T; type Target = T;
fn deref<'b>(&'b self) -> &'b T { fn deref<'b>(&'b self) -> &'b T {
unsafe { unsafe {
&*self.ptr &**self.ptr
} }
} }
} }
/// Encapsulates a reference to something that is guaranteed to be alive. This is freely copyable. /// Encapsulates a reference to something that is guaranteed to be alive. This is freely copyable.
pub struct JSRef<'a, T> { pub struct JSRef<'a, T> {
ptr: *const T, ptr: NonZero<*const T>,
chain: ContravariantLifetime<'a>, chain: ContravariantLifetime<'a>,
} }
@ -630,7 +634,7 @@ impl<'a, T: Reflectable> JSRef<'a, T> {
/// Returns the inner pointer directly. /// Returns the inner pointer directly.
pub fn extended_deref(self) -> &'a T { pub fn extended_deref(self) -> &'a T {
unsafe { unsafe {
&*self.ptr &**self.ptr
} }
} }
} }

View file

@ -39,10 +39,10 @@ macro_rules! sizeof_checker (
// Update the sizes here // Update the sizes here
sizeof_checker!(size_event_target, EventTarget, 48); sizeof_checker!(size_event_target, EventTarget, 48);
sizeof_checker!(size_node, Node, 288); sizeof_checker!(size_node, Node, 216);
sizeof_checker!(size_element, Element, 432); sizeof_checker!(size_element, Element, 328);
sizeof_checker!(size_htmlelement, HTMLElement, 464); sizeof_checker!(size_htmlelement, HTMLElement, 344);
sizeof_checker!(size_div, HTMLDivElement, 464); sizeof_checker!(size_div, HTMLDivElement, 344);
sizeof_checker!(size_span, HTMLSpanElement, 464); sizeof_checker!(size_span, HTMLSpanElement, 344);
sizeof_checker!(size_text, Text, 320); sizeof_checker!(size_text, Text, 248);
sizeof_checker!(size_characterdata, CharacterData, 320); sizeof_checker!(size_characterdata, CharacterData, 248);