jemallocator/tests/ffi.rs

96 lines
2.2 KiB
Rust
Raw Normal View History

extern crate jemalloc_sys as ffi;
extern crate jemallocator;
2018-10-30 19:34:36 +00:00
extern crate libc;
use std::mem;
2018-10-30 19:34:36 +00:00
use std::ptr;
2017-07-07 18:07:16 +00:00
use jemallocator::Jemalloc;
2018-10-30 19:34:36 +00:00
use libc::{c_char, c_void};
2017-07-07 18:07:16 +00:00
#[global_allocator]
static A: Jemalloc = Jemalloc;
#[test]
fn test_basic_alloc() {
unsafe {
let exp_size = ffi::nallocx(100, 0);
assert!(exp_size >= 100);
let mut ptr = ffi::mallocx(100, 0);
assert!(!ptr.is_null());
assert_eq!(exp_size, ffi::malloc_usable_size(ptr));
ptr = ffi::rallocx(ptr, 50, 0);
let size = ffi::xallocx(ptr, 30, 20, 0);
assert!(size >= 50);
ffi::sdallocx(ptr, 50, 0);
}
}
#[test]
fn test_mallctl() {
let ptr = unsafe { ffi::mallocx(100, 0) };
let mut allocated: usize = 0;
let mut val_len = mem::size_of_val(&allocated);
let field = "stats.allocated\0";
let mut code;
code = unsafe {
2018-10-30 19:34:36 +00:00
ffi::mallctl(
field.as_ptr() as *const _,
&mut allocated as *mut _ as *mut c_void,
&mut val_len,
ptr::null_mut(),
0,
)
};
assert_eq!(code, 0);
assert!(allocated > 0);
let mut mib = [0, 0];
let mut mib_len = 2;
code = unsafe {
ffi::mallctlnametomib(field.as_ptr() as *const _, mib.as_mut_ptr(), &mut mib_len)
};
assert_eq!(code, 0);
let mut allocated_by_mib = 0;
let code = unsafe {
2018-10-30 19:34:36 +00:00
ffi::mallctlbymib(
mib.as_ptr(),
mib_len,
&mut allocated_by_mib as *mut _ as *mut c_void,
&mut val_len,
ptr::null_mut(),
0,
)
};
assert_eq!(code, 0);
assert_eq!(allocated_by_mib, allocated);
unsafe { ffi::sdallocx(ptr, 100, 0) };
}
#[test]
fn test_stats() {
struct PrintCtx {
called_times: usize,
}
extern "C" fn write_cb(ctx: *mut c_void, _: *const c_char) {
let print_ctx = unsafe { &mut *(ctx as *mut PrintCtx) };
print_ctx.called_times += 1;
}
let mut ctx = PrintCtx { called_times: 0 };
unsafe {
ffi::malloc_stats_print(
Some(write_cb),
&mut ctx as *mut _ as *mut c_void,
ptr::null(),
);
}
2018-10-30 19:34:36 +00:00
assert_ne!(
ctx.called_times, 0,
"print should be triggered at lease once."
);
}