libtcmalloc_sys/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4//! A Rust raw wrapper over Google's TCMalloc memory allocator
5//!
6//! ## Feature flags
7#![doc = document_features::document_features!()]
8
9#[cfg(feature = "extension")]
10#[cfg_attr(docsrs, doc(cfg(feature = "extension")))]
11mod extension;
12
13#[cfg(feature = "extension")]
14#[cfg_attr(docsrs, doc(cfg(feature = "extension")))]
15pub use extension::*;
16
17unsafe extern "C" {
18    /// Allocate `size` bytes aligned by `align`.
19    ///
20    /// Return a pointer to the allocated memory or null if out of memory.
21    ///
22    /// Returns a unique pointer if called with `size` 0.
23    pub fn TCMallocInternalAlignedAlloc(
24        align: libc::size_t,
25        size: libc::size_t,
26    ) -> *mut core::ffi::c_void;
27
28    /// Free previously allocated memory.
29    ///
30    /// The pointer `ptr` must have been allocated before (or be null).
31    ///
32    /// The `align` and `size` must match the ones used to allocate `ptr`.
33    pub fn TCMallocInternalFreeAlignedSized(
34        ptr: *mut core::ffi::c_void,
35        align: libc::size_t,
36        size: libc::size_t,
37    );
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn it_frees_memory_malloc() {
46        let ptr = unsafe { TCMallocInternalAlignedAlloc(8, 8) } as *mut u8;
47        unsafe { TCMallocInternalFreeAlignedSized(ptr as *mut libc::c_void, 8, 8) };
48    }
49}