File tree Expand file tree Collapse file tree 4 files changed +30
-29
lines changed Expand file tree Collapse file tree 4 files changed +30
-29
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,11 @@ extern "C" {
202202 /// know for certain.
203203 pub fn mi_zalloc_small ( size : usize ) -> * mut c_void ;
204204
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+
205210 /// Return the used allocation size.
206211 ///
207212 /// 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" {
6565 ///
6666 /// The pointer `p` must have been allocated before (or be null).
6767 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 ;
7368}
7469
7570#[ cfg( test) ]
Original file line number Diff line number Diff line change 11use crate :: MiMalloc ;
2+ use core:: ffi:: c_void;
23
34impl MiMalloc {
45 /// Get the mimalloc version.
@@ -7,15 +8,39 @@ impl MiMalloc {
78 pub fn version ( & self ) -> u32 {
89 unsafe { ffi:: mi_version ( ) as u32 }
910 }
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+ }
1020}
1121
1222#[ cfg( test) ]
1323mod test {
1424 use super :: * ;
25+ use core:: alloc:: GlobalAlloc ;
26+ use core:: alloc:: Layout ;
1527
1628 #[ test]
1729 fn it_gets_version ( ) {
1830 let version = MiMalloc . version ( ) ;
1931 assert ! ( version != 0 ) ;
2032 }
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+ }
2146}
Original file line number Diff line number Diff line change @@ -67,17 +67,6 @@ unsafe impl GlobalAlloc for MiMalloc {
6767 }
6868}
6969
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-
8170#[ cfg( test) ]
8271mod tests {
8372 use super :: * ;
@@ -149,17 +138,4 @@ mod tests {
149138 alloc. dealloc ( ptr, layout) ;
150139 }
151140 }
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- }
165141}
You can’t perform that action at this time.
0 commit comments