diff --git a/include/mimalloc.h b/include/mimalloc.h index 1f80027..3c10a65 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -121,7 +121,6 @@ mi_decl_export void mi_thread_stats_print(FILE* out) mi_attr_noexcept; typedef void (mi_deferred_free_fun)(bool force, unsigned long long heartbeat); mi_decl_export void mi_register_deferred_free(mi_deferred_free_fun* deferred_free) mi_attr_noexcept; - // ------------------------------------------------------ // Aligned allocation // ------------------------------------------------------ @@ -196,6 +195,8 @@ 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_all_blocks, mi_block_visit_fun* visitor, void* arg); +mi_decl_export bool mi_is_in_heap_region(const void* p) mi_attr_noexcept; + // ------------------------------------------------------ // Convenience // ------------------------------------------------------ diff --git a/src/memory.c b/src/memory.c index 030541a..83e90b0 100644 --- a/src/memory.c +++ b/src/memory.c @@ -104,6 +104,16 @@ static size_t mi_good_commit_size(size_t size) { return _mi_align_up(size, _mi_os_large_page_size()); } +// Return if a pointer points into a region reserved by us. +bool mi_is_in_heap_region(const void* p) { + size_t count = mi_atomic_read(®ions_count); + for (size_t i = 0; i < count; i++) { + uint8_t* start = (uint8_t*)mi_atomic_read_ptr(®ions[i].start); + if (start != NULL && (uint8_t*)p >= start && (uint8_t*)p < start + MI_REGION_SIZE) return true; + } + return false; +} + /* ---------------------------------------------------------------------------- Commit from a region -----------------------------------------------------------------------------*/