From d342b97a5893b25181f9437c6a2e0ce649cec9d0 Mon Sep 17 00:00:00 2001 From: gnzlbg Date: Tue, 30 Oct 2018 19:56:52 +0100 Subject: [PATCH] Make malloc_stats_print argument nullable --- jemalloc-sys/src/lib.rs | 5 +++-- jemalloc-sys/tests/malloc_conf_set.rs | 22 +++++++++++++++++++++- tests/ffi.rs | 6 +++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/jemalloc-sys/src/lib.rs b/jemalloc-sys/src/lib.rs index 9594fc0..d531208 100644 --- a/jemalloc-sys/src/lib.rs +++ b/jemalloc-sys/src/lib.rs @@ -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, 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; /// Compile-time string of configuration options. /// diff --git a/jemalloc-sys/tests/malloc_conf_set.rs b/jemalloc-sys/tests/malloc_conf_set.rs index 4670f34..a54d462 100644 --- a/jemalloc-sys/tests/malloc_conf_set.rs +++ b/jemalloc-sys/tests/malloc_conf_set.rs @@ -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() + ); } } diff --git a/tests/ffi.rs b/tests/ffi.rs index f624973..94fd17b 100644 --- a/tests/ffi.rs +++ b/tests/ffi.rs @@ -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,