forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
202 lines (175 loc) · 6.44 KB
/
Copy pathbasic.rs
File metadata and controls
202 lines (175 loc) · 6.44 KB
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
use core::ffi::c_void;
use crate::{default_alloc, mimalloc};
// TODO(refactor): consider reshaping the vtable struct into `trait Allocator` impls.
use crate::{Alignment, AllocatorVTable, StdAllocator};
/// # Safety
/// `ptr` must have been allocated by mimalloc with the given `size`/`align`.
#[inline(always)]
pub(crate) unsafe fn mi_free_checked(ptr: *mut c_void, size: usize, align: usize) {
if cfg!(debug_assertions) {
// SAFETY: `mi_is_in_heap_region` accepts any pointer; remaining calls
// are sound by the caller contract above.
unsafe {
debug_assert!(mimalloc::mi_is_in_heap_region(ptr));
if mimalloc::must_use_aligned_alloc(align) {
mimalloc::mi_free_size_aligned(ptr, size, align);
} else {
mimalloc::mi_free_size(ptr, size);
}
}
} else {
let _ = (size, align);
// SAFETY: caller contract — `ptr` was allocated by mimalloc.
unsafe { mimalloc::mi_free(ptr) }
}
}
pub(crate) fn default_allocator_free(_: *mut c_void, buf: &mut [u8], _: Alignment, _: usize) {
// SAFETY: Allocator vtable invariant — `buf` was allocated by the default allocator.
unsafe { default_alloc::free(buf.as_mut_ptr().cast()) }
}
pub(crate) struct MimallocAllocator;
impl MimallocAllocator {
fn aligned_alloc(len: usize, alignment: Alignment) -> *mut u8 {
let ptr: *mut c_void = default_alloc::malloc_aligned(len, alignment.to_byte_units());
#[cfg(debug_assertions)]
{
if !ptr.is_null() {
// SAFETY: ptr is non-null and was just returned by the default allocator
let usable = unsafe { default_alloc::usable_size(ptr) };
if usable < len && !ptr.is_null() {
panic!(
"default allocator: allocated size is too small: {} < {}",
usable, len
);
}
}
}
ptr.cast::<u8>()
}
fn alloc_with_default_allocator(
_: *mut c_void,
len: usize,
alignment: Alignment,
_: usize,
) -> *mut u8 {
Self::aligned_alloc(len, alignment)
}
pub(crate) fn resize_with_default_allocator(
_: *mut c_void,
buf: &mut [u8],
_: Alignment,
new_len: usize,
_: usize,
) -> bool {
if cfg!(bun_asan) {
return false;
}
// SAFETY: buf.ptr was allocated by mimalloc (non-ASAN ⇒ default = mimalloc)
unsafe { !mimalloc::mi_expand(buf.as_mut_ptr().cast(), new_len).is_null() }
}
pub(crate) fn remap_with_default_allocator(
_: *mut c_void,
buf: &mut [u8],
alignment: Alignment,
new_len: usize,
_: usize,
) -> *mut u8 {
// SAFETY: buf.ptr was allocated by the default allocator with this alignment
unsafe {
default_alloc::realloc_aligned(
buf.as_mut_ptr().cast(),
new_len,
alignment.to_byte_units(),
)
.cast::<u8>()
}
}
const FREE_WITH_DEFAULT_ALLOCATOR: fn(*mut c_void, &mut [u8], Alignment, usize) =
default_allocator_free;
}
pub static C_ALLOCATOR: StdAllocator = StdAllocator {
// This ptr can be anything. But since it's not nullable, we should set it to something.
ptr: memory_allocator_tags::DEFAULT_ALLOCATOR_TAG_PTR,
vtable: C_ALLOCATOR_VTABLE,
};
static C_ALLOCATOR_VTABLE: &AllocatorVTable = &AllocatorVTable {
alloc: MimallocAllocator::alloc_with_default_allocator,
resize: MimallocAllocator::resize_with_default_allocator,
remap: MimallocAllocator::remap_with_default_allocator,
free: MimallocAllocator::FREE_WITH_DEFAULT_ALLOCATOR,
};
pub(crate) struct ZAllocator;
impl ZAllocator {
fn aligned_alloc(len: usize, alignment: Alignment) -> *mut u8 {
let ptr: *mut c_void = default_alloc::zalloc_aligned(len, alignment.to_byte_units());
#[cfg(debug_assertions)]
{
if !ptr.is_null() {
// SAFETY: ptr is non-null and was just returned by the default allocator
let usable = unsafe { default_alloc::usable_size(ptr) };
if usable < len {
panic!(
"default allocator: allocated size is too small: {} < {}",
usable, len
);
}
}
}
ptr.cast::<u8>()
}
fn aligned_alloc_size(ptr: *mut u8) -> usize {
// SAFETY: ptr was allocated by the default allocator
unsafe { default_alloc::usable_size(ptr.cast()) }
}
fn alloc_with_z_allocator(
_: *mut c_void,
len: usize,
alignment: Alignment,
_: usize,
) -> *mut u8 {
Self::aligned_alloc(len, alignment)
}
fn resize_with_z_allocator(
_: *mut c_void,
buf: &mut [u8],
_: Alignment,
new_len: usize,
_: usize,
) -> bool {
if new_len <= buf.len() {
return true;
}
let full_len = Self::aligned_alloc_size(buf.as_mut_ptr());
if new_len <= full_len {
return true;
}
false
}
const FREE_WITH_Z_ALLOCATOR: fn(*mut c_void, &mut [u8], Alignment, usize) =
default_allocator_free;
}
pub(crate) mod memory_allocator_tags {
use core::ffi::c_void;
const DEFAULT_ALLOCATOR_TAG: usize = 0xBEEFA110C; // "BEEFA110C" beef a110c i guess
pub(crate) const DEFAULT_ALLOCATOR_TAG_PTR: *mut c_void = DEFAULT_ALLOCATOR_TAG as *mut c_void;
const Z_ALLOCATOR_TAG: usize = 0x2a11043470123; // "z4110c4701" (Z ALLOCATOR in 1337 speak)
pub(crate) const Z_ALLOCATOR_TAG_PTR: *mut c_void = Z_ALLOCATOR_TAG as *mut c_void;
}
pub static Z_ALLOCATOR: StdAllocator = StdAllocator {
ptr: memory_allocator_tags::Z_ALLOCATOR_TAG_PTR,
vtable: &Z_ALLOCATOR_VTABLE,
};
static Z_ALLOCATOR_VTABLE: AllocatorVTable = AllocatorVTable {
alloc: ZAllocator::alloc_with_z_allocator,
resize: ZAllocator::resize_with_z_allocator,
remap: AllocatorVTable::NO_REMAP,
free: ZAllocator::FREE_WITH_Z_ALLOCATOR,
};
/// mimalloc can free allocations without being given their size.
///
/// # Safety
/// `ptr` must be null or have been allocated by mimalloc.
pub unsafe fn free_without_size(ptr: *mut c_void) {
// SAFETY: caller contract — ptr is null or was allocated by mimalloc; mi_free accepts null
unsafe { mimalloc::mi_free(ptr) }
}