dlmalloc/
global.rs

1use crate::Dlmalloc;
2use core::alloc::{GlobalAlloc, Layout};
3use core::ptr;
4
5pub use crate::sys::enable_alloc_after_fork;
6
7/// An instance of a "global allocator" backed by `Dlmalloc`
8///
9/// This API requires the `global` feature is activated, and this type
10/// implements the `GlobalAlloc` trait in the standard library.
11pub struct GlobalDlmalloc;
12
13static mut DLMALLOC: Dlmalloc = Dlmalloc::new();
14
15unsafe impl GlobalAlloc for GlobalDlmalloc {
16    #[inline]
17    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
18        let _guard = lock();
19        let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
20        (*dlmalloc).malloc(layout.size(), layout.align())
21    }
22
23    #[inline]
24    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
25        let _guard = lock();
26        let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
27        (*dlmalloc).free(ptr, layout.size(), layout.align())
28    }
29
30    #[inline]
31    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
32        let _guard = lock();
33        let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
34        (*dlmalloc).calloc(layout.size(), layout.align())
35    }
36
37    #[inline]
38    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
39        let _guard = lock();
40        let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
41        (*dlmalloc).realloc(ptr, layout.size(), layout.align(), new_size)
42    }
43}
44
45unsafe fn lock() -> impl Drop {
46    crate::sys::acquire_global_lock();
47
48    struct Guard;
49    impl Drop for Guard {
50        fn drop(&mut self) {
51            crate::sys::release_global_lock()
52        }
53    }
54
55    Guard
56}