@@ -15,7 +15,7 @@ use std::collections::{BTreeMap, HashSet, HashMap, LinkedList, VecDeque};
15
15
use std:: hash:: BuildHasher ;
16
16
use std:: hash:: Hash ;
17
17
use std:: marker:: PhantomData ;
18
- use std:: mem:: size_of;
18
+ use std:: mem:: { size_of, align_of } ;
19
19
use std:: net:: { Ipv4Addr , Ipv6Addr } ;
20
20
use std:: os:: raw:: c_void;
21
21
use std:: sync:: Arc ;
@@ -29,11 +29,11 @@ use std::rc::Rc;
29
29
/// `unsafe` because the caller must ensure that the pointer is from jemalloc.
30
30
/// FIXME: This probably interacts badly with custom allocators:
31
31
/// 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 > ( ) {
34
34
0
35
35
} else {
36
- heap_size_of_impl ( ptr)
36
+ heap_size_of_impl ( ptr as * const c_void )
37
37
}
38
38
}
39
39
@@ -105,7 +105,7 @@ impl<T: HeapSizeOf> HeapSizeOf for [T] {
105
105
impl HeapSizeOf for String {
106
106
fn heap_size_of_children ( & self ) -> usize {
107
107
unsafe {
108
- heap_size_of ( self . as_ptr ( ) as * const c_void )
108
+ heap_size_of ( self . as_ptr ( ) )
109
109
}
110
110
}
111
111
}
@@ -231,7 +231,7 @@ impl<T: HeapSizeOf + Copy> HeapSizeOf for Cell<T> {
231
231
impl < T : HeapSizeOf > HeapSizeOf for Vec < T > {
232
232
fn heap_size_of_children ( & self ) -> usize {
233
233
self . iter ( ) . fold (
234
- unsafe { heap_size_of ( self . as_ptr ( ) as * const c_void ) } ,
234
+ unsafe { heap_size_of ( self . as_ptr ( ) ) } ,
235
235
|n, elem| n + elem. heap_size_of_children ( ) )
236
236
}
237
237
}
@@ -250,7 +250,7 @@ impl<T> HeapSizeOf for Vec<Rc<T>> {
250
250
// The fate of measuring Rc<T> is still undecided, but we still want to measure
251
251
// the space used for storing them.
252
252
unsafe {
253
- heap_size_of ( self . as_ptr ( ) as * const c_void )
253
+ heap_size_of ( self . as_ptr ( ) )
254
254
}
255
255
}
256
256
}
0 commit comments