From 2a5ed355eb7a0dfd185d8976615b21a13a201978 Mon Sep 17 00:00:00 2001 From: Nathaniel Daniel Date: Sun, 19 May 2024 18:22:43 -0700 Subject: [PATCH] Gate usable_size behind extended feature --- libmimalloc-sys/src/extended.rs | 5 +++++ libmimalloc-sys/src/lib.rs | 5 ----- src/extended.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 24 ------------------------ 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/libmimalloc-sys/src/extended.rs b/libmimalloc-sys/src/extended.rs index 60a97fb..c48071d 100644 --- a/libmimalloc-sys/src/extended.rs +++ b/libmimalloc-sys/src/extended.rs @@ -201,6 +201,11 @@ extern "C" { /// small and calls this if so at runtime, so its' only worth using if you /// know for certain. pub fn mi_zalloc_small(size: usize) -> *mut c_void; + + /// Return the available bytes in a memory block. + /// + /// The returned size can be used to call `mi_expand` successfully. + pub fn mi_usable_size(p: *const c_void) -> usize; /// Return the used allocation size. /// diff --git a/libmimalloc-sys/src/lib.rs b/libmimalloc-sys/src/lib.rs index ae9c550..53721eb 100644 --- a/libmimalloc-sys/src/lib.rs +++ b/libmimalloc-sys/src/lib.rs @@ -65,11 +65,6 @@ extern "C" { /// /// The pointer `p` must have been allocated before (or be null). pub fn mi_free(p: *mut c_void); - - /// Return the available bytes in a memory block. - /// - /// The returned size can be used to call `mi_expand` successfully. - pub fn mi_usable_size(p: *const c_void) -> usize; } #[cfg(test)] diff --git a/src/extended.rs b/src/extended.rs index 9b6a9a0..be9614c 100644 --- a/src/extended.rs +++ b/src/extended.rs @@ -1,4 +1,5 @@ use crate::MiMalloc; +use core::ffi::c_void; impl MiMalloc { /// Get the mimalloc version. @@ -7,15 +8,39 @@ impl MiMalloc { pub fn version(&self) -> u32 { unsafe { ffi::mi_version() as u32 } } + + /// Return the amount of available bytes in a memory block. + /// + /// # Safety + /// `ptr` must point to a memory block allocated by mimalloc, or be null. + #[inline] + pub unsafe fn usable_size(&self, ptr: *const u8) -> usize { + ffi::mi_usable_size(ptr as *const c_void) + } } #[cfg(test)] mod test { use super::*; + use core::alloc::GlobalAlloc; + use core::alloc::Layout; #[test] fn it_gets_version() { let version = MiMalloc.version(); assert!(version != 0); } + + #[test] + fn it_checks_usable_size() { + unsafe { + let layout = Layout::from_size_align(8, 8).unwrap(); + let alloc = MiMalloc; + + let ptr = alloc.alloc(layout); + let usable_size = alloc.usable_size(ptr); + alloc.dealloc(ptr, layout); + assert!(usable_size >= 8); + } + } } diff --git a/src/lib.rs b/src/lib.rs index 20487c5..4288b2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,17 +67,6 @@ unsafe impl GlobalAlloc for MiMalloc { } } -impl MiMalloc { - /// Return the amount of available bytes in a memory block. - /// - /// # Safety - /// `ptr` must point to a memory block allocated by mimalloc, or be null. - #[inline] - pub unsafe fn usable_size(&self, ptr: *const u8) -> usize { - mi_usable_size(ptr as *const c_void) - } -} - #[cfg(test)] mod tests { use super::*; @@ -149,17 +138,4 @@ mod tests { alloc.dealloc(ptr, layout); } } - - #[test] - fn it_checks_usable_size() { - unsafe { - let layout = Layout::from_size_align(8, 8).unwrap(); - let alloc = MiMalloc; - - let ptr = alloc.alloc(layout); - let usable_size = alloc.usable_size(ptr); - alloc.dealloc(ptr, layout); - assert!(usable_size >= 8); - } - } }