diff options
Diffstat (limited to 'components/malloc_size_of/lib.rs')
-rw-r--r-- | components/malloc_size_of/lib.rs | 213 |
1 files changed, 125 insertions, 88 deletions
diff --git a/components/malloc_size_of/lib.rs b/components/malloc_size_of/lib.rs index f4774d66a3e..945ff9418b6 100644 --- a/components/malloc_size_of/lib.rs +++ b/components/malloc_size_of/lib.rs @@ -107,9 +107,11 @@ pub struct MallocSizeOfOps { } impl MallocSizeOfOps { - pub fn new(size_of: VoidPtrToSizeFn, - malloc_enclosing_size_of: Option<VoidPtrToSizeFn>, - have_seen_ptr: Option<Box<VoidPtrToBoolFnMut>>) -> Self { + pub fn new( + size_of: VoidPtrToSizeFn, + malloc_enclosing_size_of: Option<VoidPtrToSizeFn>, + have_seen_ptr: Option<Box<VoidPtrToBoolFnMut>>, + ) -> Self { MallocSizeOfOps { size_of_op: size_of, enclosing_size_of_op: malloc_enclosing_size_of, @@ -127,7 +129,7 @@ impl MallocSizeOfOps { // larger than the required alignment, but small enough that it is // always in the first page of memory and therefore not a legitimate // address. - return ptr as *const usize as usize <= 256 + return ptr as *const usize as usize <= 256; } /// Call `size_of_op` on `ptr`, first checking that the allocation isn't @@ -154,7 +156,10 @@ impl MallocSizeOfOps { /// Call `have_seen_ptr_op` on `ptr`. pub fn have_seen_ptr<T>(&mut self, ptr: *const T) -> bool { - let have_seen_ptr_op = self.have_seen_ptr_op.as_mut().expect("missing have_seen_ptr_op"); + let have_seen_ptr_op = self + .have_seen_ptr_op + .as_mut() + .expect("missing have_seen_ptr_op"); have_seen_ptr_op(ptr as *const c_void) } } @@ -257,7 +262,9 @@ impl MallocSizeOf for () { } impl<T1, T2> MallocSizeOf for (T1, T2) - where T1: MallocSizeOf, T2: MallocSizeOf +where + T1: MallocSizeOf, + T2: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { self.0.size_of(ops) + self.1.size_of(ops) @@ -265,7 +272,10 @@ impl<T1, T2> MallocSizeOf for (T1, T2) } impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3) - where T1: MallocSizeOf, T2: MallocSizeOf, T3: MallocSizeOf +where + T1: MallocSizeOf, + T2: MallocSizeOf, + T3: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) @@ -273,7 +283,11 @@ impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3) } impl<T1, T2, T3, T4> MallocSizeOf for (T1, T2, T3, T4) - where T1: MallocSizeOf, T2: MallocSizeOf, T3: MallocSizeOf, T4: MallocSizeOf +where + T1: MallocSizeOf, + T2: MallocSizeOf, + T3: MallocSizeOf, + T4: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) + self.3.size_of(ops) @@ -312,7 +326,8 @@ impl<T: MallocSizeOf> MallocSizeOf for std::cell::RefCell<T> { } impl<'a, B: ?Sized + ToOwned> MallocSizeOf for std::borrow::Cow<'a, B> - where B::Owned: MallocSizeOf +where + B::Owned: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { match *self { @@ -404,8 +419,9 @@ impl<A: smallvec::Array> MallocShallowSizeOf for smallvec::SmallVec<A> { } impl<A> MallocSizeOf for smallvec::SmallVec<A> - where A: smallvec::Array, - A::Item: MallocSizeOf +where + A: smallvec::Array, + A::Item: MallocSizeOf, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -417,8 +433,9 @@ impl<A> MallocSizeOf for smallvec::SmallVec<A> } impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S> - where T: Eq + Hash, - S: BuildHasher +where + T: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { if ops.has_malloc_enclosing_size_of() { @@ -426,7 +443,9 @@ impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S> // `ops.malloc_enclosing_size_of()` then gives us the storage size. // This assumes that the `HashSet`'s contents (values and hashes) // are all stored in a single contiguous heap allocation. - self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) + self.iter() + .next() + .map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) } else { // An estimate. self.capacity() * (size_of::<T>() + size_of::<usize>()) @@ -435,8 +454,9 @@ impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S> } impl<T, S> MallocSizeOf for std::collections::HashSet<T, S> - where T: Eq + Hash + MallocSizeOf, - S: BuildHasher, +where + T: Eq + Hash + MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -448,13 +468,16 @@ impl<T, S> MallocSizeOf for std::collections::HashSet<T, S> } impl<T, S> MallocShallowSizeOf for hashglobe::hash_set::HashSet<T, S> - where T: Eq + Hash, - S: BuildHasher +where + T: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { // See the implementation for std::collections::HashSet for details. if ops.has_malloc_enclosing_size_of() { - self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) + self.iter() + .next() + .map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) }) } else { self.capacity() * (size_of::<T>() + size_of::<usize>()) } @@ -462,8 +485,9 @@ impl<T, S> MallocShallowSizeOf for hashglobe::hash_set::HashSet<T, S> } impl<T, S> MallocSizeOf for hashglobe::hash_set::HashSet<T, S> - where T: Eq + Hash + MallocSizeOf, - S: BuildHasher, +where + T: Eq + Hash + MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -475,8 +499,9 @@ impl<T, S> MallocSizeOf for hashglobe::hash_set::HashSet<T, S> } impl<T, S> MallocShallowSizeOf for hashglobe::fake::HashSet<T, S> - where T: Eq + Hash, - S: BuildHasher, +where + T: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { use std::ops::Deref; @@ -485,8 +510,9 @@ impl<T, S> MallocShallowSizeOf for hashglobe::fake::HashSet<T, S> } impl<T, S> MallocSizeOf for hashglobe::fake::HashSet<T, S> - where T: Eq + Hash + MallocSizeOf, - S: BuildHasher, +where + T: Eq + Hash + MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { use std::ops::Deref; @@ -495,13 +521,16 @@ impl<T, S> MallocSizeOf for hashglobe::fake::HashSet<T, S> } impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S> - where K: Eq + Hash, - S: BuildHasher +where + K: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { // See the implementation for std::collections::HashSet for details. if ops.has_malloc_enclosing_size_of() { - self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) + self.values() + .next() + .map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) } else { self.capacity() * (size_of::<V>() + size_of::<K>() + size_of::<usize>()) } @@ -509,9 +538,10 @@ impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S> } impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S> - where K: Eq + Hash + MallocSizeOf, - V: MallocSizeOf, - S: BuildHasher, +where + K: Eq + Hash + MallocSizeOf, + V: MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -524,13 +554,16 @@ impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S> } impl<K, V, S> MallocShallowSizeOf for hashglobe::hash_map::HashMap<K, V, S> - where K: Eq + Hash, - S: BuildHasher +where + K: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { // See the implementation for std::collections::HashSet for details. if ops.has_malloc_enclosing_size_of() { - self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) + self.values() + .next() + .map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) }) } else { self.capacity() * (size_of::<V>() + size_of::<K>() + size_of::<usize>()) } @@ -538,9 +571,10 @@ impl<K, V, S> MallocShallowSizeOf for hashglobe::hash_map::HashMap<K, V, S> } impl<K, V, S> MallocSizeOf for hashglobe::hash_map::HashMap<K, V, S> - where K: Eq + Hash + MallocSizeOf, - V: MallocSizeOf, - S: BuildHasher, +where + K: Eq + Hash + MallocSizeOf, + V: MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { let mut n = self.shallow_size_of(ops); @@ -553,8 +587,9 @@ impl<K, V, S> MallocSizeOf for hashglobe::hash_map::HashMap<K, V, S> } impl<K, V, S> MallocShallowSizeOf for hashglobe::fake::HashMap<K, V, S> - where K: Eq + Hash, - S: BuildHasher, +where + K: Eq + Hash, + S: BuildHasher, { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { use std::ops::Deref; @@ -563,9 +598,10 @@ impl<K, V, S> MallocShallowSizeOf for hashglobe::fake::HashMap<K, V, S> } impl<K, V, S> MallocSizeOf for hashglobe::fake::HashMap<K, V, S> - where K: Eq + Hash + MallocSizeOf, - V: MallocSizeOf, - S: BuildHasher, +where + K: Eq + Hash + MallocSizeOf, + V: MallocSizeOf, + S: BuildHasher, { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { use std::ops::Deref; @@ -667,8 +703,10 @@ impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedRect<T, U> { impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedSideOffsets2D<T, U> { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.top.size_of(ops) + self.right.size_of(ops) + - self.bottom.size_of(ops) + self.left.size_of(ops) + self.top.size_of(ops) + + self.right.size_of(ops) + + self.bottom.size_of(ops) + + self.left.size_of(ops) } } @@ -680,22 +718,33 @@ impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedSize2D<T, U> { impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for euclid::TypedTransform2D<T, Src, Dst> { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.m11.size_of(ops) + self.m12.size_of(ops) + - self.m21.size_of(ops) + self.m22.size_of(ops) + - self.m31.size_of(ops) + self.m32.size_of(ops) + self.m11.size_of(ops) + + self.m12.size_of(ops) + + self.m21.size_of(ops) + + self.m22.size_of(ops) + + self.m31.size_of(ops) + + self.m32.size_of(ops) } } impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for euclid::TypedTransform3D<T, Src, Dst> { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.m11.size_of(ops) + self.m12.size_of(ops) + - self.m13.size_of(ops) + self.m14.size_of(ops) + - self.m21.size_of(ops) + self.m22.size_of(ops) + - self.m23.size_of(ops) + self.m24.size_of(ops) + - self.m31.size_of(ops) + self.m32.size_of(ops) + - self.m33.size_of(ops) + self.m34.size_of(ops) + - self.m41.size_of(ops) + self.m42.size_of(ops) + - self.m43.size_of(ops) + self.m44.size_of(ops) + self.m11.size_of(ops) + + self.m12.size_of(ops) + + self.m13.size_of(ops) + + self.m14.size_of(ops) + + self.m21.size_of(ops) + + self.m22.size_of(ops) + + self.m23.size_of(ops) + + self.m24.size_of(ops) + + self.m31.size_of(ops) + + self.m32.size_of(ops) + + self.m33.size_of(ops) + + self.m34.size_of(ops) + + self.m41.size_of(ops) + + self.m42.size_of(ops) + + self.m43.size_of(ops) + + self.m44.size_of(ops) } } @@ -712,8 +761,7 @@ impl MallocSizeOf for selectors::parser::AncestorHashes { } } -impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf - for selectors::parser::Selector<Impl> +impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf for selectors::parser::Selector<Impl> where Impl::NonTSPseudoClass: MallocSizeOf, Impl::PseudoElement: MallocSizeOf, @@ -733,8 +781,7 @@ where } } -impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf - for selectors::parser::Component<Impl> +impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf for selectors::parser::Component<Impl> where Impl::NonTSPseudoClass: MallocSizeOf, Impl::PseudoElement: MallocSizeOf, @@ -743,22 +790,13 @@ where use selectors::parser::Component; match self { - Component::AttributeOther(ref attr_selector) => { - attr_selector.size_of(ops) - } - Component::Negation(ref components) => { - components.size_of(ops) - } - Component::NonTSPseudoClass(ref pseudo) => { - (*pseudo).size_of(ops) - } - Component::Slotted(ref selector) | - Component::Host(Some(ref selector)) => { + Component::AttributeOther(ref attr_selector) => attr_selector.size_of(ops), + Component::Negation(ref components) => components.size_of(ops), + Component::NonTSPseudoClass(ref pseudo) => (*pseudo).size_of(ops), + Component::Slotted(ref selector) | Component::Host(Some(ref selector)) => { selector.size_of(ops) - } - Component::PseudoElement(ref pseudo) => { - (*pseudo).size_of(ops) - } + }, + Component::PseudoElement(ref pseudo) => (*pseudo).size_of(ops), Component::Combinator(..) | Component::ExplicitAnyNamespace | Component::ExplicitNoNamespace | @@ -845,12 +883,13 @@ macro_rules! malloc_size_of_is_0( ); malloc_size_of_is_0!(bool, char, str); -malloc_size_of_is_0!(u8, u16, u32, u64, usize); -malloc_size_of_is_0!(i8, i16, i32, i64, isize); +malloc_size_of_is_0!(u8, u16, u32, u64, u128, usize); +malloc_size_of_is_0!(i8, i16, i32, i64, i128, isize); malloc_size_of_is_0!(f32, f64); malloc_size_of_is_0!(std::sync::atomic::AtomicBool); -malloc_size_of_is_0!(std::sync::atomic::AtomicIsize, std::sync::atomic::AtomicUsize); +malloc_size_of_is_0!(std::sync::atomic::AtomicIsize); +malloc_size_of_is_0!(std::sync::atomic::AtomicUsize); malloc_size_of_is_0!(Range<u8>, Range<u16>, Range<u32>, Range<u64>, Range<usize>); malloc_size_of_is_0!(Range<i8>, Range<i16>, Range<i32>, Range<i64>, Range<isize>); @@ -919,9 +958,7 @@ malloc_size_of_is_0!(webrender_api::TransformStyle); #[cfg(feature = "servo")] impl MallocSizeOf for xml5ever::QualName { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.prefix.size_of(ops) + - self.ns.size_of(ops) + - self.local.size_of(ops) + self.prefix.size_of(ops) + self.ns.size_of(ops) + self.local.size_of(ops) } } @@ -946,9 +983,7 @@ impl MallocSizeOf for hyper::header::ContentType { #[cfg(feature = "servo")] impl MallocSizeOf for hyper::mime::Mime { fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - self.0.size_of(ops) + - self.1.size_of(ops) + - self.2.size_of(ops) + self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) } } @@ -975,10 +1010,12 @@ malloc_size_of_is_0!(time::Duration); malloc_size_of_is_0!(time::Tm); #[cfg(feature = "servo")] -impl<T> MallocSizeOf for hyper_serde::Serde<T> where - for <'de> hyper_serde::De<T>: serde::Deserialize<'de>, - for <'a> hyper_serde::Ser<'a, T>: serde::Serialize, - T: MallocSizeOf { +impl<T> MallocSizeOf for hyper_serde::Serde<T> +where + for<'de> hyper_serde::De<T>: serde::Deserialize<'de>, + for<'a> hyper_serde::Ser<'a, T>: serde::Serialize, + T: MallocSizeOf, +{ fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { self.0.size_of(ops) } @@ -1005,7 +1042,7 @@ impl MallocSizeOf for hyper::status::StatusCode { /// Measurable that defers to inner value and used to verify MallocSizeOf implementation in a /// struct. #[derive(Clone)] -pub struct Measurable<T: MallocSizeOf> (pub T); +pub struct Measurable<T: MallocSizeOf>(pub T); impl<T: MallocSizeOf> Deref for Measurable<T> { type Target = T; |