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 `alignment`.
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 BridgeTCMallocInternalNewAlignedNothrow(
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    /// Free previously allocated memory.
41    ///
42    /// The pointer `ptr` must have been allocated before.
43    ///
44    /// The `alignment` must match the one used to allocate `ptr`.
45    ///
46    /// Performance is lower than [`TCMallocInternalDeleteSizedAligned`].
47    pub fn TCMallocInternalDeleteAligned(ptr: *mut core::ffi::c_void, alignment: libc::size_t);
48
49    /// Prepare to reallocate previously allocated memory.
50    ///
51    /// Caller should do the real data migration if a returned pointer is not the same as `old_ptr`.
52    /// Client should copy bytes manually and then free old_ptr.
53    ///
54    /// The pointer `old_ptr` must have been allocated before.
55    ///
56    /// The `alignment` must match the one used to allocate `old_ptr`.
57    ///
58    /// Returns null pointer if allocation failed or `new_size` is 0.
59    ///
60    /// `old_size` will be set to the original size of the memory block.
61    pub fn BridgePrepareReallocAligned(
62        old_ptr: *mut core::ffi::c_void,
63        new_size: libc::size_t,
64        alignment: libc::size_t,
65        old_size: *mut libc::size_t,
66    ) -> *mut core::ffi::c_void;
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn it_frees_memory_malloc() {
75        let ptr = unsafe { BridgeTCMallocInternalNewAlignedNothrow(8, 16) } as *mut u8;
76        unsafe { TCMallocInternalDeleteSizedAligned(ptr as *mut libc::c_void, 8, 16) };
77    }
78}