aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/mem.rs
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2015-07-31 12:46:36 -0400
committerJosh Matthews <josh@joshmatthews.net>2015-08-03 23:05:00 -0400
commit8620fe599538effe755029e895fca6b9ca3b0f47 (patch)
treeb7e1661271c4e358a7d003cf0b4837196ad198d0 /components/script/mem.rs
parentc2497fcd49933f3782d529b891f4c8bb5de198c4 (diff)
downloadservo-8620fe599538effe755029e895fca6b9ca3b0f47.tar.gz
servo-8620fe599538effe755029e895fca6b9ca3b0f47.zip
Start reporting memory usage for Window and all nodes in all DOM trees for frame treese in script tasks.
Diffstat (limited to 'components/script/mem.rs')
-rw-r--r--components/script/mem.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/components/script/mem.rs b/components/script/mem.rs
new file mode 100644
index 00000000000..6dbd45e5b8a
--- /dev/null
+++ b/components/script/mem.rs
@@ -0,0 +1,36 @@
+/* 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/. */
+
+//! Routines for handling measuring the memory usage of arbitrary DOM nodes.
+
+use dom::bindings::codegen::InheritTypes::{DocumentCast, WindowCast, CharacterDataCast, NodeCast};
+use dom::eventtarget::{EventTarget, EventTargetTypeId};
+use dom::node::NodeTypeId;
+use libc;
+use util::mem::{HeapSizeOf, heap_size_of};
+
+// This is equivalent to measuring a Box<T>, except that DOM objects lose their
+// associated box in order to stash their pointers in a reserved slot of their
+// JS reflector. It is assumed that the caller passes a pointer to the most-derived
+// type that this pointer represents, or the actual heap usage of the pointee will
+// be under-reported.
+fn heap_size_of_self_and_children<T: HeapSizeOf>(obj: &T) -> usize {
+ heap_size_of(obj as *const T as *const libc::c_void) + obj.heap_size_of_children()
+}
+
+pub fn heap_size_of_eventtarget(target: &EventTarget) -> usize {
+ //TODO: add more specific matches for concrete element types as derive(HeapSizeOf) is
+ // added to each one.
+ match target.type_id() {
+ &EventTargetTypeId::Window =>
+ heap_size_of_self_and_children(WindowCast::to_ref(target).unwrap()),
+ &EventTargetTypeId::Node(NodeTypeId::CharacterData(_)) =>
+ heap_size_of_self_and_children(CharacterDataCast::to_ref(target).unwrap()),
+ &EventTargetTypeId::Node(NodeTypeId::Document) =>
+ heap_size_of_self_and_children(DocumentCast::to_ref(target).unwrap()),
+ &EventTargetTypeId::Node(_) =>
+ heap_size_of_self_and_children(NodeCast::to_ref(target).unwrap()),
+ _ => 0,
+ }
+}