Skip to content

Commit 75f4a06

Browse files
committed
Expose mi_theap_heap function
Allows accessing the mi_heap_t* that owns the mi_theap_t*. The internal function has been renamed to _mi_theap_heap, and all existing usages have been updated to use that. This means the internal usages are still eligible for inlining.
1 parent 6cede17 commit 75f4a06

14 files changed

Lines changed: 40 additions & 29 deletions

File tree

‎contrib/rust/sys/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ unsafe extern "C" {
438438

439439
// thread-local heaps
440440
pub fn mi_heap_theap(heap: *mut mi_heap_t) -> *mut mi_theap_t;
441+
pub fn mi_theap_heap(heap: *const mi_theap_t) -> *mut mi_heap_t;
441442
pub fn mi_theap_set_default(theap: *mut mi_theap_t) -> *mut mi_theap_t;
442443
pub safe fn mi_theap_get_default() -> *mut mi_theap_t;
443444
pub fn mi_theap_collect(theap: *mut mi_theap_t, force: bool);

‎doc/mimalloc-doc.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,11 @@ typedef struct mi_theap_s mi_theap_t;
14101410
/// the theap at each allocation call.
14111411
mi_theap_t* mi_heap_theap(mi_heap_t* heap);
14121412

1413+
/// @brief Return the owning heap for the given thread-local heap.
1414+
/// @param theap the thread-local heap
1415+
/// @return The heap that owns the `theap`
1416+
mi_theap_t* mi_theap_heap(const mi_heap_t* heap);
1417+
14131418
/// Release outstanding resources in a specific theap.
14141419
void mi_theap_collect(mi_theap_t* theap, bool force);
14151420

‎include/mimalloc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ struct mi_theap_s;
392392
typedef struct mi_theap_s mi_theap_t;
393393

394394
mi_decl_export mi_theap_t* mi_heap_theap(mi_heap_t* heap);
395+
mi_decl_export mi_heap_t* mi_theap_heap(const mi_theap_t* theap);
395396
mi_decl_export mi_theap_t* mi_theap_set_default(mi_theap_t* theap);
396397
mi_decl_export mi_theap_t* mi_theap_get_default(void);
397398
mi_decl_export void mi_theap_collect(mi_theap_t* theap, bool force) mi_attr_noexcept;

‎include/mimalloc/heap/theap.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static inline mi_heap_t* mi_theap_heap_peek(const mi_theap_t* theap) {
3636
return mi_atomic_load_ptr_relaxed(mi_heap_t,&theap->heap);
3737
}
3838

39-
static inline mi_heap_t* mi_theap_heap(const mi_theap_t* theap) {
39+
static inline mi_heap_t* _mi_theap_heap(const mi_theap_t* theap) {
4040
mi_heap_t* const heap = mi_theap_heap_peek(theap);
4141
mi_assert_internal(heap!=NULL);
4242
return heap;
@@ -48,7 +48,7 @@ static inline bool mi_theap_is_initialized(const mi_theap_t* theap) {
4848

4949
static inline mi_subproc_t* mi_theap_subproc(const mi_theap_t* theap) {
5050
mi_subproc_t* const subproc = mi_atomic_load_ptr_relaxed(mi_subproc_t,&theap->subproc);
51-
mi_assert_internal(!mi_theap_is_initialized(theap) || mi_theap_heap(theap)->subproc == subproc);
51+
mi_assert_internal(!mi_theap_is_initialized(theap) || _mi_theap_heap(theap)->subproc == subproc);
5252
return subproc;
5353
}
5454

‎src/alloc/alloc-generic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static mi_theap_t* mi_malloc_generic_admin(mi_theap_t* theap)
343343

344344
// check if the profiler is enabled (unless this theap is permanently excluded, see `mi_theap_profile_never`)
345345
if (!theap->profile_never) {
346-
mi_heap_t* const heap = mi_theap_heap(theap);
346+
mi_heap_t* const heap = _mi_theap_heap(theap);
347347
mi_profiler_t* prof = mi_atomic_load_ptr_acquire(mi_profiler_t, &heap->profiler);
348348
const bool prof_enabled = (prof!=NULL && mi_profiler_is_enabled(prof));
349349
if (prof_enabled && theap->profile_sample_rate==0) {

‎src/alloc/free.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ static mi_decl_noinline bool mi_abandoned_page_try_reclaim(mi_page_t* page, long
615615
else if (reclaim_on_free == 1 && // if cross-thread is allowed
616616
!theap->tld->is_in_threadpool && // and we are not part of a threadpool
617617
!mi_page_is_used_at_frac(page,4) && // and at least 1/4th of the page is free
618-
_mi_arena_memid_is_suitable(page->memid, mi_theap_heap(theap)->exclusive_arena)) { // and it fits our memory
618+
_mi_arena_memid_is_suitable(page->memid, _mi_theap_heap(theap)->exclusive_arena)) { // and it fits our memory
619619
// across threads
620620
max_reclaim = _mi_option_get_fast(mi_option_page_cross_thread_max_reclaim);
621621
}

‎src/alloc/page.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ bool _mi_page_extend_free(mi_theap_t* theap, mi_page_t* page) {
929929
mi_decl_nodiscard bool _mi_page_init(mi_theap_t* theap, mi_page_t* page) {
930930
mi_assert(page != NULL);
931931
mi_assert(theap!=NULL);
932-
// page->heap = (mi_is_heap_main(mi_theap_heap(theap)) ? NULL : mi_theap_heap(theap)); // faster for `mi_page_associated_theap`
932+
// page->heap = (mi_is_heap_main(_mi_theap_heap(theap)) ? NULL : _mi_theap_heap(theap)); // faster for `mi_page_associated_theap`
933933
// mi_page_set_theap(page, theap);
934934

935935
size_t page_size;
@@ -951,7 +951,7 @@ mi_decl_nodiscard bool _mi_page_init(mi_theap_t* theap, mi_page_t* page) {
951951
#endif
952952

953953
mi_assert_internal(page->heap != NULL);
954-
mi_assert_internal(page->heap == mi_theap_heap(theap));
954+
mi_assert_internal(page->heap == _mi_theap_heap(theap));
955955
mi_assert_internal(page->theap!=NULL);
956956
mi_assert_internal(page->theap == mi_page_theap(page));
957957
mi_assert_internal(page->capacity == 0);

‎src/arena/arena-page.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static mi_page_guards_t mi_theap_page_guards(const mi_theap_t* theap, size_t siz
176176
const size_t gsize = _mi_os_secure_guard_page_size();
177177
const bool guard_end = (theap->guard_end || (singleton && MI_SECURE >= 1)); // in secure mode, huge blocks are always followed by a guard page
178178
mi_page_guards_t guards = { 0, 0 };
179-
const mi_arena_t* const req_arena = mi_theap_heap(theap)->exclusive_arena;
179+
const mi_arena_t* const req_arena = _mi_theap_heap(theap)->exclusive_arena;
180180
if (req_arena != NULL && req_arena->memid.is_pinned) return guards;
181181
if (theap->guard_front && !os_align && gsize <= size/4) { guards.front = gsize; }
182182
if (guard_end && guards.front + gsize <= size/4) { guards.end = gsize; }
@@ -235,7 +235,7 @@ static bool mi_arena_try_find_abandoned_visit(mi_arena_t* arena, const void* arg
235235
const mi_arena_abandoned_visit_info_t* info = (const mi_arena_abandoned_visit_info_t*)arg;
236236
mi_assert_internal(result != NULL);
237237
mi_theap_t* const theap = info->theap;
238-
mi_heap_t* const heap = mi_theap_heap(theap);
238+
mi_heap_t* const heap = _mi_theap_heap(theap);
239239
const size_t tseq = theap->tld->thread_seq;
240240
mi_arena_pages_t* const arena_pages = mi_heap_arena_pages(heap, arena);
241241
if (arena_pages != NULL) {
@@ -270,7 +270,7 @@ static bool mi_arena_try_find_abandoned_visit(mi_arena_t* arena, const void* arg
270270

271271
static mi_page_t* mi_arenas_page_try_find_abandoned(mi_theap_t* theap, size_t slice_count, size_t block_size, mi_page_guards_t guards)
272272
{
273-
mi_heap_t* const heap = mi_theap_heap(theap);
273+
mi_heap_t* const heap = _mi_theap_heap(theap);
274274
const size_t tseq = theap->tld->thread_seq;
275275
mi_arena_t* const req_arena = heap->exclusive_arena;
276276

@@ -442,7 +442,7 @@ static bool mi_arena_page_meta_ensure_committed(mi_arena_t* arena, uint8_t* slic
442442
// Try to allocate slices from free space in the arena's, and return the separate page meta data (if available) in `*ppage_meta`.
443443
static uint8_t* mi_arenas_page_alloc_from_slices(mi_theap_t* theap, size_t slice_count, bool commit, bool allow_large,
444444
mi_memid_t* memid, mi_arena_pages_t** parena_pages, mi_page_t** ppage_meta) {
445-
mi_heap_t* const heap = mi_theap_heap(theap);
445+
mi_heap_t* const heap = _mi_theap_heap(theap);
446446
mi_tld_t* const tld = theap->tld;
447447
const int numa_node = (heap->numa_node >= 0 ? heap->numa_node : tld->numa_node);
448448

@@ -571,7 +571,7 @@ static uint8_t* mi_arenas_page_alloc_fresh_area(mi_theap_t* theap, size_t slice_
571571
*parena_pages = NULL;
572572
*ppage_meta = NULL;
573573
*memid = _mi_memid_none();
574-
mi_heap_t* const heap = mi_theap_heap(theap);
574+
mi_heap_t* const heap = _mi_theap_heap(theap);
575575

576576
// try to allocate from free space in arena's
577577
uint8_t* slice_start = NULL;
@@ -697,7 +697,7 @@ static void mi_arenas_page_init_meta(mi_theap_t* theap, mi_page_t* page, uint8_t
697697
page->free_is_zero = memid.initially_zero;
698698
page->slice_pcommitted = (uint16_t)(commit_size / _mi_os_page_size());
699699

700-
page->heap = mi_theap_heap(theap);
700+
page->heap = _mi_theap_heap(theap);
701701
if (block_size <= MI_LARGE_MAX_OBJ_SIZE && mi_memtag_use_for(memid.is_memtag, block_size)) { // huge blocks are not tagged (but have an end guard page in secure mode); over-aligned singletons up to this size are tagged
702702
mi_page_flags_set(page, true, MI_PAGE_IS_MEMTAG);
703703
}
@@ -1392,7 +1392,7 @@ static bool mi_heap_delete_page(const mi_heap_t* heap, const mi_heap_area_t* are
13921392
MI_UNUSED(block); MI_UNUSED(block_size); MI_UNUSED(heap);
13931393
const mi_heap_delete_visit_info_t* info = (const mi_heap_delete_visit_info_t*)arg;
13941394
mi_heap_t* heap_target = info->heap_target;
1395-
mi_theap_t* const theap = NULL; // info->theap; mi_assert_internal(mi_theap_heap(theap) == heap);
1395+
mi_theap_t* const theap = NULL; // info->theap; mi_assert_internal(_mi_theap_heap(theap) == heap);
13961396
mi_page_t* const page = mi_heap_area_page(area);
13971397

13981398
mi_page_claim_ownership(page); // claim ownership

‎src/heap/heap.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void mi_heap_stats_merge_to_main(mi_heap_t* heap) {
4545
}
4646

4747
bool _mi_heap_theap_set(mi_heap_t* heap, mi_theap_t* theap) {
48-
mi_assert_internal(mi_addr(theap) == 1 || mi_theap_heap(theap)==heap);
48+
mi_assert_internal(mi_addr(theap) == 1 || _mi_theap_heap(theap)==heap);
4949
mi_assert_internal(!_mi_is_empty_theap(theap));
5050
mi_assert_internal(heap->theap != 0);
5151
return _mi_thread_local_set(heap->theap,theap);

‎src/heap/process.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ static void mi_thread_theaps_done(mi_tld_t* tld)
531531
mi_theap_t* next = theap->tnext;
532532
// never destroy theaps; if a dll is linked statically with mimalloc,
533533
// there may still be delete/free calls after the mi_fls_done is called. Issue #207
534-
mi_heap_t* const heap = mi_theap_heap(theap);
534+
mi_heap_t* const heap = _mi_theap_heap(theap);
535535
mi_lock(&heap->theaps_lock) { // keep page queues stable during a heap walk
536536
_mi_theap_collect_abandon(theap);
537537
}

0 commit comments

Comments
 (0)