Skip to content

Commit c5be3f7

Browse files
ryancinsightSchrodingerZhu
authored andcommitted
Update lib.rs
1 parent a94dc54 commit c5be3f7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

snmalloc-sys/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,31 @@ extern "C" {
3333
pub fn rust_realloc(ptr: *mut c_void, alignment: size_t, old_size: size_t, new_size: size_t) -> *mut c_void;
3434

3535
/// Allocate `count` items of `size` length each.
36-
///
3736
/// Returns `null` if `count * size` overflows or on out-of-memory.
38-
///
3937
/// All items are initialized to zero.
4038
pub fn sn_calloc(count: usize, size: usize) -> *mut c_void;
4139

4240
/// Allocate `size` bytes.
43-
///
4441
/// Returns pointer to the allocated memory or null if out of memory.
4542
/// Returns a unique pointer if called with `size` 0.
4643
pub fn sn_malloc(size: usize) -> *mut c_void;
4744

4845
/// Re-allocate memory to `newsize` bytes.
49-
///
5046
/// Return pointer to the allocated memory or null if out of memory. If null
5147
/// is returned, the pointer `p` is not freed. Otherwise the original
5248
/// pointer is either freed or returned as the reallocated result (in case
5349
/// it fits in-place with the new size).
54-
///
55-
/// If `p` is null, it behaves as [`mi_malloc`]. If `newsize` is larger than
50+
/// If `p` is null, it behaves as [`sn_malloc`]. If `newsize` is larger than
5651
/// the original `size` allocated for `p`, the bytes after `size` are
5752
/// uninitialized.
5853
pub fn sn_realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
5954

6055
/// Free previously allocated memory.
61-
///
6256
/// The pointer `p` must have been allocated before (or be null).
6357
pub fn sn_free(p: *mut c_void);
58+
59+
/// Return the available bytes in a memory block.
60+
pub fn sn_malloc_usable_size(p: *const c_void) -> usize;
6461
}
6562

6663
#[cfg(test)]
@@ -94,4 +91,14 @@ mod tests {
9491
unsafe {assert_eq!(*ptr, 127)};
9592
unsafe { rust_dealloc(ptr as *mut c_void, 8, 16) };
9693
}
94+
95+
#[test]
96+
fn it_calculates_usable_size() {
97+
let ptr = unsafe { sn_malloc(32) } as *mut u8;
98+
let usable_size = unsafe { sn_malloc_usable_size(ptr as *mut c_void) };
99+
assert!(
100+
usable_size >= 32,
101+
"usable_size should at least equal to the allocated size"
102+
);
103+
}
97104
}

0 commit comments

Comments
 (0)