|
12 | 12 |
|
13 | 13 | extern crate libc;
|
14 | 14 |
|
15 |
| -use libc::{c_int, c_void, size_t}; |
| 15 | +use libc::{c_int, c_void, size_t, c_char}; |
16 | 16 |
|
17 |
| -extern { |
| 17 | +extern "C" { |
18 | 18 | #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
|
19 | 19 | link_name = "je_mallocx")]
|
20 | 20 | pub fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
|
21 | 21 | #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
|
22 | 22 | link_name = "je_rallocx")]
|
23 |
| - pub fn rallocx(ptr: *mut c_void, size: size_t, |
24 |
| - flags: c_int) -> *mut c_void; |
| 23 | + pub fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void; |
25 | 24 | #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
|
26 | 25 | link_name = "je_xallocx")]
|
27 |
| - pub fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, |
28 |
| - flags: c_int) -> size_t; |
| 26 | + pub fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t; |
29 | 27 | #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
|
30 | 28 | link_name = "je_sdallocx")]
|
31 | 29 | pub fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
|
32 | 30 | #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
|
33 | 31 | link_name = "je_nallocx")]
|
34 | 32 | pub fn nallocx(size: size_t, flags: c_int) -> size_t;
|
| 33 | + |
| 34 | + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), |
| 35 | + link_name = "je_malloc_usable_size")] |
| 36 | + pub fn malloc_usable_size(ptr: *const c_void) -> size_t; |
| 37 | + |
| 38 | + // mallctl |
| 39 | + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), |
| 40 | + link_name = "je_mallctl")] |
| 41 | + pub fn mallctl(name: *const c_char, |
| 42 | + oldp: *mut c_void, |
| 43 | + oldpenp: *mut size_t, |
| 44 | + newp: *mut c_void, |
| 45 | + newlen: size_t) |
| 46 | + -> c_int; |
| 47 | + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), |
| 48 | + link_name = "je_mallctlnametomib")] |
| 49 | + pub fn mallctlnametomib(name: *const c_char, mibp: *mut size_t, miblenp: *mut size_t) -> c_int; |
| 50 | + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), |
| 51 | + link_name = "je_mallctlbymib")] |
| 52 | + pub fn mallctlbymib(mib: *const size_t, |
| 53 | + miblen: size_t, |
| 54 | + oldp: *mut c_void, |
| 55 | + oldpenp: *mut size_t, |
| 56 | + newp: *mut c_void, |
| 57 | + newlen: size_t) |
| 58 | + -> c_int; |
| 59 | + |
| 60 | + // stats |
| 61 | + #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"), |
| 62 | + link_name = "je_malloc_stats_print")] |
| 63 | + pub fn malloc_stats_print(write_cb: extern "C" fn(*mut c_void, *const c_char), |
| 64 | + cbopaque: *mut c_void, |
| 65 | + opts: *const c_char); |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod test { |
| 70 | + use libc::{c_void, c_char}; |
| 71 | + use core::{ptr, mem}; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_basic_alloc() { |
| 75 | + unsafe { |
| 76 | + let exp_size = super::nallocx(100, 0); |
| 77 | + assert!(exp_size >= 100); |
| 78 | + |
| 79 | + let mut ptr = super::mallocx(100, 0); |
| 80 | + assert!(!ptr.is_null()); |
| 81 | + assert_eq!(exp_size, super::malloc_usable_size(ptr)); |
| 82 | + ptr = super::rallocx(ptr, 50, 0); |
| 83 | + let size = super::xallocx(ptr, 30, 20, 0); |
| 84 | + assert!(size >= 50); |
| 85 | + super::sdallocx(ptr, 50, 0); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_mallctl() { |
| 91 | + let ptr = unsafe { super::mallocx(100, 0) }; |
| 92 | + let mut allocated: usize = 0; |
| 93 | + let mut val_len = mem::size_of_val(&allocated); |
| 94 | + let field = "stats.allocated\0"; |
| 95 | + let mut code; |
| 96 | + code = unsafe { |
| 97 | + super::mallctl(field.as_ptr() as *const _, |
| 98 | + &mut allocated as *mut _ as *mut c_void, |
| 99 | + &mut val_len, |
| 100 | + ptr::null_mut(), |
| 101 | + 0) |
| 102 | + }; |
| 103 | + assert_eq!(code, 0); |
| 104 | + assert!(allocated > 0); |
| 105 | + |
| 106 | + let mut mib = [0, 0]; |
| 107 | + let mut mib_len = 2; |
| 108 | + code = unsafe { |
| 109 | + super::mallctlnametomib(field.as_ptr() as *const _, mib.as_mut_ptr(), &mut mib_len) |
| 110 | + }; |
| 111 | + assert_eq!(code, 0); |
| 112 | + let mut allocated_by_mib = 0; |
| 113 | + let code = unsafe { |
| 114 | + super::mallctlbymib(mib.as_ptr(), |
| 115 | + mib_len, |
| 116 | + &mut allocated_by_mib as *mut _ as *mut c_void, |
| 117 | + &mut val_len, |
| 118 | + ptr::null_mut(), |
| 119 | + 0) |
| 120 | + }; |
| 121 | + assert_eq!(code, 0); |
| 122 | + assert_eq!(allocated_by_mib, allocated); |
| 123 | + |
| 124 | + unsafe { super::sdallocx(ptr, 100, 0) }; |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_stats() { |
| 129 | + struct PrintCtx { |
| 130 | + called_times: usize, |
| 131 | + } |
| 132 | + |
| 133 | + extern "C" fn write_cb(ctx: *mut c_void, _: *const c_char) { |
| 134 | + let print_ctx = unsafe { &mut *(ctx as *mut PrintCtx) }; |
| 135 | + print_ctx.called_times += 1; |
| 136 | + } |
| 137 | + |
| 138 | + let mut ctx = PrintCtx { called_times: 0 }; |
| 139 | + unsafe { |
| 140 | + super::malloc_stats_print(write_cb, &mut ctx as *mut _ as *mut c_void, ptr::null()); |
| 141 | + } |
| 142 | + assert_ne!(ctx.called_times, |
| 143 | + 0, |
| 144 | + "print should be triggered at lease once."); |
| 145 | + } |
35 | 146 | }
|
0 commit comments