Rust-allocator based malloc and free


#1

I have a C library that can take pointers to malloc and free replacements for custom allocator. I thought it’d be cool to use Rust’s allocator, so that the C library can reuse jemalloc instead of libc malloc.

Does Rust expose its “raw” malloc/free anywhere? Or should I steal memory from a Vec instead?


#2

The Rust allocator has a sized delete/free operation, so it can be implemented on top of malloc/free, but not the other way round.


#3

I still need this. I could work around lack of size by storing size in the begging of the buffer (in free reading it from ptr[-1]).

But which functions allocate and free in Rust?


#4

I think you’re looking for https://doc.rust-lang.org/nightly/alloc/heap/struct.Heap.html .


#5

Yeah. What a fancy API, with alignment, padding, etc.