mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Servo currently uses `heapsize`, but Stylo/Gecko use `malloc_size_of`. `malloc_size_of` is better -- it handles various cases that `heapsize` does not -- so this patch changes Servo to use `malloc_size_of`. This patch makes the following changes to the `malloc_size_of` crate. - Adds `MallocSizeOf` trait implementations for numerous types, some built-in (e.g. `VecDeque`), some external and Servo-only (e.g. `string_cache`). - Makes `enclosing_size_of_op` optional, because vanilla jemalloc doesn't support that operation. - For `HashSet`/`HashMap`, falls back to a computed estimate when `enclosing_size_of_op` isn't available. - Adds an extern "C" `malloc_size_of` function that does the actual heap measurement; this is based on the same functions from the `heapsize` crate. This patch makes the following changes elsewhere. - Converts all the uses of `heapsize` to instead use `malloc_size_of`. - Disables the "heapsize"/"heap_size" feature for the external crates that provide it. - Removes the `HeapSizeOf` implementation from `hashglobe`. - Adds `ignore` annotations to a few `Rc`/`Arc`, because `malloc_size_of` doesn't derive those types, unlike `heapsize`.
101 lines
2.9 KiB
Rust
101 lines
2.9 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* 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/. */
|
|
|
|
//! The `Reflector` struct.
|
|
|
|
use dom::bindings::conversions::DerivedFrom;
|
|
use dom::bindings::root::DomRoot;
|
|
use dom::globalscope::GlobalScope;
|
|
use js::jsapi::{HandleObject, JSContext, JSObject, Heap};
|
|
use std::default::Default;
|
|
|
|
/// Create the reflector for a new DOM object and yield ownership to the
|
|
/// reflector.
|
|
pub fn reflect_dom_object<T, U>(
|
|
obj: Box<T>,
|
|
global: &U,
|
|
wrap_fn: unsafe fn(*mut JSContext, &GlobalScope, Box<T>) -> DomRoot<T>)
|
|
-> DomRoot<T>
|
|
where T: DomObject, U: DerivedFrom<GlobalScope>
|
|
{
|
|
let global_scope = global.upcast();
|
|
unsafe {
|
|
wrap_fn(global_scope.get_cx(), global_scope, obj)
|
|
}
|
|
}
|
|
|
|
/// A struct to store a reference to the reflector of a DOM object.
|
|
#[allow(unrooted_must_root)]
|
|
#[derive(MallocSizeOf)]
|
|
#[must_root]
|
|
// If you're renaming or moving this field, update the path in plugins::reflector as well
|
|
pub struct Reflector {
|
|
#[ignore_malloc_size_of = "defined and measured in rust-mozjs"]
|
|
object: Heap<*mut JSObject>,
|
|
}
|
|
|
|
#[allow(unrooted_must_root)]
|
|
impl PartialEq for Reflector {
|
|
fn eq(&self, other: &Reflector) -> bool {
|
|
self.object.get() == other.object.get()
|
|
}
|
|
}
|
|
|
|
impl Reflector {
|
|
/// Get the reflector.
|
|
#[inline]
|
|
pub fn get_jsobject(&self) -> HandleObject {
|
|
self.object.handle()
|
|
}
|
|
|
|
/// Initialize the reflector. (May be called only once.)
|
|
pub fn set_jsobject(&mut self, object: *mut JSObject) {
|
|
assert!(self.object.get().is_null());
|
|
assert!(!object.is_null());
|
|
self.object.set(object);
|
|
}
|
|
|
|
/// Return a pointer to the memory location at which the JS reflector
|
|
/// object is stored. Used to root the reflector, as
|
|
/// required by the JSAPI rooting APIs.
|
|
pub fn rootable(&self) -> &Heap<*mut JSObject> {
|
|
&self.object
|
|
}
|
|
|
|
/// Create an uninitialized `Reflector`.
|
|
pub fn new() -> Reflector {
|
|
Reflector {
|
|
object: Heap::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A trait to provide access to the `Reflector` for a DOM object.
|
|
pub trait DomObject: 'static {
|
|
/// Returns the receiver's reflector.
|
|
fn reflector(&self) -> &Reflector;
|
|
|
|
/// Returns the global scope of the realm that the DomObject was created in.
|
|
fn global(&self) -> DomRoot<GlobalScope> where Self: Sized {
|
|
GlobalScope::from_reflector(self)
|
|
}
|
|
}
|
|
|
|
impl DomObject for Reflector {
|
|
fn reflector(&self) -> &Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
/// A trait to initialize the `Reflector` for a DOM object.
|
|
pub trait MutDomObject: DomObject {
|
|
/// Initializes the Reflector
|
|
fn init_reflector(&mut self, obj: *mut JSObject);
|
|
}
|
|
|
|
impl MutDomObject for Reflector {
|
|
fn init_reflector(&mut self, obj: *mut JSObject) {
|
|
self.set_jsobject(obj)
|
|
}
|
|
}
|