Skip to content

Commit

Permalink
Add bindings to all jemalloc public functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SimonSapin committed Oct 30, 2017
1 parent dd5861f commit e6ea8a9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jemalloc-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jemalloc-sys"
version = "0.1.3"
version = "0.1.4"
authors = ["Alex Crichton <[email protected]>"]
build = "build.rs"
links = "jemalloc"
Expand Down
21 changes: 19 additions & 2 deletions jemalloc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ use libc::{c_int, c_void, size_t, c_char};
pub const MALLOCX_ZERO: c_int = 0x40;

extern "C" {
// Standard API
#[link_name = "_rjem_malloc"]
pub fn malloc(size: size_t) -> *mut c_void;
#[link_name = "_rjem_calloc"]
pub fn calloc(number: size_t, size: size_t) -> *mut c_void;
#[link_name = "_rjem_posix_memalign"]
pub fn posix_memalign(ptr: *mut *mut c_void, alignment: size_t, size: size_t) -> c_int;
#[link_name = "_rjem_aligned_alloc"]
pub fn aligned_alloc(alignment: size_t, size: size_t) -> *mut c_void;
#[link_name = "_rjem_realloc"]
pub fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void;
#[link_name = "_rjem_free"]
pub fn free(ptr: *mut c_void);

// Non-standard API
#[link_name = "_rjem_mallocx"]
pub fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
#[link_name = "_rjem_calloc"]
pub fn calloc(num: size_t, size: size_t) -> *mut c_void;
#[link_name = "_rjem_rallocx"]
pub fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
#[link_name = "_rjem_xallocx"]
pub fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
#[link_name = "_rjem_sallocx"]
pub fn sallocx(ptr: *const c_void, flags: c_int) -> size_t;
#[link_name = "_rjem_dallocx"]
pub fn dallocx(ptr: *mut c_void, flags: c_int);
#[link_name = "_rjem_sdallocx"]
pub fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
#[link_name = "_rjem_nallocx"]
Expand Down
18 changes: 18 additions & 0 deletions tests/smoke_ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(global_allocator)]

extern crate jemallocator;
extern crate jemalloc_sys;

// Work around https://github.com/alexcrichton/jemallocator/issues/19
#[global_allocator]
static A: jemallocator::Jemalloc = jemallocator::Jemalloc;

#[test]
fn smoke() {
unsafe {
let ptr = jemalloc_sys::malloc(4);
*(ptr as *mut u32) = 0xDECADE;
assert_eq!(*(ptr as *mut u32), 0xDECADE);
jemalloc_sys::free(ptr);
}
}

0 comments on commit e6ea8a9

Please sign in to comment.