Skip to content

Commit c70c1df

Browse files
committed
better fast path for aligned allocation; check max alloc size correctly in the aligned fallback
1 parent 605c354 commit c70c1df

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/alloc-aligned.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ terms of the MIT license. A copy of the license can be found in the file
1515
// Aligned Allocation
1616
// ------------------------------------------------------
1717

18+
static inline bool mi_is_naturally_aligned( size_t size, size_t alignment ) {
19+
// objects up to `MI_MEDIUM_OBJ_SIZE_MAX` are allocated aligned to their size (see `segment.c:_mi_segment_page_start`).
20+
// note: the size may not be not an actual bin-size but it turns out the test below is still correct for our
21+
// powers of two bin spacing (see test-api.c:test-aligned13).
22+
mi_assert_internal(_mi_is_power_of_two(alignment) && (alignment > 0));
23+
return (size <= MI_MEDIUM_OBJ_SIZE_MAX && alignment <= size && ((size + MI_PADDING_SIZE) & (alignment-1)) == 0);
24+
}
25+
26+
1827
// Fallback primitive aligned allocation -- split out for better codegen
1928
static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept
2029
{
21-
mi_assert_internal(size <= PTRDIFF_MAX);
30+
mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE));
2231
mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));
2332

24-
const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)`
25-
const size_t padsize = size + MI_PADDING_SIZE;
26-
27-
// use regular allocation if it is guaranteed to fit the alignment constraints
28-
if (offset == 0 && alignment <= padsize && padsize <= MI_MEDIUM_OBJ_SIZE_MAX && (padsize & align_mask) == 0) {
33+
// use regular allocation if it is guaranteed to fit the alignment constraints.
34+
if (offset == 0 && mi_is_naturally_aligned(size,alignment)) {
2935
void* p = _mi_heap_malloc_zero(heap, size, zero);
3036
mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);
3137
return p;
@@ -57,6 +63,7 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t*
5763
}
5864

5965
// .. and align within the allocation
66+
const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)`
6067
const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask;
6168
const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset);
6269
mi_assert_internal(adjust < alignment);
@@ -100,14 +107,14 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
100107
return NULL;
101108
}
102109

103-
if mi_unlikely(size > PTRDIFF_MAX) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
110+
if mi_unlikely(size > (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE)) { // we don't allocate more than MI_MAX_ALLOC_SIZE (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
104111
#if MI_DEBUG > 0
105112
_mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)\n", size, alignment);
106113
#endif
107114
return NULL;
108115
}
109116
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
110-
const size_t padsize = size + MI_PADDING_SIZE; // note: cannot overflow due to earlier size > PTRDIFF_MAX check
117+
const size_t padsize = size + MI_PADDING_SIZE; // note: cannot overflow due to earlier size check
111118

112119
// try first if there happens to be a small block available with just the right alignment
113120
if mi_likely(padsize <= MI_SMALL_SIZE_MAX && alignment <= padsize) {
@@ -140,15 +147,7 @@ mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned_at(mi_heap_t* he
140147

141148
mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
142149
if (alignment == 0 || !_mi_is_power_of_two(alignment)) return NULL;
143-
#if !MI_PADDING
144-
// without padding, any small sized allocation is naturally aligned (see also `_mi_segment_page_start`)
145-
if mi_likely(_mi_is_power_of_two(size) && size >= alignment && size <= MI_SMALL_SIZE_MAX)
146-
#else
147-
// with padding, we can only guarantee this for fixed alignments
148-
if mi_likely((alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2)))
149-
&& size <= MI_SMALL_SIZE_MAX)
150-
#endif
151-
{
150+
if (size <= MI_SMALL_SIZE_MAX && mi_is_naturally_aligned(size,alignment)) {
152151
// fast path for common alignment and size
153152
return mi_heap_malloc_small(heap, size);
154153
}

test/test-api.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ int main(void) {
230230
result = (((uintptr_t)p % 0x100) == 0); // #602
231231
mi_free(p);
232232
}
233+
CHECK_BODY("mimalloc-aligned13") {
234+
bool ok = true;
235+
for( size_t size = 1; size <= MI_SMALL_SIZE_MAX && ok; size++ ) {
236+
for(size_t align = 1; align <= size && ok; align *= 2 ) {
237+
void* p = mi_malloc_aligned(size,align);
238+
ok = (p != NULL && ((uintptr_t)p % align) == 0);
239+
mi_free(p);
240+
/*
241+
if (ok && align <= size && ((size + MI_PADDING_SIZE) & (align-1)) == 0) {
242+
size_t bsize = mi_good_size(size);
243+
ok = (align <= bsize && ((bsize + MI_PADDING_SIZE) & (align-1)) == 0);
244+
}
245+
*/
246+
}
247+
}
248+
result = ok;
249+
}
233250
CHECK_BODY("malloc-aligned-at1") {
234251
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
235252
};

0 commit comments

Comments
 (0)