From 3b08599cfa23526c46c849f5a9e96e2e3bb9aa5a Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Fri, 22 Jul 2022 15:53:44 +0200 Subject: [PATCH] Add public helper to check if an allocation is managed by jemalloc --- pregen/include/jemalloc/jemalloc.h | 1 + src/jemalloc.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pregen/include/jemalloc/jemalloc.h b/pregen/include/jemalloc/jemalloc.h index ce47cacb..39fb99e3 100644 --- a/pregen/include/jemalloc/jemalloc.h +++ b/pregen/include/jemalloc/jemalloc.h @@ -269,6 +269,7 @@ JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_malloc_stats_print( const char *opts); JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_malloc_usable_size( JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW; +JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_is_known_allocation(void* ptr) JEMALLOC_CXX_THROW; #ifdef JEMALLOC_OVERRIDE_MEMALIGN JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN diff --git a/src/jemalloc.c b/src/jemalloc.c index 30ec49c9..f23b3aba 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -3763,6 +3763,24 @@ je_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) { return ret; } +JEMALLOC_EXPORT int JEMALLOC_NOTHROW +je_is_known_allocation(void* ptr) { + int ret = 0; + tsdn_t *tsdn = tsdn_fetch(); + + assert(malloc_initialized() || IS_INITIALIZER); + + tsdn = tsdn_fetch(); + check_entry_exit_locking(tsdn); + + if (likely(ptr != NULL)) { + ret = ivsalloc(tsdn, ptr); + } + + check_entry_exit_locking(tsdn); + return ret; +} + /* * End non-standard functions. */