diff --git a/Cargo.toml b/Cargo.toml index 2914ec1..3226d1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "heapsize" -version = "0.2.5" +version = "0.3.0" authors = [ "The Servo Project Developers" ] description = "Infrastructure for measuring the total runtime size of an object on the heap" license = "MPL-2.0" diff --git a/src/lib.rs b/src/lib.rs index 8e14f85..0585a75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,12 +33,16 @@ extern { fn je_malloc_usable_size(ptr: *const c_void) -> size_t; } -// A wrapper for je_malloc_usable_size that handles `EMPTY` and returns `usize`. -pub fn heap_size_of(ptr: *const c_void) -> usize { +/// A wrapper for je_malloc_usable_size that handles `EMPTY` and returns `usize`. +/// +/// `unsafe` because the caller must ensure that the pointer is from jemalloc. +/// FIXME: This probably interacts badly with custom allocators: +/// https://doc.rust-lang.org/book/custom-allocators.html +pub unsafe fn heap_size_of(ptr: *const c_void) -> usize { if ptr == 0x01 as *const c_void { 0 } else { - unsafe { je_malloc_usable_size(ptr) as usize } + je_malloc_usable_size(ptr) as usize } } @@ -71,13 +75,17 @@ pub trait HeapSizeOf { impl HeapSizeOf for Box { fn heap_size_of_children(&self) -> usize { // Measure size of `self`. - heap_size_of(&**self as *const T as *const c_void) + (**self).heap_size_of_children() + unsafe { + heap_size_of(&**self as *const T as *const c_void) + (**self).heap_size_of_children() + } } } impl HeapSizeOf for String { fn heap_size_of_children(&self) -> usize { - heap_size_of(self.as_ptr() as *const c_void) + unsafe { + heap_size_of(self.as_ptr() as *const c_void) + } } } @@ -146,8 +154,10 @@ impl HeapSizeOf for Cell { impl HeapSizeOf for Vec { fn heap_size_of_children(&self) -> usize { - heap_size_of(self.as_ptr() as *const c_void) + - self.iter().fold(0, |n, elem| n + elem.heap_size_of_children()) + unsafe { + heap_size_of(self.as_ptr() as *const c_void) + + self.iter().fold(0, |n, elem| n + elem.heap_size_of_children()) + } } } @@ -155,7 +165,9 @@ impl HeapSizeOf for Vec> { fn heap_size_of_children(&self) -> usize { // The fate of measuring Rc is still undecided, but we still want to measure // the space used for storing them. - heap_size_of(self.as_ptr() as *const c_void) + unsafe { + heap_size_of(self.as_ptr() as *const c_void) + } } }