@@ -9,14 +9,27 @@ extern crate heapsize;
99use heapsize:: { HeapSizeOf , heap_size_of} ;
1010use std:: os:: raw:: c_void;
1111
12- pub const EMPTY : * mut ( ) = 0x1 as * mut ( ) ;
12+ const EMPTY : * mut ( ) = 0x1 as * mut ( ) ;
13+
14+ /// https://github.com/servo/heapsize/issues/74
15+ const ASSUME_JEMALLOC : bool = true ;
16+
17+ fn assert_size ( actual : usize , expected : usize ) {
18+ if ASSUME_JEMALLOC {
19+ assert_eq ! ( actual, expected)
20+ } else {
21+ // Some allocators give more than requested
22+ assert ! ( actual >= expected, "expected {:?} >= {:?}" , actual, expected)
23+ }
24+ }
1325
1426#[ cfg( feature = "unstable" ) ]
1527mod unstable {
1628 extern crate alloc;
1729
1830 use heapsize:: heap_size_of;
1931 use std:: os:: raw:: c_void;
32+ use super :: assert_size;
2033
2134 #[ repr( C , simd) ]
2235 struct OverAligned ( u64 , u64 , u64 , u64 ) ;
@@ -32,22 +45,22 @@ mod unstable {
3245 unsafe {
3346 // A 64 byte request is allocated exactly.
3447 let x = alloc:: heap:: allocate ( 64 , 0 ) ;
35- assert_eq ! ( heap_size_of( x as * const c_void) , 64 ) ;
48+ assert_size ( heap_size_of ( x as * const c_void ) , 64 ) ;
3649 alloc:: heap:: deallocate ( x, 64 , 0 ) ;
3750
3851 // A 255 byte request is rounded up to 256 bytes.
3952 let x = alloc:: heap:: allocate ( 255 , 0 ) ;
40- assert_eq ! ( heap_size_of( x as * const c_void) , 256 ) ;
53+ assert_size ( heap_size_of ( x as * const c_void ) , 256 ) ;
4154 alloc:: heap:: deallocate ( x, 255 , 0 ) ;
4255
4356 // A 1MiB request is allocated exactly.
4457 let x = alloc:: heap:: allocate ( 1024 * 1024 , 0 ) ;
45- assert_eq ! ( heap_size_of( x as * const c_void) , 1024 * 1024 ) ;
58+ assert_size ( heap_size_of ( x as * const c_void ) , 1024 * 1024 ) ;
4659 alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
4760
4861 // An overaligned 1MiB request is allocated exactly.
4962 let x = alloc:: heap:: allocate ( 1024 * 1024 , 32 ) ;
50- assert_eq ! ( heap_size_of( x as * const c_void) , 1024 * 1024 ) ;
63+ assert_size ( heap_size_of ( x as * const c_void ) , 1024 * 1024 ) ;
5164 alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
5265 }
5366 }
@@ -58,22 +71,22 @@ mod unstable {
5871 unsafe {
5972 // A 64 byte request is allocated exactly.
6073 let x = alloc:: heap:: allocate ( 64 , 0 ) ;
61- assert_eq ! ( heap_size_of( x as * const c_void) , 64 ) ;
74+ assert_size ( heap_size_of ( x as * const c_void ) , 64 ) ;
6275 alloc:: heap:: deallocate ( x, 64 , 0 ) ;
6376
6477 // A 255 byte request is allocated exactly.
6578 let x = alloc:: heap:: allocate ( 255 , 0 ) ;
66- assert_eq ! ( heap_size_of( x as * const c_void) , 255 ) ;
79+ assert_size ( heap_size_of ( x as * const c_void ) , 255 ) ;
6780 alloc:: heap:: deallocate ( x, 255 , 0 ) ;
6881
6982 // A 1MiB request is allocated exactly.
7083 let x = alloc:: heap:: allocate ( 1024 * 1024 , 0 ) ;
71- assert_eq ! ( heap_size_of( x as * const c_void) , 1024 * 1024 ) ;
84+ assert_size ( heap_size_of ( x as * const c_void ) , 1024 * 1024 ) ;
7285 alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
7386
7487 // An overaligned 1MiB request is over-allocated.
7588 let x = alloc:: heap:: allocate ( 1024 * 1024 , 32 ) ;
76- assert_eq ! ( heap_size_of( x as * const c_void) , 1024 * 1024 + 32 ) ;
89+ assert_size ( heap_size_of ( x as * const c_void ) , 1024 * 1024 + 32 ) ;
7790 alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
7891 }
7992 }
@@ -82,21 +95,21 @@ mod unstable {
8295 #[ test]
8396 fn test_simd ( ) {
8497 let x = Box :: new ( OverAligned ( 0 , 0 , 0 , 0 ) ) ;
85- assert_eq ! ( unsafe { heap_size_of( & * x as * const _ as * const c_void) } , 32 ) ;
98+ assert_size ( unsafe { heap_size_of ( & * x as * const _ as * const c_void ) } , 32 ) ;
8699 }
87100
88101 #[ cfg( target_os = "windows" ) ]
89102 #[ test]
90103 fn test_simd ( ) {
91104 let x = Box :: new ( OverAligned ( 0 , 0 , 0 , 0 ) ) ;
92- assert_eq ! ( unsafe { heap_size_of( & * x as * const _ as * const c_void) } , 32 + 32 ) ;
105+ assert_size ( unsafe { heap_size_of ( & * x as * const _ as * const c_void ) } , 32 + 32 ) ;
93106 }
94107}
95108
96109#[ test]
97110fn test_boxed_str ( ) {
98111 let x = "raclette" . to_owned ( ) . into_boxed_str ( ) ;
99- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
112+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
100113}
101114
102115#[ test]
@@ -111,61 +124,61 @@ fn test_heap_size() {
111124
112125 unsafe {
113126 // EMPTY is the special non-null address used to represent zero-size allocations.
114- assert_eq ! ( heap_size_of( EMPTY as * const c_void) , 0 ) ;
127+ assert_size ( heap_size_of ( EMPTY as * const c_void ) , 0 ) ;
115128 }
116129
117130 //-----------------------------------------------------------------------
118131 // Test HeapSizeOf implementations for various built-in types.
119132
120133 // Not on the heap; 0 bytes.
121134 let x = 0i64 ;
122- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
135+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
123136
124137 // An i64 is 8 bytes.
125138 let x = Box :: new ( 0i64 ) ;
126- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
139+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
127140
128141 // An ascii string with 16 chars is 16 bytes in UTF-8.
129142 let string = String :: from ( "0123456789abcdef" ) ;
130- assert_eq ! ( string. heap_size_of_children( ) , 16 ) ;
143+ assert_size ( string. heap_size_of_children ( ) , 16 ) ;
131144
132145 let string_ref: ( & String , ( ) ) = ( & string, ( ) ) ;
133- assert_eq ! ( string_ref. heap_size_of_children( ) , 0 ) ;
146+ assert_size ( string_ref. heap_size_of_children ( ) , 0 ) ;
134147
135148 let slice: & str = & * string;
136- assert_eq ! ( slice. heap_size_of_children( ) , 0 ) ;
149+ assert_size ( slice. heap_size_of_children ( ) , 0 ) ;
137150
138151 // Not on the heap.
139152 let x: Option < i32 > = None ;
140- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
153+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
141154
142155 // Not on the heap.
143156 let x = Some ( 0i64 ) ;
144- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
157+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
145158
146159 // The `Some` is not on the heap, but the Box is.
147160 let x = Some ( Box :: new ( 0i64 ) ) ;
148- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
161+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
149162
150163 // Not on the heap.
151164 let x = :: std:: sync:: Arc :: new ( 0i64 ) ;
152- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
165+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
153166
154167 // The `Arc` is not on the heap, but the Box is.
155168 let x = :: std:: sync:: Arc :: new ( Box :: new ( 0i64 ) ) ;
156- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
169+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
157170
158171 // Zero elements, no heap storage.
159172 let x: Vec < i64 > = vec ! [ ] ;
160- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
173+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
161174
162175 // Four elements, 8 bytes per element.
163176 let x = vec ! [ 0i64 , 1i64 , 2i64 , 3i64 ] ;
164- assert_eq ! ( x. heap_size_of_children( ) , 32 ) ;
177+ assert_size ( x. heap_size_of_children ( ) , 32 ) ;
165178}
166179
167180#[ test]
168181fn test_boxed_slice ( ) {
169182 let x = vec ! [ 1i64 , 2i64 ] . into_boxed_slice ( ) ;
170- assert_eq ! ( x. heap_size_of_children( ) , 16 )
183+ assert_size ( x. heap_size_of_children ( ) , 16 )
171184}
0 commit comments