Path: blob/main/system/lib/mimalloc/src/arena.c
6178 views
/* ----------------------------------------------------------------------------1Copyright (c) 2019-2023, 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/* ----------------------------------------------------------------------------8"Arenas" are fixed area's of OS memory from which we can allocate9large blocks (>= MI_ARENA_MIN_BLOCK_SIZE, 4MiB).10In contrast to the rest of mimalloc, the arenas are shared between11threads and need to be accessed using atomic operations.1213Arenas are used to for huge OS page (1GiB) reservations or for reserving14OS memory upfront which can be improve performance or is sometimes needed15on embedded devices. We can also employ this with WASI or `sbrk` systems16to reserve large arenas upfront and be able to reuse the memory more effectively.1718The arena allocation needs to be thread safe and we use an atomic bitmap to allocate.19-----------------------------------------------------------------------------*/20#include "mimalloc.h"21#include "mimalloc/internal.h"22#include "mimalloc/atomic.h"2324#include <string.h> // memset25#include <errno.h> // ENOMEM2627#include "bitmap.h" // atomic bitmap2829/* -----------------------------------------------------------30Arena allocation31----------------------------------------------------------- */3233// Block info: bit 0 contains the `in_use` bit, the upper bits the34// size in count of arena blocks.35typedef uintptr_t mi_block_info_t;36#define MI_ARENA_BLOCK_SIZE (MI_SEGMENT_SIZE) // 64MiB (must be at least MI_SEGMENT_ALIGN)37#define MI_ARENA_MIN_OBJ_SIZE (MI_ARENA_BLOCK_SIZE/2) // 32MiB38#define MI_MAX_ARENAS (112) // not more than 126 (since we use 7 bits in the memid and an arena index + 1)3940// A memory arena descriptor41typedef struct mi_arena_s {42mi_arena_id_t id; // arena id; 0 for non-specific43mi_memid_t memid; // memid of the memory area44_Atomic(uint8_t*) start; // the start of the memory area45size_t block_count; // size of the area in arena blocks (of `MI_ARENA_BLOCK_SIZE`)46size_t field_count; // number of bitmap fields (where `field_count * MI_BITMAP_FIELD_BITS >= block_count`)47size_t meta_size; // size of the arena structure itself (including its bitmaps)48mi_memid_t meta_memid; // memid of the arena structure itself (OS or static allocation)49int numa_node; // associated NUMA node50bool exclusive; // only allow allocations if specifically for this arena51bool is_large; // memory area consists of large- or huge OS pages (always committed)52_Atomic(size_t) search_idx; // optimization to start the search for free blocks53_Atomic(mi_msecs_t) purge_expire; // expiration time when blocks should be decommitted from `blocks_decommit`.54mi_bitmap_field_t* blocks_dirty; // are the blocks potentially non-zero?55mi_bitmap_field_t* blocks_committed; // are the blocks committed? (can be NULL for memory that cannot be decommitted)56mi_bitmap_field_t* blocks_purge; // blocks that can be (reset) decommitted. (can be NULL for memory that cannot be (reset) decommitted)57mi_bitmap_field_t* blocks_abandoned; // blocks that start with an abandoned segment. (This crosses API's but it is convenient to have here)58mi_bitmap_field_t blocks_inuse[1]; // in-place bitmap of in-use blocks (of size `field_count`)59// do not add further fields here as the dirty, committed, purged, and abandoned bitmaps follow the inuse bitmap fields.60} mi_arena_t;616263// The available arenas64static mi_decl_cache_align _Atomic(mi_arena_t*) mi_arenas[MI_MAX_ARENAS];65static mi_decl_cache_align _Atomic(size_t) mi_arena_count; // = 0666768//static bool mi_manage_os_memory_ex2(void* start, size_t size, bool is_large, int numa_node, bool exclusive, mi_memid_t memid, mi_arena_id_t* arena_id) mi_attr_noexcept;6970/* -----------------------------------------------------------71Arena id's72id = arena_index + 173----------------------------------------------------------- */7475static size_t mi_arena_id_index(mi_arena_id_t id) {76return (size_t)(id <= 0 ? MI_MAX_ARENAS : id - 1);77}7879static mi_arena_id_t mi_arena_id_create(size_t arena_index) {80mi_assert_internal(arena_index < MI_MAX_ARENAS);81return (int)arena_index + 1;82}8384mi_arena_id_t _mi_arena_id_none(void) {85return 0;86}8788static bool mi_arena_id_is_suitable(mi_arena_id_t arena_id, bool arena_is_exclusive, mi_arena_id_t req_arena_id) {89return ((!arena_is_exclusive && req_arena_id == _mi_arena_id_none()) ||90(arena_id == req_arena_id));91}9293bool _mi_arena_memid_is_suitable(mi_memid_t memid, mi_arena_id_t request_arena_id) {94if (memid.memkind == MI_MEM_ARENA) {95return mi_arena_id_is_suitable(memid.mem.arena.id, memid.mem.arena.is_exclusive, request_arena_id);96}97else {98return mi_arena_id_is_suitable(_mi_arena_id_none(), false, request_arena_id);99}100}101102bool _mi_arena_memid_is_os_allocated(mi_memid_t memid) {103return (memid.memkind == MI_MEM_OS);104}105106/* -----------------------------------------------------------107Arena allocations get a (currently) 16-bit memory id where the108lower 8 bits are the arena id, and the upper bits the block index.109----------------------------------------------------------- */110111static size_t mi_block_count_of_size(size_t size) {112return _mi_divide_up(size, MI_ARENA_BLOCK_SIZE);113}114115static size_t mi_arena_block_size(size_t bcount) {116return (bcount * MI_ARENA_BLOCK_SIZE);117}118119static size_t mi_arena_size(mi_arena_t* arena) {120return mi_arena_block_size(arena->block_count);121}122123static mi_memid_t mi_memid_create_arena(mi_arena_id_t id, bool is_exclusive, mi_bitmap_index_t bitmap_index) {124mi_memid_t memid = _mi_memid_create(MI_MEM_ARENA);125memid.mem.arena.id = id;126memid.mem.arena.block_index = bitmap_index;127memid.mem.arena.is_exclusive = is_exclusive;128return memid;129}130131static bool mi_arena_memid_indices(mi_memid_t memid, size_t* arena_index, mi_bitmap_index_t* bitmap_index) {132mi_assert_internal(memid.memkind == MI_MEM_ARENA);133*arena_index = mi_arena_id_index(memid.mem.arena.id);134*bitmap_index = memid.mem.arena.block_index;135return memid.mem.arena.is_exclusive;136}137138139140/* -----------------------------------------------------------141Special static area for mimalloc internal structures142to avoid OS calls (for example, for the arena metadata)143----------------------------------------------------------- */144145#define MI_ARENA_STATIC_MAX (MI_INTPTR_SIZE*MI_KiB) // 8 KiB on 64-bit146147static mi_decl_cache_align uint8_t mi_arena_static[MI_ARENA_STATIC_MAX]; // must be cache aligned, see issue #895148static mi_decl_cache_align _Atomic(size_t) mi_arena_static_top;149150static void* mi_arena_static_zalloc(size_t size, size_t alignment, mi_memid_t* memid) {151*memid = _mi_memid_none();152if (size == 0 || size > MI_ARENA_STATIC_MAX) return NULL;153const size_t toplow = mi_atomic_load_relaxed(&mi_arena_static_top);154if ((toplow + size) > MI_ARENA_STATIC_MAX) return NULL;155156// try to claim space157if (alignment < MI_MAX_ALIGN_SIZE) { alignment = MI_MAX_ALIGN_SIZE; }158const size_t oversize = size + alignment - 1;159if (toplow + oversize > MI_ARENA_STATIC_MAX) return NULL;160const size_t oldtop = mi_atomic_add_acq_rel(&mi_arena_static_top, oversize);161size_t top = oldtop + oversize;162if (top > MI_ARENA_STATIC_MAX) {163// try to roll back, ok if this fails164mi_atomic_cas_strong_acq_rel(&mi_arena_static_top, &top, oldtop);165return NULL;166}167168// success169*memid = _mi_memid_create(MI_MEM_STATIC);170memid->initially_zero = true;171const size_t start = _mi_align_up(oldtop, alignment);172uint8_t* const p = &mi_arena_static[start];173_mi_memzero_aligned(p, size);174return p;175}176177static void* mi_arena_meta_zalloc(size_t size, mi_memid_t* memid, mi_stats_t* stats) {178*memid = _mi_memid_none();179180// try static181void* p = mi_arena_static_zalloc(size, MI_MAX_ALIGN_SIZE, memid);182if (p != NULL) return p;183184// or fall back to the OS185p = _mi_os_alloc(size, memid, stats);186if (p == NULL) return NULL;187188// zero the OS memory if needed189if (!memid->initially_zero) {190_mi_memzero_aligned(p, size);191memid->initially_zero = true;192}193return p;194}195196static void mi_arena_meta_free(void* p, mi_memid_t memid, size_t size, mi_stats_t* stats) {197if (mi_memkind_is_os(memid.memkind)) {198_mi_os_free(p, size, memid, stats);199}200else {201mi_assert(memid.memkind == MI_MEM_STATIC);202}203}204205static void* mi_arena_block_start(mi_arena_t* arena, mi_bitmap_index_t bindex) {206return (arena->start + mi_arena_block_size(mi_bitmap_index_bit(bindex)));207}208209210/* -----------------------------------------------------------211Thread safe allocation in an arena212----------------------------------------------------------- */213214// claim the `blocks_inuse` bits215static bool mi_arena_try_claim(mi_arena_t* arena, size_t blocks, mi_bitmap_index_t* bitmap_idx, mi_stats_t* stats)216{217size_t idx = 0; // mi_atomic_load_relaxed(&arena->search_idx); // start from last search; ok to be relaxed as the exact start does not matter218if (_mi_bitmap_try_find_from_claim_across(arena->blocks_inuse, arena->field_count, idx, blocks, bitmap_idx, stats)) {219mi_atomic_store_relaxed(&arena->search_idx, mi_bitmap_index_field(*bitmap_idx)); // start search from found location next time around220return true;221};222return false;223}224225226/* -----------------------------------------------------------227Arena Allocation228----------------------------------------------------------- */229230static mi_decl_noinline void* mi_arena_try_alloc_at(mi_arena_t* arena, size_t arena_index, size_t needed_bcount,231bool commit, mi_memid_t* memid, mi_os_tld_t* tld)232{233MI_UNUSED(arena_index);234mi_assert_internal(mi_arena_id_index(arena->id) == arena_index);235236mi_bitmap_index_t bitmap_index;237if (!mi_arena_try_claim(arena, needed_bcount, &bitmap_index, tld->stats)) return NULL;238239// claimed it!240void* p = mi_arena_block_start(arena, bitmap_index);241*memid = mi_memid_create_arena(arena->id, arena->exclusive, bitmap_index);242memid->is_pinned = arena->memid.is_pinned;243244// none of the claimed blocks should be scheduled for a decommit245if (arena->blocks_purge != NULL) {246// this is thread safe as a potential purge only decommits parts that are not yet claimed as used (in `blocks_inuse`).247_mi_bitmap_unclaim_across(arena->blocks_purge, arena->field_count, needed_bcount, bitmap_index);248}249250// set the dirty bits (todo: no need for an atomic op here?)251if (arena->memid.initially_zero && arena->blocks_dirty != NULL) {252memid->initially_zero = _mi_bitmap_claim_across(arena->blocks_dirty, arena->field_count, needed_bcount, bitmap_index, NULL);253}254255// set commit state256if (arena->blocks_committed == NULL) {257// always committed258memid->initially_committed = true;259}260else if (commit) {261// commit requested, but the range may not be committed as a whole: ensure it is committed now262memid->initially_committed = true;263bool any_uncommitted;264_mi_bitmap_claim_across(arena->blocks_committed, arena->field_count, needed_bcount, bitmap_index, &any_uncommitted);265if (any_uncommitted) {266bool commit_zero = false;267if (!_mi_os_commit(p, mi_arena_block_size(needed_bcount), &commit_zero, tld->stats)) {268memid->initially_committed = false;269}270else {271if (commit_zero) { memid->initially_zero = true; }272}273}274}275else {276// no need to commit, but check if already fully committed277memid->initially_committed = _mi_bitmap_is_claimed_across(arena->blocks_committed, arena->field_count, needed_bcount, bitmap_index);278}279280return p;281}282283// allocate in a speficic arena284static void* mi_arena_try_alloc_at_id(mi_arena_id_t arena_id, bool match_numa_node, int numa_node, size_t size, size_t alignment,285bool commit, bool allow_large, mi_arena_id_t req_arena_id, mi_memid_t* memid, mi_os_tld_t* tld )286{287MI_UNUSED_RELEASE(alignment);288mi_assert_internal(alignment <= MI_SEGMENT_ALIGN);289const size_t bcount = mi_block_count_of_size(size);290const size_t arena_index = mi_arena_id_index(arena_id);291mi_assert_internal(arena_index < mi_atomic_load_relaxed(&mi_arena_count));292mi_assert_internal(size <= mi_arena_block_size(bcount));293294// Check arena suitability295mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[arena_index]);296if (arena == NULL) return NULL;297if (!allow_large && arena->is_large) return NULL;298if (!mi_arena_id_is_suitable(arena->id, arena->exclusive, req_arena_id)) return NULL;299if (req_arena_id == _mi_arena_id_none()) { // in not specific, check numa affinity300const bool numa_suitable = (numa_node < 0 || arena->numa_node < 0 || arena->numa_node == numa_node);301if (match_numa_node) { if (!numa_suitable) return NULL; }302else { if (numa_suitable) return NULL; }303}304305// try to allocate306void* p = mi_arena_try_alloc_at(arena, arena_index, bcount, commit, memid, tld);307mi_assert_internal(p == NULL || _mi_is_aligned(p, alignment));308return p;309}310311312// allocate from an arena with fallback to the OS313static mi_decl_noinline void* mi_arena_try_alloc(int numa_node, size_t size, size_t alignment,314bool commit, bool allow_large,315mi_arena_id_t req_arena_id, mi_memid_t* memid, mi_os_tld_t* tld )316{317MI_UNUSED(alignment);318mi_assert_internal(alignment <= MI_SEGMENT_ALIGN);319const size_t max_arena = mi_atomic_load_relaxed(&mi_arena_count);320if mi_likely(max_arena == 0) return NULL;321322if (req_arena_id != _mi_arena_id_none()) {323// try a specific arena if requested324if (mi_arena_id_index(req_arena_id) < max_arena) {325void* p = mi_arena_try_alloc_at_id(req_arena_id, true, numa_node, size, alignment, commit, allow_large, req_arena_id, memid, tld);326if (p != NULL) return p;327}328}329else {330// try numa affine allocation331for (size_t i = 0; i < max_arena; i++) {332void* p = mi_arena_try_alloc_at_id(mi_arena_id_create(i), true, numa_node, size, alignment, commit, allow_large, req_arena_id, memid, tld);333if (p != NULL) return p;334}335336// try from another numa node instead..337if (numa_node >= 0) { // if numa_node was < 0 (no specific affinity requested), all arena's have been tried already338for (size_t i = 0; i < max_arena; i++) {339void* p = mi_arena_try_alloc_at_id(mi_arena_id_create(i), false /* only proceed if not numa local */, numa_node, size, alignment, commit, allow_large, req_arena_id, memid, tld);340if (p != NULL) return p;341}342}343}344return NULL;345}346347// try to reserve a fresh arena space348static bool mi_arena_reserve(size_t req_size, bool allow_large, mi_arena_id_t req_arena_id, mi_arena_id_t *arena_id)349{350if (_mi_preloading()) return false; // use OS only while pre loading351if (req_arena_id != _mi_arena_id_none()) return false;352353const size_t arena_count = mi_atomic_load_acquire(&mi_arena_count);354if (arena_count > (MI_MAX_ARENAS - 4)) return false;355356size_t arena_reserve = mi_option_get_size(mi_option_arena_reserve);357if (arena_reserve == 0) return false;358359if (!_mi_os_has_virtual_reserve()) {360arena_reserve = arena_reserve/4; // be conservative if virtual reserve is not supported (for WASM for example)361}362arena_reserve = _mi_align_up(arena_reserve, MI_ARENA_BLOCK_SIZE);363if (arena_count >= 8 && arena_count <= 128) {364arena_reserve = ((size_t)1<<(arena_count/8)) * arena_reserve; // scale up the arena sizes exponentially365}366if (arena_reserve < req_size) return false; // should be able to at least handle the current allocation size367368// commit eagerly?369bool arena_commit = false;370if (mi_option_get(mi_option_arena_eager_commit) == 2) { arena_commit = _mi_os_has_overcommit(); }371else if (mi_option_get(mi_option_arena_eager_commit) == 1) { arena_commit = true; }372373return (mi_reserve_os_memory_ex(arena_reserve, arena_commit, allow_large, false /* exclusive? */, arena_id) == 0);374}375376377void* _mi_arena_alloc_aligned(size_t size, size_t alignment, size_t align_offset, bool commit, bool allow_large,378mi_arena_id_t req_arena_id, mi_memid_t* memid, mi_os_tld_t* tld)379{380mi_assert_internal(memid != NULL && tld != NULL);381mi_assert_internal(size > 0);382*memid = _mi_memid_none();383384const int numa_node = _mi_os_numa_node(tld); // current numa node385386// try to allocate in an arena if the alignment is small enough and the object is not too small (as for heap meta data)387if (!mi_option_is_enabled(mi_option_disallow_arena_alloc) || req_arena_id != _mi_arena_id_none()) { // is arena allocation allowed?388if (size >= MI_ARENA_MIN_OBJ_SIZE && alignment <= MI_SEGMENT_ALIGN && align_offset == 0) {389void* p = mi_arena_try_alloc(numa_node, size, alignment, commit, allow_large, req_arena_id, memid, tld);390if (p != NULL) return p;391392// otherwise, try to first eagerly reserve a new arena393if (req_arena_id == _mi_arena_id_none()) {394mi_arena_id_t arena_id = 0;395if (mi_arena_reserve(size, allow_large, req_arena_id, &arena_id)) {396// and try allocate in there397mi_assert_internal(req_arena_id == _mi_arena_id_none());398p = mi_arena_try_alloc_at_id(arena_id, true, numa_node, size, alignment, commit, allow_large, req_arena_id, memid, tld);399if (p != NULL) return p;400}401}402}403}404405// if we cannot use OS allocation, return NULL406if (mi_option_is_enabled(mi_option_disallow_os_alloc) || req_arena_id != _mi_arena_id_none()) {407errno = ENOMEM;408return NULL;409}410411// finally, fall back to the OS412if (align_offset > 0) {413return _mi_os_alloc_aligned_at_offset(size, alignment, align_offset, commit, allow_large, memid, tld->stats);414}415else {416return _mi_os_alloc_aligned(size, alignment, commit, allow_large, memid, tld->stats);417}418}419420void* _mi_arena_alloc(size_t size, bool commit, bool allow_large, mi_arena_id_t req_arena_id, mi_memid_t* memid, mi_os_tld_t* tld)421{422return _mi_arena_alloc_aligned(size, MI_ARENA_BLOCK_SIZE, 0, commit, allow_large, req_arena_id, memid, tld);423}424425426void* mi_arena_area(mi_arena_id_t arena_id, size_t* size) {427if (size != NULL) *size = 0;428size_t arena_index = mi_arena_id_index(arena_id);429if (arena_index >= MI_MAX_ARENAS) return NULL;430mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[arena_index]);431if (arena == NULL) return NULL;432if (size != NULL) { *size = mi_arena_block_size(arena->block_count); }433return arena->start;434}435436437/* -----------------------------------------------------------438Arena purge439----------------------------------------------------------- */440441static long mi_arena_purge_delay(void) {442// <0 = no purging allowed, 0=immediate purging, >0=milli-second delay443return (mi_option_get(mi_option_purge_delay) * mi_option_get(mi_option_arena_purge_mult));444}445446// reset or decommit in an arena and update the committed/decommit bitmaps447// assumes we own the area (i.e. blocks_in_use is claimed by us)448static void mi_arena_purge(mi_arena_t* arena, size_t bitmap_idx, size_t blocks, mi_stats_t* stats) {449mi_assert_internal(arena->blocks_committed != NULL);450mi_assert_internal(arena->blocks_purge != NULL);451mi_assert_internal(!arena->memid.is_pinned);452const size_t size = mi_arena_block_size(blocks);453void* const p = mi_arena_block_start(arena, bitmap_idx);454bool needs_recommit;455if (_mi_bitmap_is_claimed_across(arena->blocks_committed, arena->field_count, blocks, bitmap_idx)) {456// all blocks are committed, we can purge freely457needs_recommit = _mi_os_purge(p, size, stats);458}459else {460// some blocks are not committed -- this can happen when a partially committed block is freed461// in `_mi_arena_free` and it is conservatively marked as uncommitted but still scheduled for a purge462// we need to ensure we do not try to reset (as that may be invalid for uncommitted memory),463// and also undo the decommit stats (as it was already adjusted)464mi_assert_internal(mi_option_is_enabled(mi_option_purge_decommits));465needs_recommit = _mi_os_purge_ex(p, size, false /* allow reset? */, stats);466if (needs_recommit) { _mi_stat_increase(&_mi_stats_main.committed, size); }467}468469// clear the purged blocks470_mi_bitmap_unclaim_across(arena->blocks_purge, arena->field_count, blocks, bitmap_idx);471// update committed bitmap472if (needs_recommit) {473_mi_bitmap_unclaim_across(arena->blocks_committed, arena->field_count, blocks, bitmap_idx);474}475}476477// Schedule a purge. This is usually delayed to avoid repeated decommit/commit calls.478// Note: assumes we (still) own the area as we may purge immediately479static void mi_arena_schedule_purge(mi_arena_t* arena, size_t bitmap_idx, size_t blocks, mi_stats_t* stats) {480mi_assert_internal(arena->blocks_purge != NULL);481const long delay = mi_arena_purge_delay();482if (delay < 0) return; // is purging allowed at all?483484if (_mi_preloading() || delay == 0) {485// decommit directly486mi_arena_purge(arena, bitmap_idx, blocks, stats);487}488else {489// schedule decommit490mi_msecs_t expire = mi_atomic_loadi64_relaxed(&arena->purge_expire);491if (expire != 0) {492mi_atomic_addi64_acq_rel(&arena->purge_expire, (mi_msecs_t)(delay/10)); // add smallish extra delay493}494else {495mi_atomic_storei64_release(&arena->purge_expire, _mi_clock_now() + delay);496}497_mi_bitmap_claim_across(arena->blocks_purge, arena->field_count, blocks, bitmap_idx, NULL);498}499}500501// purge a range of blocks502// return true if the full range was purged.503// assumes we own the area (i.e. blocks_in_use is claimed by us)504static bool mi_arena_purge_range(mi_arena_t* arena, size_t idx, size_t startidx, size_t bitlen, size_t purge, mi_stats_t* stats) {505const size_t endidx = startidx + bitlen;506size_t bitidx = startidx;507bool all_purged = false;508while (bitidx < endidx) {509// count consequetive ones in the purge mask510size_t count = 0;511while (bitidx + count < endidx && (purge & ((size_t)1 << (bitidx + count))) != 0) {512count++;513}514if (count > 0) {515// found range to be purged516const mi_bitmap_index_t range_idx = mi_bitmap_index_create(idx, bitidx);517mi_arena_purge(arena, range_idx, count, stats);518if (count == bitlen) {519all_purged = true;520}521}522bitidx += (count+1); // +1 to skip the zero bit (or end)523}524return all_purged;525}526527// returns true if anything was purged528static bool mi_arena_try_purge(mi_arena_t* arena, mi_msecs_t now, bool force, mi_stats_t* stats)529{530if (arena->memid.is_pinned || arena->blocks_purge == NULL) return false;531mi_msecs_t expire = mi_atomic_loadi64_relaxed(&arena->purge_expire);532if (expire == 0) return false;533if (!force && expire > now) return false;534535// reset expire (if not already set concurrently)536mi_atomic_casi64_strong_acq_rel(&arena->purge_expire, &expire, (mi_msecs_t)0);537538// potential purges scheduled, walk through the bitmap539bool any_purged = false;540bool full_purge = true;541for (size_t i = 0; i < arena->field_count; i++) {542size_t purge = mi_atomic_load_relaxed(&arena->blocks_purge[i]);543if (purge != 0) {544size_t bitidx = 0;545while (bitidx < MI_BITMAP_FIELD_BITS) {546// find consequetive range of ones in the purge mask547size_t bitlen = 0;548while (bitidx + bitlen < MI_BITMAP_FIELD_BITS && (purge & ((size_t)1 << (bitidx + bitlen))) != 0) {549bitlen++;550}551// try to claim the longest range of corresponding in_use bits552const mi_bitmap_index_t bitmap_index = mi_bitmap_index_create(i, bitidx);553while( bitlen > 0 ) {554if (_mi_bitmap_try_claim(arena->blocks_inuse, arena->field_count, bitlen, bitmap_index)) {555break;556}557bitlen--;558}559// actual claimed bits at `in_use`560if (bitlen > 0) {561// read purge again now that we have the in_use bits562purge = mi_atomic_load_acquire(&arena->blocks_purge[i]);563if (!mi_arena_purge_range(arena, i, bitidx, bitlen, purge, stats)) {564full_purge = false;565}566any_purged = true;567// release the claimed `in_use` bits again568_mi_bitmap_unclaim(arena->blocks_inuse, arena->field_count, bitlen, bitmap_index);569}570bitidx += (bitlen+1); // +1 to skip the zero (or end)571} // while bitidx572} // purge != 0573}574// if not fully purged, make sure to purge again in the future575if (!full_purge) {576const long delay = mi_arena_purge_delay();577mi_msecs_t expected = 0;578mi_atomic_casi64_strong_acq_rel(&arena->purge_expire,&expected,_mi_clock_now() + delay);579}580return any_purged;581}582583static void mi_arenas_try_purge( bool force, bool visit_all, mi_stats_t* stats ) {584if (_mi_preloading() || mi_arena_purge_delay() <= 0) return; // nothing will be scheduled585586const size_t max_arena = mi_atomic_load_acquire(&mi_arena_count);587if (max_arena == 0) return;588589// allow only one thread to purge at a time590static mi_atomic_guard_t purge_guard;591mi_atomic_guard(&purge_guard)592{593mi_msecs_t now = _mi_clock_now();594size_t max_purge_count = (visit_all ? max_arena : 1);595for (size_t i = 0; i < max_arena; i++) {596mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[i]);597if (arena != NULL) {598if (mi_arena_try_purge(arena, now, force, stats)) {599if (max_purge_count <= 1) break;600max_purge_count--;601}602}603}604}605}606607608/* -----------------------------------------------------------609Arena free610----------------------------------------------------------- */611612void _mi_arena_free(void* p, size_t size, size_t committed_size, mi_memid_t memid, mi_stats_t* stats) {613mi_assert_internal(size > 0 && stats != NULL);614mi_assert_internal(committed_size <= size);615if (p==NULL) return;616if (size==0) return;617const bool all_committed = (committed_size == size);618619if (mi_memkind_is_os(memid.memkind)) {620// was a direct OS allocation, pass through621if (!all_committed && committed_size > 0) {622// if partially committed, adjust the committed stats (as `_mi_os_free` will increase decommit by the full size)623_mi_stat_decrease(&_mi_stats_main.committed, committed_size);624}625_mi_os_free(p, size, memid, stats);626}627else if (memid.memkind == MI_MEM_ARENA) {628// allocated in an arena629size_t arena_idx;630size_t bitmap_idx;631mi_arena_memid_indices(memid, &arena_idx, &bitmap_idx);632mi_assert_internal(arena_idx < MI_MAX_ARENAS);633mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t,&mi_arenas[arena_idx]);634mi_assert_internal(arena != NULL);635const size_t blocks = mi_block_count_of_size(size);636637// checks638if (arena == NULL) {639_mi_error_message(EINVAL, "trying to free from an invalid arena: %p, size %zu, memid: 0x%zx\n", p, size, memid);640return;641}642mi_assert_internal(arena->field_count > mi_bitmap_index_field(bitmap_idx));643if (arena->field_count <= mi_bitmap_index_field(bitmap_idx)) {644_mi_error_message(EINVAL, "trying to free from an invalid arena block: %p, size %zu, memid: 0x%zx\n", p, size, memid);645return;646}647648// need to set all memory to undefined as some parts may still be marked as no_access (like padding etc.)649mi_track_mem_undefined(p,size);650651// potentially decommit652if (arena->memid.is_pinned || arena->blocks_committed == NULL) {653mi_assert_internal(all_committed);654}655else {656mi_assert_internal(arena->blocks_committed != NULL);657mi_assert_internal(arena->blocks_purge != NULL);658659if (!all_committed) {660// mark the entire range as no longer committed (so we recommit the full range when re-using)661_mi_bitmap_unclaim_across(arena->blocks_committed, arena->field_count, blocks, bitmap_idx);662mi_track_mem_noaccess(p,size);663if (committed_size > 0) {664// if partially committed, adjust the committed stats (is it will be recommitted when re-using)665// in the delayed purge, we now need to not count a decommit if the range is not marked as committed.666_mi_stat_decrease(&_mi_stats_main.committed, committed_size);667}668// note: if not all committed, it may be that the purge will reset/decommit the entire range669// that contains already decommitted parts. Since purge consistently uses reset or decommit that670// works (as we should never reset decommitted parts).671}672// (delay) purge the entire range673mi_arena_schedule_purge(arena, bitmap_idx, blocks, stats);674}675676// and make it available to others again677bool all_inuse = _mi_bitmap_unclaim_across(arena->blocks_inuse, arena->field_count, blocks, bitmap_idx);678if (!all_inuse) {679_mi_error_message(EAGAIN, "trying to free an already freed arena block: %p, size %zu\n", p, size);680return;681};682}683else {684// arena was none, external, or static; nothing to do685mi_assert_internal(memid.memkind < MI_MEM_OS);686}687688// purge expired decommits689mi_arenas_try_purge(false, false, stats);690}691692// destroy owned arenas; this is unsafe and should only be done using `mi_option_destroy_on_exit`693// for dynamic libraries that are unloaded and need to release all their allocated memory.694static void mi_arenas_unsafe_destroy(void) {695const size_t max_arena = mi_atomic_load_relaxed(&mi_arena_count);696size_t new_max_arena = 0;697for (size_t i = 0; i < max_arena; i++) {698mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[i]);699if (arena != NULL) {700if (arena->start != NULL && mi_memkind_is_os(arena->memid.memkind)) {701mi_atomic_store_ptr_release(mi_arena_t, &mi_arenas[i], NULL);702_mi_os_free(arena->start, mi_arena_size(arena), arena->memid, &_mi_stats_main);703}704else {705new_max_arena = i;706}707mi_arena_meta_free(arena, arena->meta_memid, arena->meta_size, &_mi_stats_main);708}709}710711// try to lower the max arena.712size_t expected = max_arena;713mi_atomic_cas_strong_acq_rel(&mi_arena_count, &expected, new_max_arena);714}715716// Purge the arenas; if `force_purge` is true, amenable parts are purged even if not yet expired717void _mi_arenas_collect(bool force_purge, mi_stats_t* stats) {718mi_arenas_try_purge(force_purge, force_purge /* visit all? */, stats);719}720721// destroy owned arenas; this is unsafe and should only be done using `mi_option_destroy_on_exit`722// for dynamic libraries that are unloaded and need to release all their allocated memory.723void _mi_arena_unsafe_destroy_all(mi_stats_t* stats) {724mi_arenas_unsafe_destroy();725_mi_arenas_collect(true /* force purge */, stats); // purge non-owned arenas726}727728// Is a pointer inside any of our arenas?729bool _mi_arena_contains(const void* p) {730const size_t max_arena = mi_atomic_load_relaxed(&mi_arena_count);731for (size_t i = 0; i < max_arena; i++) {732mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[i]);733if (arena != NULL && arena->start <= (const uint8_t*)p && arena->start + mi_arena_block_size(arena->block_count) > (const uint8_t*)p) {734return true;735}736}737return false;738}739740/* -----------------------------------------------------------741Abandoned blocks/segments.742This is used to atomically abandon/reclaim segments743(and crosses the arena API but it is convenient to have here).744Abandoned segments still have live blocks; they get reclaimed745when a thread frees a block in it, or when a thread needs a fresh746segment; these threads scan the abandoned segments through747the arena bitmaps.748----------------------------------------------------------- */749750// Maintain a count of all abandoned segments751static mi_decl_cache_align _Atomic(size_t)abandoned_count;752753size_t _mi_arena_segment_abandoned_count(void) {754return mi_atomic_load_relaxed(&abandoned_count);755}756757// reclaim a specific abandoned segment; `true` on success.758// sets the thread_id.759bool _mi_arena_segment_clear_abandoned(mi_segment_t* segment )760{761if (segment->memid.memkind != MI_MEM_ARENA) {762// not in an arena, consider it un-abandoned now.763// but we need to still claim it atomically -- we use the thread_id for that.764size_t expected = 0;765if (mi_atomic_cas_strong_acq_rel(&segment->thread_id, &expected, _mi_thread_id())) {766mi_atomic_decrement_relaxed(&abandoned_count);767return true;768}769else {770return false;771}772}773// arena segment: use the blocks_abandoned bitmap.774size_t arena_idx;775size_t bitmap_idx;776mi_arena_memid_indices(segment->memid, &arena_idx, &bitmap_idx);777mi_assert_internal(arena_idx < MI_MAX_ARENAS);778mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[arena_idx]);779mi_assert_internal(arena != NULL);780bool was_marked = _mi_bitmap_unclaim(arena->blocks_abandoned, arena->field_count, 1, bitmap_idx);781if (was_marked) {782mi_assert_internal(mi_atomic_load_relaxed(&segment->thread_id) == 0);783mi_atomic_decrement_relaxed(&abandoned_count);784mi_atomic_store_release(&segment->thread_id, _mi_thread_id());785}786// mi_assert_internal(was_marked);787mi_assert_internal(!was_marked || _mi_bitmap_is_claimed(arena->blocks_inuse, arena->field_count, 1, bitmap_idx));788//mi_assert_internal(arena->blocks_committed == NULL || _mi_bitmap_is_claimed(arena->blocks_committed, arena->field_count, 1, bitmap_idx));789return was_marked;790}791792// mark a specific segment as abandoned793// clears the thread_id.794void _mi_arena_segment_mark_abandoned(mi_segment_t* segment)795{796mi_atomic_store_release(&segment->thread_id, 0);797mi_assert_internal(segment->used == segment->abandoned);798if (segment->memid.memkind != MI_MEM_ARENA) {799// not in an arena; count it as abandoned and return800mi_atomic_increment_relaxed(&abandoned_count);801return;802}803size_t arena_idx;804size_t bitmap_idx;805mi_arena_memid_indices(segment->memid, &arena_idx, &bitmap_idx);806mi_assert_internal(arena_idx < MI_MAX_ARENAS);807mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[arena_idx]);808mi_assert_internal(arena != NULL);809const bool was_unmarked = _mi_bitmap_claim(arena->blocks_abandoned, arena->field_count, 1, bitmap_idx, NULL);810if (was_unmarked) { mi_atomic_increment_relaxed(&abandoned_count); }811mi_assert_internal(was_unmarked);812mi_assert_internal(_mi_bitmap_is_claimed(arena->blocks_inuse, arena->field_count, 1, bitmap_idx));813}814815// start a cursor at a randomized arena816void _mi_arena_field_cursor_init(mi_heap_t* heap, mi_arena_field_cursor_t* current) {817const size_t max_arena = mi_atomic_load_relaxed(&mi_arena_count);818current->start = (max_arena == 0 ? 0 : (mi_arena_id_t)( _mi_heap_random_next(heap) % max_arena));819current->count = 0;820current->bitmap_idx = 0;821}822823// reclaim abandoned segments824// this does not set the thread id (so it appears as still abandoned)825mi_segment_t* _mi_arena_segment_clear_abandoned_next(mi_arena_field_cursor_t* previous )826{827const int max_arena = (int)mi_atomic_load_relaxed(&mi_arena_count);828if (max_arena <= 0 || mi_atomic_load_relaxed(&abandoned_count) == 0) return NULL;829830int count = previous->count;831size_t field_idx = mi_bitmap_index_field(previous->bitmap_idx);832size_t bit_idx = mi_bitmap_index_bit_in_field(previous->bitmap_idx) + 1;833// visit arena's (from previous)834for (; count < max_arena; count++, field_idx = 0, bit_idx = 0) {835mi_arena_id_t arena_idx = previous->start + count;836if (arena_idx >= max_arena) { arena_idx = arena_idx % max_arena; } // wrap around837mi_arena_t* arena = mi_atomic_load_ptr_acquire(mi_arena_t, &mi_arenas[arena_idx]);838if (arena != NULL) {839// visit the abandoned fields (starting at previous_idx)840for ( ; field_idx < arena->field_count; field_idx++, bit_idx = 0) {841size_t field = mi_atomic_load_relaxed(&arena->blocks_abandoned[field_idx]);842if mi_unlikely(field != 0) { // skip zero fields quickly843// visit each set bit in the field (todo: maybe use `ctz` here?)844for ( ; bit_idx < MI_BITMAP_FIELD_BITS; bit_idx++) {845// pre-check if the bit is set846size_t mask = ((size_t)1 << bit_idx);847if mi_unlikely((field & mask) == mask) {848mi_bitmap_index_t bitmap_idx = mi_bitmap_index_create(field_idx, bit_idx);849// try to reclaim it atomically850if (_mi_bitmap_unclaim(arena->blocks_abandoned, arena->field_count, 1, bitmap_idx)) {851mi_atomic_decrement_relaxed(&abandoned_count);852previous->bitmap_idx = bitmap_idx;853previous->count = count;854mi_assert_internal(_mi_bitmap_is_claimed(arena->blocks_inuse, arena->field_count, 1, bitmap_idx));855mi_segment_t* segment = (mi_segment_t*)mi_arena_block_start(arena, bitmap_idx);856mi_assert_internal(mi_atomic_load_relaxed(&segment->thread_id) == 0);857//mi_assert_internal(arena->blocks_committed == NULL || _mi_bitmap_is_claimed(arena->blocks_committed, arena->field_count, 1, bitmap_idx));858return segment;859}860}861}862}863}864}865}866// no more found867previous->bitmap_idx = 0;868previous->count = 0;869return NULL;870}871872873/* -----------------------------------------------------------874Add an arena.875----------------------------------------------------------- */876877static bool mi_arena_add(mi_arena_t* arena, mi_arena_id_t* arena_id, mi_stats_t* stats) {878mi_assert_internal(arena != NULL);879mi_assert_internal((uintptr_t)mi_atomic_load_ptr_relaxed(uint8_t,&arena->start) % MI_SEGMENT_ALIGN == 0);880mi_assert_internal(arena->block_count > 0);881if (arena_id != NULL) { *arena_id = -1; }882883size_t i = mi_atomic_increment_acq_rel(&mi_arena_count);884if (i >= MI_MAX_ARENAS) {885mi_atomic_decrement_acq_rel(&mi_arena_count);886return false;887}888_mi_stat_counter_increase(&stats->arena_count,1);889arena->id = mi_arena_id_create(i);890mi_atomic_store_ptr_release(mi_arena_t,&mi_arenas[i], arena);891if (arena_id != NULL) { *arena_id = arena->id; }892return true;893}894895static bool mi_manage_os_memory_ex2(void* start, size_t size, bool is_large, int numa_node, bool exclusive, mi_memid_t memid, mi_arena_id_t* arena_id) mi_attr_noexcept896{897if (arena_id != NULL) *arena_id = _mi_arena_id_none();898if (size < MI_ARENA_BLOCK_SIZE) return false;899900if (is_large) {901mi_assert_internal(memid.initially_committed && memid.is_pinned);902}903904const size_t bcount = size / MI_ARENA_BLOCK_SIZE;905const size_t fields = _mi_divide_up(bcount, MI_BITMAP_FIELD_BITS);906const size_t bitmaps = (memid.is_pinned ? 3 : 5);907const size_t asize = sizeof(mi_arena_t) + (bitmaps*fields*sizeof(mi_bitmap_field_t));908mi_memid_t meta_memid;909mi_arena_t* arena = (mi_arena_t*)mi_arena_meta_zalloc(asize, &meta_memid, &_mi_stats_main); // TODO: can we avoid allocating from the OS?910if (arena == NULL) return false;911912// already zero'd due to zalloc913// _mi_memzero(arena, asize);914arena->id = _mi_arena_id_none();915arena->memid = memid;916arena->exclusive = exclusive;917arena->meta_size = asize;918arena->meta_memid = meta_memid;919arena->block_count = bcount;920arena->field_count = fields;921arena->start = (uint8_t*)start;922arena->numa_node = numa_node; // TODO: or get the current numa node if -1? (now it allows anyone to allocate on -1)923arena->is_large = is_large;924arena->purge_expire = 0;925arena->search_idx = 0;926// consequetive bitmaps927arena->blocks_dirty = &arena->blocks_inuse[fields]; // just after inuse bitmap928arena->blocks_abandoned = &arena->blocks_inuse[2 * fields]; // just after dirty bitmap929arena->blocks_committed = (arena->memid.is_pinned ? NULL : &arena->blocks_inuse[3*fields]); // just after abandoned bitmap930arena->blocks_purge = (arena->memid.is_pinned ? NULL : &arena->blocks_inuse[4*fields]); // just after committed bitmap931// initialize committed bitmap?932if (arena->blocks_committed != NULL && arena->memid.initially_committed) {933memset((void*)arena->blocks_committed, 0xFF, fields*sizeof(mi_bitmap_field_t)); // cast to void* to avoid atomic warning934}935936// and claim leftover blocks if needed (so we never allocate there)937ptrdiff_t post = (fields * MI_BITMAP_FIELD_BITS) - bcount;938mi_assert_internal(post >= 0);939if (post > 0) {940// don't use leftover bits at the end941mi_bitmap_index_t postidx = mi_bitmap_index_create(fields - 1, MI_BITMAP_FIELD_BITS - post);942_mi_bitmap_claim(arena->blocks_inuse, fields, post, postidx, NULL);943}944return mi_arena_add(arena, arena_id, &_mi_stats_main);945946}947948bool mi_manage_os_memory_ex(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node, bool exclusive, mi_arena_id_t* arena_id) mi_attr_noexcept {949mi_memid_t memid = _mi_memid_create(MI_MEM_EXTERNAL);950memid.initially_committed = is_committed;951memid.initially_zero = is_zero;952memid.is_pinned = is_large;953return mi_manage_os_memory_ex2(start,size,is_large,numa_node,exclusive,memid, arena_id);954}955956// Reserve a range of regular OS memory957int mi_reserve_os_memory_ex(size_t size, bool commit, bool allow_large, bool exclusive, mi_arena_id_t* arena_id) mi_attr_noexcept {958if (arena_id != NULL) *arena_id = _mi_arena_id_none();959size = _mi_align_up(size, MI_ARENA_BLOCK_SIZE); // at least one block960mi_memid_t memid;961void* start = _mi_os_alloc_aligned(size, MI_SEGMENT_ALIGN, commit, allow_large, &memid, &_mi_stats_main);962if (start == NULL) return ENOMEM;963const bool is_large = memid.is_pinned; // todo: use separate is_large field?964if (!mi_manage_os_memory_ex2(start, size, is_large, -1 /* numa node */, exclusive, memid, arena_id)) {965_mi_os_free_ex(start, size, commit, memid, &_mi_stats_main);966_mi_verbose_message("failed to reserve %zu KiB memory\n", _mi_divide_up(size, 1024));967return ENOMEM;968}969_mi_verbose_message("reserved %zu KiB memory%s\n", _mi_divide_up(size, 1024), is_large ? " (in large os pages)" : "");970return 0;971}972973974// Manage a range of regular OS memory975bool mi_manage_os_memory(void* start, size_t size, bool is_committed, bool is_large, bool is_zero, int numa_node) mi_attr_noexcept {976return mi_manage_os_memory_ex(start, size, is_committed, is_large, is_zero, numa_node, false /* exclusive? */, NULL);977}978979// Reserve a range of regular OS memory980int mi_reserve_os_memory(size_t size, bool commit, bool allow_large) mi_attr_noexcept {981return mi_reserve_os_memory_ex(size, commit, allow_large, false, NULL);982}983984985/* -----------------------------------------------------------986Debugging987----------------------------------------------------------- */988989static size_t mi_debug_show_bitmap(const char* prefix, const char* header, size_t block_count, mi_bitmap_field_t* fields, size_t field_count ) {990_mi_verbose_message("%s%s:\n", prefix, header);991size_t bcount = 0;992size_t inuse_count = 0;993for (size_t i = 0; i < field_count; i++) {994char buf[MI_BITMAP_FIELD_BITS + 1];995uintptr_t field = mi_atomic_load_relaxed(&fields[i]);996for (size_t bit = 0; bit < MI_BITMAP_FIELD_BITS; bit++, bcount++) {997if (bcount < block_count) {998bool inuse = ((((uintptr_t)1 << bit) & field) != 0);999if (inuse) inuse_count++;1000buf[bit] = (inuse ? 'x' : '.');1001}1002else {1003buf[bit] = ' ';1004}1005}1006buf[MI_BITMAP_FIELD_BITS] = 0;1007_mi_verbose_message("%s %s\n", prefix, buf);1008}1009_mi_verbose_message("%s total ('x'): %zu\n", prefix, inuse_count);1010return inuse_count;1011}10121013void mi_debug_show_arenas(bool show_inuse, bool show_abandoned, bool show_purge) mi_attr_noexcept {1014size_t max_arenas = mi_atomic_load_relaxed(&mi_arena_count);1015size_t inuse_total = 0;1016size_t abandoned_total = 0;1017size_t purge_total = 0;1018for (size_t i = 0; i < max_arenas; i++) {1019mi_arena_t* arena = mi_atomic_load_ptr_relaxed(mi_arena_t, &mi_arenas[i]);1020if (arena == NULL) break;1021_mi_verbose_message("arena %zu: %zu blocks of size %zuMiB (in %zu fields) %s\n", i, arena->block_count, MI_ARENA_BLOCK_SIZE / MI_MiB, arena->field_count, (arena->memid.is_pinned ? ", pinned" : ""));1022if (show_inuse) {1023inuse_total += mi_debug_show_bitmap(" ", "inuse blocks", arena->block_count, arena->blocks_inuse, arena->field_count);1024}1025if (arena->blocks_committed != NULL) {1026mi_debug_show_bitmap(" ", "committed blocks", arena->block_count, arena->blocks_committed, arena->field_count);1027}1028if (show_abandoned) {1029abandoned_total += mi_debug_show_bitmap(" ", "abandoned blocks", arena->block_count, arena->blocks_abandoned, arena->field_count);1030}1031if (show_purge && arena->blocks_purge != NULL) {1032purge_total += mi_debug_show_bitmap(" ", "purgeable blocks", arena->block_count, arena->blocks_purge, arena->field_count);1033}1034}1035if (show_inuse) _mi_verbose_message("total inuse blocks : %zu\n", inuse_total);1036if (show_abandoned) _mi_verbose_message("total abandoned blocks: %zu\n", abandoned_total);1037if (show_purge) _mi_verbose_message("total purgeable blocks: %zu\n", purge_total);1038}103910401041/* -----------------------------------------------------------1042Reserve a huge page arena.1043----------------------------------------------------------- */1044// reserve at a specific numa node1045int mi_reserve_huge_os_pages_at_ex(size_t pages, int numa_node, size_t timeout_msecs, bool exclusive, mi_arena_id_t* arena_id) mi_attr_noexcept {1046if (arena_id != NULL) *arena_id = -1;1047if (pages==0) return 0;1048if (numa_node < -1) numa_node = -1;1049if (numa_node >= 0) numa_node = numa_node % _mi_os_numa_node_count();1050size_t hsize = 0;1051size_t pages_reserved = 0;1052mi_memid_t memid;1053void* p = _mi_os_alloc_huge_os_pages(pages, numa_node, timeout_msecs, &pages_reserved, &hsize, &memid);1054if (p==NULL || pages_reserved==0) {1055_mi_warning_message("failed to reserve %zu GiB huge pages\n", pages);1056return ENOMEM;1057}1058_mi_verbose_message("numa node %i: reserved %zu GiB huge pages (of the %zu GiB requested)\n", numa_node, pages_reserved, pages);10591060if (!mi_manage_os_memory_ex2(p, hsize, true, numa_node, exclusive, memid, arena_id)) {1061_mi_os_free(p, hsize, memid, &_mi_stats_main);1062return ENOMEM;1063}1064return 0;1065}10661067int mi_reserve_huge_os_pages_at(size_t pages, int numa_node, size_t timeout_msecs) mi_attr_noexcept {1068return mi_reserve_huge_os_pages_at_ex(pages, numa_node, timeout_msecs, false, NULL);1069}10701071// reserve huge pages evenly among the given number of numa nodes (or use the available ones as detected)1072int mi_reserve_huge_os_pages_interleave(size_t pages, size_t numa_nodes, size_t timeout_msecs) mi_attr_noexcept {1073if (pages == 0) return 0;10741075// pages per numa node1076size_t numa_count = (numa_nodes > 0 ? numa_nodes : _mi_os_numa_node_count());1077if (numa_count <= 0) numa_count = 1;1078const size_t pages_per = pages / numa_count;1079const size_t pages_mod = pages % numa_count;1080const size_t timeout_per = (timeout_msecs==0 ? 0 : (timeout_msecs / numa_count) + 50);10811082// reserve evenly among numa nodes1083for (size_t numa_node = 0; numa_node < numa_count && pages > 0; numa_node++) {1084size_t node_pages = pages_per; // can be 01085if (numa_node < pages_mod) node_pages++;1086int err = mi_reserve_huge_os_pages_at(node_pages, (int)numa_node, timeout_per);1087if (err) return err;1088if (pages < node_pages) {1089pages = 0;1090}1091else {1092pages -= node_pages;1093}1094}10951096return 0;1097}10981099int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserved) mi_attr_noexcept {1100MI_UNUSED(max_secs);1101_mi_warning_message("mi_reserve_huge_os_pages is deprecated: use mi_reserve_huge_os_pages_interleave/at instead\n");1102if (pages_reserved != NULL) *pages_reserved = 0;1103int err = mi_reserve_huge_os_pages_interleave(pages, 0, (size_t)(max_secs * 1000.0));1104if (err==0 && pages_reserved!=NULL) *pages_reserved = pages;1105return err;1106}1107110811091110