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.
This commit is contained in:
Jason Evans 2012-02-29 10:37:27 -08:00
parent 5965631636
commit 4bb0983013
3 changed files with 28 additions and 4 deletions

View File

@ -144,10 +144,6 @@ static const bool config_ivsalloc =
#include <malloc/malloc.h> #include <malloc/malloc.h>
#endif #endif
#ifdef JEMALLOC_LAZY_LOCK
#include <dlfcn.h>
#endif
#define RB_COMPACT #define RB_COMPACT
#include "jemalloc/internal/rb.h" #include "jemalloc/internal/rb.h"
#include "jemalloc/internal/qr.h" #include "jemalloc/internal/qr.h"

View File

@ -1272,6 +1272,30 @@ JEMALLOC_P(valloc)(size_t size)
} }
#endif #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 */ #endif /* JEMALLOC_PREFIX */
/* /*
* End non-standard override functions. * End non-standard override functions.

View File

@ -1,6 +1,10 @@
#define JEMALLOC_MUTEX_C_ #define JEMALLOC_MUTEX_C_
#include "jemalloc/internal/jemalloc_internal.h" #include "jemalloc/internal/jemalloc_internal.h"
#ifdef JEMALLOC_LAZY_LOCK
#include <dlfcn.h>
#endif
/******************************************************************************/ /******************************************************************************/
/* Data. */ /* Data. */