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 /// Reallocate previously allocated memory.
50 ///
51 /// The pointer `old_ptr` must have been allocated before.
52 ///
53 /// The `alignment` must match the one used to allocate `old_ptr`.
54 ///
55 /// Returned pointer should freed with [`TCMallocInternalDeleteAligned`].
56 pub fn BridgeReallocAligned(
57 old_ptr: *mut core::ffi::c_void,
58 new_size: libc::size_t,
59 alignment: libc::size_t,
60 ) -> *mut core::ffi::c_void;
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn it_frees_memory_malloc() {
69 let ptr = unsafe { BridgeTCMallocInternalNewAlignedNothrow(8, 16) } as *mut u8;
70 unsafe { TCMallocInternalDeleteSizedAligned(ptr as *mut libc::c_void, 8, 16) };
71 }
72}