Skip to content

Commit 310ffbe

Browse files
Gate usable_size behind extended feature
1 parent bb3dce1 commit 310ffbe

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

libmimalloc-sys/src/extended.rs

+5
Original file line numberDiff line numberDiff 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`.

libmimalloc-sys/src/lib.rs

-5
Original file line numberDiff line numberDiff 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)]

src/extended.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::MiMalloc;
2+
use core::ffi::c_void;
23

34
impl 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)]
1323
mod 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
}

src/lib.rs

-24
Original file line numberDiff line numberDiff 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)]
8271
mod 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
}

0 commit comments

Comments
 (0)