From cd5f46e9996eee71a4e66102768d65c82a700f72 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Wed, 17 May 2017 15:31:15 -0400 Subject: [PATCH] Accommodate change of empty vector representation in nightly libstd. The empty sentinel value used to be 0x1; now it is the minimum alignment of the contained type. To support both nightly and stable versions of rustc, we check for any pointer that is equal or less than the minimum alignment of the type that heap_size_of receives. If the given pointer is larger, we pass it on to the jemalloc heap-measuring API. --- Cargo.toml | 2 +- src/lib.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bf0b41b..afb2135 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "heapsize" -version = "0.3.9" +version = "0.4.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 e9a1ed2..fc5900b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ use std::collections::{BTreeMap, HashSet, HashMap, LinkedList, VecDeque}; use std::hash::BuildHasher; use std::hash::Hash; use std::marker::PhantomData; -use std::mem::size_of; +use std::mem::{size_of, align_of}; use std::net::{Ipv4Addr, Ipv6Addr}; use std::os::raw::c_void; use std::sync::Arc; @@ -29,11 +29,11 @@ use std::rc::Rc; /// `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 { +pub unsafe fn heap_size_of(ptr: *const T) -> usize { + if ptr as usize <= align_of::() { 0 } else { - heap_size_of_impl(ptr) + heap_size_of_impl(ptr as *const c_void) } } @@ -105,7 +105,7 @@ impl HeapSizeOf for [T] { impl HeapSizeOf for String { fn heap_size_of_children(&self) -> usize { unsafe { - heap_size_of(self.as_ptr() as *const c_void) + heap_size_of(self.as_ptr()) } } } @@ -231,7 +231,7 @@ impl HeapSizeOf for Cell { impl HeapSizeOf for Vec { fn heap_size_of_children(&self) -> usize { self.iter().fold( - unsafe { heap_size_of(self.as_ptr() as *const c_void) }, + unsafe { heap_size_of(self.as_ptr()) }, |n, elem| n + elem.heap_size_of_children()) } } @@ -250,7 +250,7 @@ impl HeapSizeOf for Vec> { // The fate of measuring Rc is still undecided, but we still want to measure // the space used for storing them. unsafe { - heap_size_of(self.as_ptr() as *const c_void) + heap_size_of(self.as_ptr()) } } }