malloc_freq/
lib.rs

1//! Companion crate to [`malloc_freq`].  This crate compiles into a dynamic library that can be
2//! loaded via `LD_PRELOAD` to intercept `malloc` calls issued by the program and redirect them
3//! to the `malloc_freq` profiler.
4
5use libc::c_void;
6use malloc_freq::ProfAllocator;
7
8/// When this library is loaded with `LD_PRELOAD`, this `malloc` implementation
9/// catches `malloc` calls performed by the program and records them in the `malloc_freq`
10/// profile before invoking the original `libc` malloc.
11///
12/// # Safety
13///
14/// This method internally uses [`libc::malloc`], which is `unsafe extern "C"`.
15#[no_mangle]
16pub unsafe extern "C" fn malloc(size: libc::size_t) -> *mut c_void {
17    ProfAllocator::malloc(size)
18}