#if !defined(MI_IN_ALLOC_C)
#error "this file should be included from 'alloc.c' (so aliases can work from alloc-override)"
#include "mimalloc.h"
#include "mimalloc/internal.h"
#include "mimalloc/prim.h"
#endif
static void mi_check_padding(const mi_page_t* page, const mi_block_t* block);
static bool mi_check_is_double_free(const mi_page_t* page, const mi_block_t* block);
static size_t mi_page_usable_size_of(const mi_page_t* page, const mi_block_t* block, bool was_guarded);
static void mi_stat_free(const mi_page_t* page, const mi_block_t* block);
static inline void mi_free_block_local(mi_page_t* page, mi_block_t* block, bool was_guarded, bool track_stats, bool check_full)
{
MI_UNUSED(was_guarded);
if mi_unlikely(mi_check_is_double_free(page, block)) return;
if (!was_guarded) { mi_check_padding(page, block); }
if (track_stats) { mi_stat_free(page, block); }
#if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN
memset(block, MI_DEBUG_FREED, mi_page_block_size(page));
#endif
if (track_stats) { mi_track_free_size(block, mi_page_usable_size_of(page, block, was_guarded)); }
mi_block_set_next(page, block, page->local_free);
page->local_free = block;
if mi_unlikely(--page->used == 0) {
if (page->retire_expire==0) { _mi_page_retire(page);
}
}
else if mi_unlikely(check_full && mi_page_is_in_full(page)) {
_mi_page_unfull(page);
}
}
static void mi_decl_noinline mi_free_try_collect_mt(mi_page_t* page, mi_block_t* mt_free) mi_attr_noexcept;
static inline void mi_free_block_mt(mi_page_t* page, mi_block_t* block, bool was_guarded) mi_attr_noexcept
{
MI_UNUSED(was_guarded);
mi_stat_free(page, block); mi_track_free_size(block, mi_page_usable_size_of(page, block, was_guarded));
#if (MI_DEBUG>0) && !MI_TRACK_ENABLED && !MI_TSAN
if (!was_guarded) {
size_t dbgsize = mi_usable_size(block);
if (dbgsize > MI_MiB) { dbgsize = MI_MiB; }
_mi_memset_aligned(block, MI_DEBUG_FREED, dbgsize);
}
#endif
mi_thread_free_t tf_new;
mi_thread_free_t tf_old = mi_atomic_load_relaxed(&page->xthread_free);
do {
mi_block_set_next(page, block, mi_tf_block(tf_old));
tf_new = mi_tf_create(block, true );
} while (!mi_atomic_cas_weak_acq_rel(&page->xthread_free, &tf_old, tf_new));
const bool is_owned_now = !mi_tf_is_owned(tf_old);
if (is_owned_now) {
mi_assert_internal(mi_page_is_abandoned(page));
mi_free_try_collect_mt(page,block);
}
}
mi_block_t* _mi_page_ptr_unalign(const mi_page_t* page, const void* p) {
mi_assert_internal(page!=NULL && p!=NULL);
const size_t diff = (uint8_t*)p - mi_page_start(page);
const size_t block_size = mi_page_block_size(page);
const size_t adjust = (_mi_is_power_of_two(block_size) ? diff & (block_size - 1) : diff % block_size);
return (mi_block_t*)((uintptr_t)p - adjust);
}
#if MI_GUARDED
static void mi_block_unguard(mi_page_t* page, mi_block_t* block, void* p); static inline bool mi_block_check_unguard(mi_page_t* page, mi_block_t* block, void* p) {
if (mi_block_ptr_is_guarded(block, p)) {
mi_block_unguard(page, block, p);
return true;
}
else {
return false;
}
}
#else
static inline bool mi_block_check_unguard(mi_page_t* page, mi_block_t* block, void* p) {
MI_UNUSED(page); MI_UNUSED(block); MI_UNUSED(p);
return false;
}
#endif
static inline mi_block_t* mi_validate_block_from_ptr( const mi_page_t* page, void* p ) {
mi_assert(_mi_page_ptr_unalign(page,p) == (mi_block_t*)p); #if MI_SECURE > 0
return _mi_page_ptr_unalign(page,p);
#else
MI_UNUSED(page);
return (mi_block_t*)p;
#endif
}
static void mi_decl_noinline mi_free_generic_local(mi_page_t* page, void* p) mi_attr_noexcept {
mi_assert_internal(p!=NULL && page != NULL);
mi_block_t* const block = (mi_page_has_interior_pointers(page) ? _mi_page_ptr_unalign(page, p) : mi_validate_block_from_ptr(page,p));
const bool was_guarded = mi_block_check_unguard(page, block, p);
mi_free_block_local(page, block, was_guarded, true , true );
}
static void mi_decl_noinline mi_free_generic_mt(mi_page_t* page, void* p) mi_attr_noexcept {
mi_assert_internal(p!=NULL && page != NULL);
mi_block_t* const block = (mi_page_has_interior_pointers(page) ? _mi_page_ptr_unalign(page, p) : mi_validate_block_from_ptr(page,p));
const bool was_guarded = mi_block_check_unguard(page, block, p);
mi_free_block_mt(page, block, was_guarded);
}
void mi_decl_noinline _mi_free_generic(mi_page_t* page, bool is_local, void* p) mi_attr_noexcept {
if (is_local) mi_free_generic_local(page,p);
else mi_free_generic_mt(page,p);
}
static inline mi_page_t* mi_validate_ptr_page(const void* p, const char* msg)
{
MI_UNUSED_RELEASE(msg);
#if MI_DEBUG
if mi_unlikely(((uintptr_t)p & (MI_INTPTR_SIZE - 1)) != 0 && !mi_option_is_enabled(mi_option_guarded_precise)) {
_mi_error_message(EINVAL, "%s: invalid (unaligned) pointer: %p\n", msg, p);
return NULL;
}
mi_page_t* page = _mi_safe_ptr_page(p);
if (p != NULL && page == NULL) {
_mi_error_message(EINVAL, "%s: invalid pointer: %p\n", msg, p);
}
return page;
#else
return _mi_ptr_page(p);
#endif
}
static mi_decl_forceinline void mi_free_ex(void* p, size_t* usable, mi_page_t* page)
{
if mi_unlikely(page==NULL) return; mi_assert_internal(p!=NULL && page!=NULL);
if (usable!=NULL) { *usable = mi_page_usable_block_size(page); }
const mi_threadid_t xtid = (_mi_prim_thread_id() ^ mi_page_xthread_id(page));
if mi_likely(xtid == 0) { mi_block_t* const block = mi_validate_block_from_ptr(page,p);
mi_free_block_local(page, block, false , true , false );
}
else if (xtid <= MI_PAGE_FLAG_MASK) { mi_free_generic_local(page, p);
}
else if ((xtid & MI_PAGE_FLAG_MASK) == 0) { mi_block_t* const block = mi_validate_block_from_ptr(page,p);
mi_free_block_mt(page,block,false );
}
else {
mi_free_generic_mt(page, p);
}
}
void mi_free(void* p) mi_attr_noexcept {
mi_page_t* const page = mi_validate_ptr_page(p,"mi_free");
mi_free_ex(p, NULL, page);
}
void mi_ufree(void* p, size_t* usable) mi_attr_noexcept {
mi_page_t* const page = mi_validate_ptr_page(p,"mi_ufree");
mi_free_ex(p, usable, page);
}
void mi_free_small(void* p) mi_attr_noexcept {
#if MI_PAGE_META_ALIGNED_FREE_SMALL
#if MI_GUARDED
#warning "MI_PAGE_META_ALIGNED_FREE_SMALL ignored as MI_GUARDED is defined"
mi_free(p);
#elif MI_ARENA_SLICE_ALIGN < MI_SMALL_PAGE_SIZE
#warning "MI_PAGE_META_ALIGNED_FREE_SMALL ignored as the MI_ARENA_SLICE_ALIGN is less than the small page size"
mi_free(p);
#else
mi_page_t* const page = (mi_page_t*)_mi_align_down_ptr(p,MI_SMALL_PAGE_SIZE);
mi_assert(page == mi_validate_ptr_page(p,"mi_free_small"));
mi_assert((void*)page == _mi_align_down_ptr(page->page_start,MI_SMALL_PAGE_SIZE));
mi_assert(page->block_size <= MI_SMALL_SIZE_MAX); mi_free_ex(p, NULL, page);
#endif
#else
mi_free(p);
#endif
}
static bool mi_abandoned_page_try_free(mi_page_t* page)
{
if (!mi_page_all_free(page)) return false;
_mi_arenas_page_unabandon(page,NULL);
_mi_arenas_page_free(page,NULL); return true;
}
static bool mi_abandoned_page_try_reabandon_to_mapped(mi_page_t* page)
{
if (mi_page_is_mostly_used(page)) return false; if (page->memid.memkind != MI_MEM_ARENA || mi_page_is_abandoned_mapped(page)) return false;
mi_assert(!mi_page_is_full(page));
return _mi_arenas_page_try_reabandon_to_mapped(page);
}
static void mi_abandoned_page_unown_from_free(mi_page_t* page, mi_block_t* expected_thread_free) {
mi_assert_internal(mi_page_is_owned(page));
mi_assert_internal(mi_page_is_abandoned(page));
mi_assert_internal(!mi_page_all_free(page));
mi_thread_free_t tf_expect = mi_tf_create(expected_thread_free, true);
mi_thread_free_t tf_new = mi_tf_create(expected_thread_free, false);
while mi_unlikely(!mi_atomic_cas_weak_acq_rel(&page->xthread_free, &tf_expect, tf_new)) {
mi_assert_internal(mi_tf_is_owned(tf_expect));
while (mi_tf_block(tf_expect) != NULL) {
_mi_page_free_collect(page,false); if (mi_abandoned_page_try_free(page)) return;
if (mi_abandoned_page_try_reabandon_to_mapped(page)) return;
tf_expect = mi_atomic_load_relaxed(&page->xthread_free);
}
mi_assert_internal(mi_tf_block(tf_expect)==NULL);
tf_new = mi_tf_create(NULL, false);
}
}
static inline bool mi_page_queue_len_is_atmost( mi_theap_t* theap, size_t block_size, long atmost) {
if (atmost < 0) return false;
mi_page_queue_t* const pq = mi_page_queue(theap,block_size);
mi_assert_internal(pq!=NULL);
return (pq->count <= (size_t)atmost);
}
static mi_decl_noinline bool mi_abandoned_page_try_reclaim(mi_page_t* page, long reclaim_on_free) mi_attr_noexcept
{
mi_assert_internal(mi_page_is_owned(page));
mi_assert_internal(mi_page_is_abandoned(page));
mi_assert_internal(!mi_page_all_free(page));
mi_assert_internal(page->block_size <= MI_SMALL_SIZE_MAX);
mi_assert_internal(reclaim_on_free >= 0);
if (!_mi_thread_is_initialized()) return false;
mi_theap_t* const theap = _mi_page_associated_theap_peek(page);
if (theap==NULL || !theap->allow_page_reclaim) return false;
long max_reclaim = 0;
if mi_likely(theap == page->theap) { max_reclaim = _mi_option_get_fast(theap->tld->is_in_threadpool ? mi_option_page_cross_thread_max_reclaim : mi_option_page_max_reclaim);
}
else if (reclaim_on_free == 1 && !theap->tld->is_in_threadpool && !mi_page_is_mostly_used(page) && _mi_arena_memid_is_suitable(page->memid, _mi_theap_heap(theap)->exclusive_arena)) { max_reclaim = _mi_option_get_fast(mi_option_page_cross_thread_max_reclaim);
}
if (max_reclaim >= 0 && !mi_page_queue_len_is_atmost(theap, page->block_size, max_reclaim)) {
return false;
}
_mi_arenas_page_unabandon(page, theap);
_mi_theap_page_reclaim(theap, page);
mi_theap_stat_counter_increase(theap, pages_reclaim_on_free, 1);
return true;
}
static void mi_decl_noinline mi_free_try_collect_mt(mi_page_t* page, mi_block_t* mt_free) mi_attr_noexcept
{
mi_assert_internal(mi_page_is_owned(page));
mi_assert_internal(mi_page_is_abandoned(page));
mi_assert_internal(mt_free != NULL);
if (page->block_size <= MI_SMALL_SIZE_MAX) {
mi_assert_internal(page->reserved>=16); _mi_page_free_collect_partly(page, mt_free);
}
else {
_mi_page_free_collect(page,false );
mt_free = NULL; }
const long reclaim_on_free = _mi_option_get_fast(mi_option_page_reclaim_on_free);
#if MI_DEBUG > 1
if (mi_page_is_singleton(page)) { mi_assert_internal(mi_page_all_free(page)); }
if (mi_page_is_full(page)) { mi_assert(mi_page_is_mostly_used(page)); }
#endif
if (mi_abandoned_page_try_free(page)) return;
if (page->block_size <= MI_SMALL_SIZE_MAX && reclaim_on_free >= 0) { if (mi_abandoned_page_try_reclaim(page, reclaim_on_free)) return;
}
if (mi_abandoned_page_try_reabandon_to_mapped(page)) return;
mi_abandoned_page_unown_from_free(page, mt_free);
}
static size_t mi_decl_noinline mi_page_usable_aligned_size_of(const mi_page_t* page, const void* p) mi_attr_noexcept {
const mi_block_t* block = _mi_page_ptr_unalign(page, p);
const bool is_guarded = mi_block_ptr_is_guarded(block,p);
const size_t size = mi_page_usable_size_of(page, block, is_guarded);
const ptrdiff_t adjust = (uint8_t*)p - (uint8_t*)block;
mi_assert_internal(adjust >= 0 && (size_t)adjust <= size);
const size_t aligned_size = (size - adjust);
return aligned_size;
}
static inline size_t _mi_usable_size(const void* p, const mi_page_t* page) mi_attr_noexcept {
if mi_unlikely(page==NULL) return 0;
if mi_likely(!mi_page_has_interior_pointers(page)) {
const mi_block_t* block = (const mi_block_t*)p;
return mi_page_usable_size_of(page, block, false );
}
else {
return mi_page_usable_aligned_size_of(page, p);
}
}
mi_decl_nodiscard size_t mi_usable_size(const void* p) mi_attr_noexcept {
const mi_page_t* const page = mi_validate_ptr_page(p,"mi_usable_size");
return _mi_usable_size(p,page);
}
void mi_free_size(void* p, size_t size) mi_attr_noexcept {
MI_UNUSED_RELEASE(size);
#if MI_DEBUG
const mi_page_t* const page = mi_validate_ptr_page(p,"mi_free_size");
const size_t available = _mi_usable_size(p,page);
mi_assert(p == NULL || size <= available || available == 0 );
#endif
mi_free(p);
}
void mi_free_size_aligned(void* p, size_t size, size_t alignment) mi_attr_noexcept {
MI_UNUSED_RELEASE(alignment);
mi_assert(((uintptr_t)p % alignment) == 0);
mi_free_size(p,size);
}
void mi_free_aligned(void* p, size_t alignment) mi_attr_noexcept {
MI_UNUSED_RELEASE(alignment);
mi_assert(((uintptr_t)p % alignment) == 0);
mi_free(p);
}
#if (MI_ENCODE_FREELIST && (MI_SECURE>=4 || MI_DEBUG!=0))
static bool mi_list_contains(const mi_page_t* page, const mi_block_t* list, const mi_block_t* elem) {
while (list != NULL) {
if (elem==list) return true;
list = mi_block_next(page, list);
}
return false;
}
static mi_decl_noinline bool mi_check_is_double_freex(const mi_page_t* page, const mi_block_t* block) {
if (mi_list_contains(page, page->free, block) ||
mi_list_contains(page, page->local_free, block) ||
mi_list_contains(page, mi_page_thread_free(page), block))
{
_mi_error_message(EAGAIN, "double free detected of block %p with size %zu\n", block, mi_page_block_size(page));
return true;
}
return false;
}
#define mi_track_page(page,access) { size_t psize; void* pstart = _mi_page_start(_mi_page_segment(page),page,&psize); mi_track_mem_##access( pstart, psize); }
static inline bool mi_check_is_double_free(const mi_page_t* page, const mi_block_t* block) {
bool is_double_free = false;
mi_block_t* n = mi_block_nextx(page, block, page->keys); if (((uintptr_t)n & (MI_INTPTR_SIZE-1))==0 && (n==NULL || mi_is_in_same_page(block, n))) {
is_double_free = mi_check_is_double_freex(page, block);
}
return is_double_free;
}
#else
static inline bool mi_check_is_double_free(const mi_page_t* page, const mi_block_t* block) {
MI_UNUSED(page);
MI_UNUSED(block);
return false;
}
#endif
#if MI_PADDING
static bool mi_page_decode_padding(const mi_page_t* page, const mi_block_t* block, size_t* delta, size_t* bsize) {
*bsize = mi_page_usable_block_size(page);
const mi_padding_t* const padding = (mi_padding_t*)((uint8_t*)block + *bsize);
mi_track_mem_defined(padding,sizeof(mi_padding_t));
*delta = padding->delta;
uint32_t canary = padding->canary;
uintptr_t keys[2];
keys[0] = page->keys[0];
keys[1] = page->keys[1];
bool ok = (mi_ptr_encode_canary(page,block,keys) == canary && *delta <= *bsize);
mi_track_mem_noaccess(padding,sizeof(mi_padding_t));
return ok;
}
static size_t mi_page_usable_size_of(const mi_page_t* page, const mi_block_t* block, bool is_guarded) {
if (is_guarded) {
const size_t bsize = mi_page_block_size(page);
return (bsize - _mi_os_page_size());
}
else {
size_t bsize;
size_t delta;
bool ok = mi_page_decode_padding(page, block, &delta, &bsize);
mi_assert_internal(ok); mi_assert_internal(delta <= bsize);
return (ok ? bsize - delta : 0);
}
}
void _mi_padding_shrink(const mi_page_t* page, const mi_block_t* block, const size_t min_size) {
size_t bsize;
size_t delta;
bool ok = mi_page_decode_padding(page, block, &delta, &bsize);
mi_assert_internal(ok);
if (!ok || (bsize - delta) >= min_size) return; mi_assert_internal(bsize >= min_size);
if (bsize < min_size) return; size_t new_delta = (bsize - min_size);
mi_assert_internal(new_delta < bsize);
mi_padding_t* padding = (mi_padding_t*)((uint8_t*)block + bsize);
mi_track_mem_defined(padding,sizeof(mi_padding_t));
padding->delta = (uint32_t)new_delta;
mi_track_mem_noaccess(padding,sizeof(mi_padding_t));
}
#else
static size_t mi_page_usable_size_of(const mi_page_t* page, const mi_block_t* block, bool is_guarded) {
MI_UNUSED(is_guarded); MI_UNUSED(block);
return mi_page_usable_block_size(page);
}
void _mi_padding_shrink(const mi_page_t* page, const mi_block_t* block, const size_t min_size) {
MI_UNUSED(page); MI_UNUSED(block); MI_UNUSED(min_size);
}
#endif
#if MI_PADDING && MI_PADDING_CHECK
static bool mi_verify_padding(const mi_page_t* page, const mi_block_t* block, size_t* size, size_t* wrong) {
size_t bsize;
size_t delta;
bool ok = mi_page_decode_padding(page, block, &delta, &bsize);
*size = *wrong = bsize;
if (!ok) return false;
mi_assert_internal(bsize >= delta);
*size = bsize - delta;
if (!mi_page_is_huge(page)) {
uint8_t* fill = (uint8_t*)block + bsize - delta;
const size_t maxpad = (delta > MI_MAX_ALIGN_SIZE ? MI_MAX_ALIGN_SIZE : delta); mi_track_mem_defined(fill, maxpad);
for (size_t i = 0; i < maxpad; i++) {
if (fill[i] != MI_DEBUG_PADDING) {
*wrong = bsize - delta + i;
ok = false;
break;
}
}
mi_track_mem_noaccess(fill, maxpad);
}
return ok;
}
static void mi_check_padding(const mi_page_t* page, const mi_block_t* block) {
size_t size;
size_t wrong;
if (!mi_verify_padding(page,block,&size,&wrong)) {
_mi_error_message(EFAULT, "buffer overflow in theap block %p of size %zu: write after %zu bytes\n", block, size, wrong );
}
}
#else
static void mi_check_padding(const mi_page_t* page, const mi_block_t* block) {
MI_UNUSED(page);
MI_UNUSED(block);
}
#endif
#if (MI_STAT>0)
static void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
MI_UNUSED(block);
mi_theap_t* const theap = _mi_theap_default();
if (!mi_theap_is_initialized(theap)) return;
const size_t bsize = mi_page_usable_block_size(page);
if (bsize <= MI_LARGE_MAX_OBJ_SIZE) {
mi_theap_stat_decrease(theap, malloc_normal, bsize);
#if (MI_STAT > 1)
mi_theap_stat_decrease(theap, malloc_bins[_mi_bin(bsize)], 1);
#endif
}
else {
const size_t bpsize = mi_page_block_size(page); mi_theap_stat_decrease(theap, malloc_huge, bpsize);
}
}
#else
void mi_stat_free(const mi_page_t* page, const mi_block_t* block) {
MI_UNUSED(page); MI_UNUSED(block);
}
#endif
#if MI_GUARDED
static void mi_block_unguard(mi_page_t* page, mi_block_t* block, void* p) {
MI_UNUSED(p);
mi_assert_internal(mi_block_ptr_is_guarded(block, p));
mi_assert_internal(mi_page_has_interior_pointers(page));
mi_assert_internal((uint8_t*)p - (uint8_t*)block >= (ptrdiff_t)sizeof(mi_block_t));
mi_assert_internal(block->next == MI_BLOCK_TAG_GUARDED);
const size_t bsize = mi_page_block_size(page);
const size_t psize = _mi_os_page_size();
mi_assert_internal(bsize > psize);
mi_assert_internal(!page->memid.is_pinned);
void* gpage = (uint8_t*)block + bsize - psize;
mi_assert_internal(_mi_is_aligned(gpage, psize));
_mi_os_unprotect(gpage, psize);
}
#endif