mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-06 15:29:31 +03:00
merge from dev (MI_ALIGNED_MAX)
This commit is contained in:
commit
abbff9c030
7 changed files with 118 additions and 45 deletions
|
@ -268,11 +268,6 @@ static inline size_t _mi_wsize_from_size(size_t size) {
|
||||||
return (size + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
|
return (size + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does malloc satisfy the alignment constraints already?
|
|
||||||
static inline bool mi_malloc_satisfies_alignment(size_t alignment, size_t size) {
|
|
||||||
return (alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overflow detecting multiply
|
// Overflow detecting multiply
|
||||||
#if __has_builtin(__builtin_umul_overflow) || (defined(__GNUC__) && (__GNUC__ >= 5))
|
#if __has_builtin(__builtin_umul_overflow) || (defined(__GNUC__) && (__GNUC__ >= 5))
|
||||||
#include <limits.h> // UINT_MAX, ULONG_MAX
|
#include <limits.h> // UINT_MAX, ULONG_MAX
|
||||||
|
|
|
@ -162,11 +162,17 @@ typedef int32_t mi_ssize_t;
|
||||||
#define MI_BIN_HUGE (73U)
|
#define MI_BIN_HUGE (73U)
|
||||||
|
|
||||||
#if (MI_MEDIUM_OBJ_WSIZE_MAX >= 655360)
|
#if (MI_MEDIUM_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
|
||||||
|
#if (MI_ALIGNED_MAX % MI_SEGMENT_SLICE_SIZE != 0)
|
||||||
|
#error "mimalloc internal: the max aligned boundary must be an integral multiple of the segment slice size"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Maximum slice offset (7)
|
// Maximum slice offset (15)
|
||||||
#define MI_MAX_SLICE_OFFSET ((MI_MEDIUM_PAGE_SIZE / MI_SEGMENT_SLICE_SIZE) - 1)
|
#define MI_MAX_SLICE_OFFSET ((MI_ALIGNED_MAX / MI_SEGMENT_SLICE_SIZE) - 1)
|
||||||
|
|
||||||
// Used as a special value to encode block sizes in 32 bits.
|
// Used as a special value to encode block sizes in 32 bits.
|
||||||
#define MI_HUGE_BLOCK_SIZE ((uint32_t)MI_HUGE_OBJ_SIZE_MAX)
|
#define MI_HUGE_BLOCK_SIZE ((uint32_t)MI_HUGE_OBJ_SIZE_MAX)
|
||||||
|
|
|
@ -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
|
// Note that `alignment` always follows `size` for consistency with unaligned
|
||||||
// allocation, but unfortunately this differs from `posix_memalign` and `aligned_alloc`.
|
// 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(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);
|
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);
|
||||||
|
|
|
@ -14,31 +14,14 @@ terms of the MIT license. A copy of the license can be found in the file
|
||||||
// Aligned Allocation
|
// Aligned Allocation
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|
||||||
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 {
|
// Fallback primitive aligned allocation -- split out for better codegen
|
||||||
// note: we don't require `size > offset`, we just guarantee that
|
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
|
||||||
// the address at offset is aligned regardless of the allocated size.
|
|
||||||
mi_assert(alignment > 0);
|
|
||||||
if (mi_unlikely(size > PTRDIFF_MAX)) return NULL; // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
|
|
||||||
if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) return NULL; // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
|
|
||||||
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
|
|
||||||
const size_t padsize = size + MI_PADDING_SIZE;
|
|
||||||
if (mi_likely(padsize <= MI_SMALL_SIZE_MAX)) {
|
|
||||||
mi_page_t* page = _mi_heap_get_free_small_page(heap,padsize);
|
|
||||||
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_assert_internal(size <= PTRDIFF_MAX);
|
||||||
mi_heap_stat_increase( heap, malloc, size);
|
mi_assert_internal(alignment!=0 && _mi_is_power_of_two(alignment) && alignment <= MI_ALIGNED_MAX);
|
||||||
#endif
|
|
||||||
void* p = _mi_page_malloc(heap,page,padsize); // TODO: inline _mi_page_malloc
|
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
|
||||||
mi_assert_internal(p != NULL);
|
const size_t padsize = size + MI_PADDING_SIZE;
|
||||||
mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);
|
|
||||||
if (zero) _mi_block_zero_init(page,p,size);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// use regular allocation if it is guaranteed to fit the alignment constraints
|
// use regular allocation if it is guaranteed to fit the alignment constraints
|
||||||
if (offset==0 && alignment<=padsize && padsize<=MI_MAX_ALIGN_GUARANTEE && (padsize&align_mask)==0) {
|
if (offset==0 && alignment<=padsize && padsize<=MI_MAX_ALIGN_GUARANTEE && (padsize&align_mask)==0) {
|
||||||
|
@ -61,14 +44,83 @@ static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t
|
||||||
return aligned_p;
|
return aligned_p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Primitive aligned allocation
|
||||||
|
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);
|
||||||
|
if (mi_unlikely(alignment==0 || !_mi_is_power_of_two(alignment))) { // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
|
||||||
|
#if MI_DEBUG > 0
|
||||||
|
_mi_error_message(EOVERFLOW, "aligned allocation requires the alignment to be a power-of-two (size %zu, alignment %zu)", size, alignment);
|
||||||
|
#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_DEBUG > 0
|
||||||
|
_mi_error_message(EOVERFLOW, "aligned allocation has a maximum alignment of %zu (size %zu, alignment %zu)", MI_ALIGNED_MAX, size, alignment);
|
||||||
|
#endif
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (mi_unlikely(size > PTRDIFF_MAX)) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
|
||||||
|
#if MI_DEBUG > 0
|
||||||
|
_mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)", size, alignment);
|
||||||
|
#endif
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
|
||||||
|
const size_t padsize = size + MI_PADDING_SIZE; // note: cannot overflow due to earlier size > PTRDIFF_MAX check
|
||||||
|
|
||||||
|
// try first if there happens to be a small block available with just the right alignment
|
||||||
|
if (mi_likely(padsize <= MI_SMALL_SIZE_MAX)) {
|
||||||
|
mi_page_t* page = _mi_heap_get_free_small_page(heap, padsize);
|
||||||
|
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);
|
||||||
|
#endif
|
||||||
|
void* p = _mi_page_malloc(heap, page, padsize); // 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); }
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// fallback
|
||||||
|
return mi_heap_malloc_zero_aligned_at_fallback(heap, size, alignment, offset, zero);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Optimized mi_heap_malloc_aligned / mi_malloc_aligned
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
mi_decl_restrict void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
|
mi_decl_restrict void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
|
||||||
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false);
|
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
|
mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
|
||||||
|
#if !MI_PADDING
|
||||||
|
// without padding, any small sized allocation is naturally aligned (see also `_mi_segment_page_start`)
|
||||||
|
if (!_mi_is_power_of_two(alignment)) return NULL;
|
||||||
|
if (mi_likely(_mi_is_power_of_two(size) && size >= alignment && size <= MI_SMALL_SIZE_MAX))
|
||||||
|
#else
|
||||||
|
// with padding, we can only guarantee this for fixed alignments
|
||||||
|
if (mi_likely((alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2)))
|
||||||
|
&& size <= MI_SMALL_SIZE_MAX))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
// fast path for common alignment and size
|
||||||
|
return mi_heap_malloc_small(heap, size);
|
||||||
|
}
|
||||||
|
else {
|
||||||
return mi_heap_malloc_aligned_at(heap, size, alignment, 0);
|
return mi_heap_malloc_aligned_at(heap, size, alignment, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Aligned Allocation
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
mi_decl_restrict void* mi_heap_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
|
mi_decl_restrict void* mi_heap_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
|
||||||
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true);
|
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true);
|
||||||
|
@ -113,6 +165,10 @@ mi_decl_restrict void* mi_calloc_aligned(size_t count, size_t size, size_t align
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Aligned re-allocation
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
static void* mi_heap_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset, bool zero) mi_attr_noexcept {
|
static void* mi_heap_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset, bool zero) mi_attr_noexcept {
|
||||||
mi_assert(alignment > 0);
|
mi_assert(alignment > 0);
|
||||||
if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
|
if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
|
||||||
|
|
|
@ -57,8 +57,8 @@ int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept
|
||||||
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
|
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
|
||||||
if (p == NULL) return EINVAL;
|
if (p == NULL) return EINVAL;
|
||||||
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
||||||
if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
|
if (alignment==0 || !_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
|
||||||
void* q = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
void* q = mi_malloc_aligned(size, alignment);
|
||||||
if (q==NULL && size != 0) return ENOMEM;
|
if (q==NULL && size != 0) return ENOMEM;
|
||||||
mi_assert_internal(((uintptr_t)q % alignment) == 0);
|
mi_assert_internal(((uintptr_t)q % alignment) == 0);
|
||||||
*p = q;
|
*p = q;
|
||||||
|
@ -66,7 +66,7 @@ int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept
|
||||||
}
|
}
|
||||||
|
|
||||||
mi_decl_restrict void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept {
|
mi_decl_restrict void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept {
|
||||||
void* p = (mi_malloc_satisfies_alignment(alignment,size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
void* p = mi_malloc_aligned(size, alignment);
|
||||||
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -83,9 +83,14 @@ mi_decl_restrict void* mi_pvalloc(size_t size) mi_attr_noexcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept {
|
mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept {
|
||||||
if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL;
|
if (mi_unlikely((size&(alignment-1)) != 0)) { // C11 requires alignment>0 && integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
|
||||||
if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
|
#if MI_DEBUG > 0
|
||||||
void* p = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
_mi_error_message(EOVERFLOW, "(mi_)aligned_alloc requires the size to be an integral multiple of the alignment (size %zu, alignment %zu)", size, alignment);
|
||||||
|
#endif
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// C11 also requires alignment to be a power-of-two which is checked in mi_malloc_aligned
|
||||||
|
void* p = mi_malloc_aligned(size, alignment);
|
||||||
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,6 +158,16 @@ int main(void) {
|
||||||
CHECK_BODY("malloc-aligned5", {
|
CHECK_BODY("malloc-aligned5", {
|
||||||
void* p = mi_malloc_aligned(4097,4096); size_t usable = mi_usable_size(p); result = usable >= 4097 && usable < 10000; mi_free(p);
|
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 = 1UL << i;
|
||||||
|
p = mi_malloc_aligned(2*align, align);
|
||||||
|
ok = (p != NULL && (uintptr_t)(p) % align == 0); mi_free(p);
|
||||||
|
}
|
||||||
|
result = ok;
|
||||||
|
});
|
||||||
CHECK_BODY("malloc-aligned-at1", {
|
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);
|
void* p = mi_malloc_aligned_at(48,32,0); result = (p != NULL && ((uintptr_t)(p) + 0) % 32 == 0); mi_free(p);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue