diff --git a/include/mimalloc-types.h b/include/mimalloc-types.h index c9f399df..d45a1eea 100644 --- a/include/mimalloc-types.h +++ b/include/mimalloc-types.h @@ -158,7 +158,10 @@ typedef int32_t mi_ssize_t; #define MI_BIN_HUGE (73U) #if (MI_LARGE_OBJ_WSIZE_MAX >= 655360) -#error "define more bins" +#error "mimalloc internal: define more bins" +#endif +#if (MI_ALIGNED_MAX > MI_SEGMENT_SIZE/2) +#error "mimalloc internal: the max aligned boundary is too large for the segment size" #endif // Used as a special value to encode block sizes in 32 bits. diff --git a/include/mimalloc.h b/include/mimalloc.h index 9c3e2dea..b65027d3 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -166,6 +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 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 724c0a1b..15062389 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -20,6 +20,7 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t mi_assert(alignment > 0); 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 ) + if (mi_unlikely(alignment>MI_ALIGNED_MAX)) return NULL; // we cannot align at a boundary larger than this (or otherwise we cannot find segment headers) 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