Say farewell to in-tree HeapSizeOf

This commit is contained in:
Anthony Ramine 2016-01-30 15:06:31 +01:00
parent 9932a5cf82
commit cb5cd8d881
80 changed files with 245 additions and 733 deletions

View file

@ -32,6 +32,6 @@ git = "https://github.com/servo/ipc-channel"
[dependencies]
cookie = "0.2"
hyper = "0.7"
url = "0.5.4"
url = {version = "0.5.4", features = ["heap_size"]}
time = "0.1"
flate2 = "0.2.0"

View file

@ -12,4 +12,4 @@ doctest = false
path = "../../../components/plugins"
[dependencies]
url = "0.5.4"
url = {version = "0.5.4", features = ["heap_size"]}

View file

@ -25,8 +25,8 @@ path = "../../../components/util"
[dependencies]
app_units = {version = "0.2", features = ["plugins"]}
url = "0.5.4"
cssparser = "0.5.2"
selectors = "0.4.1"
string_cache = "0.2.7"
url = {version = "0.5.4", features = ["heap_size"]}
cssparser = {version = "0.5.2", features = ["heap_size"]}
selectors = {version = "0.4.1", features = ["heap_size"]}
string_cache = {version = "0.2.7", features = ["heap_size"]}
euclid = {version = "0.6.1", features = ["plugins"]}

View file

@ -16,6 +16,5 @@ extern crate util;
#[cfg(test)] mod logical_geometry;
#[cfg(test)] mod thread;
#[cfg(test)] mod vec;
#[cfg(test)] mod mem;
#[cfg(test)] mod str;
#[cfg(test)] mod opts;

View file

@ -1,106 +0,0 @@
/* 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/. */
use libc::c_void;
use util::mem::{HeapSizeOf, heap_size_of};
struct Four;
impl HeapSizeOf for Four {
fn heap_size_of_children(&self) -> usize {
4
}
}
#[derive(HeapSizeOf)]
struct Eight(Four, Four, bool, bool, bool);
#[derive(HeapSizeOf)]
enum EightOrFour {
Eight(Eight),
Four(Four),
Zero(u8)
}
#[test]
fn test_heap_size() {
// Note: jemalloc often rounds up request sizes. However, it does not round up for request
// sizes of 8 and higher that are powers of two. We take advantage of knowledge here to make
// the sizes of various heap-allocated blocks predictable.
//-----------------------------------------------------------------------
// Start with basic heap block measurement.
unsafe {
// EMPTY is the special non-null address used to represent zero-size allocations.
assert_eq!(heap_size_of(::alloc::heap::EMPTY as *const c_void), 0);
// A 64 byte request is allocated exactly.
let x = ::alloc::heap::allocate(64, 0);
assert_eq!(heap_size_of(x as *const c_void), 64);
::alloc::heap::deallocate(x, 64, 0);
// A 255 byte request is rounded up to 256 bytes.
let x = ::alloc::heap::allocate(255, 0);
assert_eq!(heap_size_of(x as *const c_void), 256);
::alloc::heap::deallocate(x, 255, 0);
// A 1MiB request is allocated exactly.
let x = ::alloc::heap::allocate(1024 * 1024, 0);
assert_eq!(heap_size_of(x as *const c_void), 1024 * 1024);
::alloc::heap::deallocate(x, 1024 * 1024, 0);
}
//-----------------------------------------------------------------------
// Test HeapSizeOf implementations for various built-in types.
// Not on the heap; 0 bytes.
let x = 0i64;
assert_eq!(x.heap_size_of_children(), 0);
// An i64 is 8 bytes.
let x = Box::new(0i64);
assert_eq!(x.heap_size_of_children(), 8);
// An ascii string with 16 chars is 16 bytes in UTF-8.
assert_eq!(String::from("0123456789abcdef").heap_size_of_children(), 16);
// Not on the heap.
let x: Option<i32> = None;
assert_eq!(x.heap_size_of_children(), 0);
// Not on the heap.
let x = Some(0i64);
assert_eq!(x.heap_size_of_children(), 0);
// The `Some` is not on the heap, but the Box is.
let x = Some(Box::new(0i64));
assert_eq!(x.heap_size_of_children(), 8);
// Not on the heap.
let x = ::std::sync::Arc::new(0i64);
assert_eq!(x.heap_size_of_children(), 0);
// The `Arc` is not on the heap, but the Box is.
let x = ::std::sync::Arc::new(Box::new(0i64));
assert_eq!(x.heap_size_of_children(), 8);
// Zero elements, no heap storage.
let x: Vec<i64> = vec![];
assert_eq!(x.heap_size_of_children(), 0);
// Four elements, 8 bytes per element.
let x = vec![0i64, 1i64, 2i64, 3i64];
assert_eq!(x.heap_size_of_children(), 32);
//-----------------------------------------------------------------------
// Test the HeapSizeOf auto-deriving.
assert_eq!(Four.heap_size_of_children(), 4);
let eight = Eight(Four, Four, true, true, true);
assert_eq!(eight.heap_size_of_children(), 8);
assert_eq!(EightOrFour::Eight(eight).heap_size_of_children(), 8);
assert_eq!(EightOrFour::Four(Four).heap_size_of_children(), 4);
assert_eq!(EightOrFour::Zero(1).heap_size_of_children(), 0);
}