From 4bb09830133ffa8b27a95bc3727558007722c152 Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Wed, 29 Feb 2012 10:37:27 -0800 Subject: Use glibc allocator hooks. When jemalloc is used as a libc malloc replacement (i.e. not prefixed), some particular setups may end up inconsistently calling malloc from libc and free from jemalloc, or the other way around. glibc provides hooks to make its functions use alternative implementations. Use them. Submitted by Karl Tomlinson and Mike Hommey. --- include/jemalloc/internal/jemalloc_internal.h.in | 4 ---- src/jemalloc.c | 24 ++++++++++++++++++++++++ src/mutex.c | 4 ++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/jemalloc/internal/jemalloc_internal.h.in b/include/jemalloc/internal/jemalloc_internal.h.in index 35c05f4..c21c218 100644 --- a/include/jemalloc/internal/jemalloc_internal.h.in +++ b/include/jemalloc/internal/jemalloc_internal.h.in @@ -144,10 +144,6 @@ static const bool config_ivsalloc = #include #endif -#ifdef JEMALLOC_LAZY_LOCK -#include -#endif - #define RB_COMPACT #include "jemalloc/internal/rb.h" #include "jemalloc/internal/qr.h" diff --git a/src/jemalloc.c b/src/jemalloc.c index 535efaa..ccc3a20 100644 --- a/src/jemalloc.c +++ b/src/jemalloc.c @@ -1272,6 +1272,30 @@ JEMALLOC_P(valloc)(size_t size) } #endif +#if defined(__GLIBC__) && !defined(__UCLIBC__) +/* + * glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible + * to inconsistently reference libc's malloc(3)-compatible functions + * (https://bugzilla.mozilla.org/show_bug.cgi?id=493541). + * + * These definitions interpose hooks in glibc.  The functions are actually + * passed an extra argument for the caller return address, which will be + * ignored. + */ +JEMALLOC_ATTR(visibility("default")) +void (* const __free_hook)(void *ptr) = JEMALLOC_P(free); + +JEMALLOC_ATTR(visibility("default")) +void *(* const __malloc_hook)(size_t size) = JEMALLOC_P(malloc); + +JEMALLOC_ATTR(visibility("default")) +void *(* const __realloc_hook)(void *ptr, size_t size) = JEMALLOC_P(realloc); + +JEMALLOC_ATTR(visibility("default")) +void *(* const __memalign_hook)(size_t alignment, size_t size) = + JEMALLOC_P(memalign); +#endif + #endif /* JEMALLOC_PREFIX */ /* * End non-standard override functions. diff --git a/src/mutex.c b/src/mutex.c index ca89ef1..0e09060 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -1,6 +1,10 @@ #define JEMALLOC_MUTEX_C_ #include "jemalloc/internal/jemalloc_internal.h" +#ifdef JEMALLOC_LAZY_LOCK +#include +#endif + /******************************************************************************/ /* Data. */ -- cgit v0.12