From 24777f6a9116d51a1ba6f79e619bedeef3b4d06f Mon Sep 17 00:00:00 2001 From: daan Date: Wed, 11 Sep 2019 17:49:28 -0700 Subject: [PATCH] limit aligned allocation to power-of-two alignment --- include/mimalloc-internal.h | 2 +- src/alloc-aligned.c | 31 +++++++++++++++++++------------ src/alloc-posix.c | 3 ++- test/test-api.c | 8 ++++---- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 8a81337b..e99e6df6 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -168,7 +168,7 @@ static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) { #endif } -// Is `x` a power of two? +// Is `x` a power of two? (0 is considered a power of two) static inline bool _mi_is_power_of_two(uintptr_t x) { return ((x & (x - 1)) == 0); } diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index b629d57b..352f07b2 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -14,24 +14,24 @@ terms of the MIT license. A copy of the license can be found in the file // Aligned Allocation // ------------------------------------------------------ -static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset, bool zero) mi_attr_noexcept { +static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept { // note: we don't require `size > offset`, we just guarantee that // the address at offset is aligned regardless of the allocated size. mi_assert(alignment > 0 && alignment % sizeof(void*) == 0); - if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see ) - // use regular allocation if it is guaranteed to fit the alignment constraints - if (mi_unlikely(alignment <= sizeof(void*))) return _mi_heap_malloc_zero(heap, size, zero); - - // try if there is a current small block with just the right alignment - if (size <= MI_SMALL_SIZE_MAX) { + if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see ) + if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see ) + const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)` + + // try if there is a small block available with just the right alignment + if (mi_likely(size <= MI_SMALL_SIZE_MAX)) { mi_page_t* page = _mi_heap_get_free_small_page(heap,size); - if (page->free != NULL && - (((uintptr_t)page->free + offset) % alignment) == 0) + const bool is_aligned = (((uintptr_t)page->free+offset) & align_mask)==0; + if (mi_likely(page->free != NULL && is_aligned)) { #if MI_STAT>1 - mi_heap_stat_increase( heap, malloc, size); + mi_heap_stat_increase( heap, malloc, size); #endif - void* p = _mi_page_malloc(heap,page,size); + void* p = _mi_page_malloc(heap,page,size); // TODO: inline _mi_page_malloc mi_assert_internal(p != NULL); mi_assert_internal(((uintptr_t)p + offset) % alignment == 0); if (zero) _mi_block_zero_init(page,p,size); @@ -39,12 +39,19 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* heap, size_t size, size_t } } + // use regular allocation if it is guaranteed to fit the alignment constraints + if (offset==0 && alignment<=size && size<=MI_MEDIUM_OBJ_SIZE_MAX && (size&align_mask)==0) { + void* p = _mi_heap_malloc_zero(heap, size, zero); + mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0); + return p; + } + // otherwise over-allocate void* p = _mi_heap_malloc_zero(heap, size + alignment - 1, zero); if (p == NULL) return NULL; // .. and align within the allocation - uintptr_t adjust = alignment - (((uintptr_t)p + offset) % alignment); + uintptr_t adjust = alignment - (((uintptr_t)p + offset) & align_mask); mi_assert_internal(adjust % sizeof(uintptr_t) == 0); void* aligned_p = (adjust == alignment ? p : (void*)((uintptr_t)p + adjust)); if (aligned_p != p) mi_page_set_has_aligned(_mi_ptr_page(p), true); diff --git a/src/alloc-posix.c b/src/alloc-posix.c index f64cb1f6..505e42e4 100644 --- a/src/alloc-posix.c +++ b/src/alloc-posix.c @@ -71,7 +71,8 @@ void* mi_pvalloc(size_t size) mi_attr_noexcept { } void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept { - if (alignment != 0 && (size%alignment) != 0) return NULL; // C11 required; + if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL; + if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see return mi_malloc_aligned(size, alignment); } diff --git a/test/test-api.c b/test/test-api.c index 5f3395fb..bd2291da 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -121,16 +121,16 @@ int main() { // Aligned API // --------------------------------------------------- CHECK_BODY("malloc-aligned1", { - void* p = mi_malloc_aligned(32,24); result = (p != NULL && (uintptr_t)(p) % 24 == 0); mi_free(p); + void* p = mi_malloc_aligned(32,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p); }); CHECK_BODY("malloc-aligned2", { - void* p = mi_malloc_aligned(48,24); result = (p != NULL && (uintptr_t)(p) % 24 == 0); mi_free(p); + void* p = mi_malloc_aligned(48,32); result = (p != NULL && (uintptr_t)(p) % 32 == 0); mi_free(p); }); CHECK_BODY("malloc-aligned-at1", { - void* p = mi_malloc_aligned_at(48,24,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 24 == 0); mi_free(p); + void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p); }); CHECK_BODY("malloc-aligned-at2", { - void* p = mi_malloc_aligned_at(50,24,8); result = (p != NULL && ((uintptr_t)(p) + 8) % 24 == 0); mi_free(p); + void* p = mi_malloc_aligned_at(50,32,8); result = (p != NULL && ((uintptr_t)(p) + 8) % 32 == 0); mi_free(p); }); // ---------------------------------------------------