Skip to content

Commit 3ba384f

Browse files
authored
Merge pull request #117 from nathaniel-daniel/fix-pr-106
2 parents 679b170 + 310ffbe commit 3ba384f

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
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

-21
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ unsafe impl GlobalAlloc for MiMalloc {
6767
}
6868
}
6969

70-
impl MiMalloc {
71-
#[allow(dead_code)]
72-
#[inline]
73-
unsafe fn usable_size(&self, ptr: *const u8) -> usize {
74-
mi_usable_size(ptr as *const c_void)
75-
}
76-
}
77-
7870
#[cfg(test)]
7971
mod tests {
8072
use super::*;
@@ -146,17 +138,4 @@ mod tests {
146138
alloc.dealloc(ptr, layout);
147139
}
148140
}
149-
150-
#[test]
151-
fn it_usable_size() {
152-
unsafe {
153-
let layout = Layout::from_size_align(8, 8).unwrap();
154-
let alloc = MiMalloc;
155-
156-
let ptr = alloc.alloc(layout);
157-
let usable_size = alloc.usable_size(ptr);
158-
alloc.dealloc(ptr, layout);
159-
assert!(usable_size >= 8);
160-
}
161-
}
162141
}

0 commit comments

Comments
 (0)