Skip to content

Commit

Permalink
Gate usable_size behind extended feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-daniel committed May 20, 2024
1 parent bb3dce1 commit 310ffbe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
5 changes: 5 additions & 0 deletions libmimalloc-sys/src/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ extern "C" {
/// 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.
///
/// Returns the size `n` that will be allocated, where `n >= size`.
Expand Down
5 changes: 0 additions & 5 deletions libmimalloc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
25 changes: 25 additions & 0 deletions src/extended.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::MiMalloc;
use core::ffi::c_void;

impl MiMalloc {
/// Get the mimalloc version.
Expand All @@ -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);
}
}
}
24 changes: 0 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 310ffbe

Please sign in to comment.