File tree 4 files changed +30
-29
lines changed
4 files changed +30
-29
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,11 @@ extern "C" {
202
202
/// know for certain.
203
203
pub fn mi_zalloc_small ( size : usize ) -> * mut c_void ;
204
204
205
+ /// Return the available bytes in a memory block.
206
+ ///
207
+ /// The returned size can be used to call `mi_expand` successfully.
208
+ pub fn mi_usable_size ( p : * const c_void ) -> usize ;
209
+
205
210
/// Return the used allocation size.
206
211
///
207
212
/// Returns the size `n` that will be allocated, where `n >= size`.
Original file line number Diff line number Diff line change @@ -65,11 +65,6 @@ extern "C" {
65
65
///
66
66
/// The pointer `p` must have been allocated before (or be null).
67
67
pub fn mi_free ( p : * mut c_void ) ;
68
-
69
- /// Return the available bytes in a memory block.
70
- ///
71
- /// The returned size can be used to call `mi_expand` successfully.
72
- pub fn mi_usable_size ( p : * const c_void ) -> usize ;
73
68
}
74
69
75
70
#[ cfg( test) ]
Original file line number Diff line number Diff line change 1
1
use crate :: MiMalloc ;
2
+ use core:: ffi:: c_void;
2
3
3
4
impl MiMalloc {
4
5
/// Get the mimalloc version.
@@ -7,15 +8,39 @@ impl MiMalloc {
7
8
pub fn version ( & self ) -> u32 {
8
9
unsafe { ffi:: mi_version ( ) as u32 }
9
10
}
11
+
12
+ /// Return the amount of available bytes in a memory block.
13
+ ///
14
+ /// # Safety
15
+ /// `ptr` must point to a memory block allocated by mimalloc, or be null.
16
+ #[ inline]
17
+ pub unsafe fn usable_size ( & self , ptr : * const u8 ) -> usize {
18
+ ffi:: mi_usable_size ( ptr as * const c_void )
19
+ }
10
20
}
11
21
12
22
#[ cfg( test) ]
13
23
mod test {
14
24
use super :: * ;
25
+ use core:: alloc:: GlobalAlloc ;
26
+ use core:: alloc:: Layout ;
15
27
16
28
#[ test]
17
29
fn it_gets_version ( ) {
18
30
let version = MiMalloc . version ( ) ;
19
31
assert ! ( version != 0 ) ;
20
32
}
33
+
34
+ #[ test]
35
+ fn it_checks_usable_size ( ) {
36
+ unsafe {
37
+ let layout = Layout :: from_size_align ( 8 , 8 ) . unwrap ( ) ;
38
+ let alloc = MiMalloc ;
39
+
40
+ let ptr = alloc. alloc ( layout) ;
41
+ let usable_size = alloc. usable_size ( ptr) ;
42
+ alloc. dealloc ( ptr, layout) ;
43
+ assert ! ( usable_size >= 8 ) ;
44
+ }
45
+ }
21
46
}
Original file line number Diff line number Diff line change @@ -67,17 +67,6 @@ unsafe impl GlobalAlloc for MiMalloc {
67
67
}
68
68
}
69
69
70
- impl MiMalloc {
71
- /// Return the amount of available bytes in a memory block.
72
- ///
73
- /// # Safety
74
- /// `ptr` must point to a memory block allocated by mimalloc, or be null.
75
- #[ inline]
76
- pub unsafe fn usable_size ( & self , ptr : * const u8 ) -> usize {
77
- mi_usable_size ( ptr as * const c_void )
78
- }
79
- }
80
-
81
70
#[ cfg( test) ]
82
71
mod tests {
83
72
use super :: * ;
@@ -149,17 +138,4 @@ mod tests {
149
138
alloc. dealloc ( ptr, layout) ;
150
139
}
151
140
}
152
-
153
- #[ test]
154
- fn it_checks_usable_size ( ) {
155
- unsafe {
156
- let layout = Layout :: from_size_align ( 8 , 8 ) . unwrap ( ) ;
157
- let alloc = MiMalloc ;
158
-
159
- let ptr = alloc. alloc ( layout) ;
160
- let usable_size = alloc. usable_size ( ptr) ;
161
- alloc. dealloc ( ptr, layout) ;
162
- assert ! ( usable_size >= 8 ) ;
163
- }
164
- }
165
141
}
You can’t perform that action at this time.
0 commit comments