Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit cd5f46e

Browse files
committed
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.
1 parent 5f2f5b7 commit cd5f46e

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "heapsize"
3-
version = "0.3.9"
3+
version = "0.4.0"
44
authors = [ "The Servo Project Developers" ]
55
description = "Infrastructure for measuring the total runtime size of an object on the heap"
66
license = "MPL-2.0"

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::collections::{BTreeMap, HashSet, HashMap, LinkedList, VecDeque};
1515
use std::hash::BuildHasher;
1616
use std::hash::Hash;
1717
use std::marker::PhantomData;
18-
use std::mem::size_of;
18+
use std::mem::{size_of, align_of};
1919
use std::net::{Ipv4Addr, Ipv6Addr};
2020
use std::os::raw::c_void;
2121
use std::sync::Arc;
@@ -29,11 +29,11 @@ use std::rc::Rc;
2929
/// `unsafe` because the caller must ensure that the pointer is from jemalloc.
3030
/// FIXME: This probably interacts badly with custom allocators:
3131
/// https://doc.rust-lang.org/book/custom-allocators.html
32-
pub unsafe fn heap_size_of(ptr: *const c_void) -> usize {
33-
if ptr == 0x01 as *const c_void {
32+
pub unsafe fn heap_size_of<T>(ptr: *const T) -> usize {
33+
if ptr as usize <= align_of::<T>() {
3434
0
3535
} else {
36-
heap_size_of_impl(ptr)
36+
heap_size_of_impl(ptr as *const c_void)
3737
}
3838
}
3939

@@ -105,7 +105,7 @@ impl<T: HeapSizeOf> HeapSizeOf for [T] {
105105
impl HeapSizeOf for String {
106106
fn heap_size_of_children(&self) -> usize {
107107
unsafe {
108-
heap_size_of(self.as_ptr() as *const c_void)
108+
heap_size_of(self.as_ptr())
109109
}
110110
}
111111
}
@@ -231,7 +231,7 @@ impl<T: HeapSizeOf + Copy> HeapSizeOf for Cell<T> {
231231
impl<T: HeapSizeOf> HeapSizeOf for Vec<T> {
232232
fn heap_size_of_children(&self) -> usize {
233233
self.iter().fold(
234-
unsafe { heap_size_of(self.as_ptr() as *const c_void) },
234+
unsafe { heap_size_of(self.as_ptr()) },
235235
|n, elem| n + elem.heap_size_of_children())
236236
}
237237
}
@@ -250,7 +250,7 @@ impl<T> HeapSizeOf for Vec<Rc<T>> {
250250
// The fate of measuring Rc<T> is still undecided, but we still want to measure
251251
// the space used for storing them.
252252
unsafe {
253-
heap_size_of(self.as_ptr() as *const c_void)
253+
heap_size_of(self.as_ptr())
254254
}
255255
}
256256
}

0 commit comments

Comments
 (0)