Skip to content

Commit ec76cf5

Browse files
authored
Merge pull request #100 from nathaniel-daniel/fix-issue-87
Remove `may_use_unaligned_api`
2 parents 7929d57 + 8b579cc commit ec76cf5

File tree

1 file changed

+3
-42
lines changed

1 file changed

+3
-42
lines changed

src/lib.rs

+3-42
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,6 @@ use core::alloc::{GlobalAlloc, Layout};
2929
use core::ffi::c_void;
3030
use ffi::*;
3131

32-
// `MI_MAX_ALIGN_SIZE` is 16 unless manually overridden:
33-
// https://github.com/microsoft/mimalloc/blob/15220c68/include/mimalloc-types.h#L22
34-
//
35-
// If it changes on us, we should consider either manually overriding it, or
36-
// expose it from the -sys crate (in order to catch updates)
37-
const MI_MAX_ALIGN_SIZE: usize = 16;
38-
39-
// Note: this doesn't take a layout directly because doing so would be wrong for
40-
// reallocation
41-
#[inline]
42-
fn may_use_unaligned_api(size: usize, alignment: usize) -> bool {
43-
// Required by `GlobalAlloc`. Note that while allocators aren't allowed to
44-
// unwind in rust, this is only in debug mode, and can only happen if the
45-
// caller already caused UB by passing in an invalid layout.
46-
debug_assert!(size != 0 && alignment.is_power_of_two());
47-
48-
// This logic is based on the discussion [here]. We don't bother with the
49-
// 3rd suggested test due to it being high cost (calling `mi_good_size`)
50-
// compared to the other checks, and also feeling like it relies on too much
51-
// implementation-specific behavior.
52-
//
53-
// [here]: https://github.com/microsoft/mimalloc/issues/314#issuecomment-708541845
54-
55-
(alignment <= MI_MAX_ALIGN_SIZE && size >= alignment)
56-
|| (alignment == size && alignment <= 4096)
57-
}
58-
5932
/// Drop-in mimalloc global allocator.
6033
///
6134
/// ## Usage
@@ -70,20 +43,12 @@ pub struct MiMalloc;
7043
unsafe impl GlobalAlloc for MiMalloc {
7144
#[inline]
7245
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
73-
if may_use_unaligned_api(layout.size(), layout.align()) {
74-
mi_malloc(layout.size()) as *mut u8
75-
} else {
76-
mi_malloc_aligned(layout.size(), layout.align()) as *mut u8
77-
}
46+
mi_malloc_aligned(layout.size(), layout.align()) as *mut u8
7847
}
7948

8049
#[inline]
8150
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
82-
if may_use_unaligned_api(layout.size(), layout.align()) {
83-
mi_zalloc(layout.size()) as *mut u8
84-
} else {
85-
mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
86-
}
51+
mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
8752
}
8853

8954
#[inline]
@@ -93,11 +58,7 @@ unsafe impl GlobalAlloc for MiMalloc {
9358

9459
#[inline]
9560
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
96-
if may_use_unaligned_api(new_size, layout.align()) {
97-
mi_realloc(ptr as *mut c_void, new_size) as *mut u8
98-
} else {
99-
mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8
100-
}
61+
mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8
10162
}
10263
}
10364

0 commit comments

Comments
 (0)