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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#![no_std]
extern crate libc;
extern crate libmimalloc_sys as ffi;
use core::ffi::c_void;
use core::alloc::{GlobalAlloc, Layout};
use libmimalloc_sys::*;
#[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 MIN_ALIGN: usize = 8;
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "mips64",
target_arch = "s390x",
target_arch = "sparc64"
)))]
const MIN_ALIGN: usize = 16;
pub struct MiMalloc;
#[cfg(target_os = "linux")]
const ONE_G: usize = 1073741824;
#[cfg(target_os = "linux")]
#[inline]
fn hugepage_align(layout: Layout) -> usize {
let mut len_bytes = layout.size();
let rem = len_bytes % ONE_G;
if rem != 0 {
len_bytes += ONE_G - rem;
}
debug_assert_eq!(len_bytes % ONE_G, 0);
len_bytes
}
#[cfg(target_os = "linux")]
#[inline]
unsafe fn hugepage_alloc_zeroed(layout: Layout) -> *mut u8 {
let len_bytes = hugepage_align(layout);
const FLAGS: libc::c_int = libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_HUGETLB;
const PROTECT_FLAGS: libc::c_int = libc::PROT_READ | libc::PROT_WRITE;
let addr = core::ptr::null_mut();
let ptr: *mut libc::c_void = libc::mmap(addr, len_bytes, PROTECT_FLAGS, FLAGS, -1, 0);
if ptr == libc::MAP_FAILED {
return core::ptr::null_mut();
} else {
return ptr as *mut u8;
}
}
#[cfg(target_os = "linux")]
#[inline]
unsafe fn hugepage_dealloc(ptr: *mut u8, layout: Layout) -> libc::c_int {
let len_bytes = hugepage_align(layout);
libc::munmap(ptr as *mut libc::c_void, len_bytes)
}
unsafe impl GlobalAlloc for MiMalloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
#[cfg(target_os = "linux")]
{
if layout.size() >= ONE_G {
let ptr = hugepage_alloc_zeroed(layout);
if !ptr.is_null() {
return ptr;
}
}
}
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
mi_malloc(layout.size()) as *mut u8
} else {
#[cfg(target_os = "macos")]
if layout.align() > (1 << 31) {
return core::ptr::null_mut();
}
mi_malloc_aligned(layout.size(), layout.align()) as *mut u8
}
}
#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
#[cfg(target_os = "linux")]
{
if layout.size() >= ONE_G {
let ptr = hugepage_alloc_zeroed(layout);
if !ptr.is_null() {
return ptr;
}
}
}
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
mi_zalloc(layout.size()) as *mut u8
} else {
#[cfg(target_os = "macos")]
if layout.align() > (1 << 31) {
return core::ptr::null_mut();
}
mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
}
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
#[cfg(target_os = "linux")]
{
if layout.size() >= ONE_G {
let ret = hugepage_dealloc(ptr, layout);
if ret == 0 {
return ();
}
}
}
mi_free(ptr as *mut c_void);
}
#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
mi_realloc(ptr as *mut c_void, new_size) as *mut u8
} else {
mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_frees_allocated_memory() {
unsafe {
let layout = Layout::from_size_align(8, 8).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc(layout);
alloc.dealloc(ptr, layout);
}
}
#[test]
fn it_frees_allocated_big_memory() {
unsafe {
let layout = Layout::from_size_align(1 << 20, 32).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc(layout);
alloc.dealloc(ptr, layout);
}
}
#[test]
fn it_frees_zero_allocated_memory() {
unsafe {
let layout = Layout::from_size_align(8, 8).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc_zeroed(layout);
alloc.dealloc(ptr, layout);
}
}
#[test]
fn it_frees_zero_allocated_big_memory() {
unsafe {
let layout = Layout::from_size_align(1 << 20, 32).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc_zeroed(layout);
alloc.dealloc(ptr, layout);
}
}
#[test]
fn it_frees_reallocated_memory() {
unsafe {
let layout = Layout::from_size_align(8, 8).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc(layout);
let ptr = alloc.realloc(ptr, layout, 16);
alloc.dealloc(ptr, layout);
}
}
#[test]
fn it_frees_reallocated_big_memory() {
unsafe {
let layout = Layout::from_size_align(1 << 20, 32).unwrap();
let alloc = MiMalloc;
let ptr = alloc.alloc(layout);
let ptr = alloc.realloc(ptr, layout, 2 << 20);
alloc.dealloc(ptr, layout);
}
}
}