From 89090510bd965ca48a83857eaa6b6e78a951bf7e Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sat, 18 Dec 2021 11:11:44 -0800 Subject: [PATCH] update alignment tests --- include/mimalloc-types.h | 2 +- include/mimalloc.h | 2 +- src/alloc-aligned.c | 6 +++--- test/test-api.c | 20 +++++++++++++------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/mimalloc-types.h b/include/mimalloc-types.h index d45a1eea..b349dfc3 100644 --- a/include/mimalloc-types.h +++ b/include/mimalloc-types.h @@ -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 diff --git a/include/mimalloc.h b/include/mimalloc.h index b65027d3..a0b71389 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -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); diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index 3a608b83..6b15e653 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -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; } diff --git a/test/test-api.c b/test/test-api.c index 56835d00..96817337 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -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);