Skip to content

Commit fff3f95

Browse files
committed
Add bindings to all jemalloc public functions
That is, those documented in http://jemalloc.net/jemalloc.3.html For example `malloc` and `free` can be useful when implementing callbacks for a C library that doesn’t provide a size when deallocating: https://www.freetype.org/freetype2/docs/reference/ft2-system_interface.html
1 parent dd5861f commit fff3f95

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

jemalloc-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "jemalloc-sys"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
authors = ["Alex Crichton <alex@alexcrichton.com>"]
55
build = "build.rs"
66
links = "jemalloc"

jemalloc-sys/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,31 @@ use libc::{c_int, c_void, size_t, c_char};
1717
pub const MALLOCX_ZERO: c_int = 0x40;
1818

1919
extern "C" {
20+
// Standard API
21+
#[link_name = "_rjem_malloc"]
22+
pub fn malloc(size: size_t) -> *mut c_void;
23+
#[link_name = "_rjem_calloc"]
24+
pub fn calloc(number: size_t, size: size_t) -> *mut c_void;
25+
#[link_name = "_rjem_posix_memalign"]
26+
pub fn posix_memalign(ptr: *mut *mut c_void, alignment: size_t, size: size_t) -> c_int;
27+
#[link_name = "_rjem_aligned_alloc"]
28+
pub fn aligned_alloc(alignment: size_t, size: size_t) -> *mut c_void;
29+
#[link_name = "_rjem_realloc"]
30+
pub fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void;
31+
#[link_name = "_rjem_free"]
32+
pub fn free(ptr: *mut c_void);
33+
34+
// Non-standard API
2035
#[link_name = "_rjem_mallocx"]
2136
pub fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
22-
#[link_name = "_rjem_calloc"]
23-
pub fn calloc(num: size_t, size: size_t) -> *mut c_void;
2437
#[link_name = "_rjem_rallocx"]
2538
pub fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
2639
#[link_name = "_rjem_xallocx"]
2740
pub fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
41+
#[link_name = "_rjem_sallocx"]
42+
pub fn sallocx(ptr: *mut c_void, flags: c_int) -> size_t;
43+
#[link_name = "_rjem_dallocx"]
44+
pub fn dallocx(ptr: *mut c_void, flags: c_int);
2845
#[link_name = "_rjem_sdallocx"]
2946
pub fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
3047
#[link_name = "_rjem_nallocx"]

tests/smoke_ffi.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(global_allocator)]
2+
3+
extern crate jemallocator;
4+
extern crate jemalloc_sys;
5+
6+
// Work around https://github.com/alexcrichton/jemallocator/issues/19
7+
#[global_allocator]
8+
static A: jemallocator::Jemalloc = jemallocator::Jemalloc;
9+
10+
#[test]
11+
fn smoke() {
12+
unsafe {
13+
let ptr = jemalloc_sys::malloc(4);
14+
*(ptr as *mut u32) = 0xDECADE;
15+
assert_eq!(*(ptr as *mut u32), 0xDECADE);
16+
jemalloc_sys::free(ptr);
17+
}
18+
}

0 commit comments

Comments
 (0)