@@ -9,14 +9,27 @@ extern crate heapsize;
9
9
use heapsize:: { HeapSizeOf , heap_size_of} ;
10
10
use std:: os:: raw:: c_void;
11
11
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
+ }
13
25
14
26
#[ cfg( feature = "unstable" ) ]
15
27
mod unstable {
16
28
extern crate alloc;
17
29
18
30
use heapsize:: heap_size_of;
19
31
use std:: os:: raw:: c_void;
32
+ use super :: assert_size;
20
33
21
34
#[ repr( C , simd) ]
22
35
struct OverAligned ( u64 , u64 , u64 , u64 ) ;
@@ -32,22 +45,22 @@ mod unstable {
32
45
unsafe {
33
46
// A 64 byte request is allocated exactly.
34
47
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 ) ;
36
49
alloc:: heap:: deallocate ( x, 64 , 0 ) ;
37
50
38
51
// A 255 byte request is rounded up to 256 bytes.
39
52
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 ) ;
41
54
alloc:: heap:: deallocate ( x, 255 , 0 ) ;
42
55
43
56
// A 1MiB request is allocated exactly.
44
57
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 ) ;
46
59
alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
47
60
48
61
// An overaligned 1MiB request is allocated exactly.
49
62
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 ) ;
51
64
alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
52
65
}
53
66
}
@@ -58,22 +71,22 @@ mod unstable {
58
71
unsafe {
59
72
// A 64 byte request is allocated exactly.
60
73
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 ) ;
62
75
alloc:: heap:: deallocate ( x, 64 , 0 ) ;
63
76
64
77
// A 255 byte request is allocated exactly.
65
78
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 ) ;
67
80
alloc:: heap:: deallocate ( x, 255 , 0 ) ;
68
81
69
82
// A 1MiB request is allocated exactly.
70
83
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 ) ;
72
85
alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
73
86
74
87
// An overaligned 1MiB request is over-allocated.
75
88
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 ) ;
77
90
alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
78
91
}
79
92
}
@@ -82,21 +95,21 @@ mod unstable {
82
95
#[ test]
83
96
fn test_simd ( ) {
84
97
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 ) ;
86
99
}
87
100
88
101
#[ cfg( target_os = "windows" ) ]
89
102
#[ test]
90
103
fn test_simd ( ) {
91
104
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 ) ;
93
106
}
94
107
}
95
108
96
109
#[ test]
97
110
fn test_boxed_str ( ) {
98
111
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 ) ;
100
113
}
101
114
102
115
#[ test]
@@ -111,61 +124,61 @@ fn test_heap_size() {
111
124
112
125
unsafe {
113
126
// 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 ) ;
115
128
}
116
129
117
130
//-----------------------------------------------------------------------
118
131
// Test HeapSizeOf implementations for various built-in types.
119
132
120
133
// Not on the heap; 0 bytes.
121
134
let x = 0i64 ;
122
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
135
+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
123
136
124
137
// An i64 is 8 bytes.
125
138
let x = Box :: new ( 0i64 ) ;
126
- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
139
+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
127
140
128
141
// An ascii string with 16 chars is 16 bytes in UTF-8.
129
142
let string = String :: from ( "0123456789abcdef" ) ;
130
- assert_eq ! ( string. heap_size_of_children( ) , 16 ) ;
143
+ assert_size ( string. heap_size_of_children ( ) , 16 ) ;
131
144
132
145
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 ) ;
134
147
135
148
let slice: & str = & * string;
136
- assert_eq ! ( slice. heap_size_of_children( ) , 0 ) ;
149
+ assert_size ( slice. heap_size_of_children ( ) , 0 ) ;
137
150
138
151
// Not on the heap.
139
152
let x: Option < i32 > = None ;
140
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
153
+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
141
154
142
155
// Not on the heap.
143
156
let x = Some ( 0i64 ) ;
144
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
157
+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
145
158
146
159
// The `Some` is not on the heap, but the Box is.
147
160
let x = Some ( Box :: new ( 0i64 ) ) ;
148
- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
161
+ assert_size ( x. heap_size_of_children ( ) , 8 ) ;
149
162
150
163
// Not on the heap.
151
164
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 ) ;
153
166
154
167
// The `Arc` is not on the heap, but the Box is.
155
168
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 ) ;
157
170
158
171
// Zero elements, no heap storage.
159
172
let x: Vec < i64 > = vec ! [ ] ;
160
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
173
+ assert_size ( x. heap_size_of_children ( ) , 0 ) ;
161
174
162
175
// Four elements, 8 bytes per element.
163
176
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 ) ;
165
178
}
166
179
167
180
#[ test]
168
181
fn test_boxed_slice ( ) {
169
182
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 )
171
184
}
0 commit comments