Skip to content

Commit 8909051

Browse files
committed
update alignment tests
1 parent 30a99e2 commit 8909051

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

include/mimalloc-types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ typedef int32_t mi_ssize_t;
160160
#if (MI_LARGE_OBJ_WSIZE_MAX >= 655360)
161161
#error "mimalloc internal: define more bins"
162162
#endif
163-
#if (MI_ALIGNED_MAX > MI_SEGMENT_SIZE/2)
163+
#if (MI_ALIGNMENT_MAX > MI_SEGMENT_SIZE/2)
164164
#error "mimalloc internal: the max aligned boundary is too large for the segment size"
165165
#endif
166166

include/mimalloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ mi_decl_export void mi_process_info(size_t* elapsed_msecs, size_t* user_msecs, s
166166
// Note that `alignment` always follows `size` for consistency with unaligned
167167
// allocation, but unfortunately this differs from `posix_memalign` and `aligned_alloc`.
168168
// -------------------------------------------------------------------------------------
169-
#define MI_ALIGNED_MAX (1024*1024UL) // maximum supported alignment is 1MiB
169+
#define MI_ALIGNMENT_MAX (1024*1024UL) // maximum supported alignment is 1MiB
170170

171171
mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1) mi_attr_alloc_align(2);
172172
mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept mi_attr_malloc mi_attr_alloc_size(1);

src/alloc-aligned.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ terms of the MIT license. A copy of the license can be found in the file
1818
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
1919
{
2020
mi_assert_internal(size <= PTRDIFF_MAX);
21-
mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNED_MAX);
21+
mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNMENT_MAX);
2222

2323
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
2424
const size_t padsize = size + MI_PADDING_SIZE;
@@ -55,9 +55,9 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
5555
#endif
5656
return NULL;
5757
}
58-
if (mi_unlikely(alignment > MI_ALIGNED_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers)
58+
if (mi_unlikely(alignment > MI_ALIGNMENT_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers)
5959
#if MI_DEBUG > 0
60-
_mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNED_MAX, size, alignment);
60+
_mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNMENT_MAX, size, alignment);
6161
#endif
6262
return NULL;
6363
}

test/test-api.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,26 @@ int main(void) {
159159
void* p = mi_malloc_aligned(4097,4096); size_t usable = mi_usable_size(p); result = usable >= 4097 && usable < 10000; mi_free(p);
160160
});
161161
CHECK_BODY("malloc-aligned6", {
162-
void* p;
163162
bool ok = true;
164-
for (int i = 1; i < 8 && ok; i++) {
165-
size_t align = (size_t)1 << i;
166-
p = mi_malloc_aligned(2*align, align);
167-
ok = (p != NULL && (uintptr_t)(p) % align == 0); mi_free(p);
163+
for (size_t align = 1; align <= MI_ALIGNMENT_MAX && ok; align *= 2) {
164+
void* ps[8];
165+
for (int i = 0; i < 8 && ok; i++) {
166+
ps[i] = mi_malloc_aligned(align/2 /*size*/, align);
167+
if (ps[i] == NULL || (uintptr_t)(ps[i]) % align != 0) {
168+
ok = false;
169+
}
170+
}
171+
for (int i = 0; i < 8 && ok; i++) {
172+
mi_free(ps[i]);
173+
}
168174
}
169175
result = ok;
170176
});
171177
CHECK_BODY("malloc-aligned7", {
172-
void* p = mi_malloc_aligned(1024,MI_ALIGNED_MAX); mi_free(p);
178+
void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX); mi_free(p);
173179
});
174180
CHECK_BODY("malloc-aligned8", {
175-
void* p = mi_malloc_aligned(1024,2*MI_ALIGNED_MAX); mi_free(p);
181+
void* p = mi_malloc_aligned(1024,2*MI_ALIGNMENT_MAX); mi_free(p);
176182
});
177183
CHECK_BODY("malloc-aligned-at1", {
178184
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);

0 commit comments

Comments
 (0)