From 2af2d8e2b4272475374a7606f8767b9f7d0fdc73 Mon Sep 17 00:00:00 2001 From: Anshika Gupta Date: Mon, 4 May 2026 19:27:34 +0530 Subject: [PATCH] Added mi_thread_visit_blocks API --- include/mimalloc.h | 4 ++++ src/heap.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/mimalloc.h b/include/mimalloc.h index 6522c9ebc..960b15844 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -289,6 +289,10 @@ typedef bool (mi_cdecl mi_block_visit_fun)(const mi_heap_t* heap, const mi_heap_ mi_decl_export bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_blocks, mi_block_visit_fun* visitor, void* arg); + +//Visiting all blocks of all heaps in a thread +mi_decl_export bool mi_thread_visit_blocks(bool visit_blocks, mi_block_visit_fun* visitor, void* arg); + // Experimental mi_decl_nodiscard mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept; mi_decl_nodiscard mi_decl_export bool mi_is_redirected(void) mi_attr_noexcept; diff --git a/src/heap.c b/src/heap.c index 84a26a55d..158a1ad32 100644 --- a/src/heap.c +++ b/src/heap.c @@ -729,6 +729,7 @@ bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_blocks, mi_block_vis + static const mi_page_t* mi_safe_ptr_page(void* p) { const mi_segment_t* const segment = _mi_ptr_segment(p); if mi_unlikely(segment==NULL) return NULL; @@ -760,3 +761,20 @@ bool mi_unsafe_heap_page_is_under_utilized(mi_heap_t* heap, void* p, size_t perc if (perc_threshold>=100) return true; return (perc_threshold >= ((100UL*page->used) / page->capacity)); } + +//Visiting all blocks of all heaps in a thread +bool mi_thread_visit_blocks(bool visit_blocks, mi_block_visit_fun* visitor, void* arg) { + mi_heap_t* heap = mi_heap_get_default(); + if (heap == NULL) return false; + + mi_heap_t* curr = heap->tld->heaps; + while (curr != NULL) { + mi_heap_t* next = curr->next; + if (!mi_heap_visit_blocks(curr, visit_blocks, visitor, arg)) { + return false; + } + curr = next; + } + + return true; +}