1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![no_std]
use core::alloc::{GlobalAlloc, Layout};

#[cfg(all(any(
    target_arch = "x86",
    target_arch = "arm",
    target_arch = "mips",
    target_arch = "powerpc",
    target_arch = "powerpc64",
    target_arch = "asmjs",
    target_arch = "wasm32"
)))]
const MAX_ALIGN_T: usize = 8;
#[cfg(all(any(
    target_arch = "x86_64",
    target_arch = "aarch64",
    target_arch = "mips64",
    target_arch = "s390x",
    target_arch = "sparc64"
)))]
const MAX_ALIGN_T: usize = 16;

#[derive(Copy, Clone, Default, Debug)]
pub struct Mimalloc;

fn fundamental_alignment(size: usize, align: usize) -> bool {
    align <= MAX_ALIGN_T && align <= size
}

unsafe impl GlobalAlloc for Mimalloc {
    #[inline]
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let size = layout.size();
        let align = layout.align();
        let ptr = if fundamental_alignment(size, align) {
            mimalloc_sys::mi_malloc(size as _)
        } else {
            mimalloc_sys::mi_malloc_aligned(size as _, align as _)
        };

        ptr as *mut u8
    }

    #[inline]
    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        let size = layout.size();
        let align = layout.align();
        let ptr = if fundamental_alignment(size, align) {
            mimalloc_sys::mi_zalloc(size as _)
        } else {
            mimalloc_sys::mi_zalloc_aligned(size as _, align as _)
        };

        ptr as *mut u8
    }

    #[inline]
    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
        mimalloc_sys::mi_free(ptr as *mut _);
    }

    #[inline]
    unsafe fn realloc(
        &self,
        ptr: *mut u8,
        layout: Layout,
        new_size: usize,
    ) -> *mut u8 {
        let size = layout.size();
        let align = layout.align();
        let ptr = if fundamental_alignment(size, align) {
            mimalloc_sys::mi_realloc(ptr as *mut _, new_size)
        } else {
            mimalloc_sys::mi_realloc_aligned(ptr as *mut _, new_size, align)
        };

        ptr as *mut u8
    }
}