Skip to content

Commit ed77937

Browse files
authored
Merge pull request gnzlbg#5 from BusyJay/busyjay/expose-more-api
bindings: add more ABI
2 parents a022ca4 + 643388a commit ed77937

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ before_script:
1111
script:
1212
- cargo build --verbose --target $TARGET
1313
- cargo test --verbose --target $TARGET
14+
- cargo test --verbose --target $TARGET -p jemalloc-sys
1415
- cargo doc
1516
after_success:
1617
- travis-cargo --only nightly doc-upload

jemalloc-sys/src/lib.rs

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,135 @@
1212

1313
extern crate libc;
1414

15-
use libc::{c_int, c_void, size_t};
15+
use libc::{c_int, c_void, size_t, c_char};
1616

17-
extern {
17+
extern "C" {
1818
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
1919
link_name = "je_mallocx")]
2020
pub fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
2121
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
2222
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;
2524
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
2625
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;
2927
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
3028
link_name = "je_sdallocx")]
3129
pub fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
3230
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
3331
link_name = "je_nallocx")]
3432
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+
}
35146
}

0 commit comments

Comments
 (0)