libmimalloc_sys/lib.rs
1#![no_std]
2// Copyright 2019 Octavian Oncescu
3
4use core::ffi::c_void;
5
6#[cfg(feature = "extended")]
7mod extended;
8#[cfg(feature = "extended")]
9pub use extended::*;
10
11extern "C" {
12 /// Allocate zero-initialized `size` bytes.
13 ///
14 /// Returns a pointer to newly allocated zero-initialized memory, or null if
15 /// out of memory.
16 pub fn mi_zalloc(size: usize) -> *mut c_void;
17
18 /// Allocate `size` bytes.
19 ///
20 /// Returns pointer to the allocated memory or null if out of memory.
21 /// Returns a unique pointer if called with `size` 0.
22 pub fn mi_malloc(size: usize) -> *mut c_void;
23
24 /// Re-allocate memory to `newsize` bytes.
25 ///
26 /// Return pointer to the allocated memory or null if out of memory. If null
27 /// is returned, the pointer `p` is not freed. Otherwise the original
28 /// pointer is either freed or returned as the reallocated result (in case
29 /// it fits in-place with the new size).
30 ///
31 /// If `p` is null, it behaves as [`mi_malloc`]. If `newsize` is larger than
32 /// the original `size` allocated for `p`, the bytes after `size` are
33 /// uninitialized.
34 pub fn mi_realloc(p: *mut c_void, newsize: usize) -> *mut c_void;
35
36 /// Allocate `size` bytes aligned by `alignment`, initialized to zero.
37 ///
38 /// Return pointer to the allocated memory or null if out of memory.
39 ///
40 /// Returns a unique pointer if called with `size` 0.
41 pub fn mi_zalloc_aligned(size: usize, alignment: usize) -> *mut c_void;
42
43 /// Allocate `size` bytes aligned by `alignment`.
44 ///
45 /// Return pointer to the allocated memory or null if out of memory.
46 ///
47 /// Returns a unique pointer if called with `size` 0.
48 pub fn mi_malloc_aligned(size: usize, alignment: usize) -> *mut c_void;
49
50 /// Re-allocate memory to `newsize` bytes, aligned by `alignment`.
51 ///
52 /// Return pointer to the allocated memory or null if out of memory. If null
53 /// is returned, the pointer `p` is not freed. Otherwise the original
54 /// pointer is either freed or returned as the reallocated result (in case
55 /// it fits in-place with the new size).
56 ///
57 /// If `p` is null, it behaves as [`mi_malloc_aligned`]. If `newsize` is
58 /// larger than the original `size` allocated for `p`, the bytes after
59 /// `size` are uninitialized.
60 pub fn mi_realloc_aligned(p: *mut c_void, newsize: usize, alignment: usize) -> *mut c_void;
61
62 /// Free previously allocated memory.
63 ///
64 /// The pointer `p` must have been allocated before (or be null).
65 pub fn mi_free(p: *mut c_void);
66}
67
68/// When using the `"override"` feature flag, the user wants us to globally
69/// override the system allocator.
70///
71/// However, since we build and link `mimalloc` as a static library/archive,
72/// the linker may decide to not care about our overrides if it can't directly
73/// see references to the symbols, see the following link for details:
74/// <https://maskray.me/blog/2021-06-20-symbol-processing#archive-processing>
75///
76/// This is problematic if `mimalloc` is used from a library that by itself
77/// doesn't allocate, yet invokes other shared libraries that do, since then
78/// the linker wouldn't see any references to `malloc`/`free`, and the symbols
79/// would not be overridden.
80///
81/// To avoid this problem, we make sure that the allocator functions are
82/// visible to the linker.
83///
84/// To avoid this problem, we reference `mi_malloc` in a `#[used]` static.
85/// This makes it known to `rustc`, which will create a reference to it in a
86/// `symbols.o` stub file that is later passed directly to the linker (instead
87/// of being in an archive). See the following link for details on how this
88/// works: <https://github.com/rust-lang/rust/pull/95604>
89///
90/// NOTE: This works because `mimalloc` is compiled into a single object file
91/// in `static.c`. If it was split across multiple files, we'd need to
92/// reference each symbol. See also the comment at the top of `static.c`.
93///
94/// NOTE: On macOS, mimalloc doesn't just override malloc/free, but also
95/// registers itself with the allocator's zone APIs in a ctor
96/// (`_mi_macos_override_malloc`, marked with `__attribute__((constructor))`).
97/// Similarly to above, for the Mach-O linker to actually consider ctors as
98/// "used" when defined in an archive member in a static library, so we need
99/// to explicitly reference something in the object file. The constructor
100/// symbol itself is static, so we can't get a reference to that, so instead
101/// we reference `mi_malloc` here too).
102#[cfg(feature = "override")]
103mod set_up_statics {
104 use super::*;
105 #[used] // Could be `#[used(linker)]` once stable
106 static USED: unsafe extern "C" fn(usize) -> *mut c_void = mi_malloc;
107}
108
109#[cfg(test)]
110mod tests {
111 extern crate libc;
112 use super::*;
113
114 #[test]
115 fn it_frees_memory_malloc() {
116 let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
117 unsafe { mi_free(ptr as *mut c_void) };
118 }
119
120 #[test]
121 fn it_frees_memory_zalloc() {
122 let ptr = unsafe { mi_zalloc_aligned(8, 8) } as *mut u8;
123 unsafe { mi_free(ptr as *mut c_void) };
124 }
125
126 #[test]
127 fn it_frees_memory_realloc() {
128 let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
129 let ptr = unsafe { mi_realloc_aligned(ptr as *mut c_void, 8, 8) } as *mut u8;
130 unsafe { mi_free(ptr as *mut c_void) };
131 }
132
133 #[cfg(all(feature = "override", target_vendor = "apple"))]
134 #[test]
135 fn mimalloc_and_libc_are_interoperable_when_overridden() {
136 let ptr = unsafe { mi_malloc(42) };
137 unsafe { libc::free(ptr) };
138 }
139}