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. But access to memory by this pointer
23    /// is undefined behaviour.
24    pub fn TCMallocInternalNewAlignedNothrowBridge(
25        size: libc::size_t,
26        alignment: libc::size_t,
27    ) -> *mut core::ffi::c_void;
28
29    /// Free previously allocated memory.
30    ///
31    /// The pointer `ptr` must have been allocated before.
32    ///
33    /// The `alignment` and `size` must match the ones used to allocate `ptr`.
34    pub fn TCMallocInternalDeleteSizedAligned(
35        ptr: *mut core::ffi::c_void,
36        size: libc::size_t,
37        alignment: libc::size_t,
38    );
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn it_frees_memory_malloc() {
47        let ptr = unsafe { TCMallocInternalNewAlignedNothrowBridge(8, 16) } as *mut u8;
48        unsafe { TCMallocInternalDeleteSizedAligned(ptr as *mut libc::c_void, 8, 16) };
49    }
50}