Path: blob/main/system/lib/mimalloc/src/alloc-aligned.c
6175 views
/* ----------------------------------------------------------------------------1Copyright (c) 2018-2021, Microsoft Research, Daan Leijen2This is free software; you can redistribute it and/or modify it under the3terms of the MIT license. A copy of the license can be found in the file4"LICENSE" at the root of this distribution.5-----------------------------------------------------------------------------*/67#include "mimalloc.h"8#include "mimalloc/internal.h"9#include "mimalloc/prim.h" // mi_prim_get_default_heap1011#include <string.h> // memset1213// ------------------------------------------------------14// Aligned Allocation15// ------------------------------------------------------1617static bool mi_malloc_is_naturally_aligned( size_t size, size_t alignment ) {18// objects up to `MI_MAX_ALIGN_GUARANTEE` are allocated aligned to their size (see `segment.c:_mi_segment_page_start`).19mi_assert_internal(_mi_is_power_of_two(alignment) && (alignment > 0));20if (alignment > size) return false;21if (alignment <= MI_MAX_ALIGN_SIZE) return true;22const size_t bsize = mi_good_size(size);23return (bsize <= MI_MAX_ALIGN_GUARANTEE && (bsize & (alignment-1)) == 0);24}2526// Fallback aligned allocation that over-allocates -- split out for better codegen27static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_overalloc(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept28{29mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE));30mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));3132void* p;33size_t oversize;34if mi_unlikely(alignment > MI_BLOCK_ALIGNMENT_MAX) {35// use OS allocation for very large alignment and allocate inside a huge page (dedicated segment with 1 page)36// This can support alignments >= MI_SEGMENT_SIZE by ensuring the object can be aligned at a point in the37// first (and single) page such that the segment info is `MI_SEGMENT_SIZE` bytes before it (so it can be found by aligning the pointer down)38if mi_unlikely(offset != 0) {39// todo: cannot support offset alignment for very large alignments yet40#if MI_DEBUG > 041_mi_error_message(EOVERFLOW, "aligned allocation with a very large alignment cannot be used with an alignment offset (size %zu, alignment %zu, offset %zu)\n", size, alignment, offset);42#endif43return NULL;44}45oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size);46p = _mi_heap_malloc_zero_ex(heap, oversize, false, alignment); // the page block size should be large enough to align in the single huge page block47// zero afterwards as only the area from the aligned_p may be committed!48if (p == NULL) return NULL;49}50else {51// otherwise over-allocate52oversize = size + alignment - 1;53p = _mi_heap_malloc_zero(heap, oversize, zero);54if (p == NULL) return NULL;55}5657// .. and align within the allocation58const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)`59const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask;60const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset);61mi_assert_internal(adjust < alignment);62void* aligned_p = (void*)((uintptr_t)p + adjust);63if (aligned_p != p) {64mi_page_t* page = _mi_ptr_page(p);65mi_page_set_has_aligned(page, true);66_mi_padding_shrink(page, (mi_block_t*)p, adjust + size);67}68// todo: expand padding if overallocated ?6970mi_assert_internal(mi_page_usable_block_size(_mi_ptr_page(p)) >= adjust + size);71mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_page(aligned_p), aligned_p));72mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);73mi_assert_internal(mi_usable_size(aligned_p)>=size);74mi_assert_internal(mi_usable_size(p) == mi_usable_size(aligned_p)+adjust);7576// now zero the block if needed77if (alignment > MI_BLOCK_ALIGNMENT_MAX) {78// for the tracker, on huge aligned allocations only the memory from the start of the large block is defined79mi_track_mem_undefined(aligned_p, size);80if (zero) {81_mi_memzero_aligned(aligned_p, mi_usable_size(aligned_p));82}83}8485if (p != aligned_p) {86mi_track_align(p,aligned_p,adjust,mi_usable_size(aligned_p));87}88return aligned_p;89}9091// Generic primitive aligned allocation -- split out for better codegen92static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_generic(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept93{94mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));95// we don't allocate more than MI_MAX_ALLOC_SIZE (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)96if mi_unlikely(size > (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE)) {97#if MI_DEBUG > 098_mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)\n", size, alignment);99#endif100return NULL;101}102103// use regular allocation if it is guaranteed to fit the alignment constraints.104// this is important to try as the fast path in `mi_heap_malloc_zero_aligned` only works when there exist105// a page with the right block size, and if we always use the over-alloc fallback that would never happen.106if (offset == 0 && mi_malloc_is_naturally_aligned(size,alignment)) {107void* p = _mi_heap_malloc_zero(heap, size, zero);108mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);109const bool is_aligned_or_null = (((uintptr_t)p) & (alignment-1))==0;110if mi_likely(is_aligned_or_null) {111return p;112}113else {114// this should never happen if the `mi_malloc_is_naturally_aligned` check is correct..115mi_assert(false);116mi_free(p);117}118}119120// fall back to over-allocation121return mi_heap_malloc_zero_aligned_at_overalloc(heap,size,alignment,offset,zero);122}123124// Primitive aligned allocation125static 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_noexcept126{127// note: we don't require `size > offset`, we just guarantee that the address at offset is aligned regardless of the allocated size.128if mi_unlikely(alignment == 0 || !_mi_is_power_of_two(alignment)) { // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)129#if MI_DEBUG > 0130_mi_error_message(EOVERFLOW, "aligned allocation requires the alignment to be a power-of-two (size %zu, alignment %zu)\n", size, alignment);131#endif132return NULL;133}134135// try first if there happens to be a small block available with just the right alignment136if mi_likely(size <= MI_SMALL_SIZE_MAX && alignment <= size) {137const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`138const size_t padsize = size + MI_PADDING_SIZE;139mi_page_t* page = _mi_heap_get_free_small_page(heap, padsize);140if mi_likely(page->free != NULL) {141const bool is_aligned = (((uintptr_t)page->free + offset) & align_mask)==0;142if mi_likely(is_aligned)143{144#if MI_STAT>1145mi_heap_stat_increase(heap, malloc, size);146#endif147void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen148mi_assert_internal(p != NULL);149mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);150mi_track_malloc(p,size,zero);151return p;152}153}154}155156// fallback to generic aligned allocation157return mi_heap_malloc_zero_aligned_at_generic(heap, size, alignment, offset, zero);158}159160161// ------------------------------------------------------162// Optimized mi_heap_malloc_aligned / mi_malloc_aligned163// ------------------------------------------------------164165mi_decl_nodiscard 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 {166return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false);167}168169mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {170return mi_heap_malloc_aligned_at(heap, size, alignment, 0);171}172173// ensure a definition is emitted174#if defined(__cplusplus)175void* _mi_extern_heap_malloc_aligned = (void*)&mi_heap_malloc_aligned;176#endif177178// ------------------------------------------------------179// Aligned Allocation180// ------------------------------------------------------181182mi_decl_nodiscard 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 {183return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true);184}185186mi_decl_nodiscard mi_decl_restrict void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {187return mi_heap_zalloc_aligned_at(heap, size, alignment, 0);188}189190mi_decl_nodiscard mi_decl_restrict void* mi_heap_calloc_aligned_at(mi_heap_t* heap, size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {191size_t total;192if (mi_count_size_overflow(count, size, &total)) return NULL;193return mi_heap_zalloc_aligned_at(heap, total, alignment, offset);194}195196mi_decl_nodiscard mi_decl_restrict void* mi_heap_calloc_aligned(mi_heap_t* heap, size_t count, size_t size, size_t alignment) mi_attr_noexcept {197return mi_heap_calloc_aligned_at(heap,count,size,alignment,0);198}199200mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {201return mi_heap_malloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);202}203204mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {205return mi_heap_malloc_aligned(mi_prim_get_default_heap(), size, alignment);206}207208mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {209return mi_heap_zalloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);210}211212mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {213return mi_heap_zalloc_aligned(mi_prim_get_default_heap(), size, alignment);214}215216mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {217return mi_heap_calloc_aligned_at(mi_prim_get_default_heap(), count, size, alignment, offset);218}219220mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned(size_t count, size_t size, size_t alignment) mi_attr_noexcept {221return mi_heap_calloc_aligned(mi_prim_get_default_heap(), count, size, alignment);222}223224225// ------------------------------------------------------226// Aligned re-allocation227// ------------------------------------------------------228229static 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 {230mi_assert(alignment > 0);231if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);232if (p == NULL) return mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero);233size_t size = mi_usable_size(p);234if (newsize <= size && newsize >= (size - (size / 2))235&& (((uintptr_t)p + offset) % alignment) == 0) {236return p; // reallocation still fits, is aligned and not more than 50% waste237}238else {239// note: we don't zero allocate upfront so we only zero initialize the expanded part240void* newp = mi_heap_malloc_aligned_at(heap,newsize,alignment,offset);241if (newp != NULL) {242if (zero && newsize > size) {243// also set last word in the previous allocation to zero to ensure any padding is zero-initialized244size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0);245_mi_memzero((uint8_t*)newp + start, newsize - start);246}247_mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize));248mi_free(p); // only free if successful249}250return newp;251}252}253254static void* mi_heap_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero) mi_attr_noexcept {255mi_assert(alignment > 0);256if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);257size_t offset = ((uintptr_t)p % alignment); // use offset of previous allocation (p can be NULL)258return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero);259}260261mi_decl_nodiscard void* mi_heap_realloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {262return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false);263}264265mi_decl_nodiscard void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {266return mi_heap_realloc_zero_aligned(heap,p,newsize,alignment,false);267}268269mi_decl_nodiscard void* mi_heap_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {270return mi_heap_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true);271}272273mi_decl_nodiscard void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {274return mi_heap_realloc_zero_aligned(heap, p, newsize, alignment, true);275}276277mi_decl_nodiscard void* mi_heap_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {278size_t total;279if (mi_count_size_overflow(newcount, size, &total)) return NULL;280return mi_heap_rezalloc_aligned_at(heap, p, total, alignment, offset);281}282283mi_decl_nodiscard void* mi_heap_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {284size_t total;285if (mi_count_size_overflow(newcount, size, &total)) return NULL;286return mi_heap_rezalloc_aligned(heap, p, total, alignment);287}288289mi_decl_nodiscard void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {290return mi_heap_realloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);291}292293mi_decl_nodiscard void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {294return mi_heap_realloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);295}296297mi_decl_nodiscard void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {298return mi_heap_rezalloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);299}300301mi_decl_nodiscard void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {302return mi_heap_rezalloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);303}304305mi_decl_nodiscard void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {306return mi_heap_recalloc_aligned_at(mi_prim_get_default_heap(), p, newcount, size, alignment, offset);307}308309mi_decl_nodiscard void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {310return mi_heap_recalloc_aligned(mi_prim_get_default_heap(), p, newcount, size, alignment);311}312313314