Make malloc_stats_print argument nullable

This commit is contained in:
gnzlbg 2018-10-30 19:56:52 +01:00 committed by gnzlbg
parent d9463c3f72
commit d342b97a58
3 changed files with 29 additions and 4 deletions

View File

@ -528,7 +528,7 @@ extern "C" {
/// counters that track thread cache operations.
#[cfg_attr(prefixed, link_name = "_rjem_malloc_stats_print")]
pub fn malloc_stats_print(
write_cb: unsafe extern "C" fn(*mut c_void, *const c_char),
write_cb: Option<unsafe extern "C" fn(*mut c_void, *const c_char)>,
cbopaque: *mut c_void,
opts: *const c_char,
);
@ -544,7 +544,8 @@ extern "C" {
/// Please note that doing anything which tries to allocate memory in this
/// function is likely to result in a crash or deadlock.
#[cfg_attr(prefixed, link_name = "_rjem_malloc_message")]
pub static mut malloc_message: unsafe extern "C" fn(cbopaque: *mut c_void, s: *const c_char);
pub static mut malloc_message:
Option<unsafe extern "C" fn(cbopaque: *mut c_void, s: *const c_char)>;
/// Compile-time string of configuration options.
///

View File

@ -11,7 +11,7 @@ union U {
#[cfg_attr(not(prefixed), no_mangle)]
pub static malloc_conf: Option<&'static libc::c_char> = Some(unsafe {
U {
x: &b"abort:true\0"[0],
x: &b"stats_print_opts:mdal\0"[0],
}
.y
});
@ -20,5 +20,25 @@ pub static malloc_conf: Option<&'static libc::c_char> = Some(unsafe {
fn malloc_conf_set() {
unsafe {
assert_eq!(jemalloc_sys::malloc_conf, malloc_conf);
let mut ptr: *const libc::c_char = std::ptr::null();
let mut ptr_len: libc::size_t = std::mem::size_of::<*const libc::c_char>() as libc::size_t;
let r = jemalloc_sys::mallctl(
&b"opt.stats_print_opts\0"[0] as *const _ as *const libc::c_char,
&mut ptr as *mut *const _ as *mut libc::c_void,
&mut ptr_len as *mut _,
std::ptr::null_mut(),
0,
);
assert_eq!(r, 0);
assert!(!ptr.is_null());
let s = std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned();
assert!(
s.contains("mdal"),
"opt.stats_print_opts: \"{}\" (len = {})",
s,
s.len()
);
}
}

View File

@ -82,7 +82,11 @@ fn test_stats() {
let mut ctx = PrintCtx { called_times: 0 };
unsafe {
ffi::malloc_stats_print(write_cb, &mut ctx as *mut _ as *mut c_void, ptr::null());
ffi::malloc_stats_print(
Some(write_cb),
&mut ctx as *mut _ as *mut c_void,
ptr::null(),
);
}
assert_ne!(
ctx.called_times, 0,