From dcbe772f1ff19e35c50ebbf3cb34de41bc17e7b4 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 2 Apr 2022 07:33:09 +0100 Subject: [PATCH] new utility to name a region --- include/mimalloc-internal.h | 1 + include/mimalloc.h | 2 ++ src/os.c | 18 ++++++++++++++++++ src/page.c | 4 ++++ src/region.c | 1 - 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 16be12518..a294cf858 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -78,6 +78,7 @@ void* _mi_os_alloc(size_t size, mi_stats_t* stats); // to allocat void _mi_os_free(void* p, size_t size, mi_stats_t* stats); // to free thread local data size_t _mi_os_good_alloc_size(size_t size); bool _mi_os_has_overcommit(void); +bool _mi_os_alloc_named(void* p, size_t size, const char *name); // memory.c void* _mi_mem_alloc_aligned(size_t size, size_t alignment, bool* commit, bool* large, bool* is_pinned, bool* is_zero, size_t* id, mi_os_tld_t* tld); diff --git a/include/mimalloc.h b/include/mimalloc.h index 91ad352bf..b840f02c2 100644 --- a/include/mimalloc.h +++ b/include/mimalloc.h @@ -380,6 +380,8 @@ mi_decl_nodiscard mi_decl_export mi_decl_restrict void* mi_new_n(size_t count, s mi_decl_nodiscard mi_decl_export void* mi_new_realloc(void* p, size_t newsize) mi_attr_alloc_size(2); mi_decl_nodiscard mi_decl_export void* mi_new_reallocn(void* p, size_t newcount, size_t size) mi_attr_alloc_size2(2, 3); +mi_decl_export bool mi_alloc_named(void* p, size_t size, const char *name); + #ifdef __cplusplus } #endif diff --git a/src/os.c b/src/os.c index 757e8cabf..d5a87c07c 100644 --- a/src/os.c +++ b/src/os.c @@ -45,6 +45,7 @@ terms of the MIT license. A copy of the license can be found in the file #else #include #endif +#include #endif #if defined(__APPLE__) #include @@ -1437,3 +1438,20 @@ int _mi_os_numa_node_get(mi_os_tld_t* tld) { if (numa_node >= numa_count) { numa_node = numa_node % numa_count; } return (int)numa_node; } + +bool _mi_os_alloc_named(void* p, size_t size, const char *name) { + mi_assert_internal(p != NULL); + mi_assert_internal(name != NULL); +#if defined(__linux__) +#if !defined(PR_SET_VMA) +#define PR_SET_VMA 0x53564d41 +#define PR_SET_VMA_ANON_NAME 0 +#endif + return (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, (unsigned long)p, size, (unsigned long)name) == 0); +#else + MI_UNUSED(p); + MI_UNUSED(size); + MI_UNUSED(name); + return false; +#endif +} diff --git a/src/page.c b/src/page.c index 6efeba2ab..51cbf30cf 100644 --- a/src/page.c +++ b/src/page.c @@ -852,3 +852,7 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size) mi_attr_noexcept // and try again, this time succeeding! (i.e. this should never recurse) return _mi_page_malloc(heap, page, size); } + +bool mi_alloc_named(void* p, size_t size, const char *name) { + return _mi_os_alloc_named(p, size, name); +} diff --git a/src/region.c b/src/region.c index 2d73025e0..228fa1551 100644 --- a/src/region.c +++ b/src/region.c @@ -164,7 +164,6 @@ static bool mi_memid_is_arena(size_t id, mem_region_t** region, mi_bitmap_index_ } } - /* ---------------------------------------------------------------------------- Allocate a region is allocated from the OS (or an arena) -----------------------------------------------------------------------------*/