update alignment tests

This commit is contained in:
Daan Leijen 2021-12-18 11:11:44 -08:00
parent 30a99e2c51
commit 89090510bd
4 changed files with 18 additions and 12 deletions

View File

@ -160,7 +160,7 @@ typedef int32_t mi_ssize_t;
#if (MI_LARGE_OBJ_WSIZE_MAX >= 655360)
#error "mimalloc internal: define more bins"
#endif
#if (MI_ALIGNED_MAX > MI_SEGMENT_SIZE/2)
#if (MI_ALIGNMENT_MAX > MI_SEGMENT_SIZE/2)
#error "mimalloc internal: the max aligned boundary is too large for the segment size"
#endif

View File

@ -166,7 +166,7 @@ mi_decl_export void mi_process_info(size_t* elapsed_msecs, size_t* user_msecs, s
// Note that `alignment` always follows `size` for consistency with unaligned
// allocation, but unfortunately this differs from `posix_memalign` and `aligned_alloc`.
// -------------------------------------------------------------------------------------
#define MI_ALIGNED_MAX (1024*1024UL) // maximum supported alignment is 1MiB
#define MI_ALIGNMENT_MAX (1024*1024UL) // maximum supported alignment is 1MiB
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);
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);

View File

@ -18,7 +18,7 @@ terms of the MIT license. A copy of the license can be found in the file
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
{
mi_assert_internal(size <= PTRDIFF_MAX);
mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNED_MAX);
mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNMENT_MAX);
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
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
#endif
return NULL;
}
if (mi_unlikely(alignment > MI_ALIGNED_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers)
if (mi_unlikely(alignment > MI_ALIGNMENT_MAX)) { // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers)
#if MI_DEBUG > 0
_mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNED_MAX, size, alignment);
_mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)\n", MI_ALIGNMENT_MAX, size, alignment);
#endif
return NULL;
}

View File

@ -159,20 +159,26 @@ int main(void) {
void* p = mi_malloc_aligned(4097,4096); size_t usable = mi_usable_size(p); result = usable >= 4097 && usable < 10000; mi_free(p);
});
CHECK_BODY("malloc-aligned6", {
void* p;
bool ok = true;
for (int i = 1; i < 8 && ok; i++) {
size_t align = (size_t)1 << i;
p = mi_malloc_aligned(2*align, align);
ok = (p != NULL && (uintptr_t)(p) % align == 0); mi_free(p);
for (size_t align = 1; align <= MI_ALIGNMENT_MAX && ok; align *= 2) {
void* ps[8];
for (int i = 0; i < 8 && ok; i++) {
ps[i] = mi_malloc_aligned(align/2 /*size*/, align);
if (ps[i] == NULL || (uintptr_t)(ps[i]) % align != 0) {
ok = false;
}
}
for (int i = 0; i < 8 && ok; i++) {
mi_free(ps[i]);
}
}
result = ok;
});
CHECK_BODY("malloc-aligned7", {
void* p = mi_malloc_aligned(1024,MI_ALIGNED_MAX); mi_free(p);
void* p = mi_malloc_aligned(1024,MI_ALIGNMENT_MAX); mi_free(p);
});
CHECK_BODY("malloc-aligned8", {
void* p = mi_malloc_aligned(1024,2*MI_ALIGNED_MAX); mi_free(p);
void* p = mi_malloc_aligned(1024,2*MI_ALIGNMENT_MAX); mi_free(p);
});
CHECK_BODY("malloc-aligned-at1", {
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);