From d6f5796a928de6822b484aa5cf0ef7a75e870a95 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Thu, 5 Apr 2012 09:20:53 +0200 Subject: [PATCH] Bug 738176 - Completely disable jemalloc when it's supposed to be disabled on OSX, and cleanup exposed APIs. r=jlebar,r=khuey --- configure.in | 7 --- gfx/thebes/gfxImageSurface.cpp | 2 +- ipc/chromium/src/base/process_util_linux.cc | 4 -- js/src/configure.in | 7 --- memory/jemalloc/jemalloc.c | 13 ++-- memory/jemalloc/jemalloc.h | 62 ++++++++----------- memory/mozalloc/mozalloc.cpp | 30 +++------ memory/mozalloc/mozalloc.h | 6 +- memory/mozalloc/mozalloc_macro_wrappers.h | 4 +- .../mozalloc/mozalloc_undef_macro_wrappers.h | 4 +- mozglue/build/Makefile.in | 4 -- mozglue/build/mozglue.def.in | 2 +- storage/src/mozStorageService.cpp | 14 +---- xpcom/base/nsMemoryReporterManager.cpp | 17 +---- 14 files changed, 54 insertions(+), 122 deletions(-) diff --git a/configure.in b/configure.in index be931d0f4eb..fcdb8c8a39a 100644 --- a/configure.in +++ b/configure.in @@ -7214,13 +7214,6 @@ else AC_MSG_ERROR([--enable-jemalloc not supported on ${target}]) ;; esac - - if test "$OS_ARCH" != "WINNT"; then - dnl NB: this must be kept in sync with jemalloc.h - AC_DEFINE(HAVE_JEMALLOC_VALLOC) - fi - AC_DEFINE(HAVE_JEMALLOC_POSIX_MEMALIGN) - AC_DEFINE(HAVE_JEMALLOC_MEMALIGN) fi # MOZ_MEMORY AC_SUBST(MOZ_MEMORY) AC_SUBST(MOZ_GLUE_LDFLAGS) diff --git a/gfx/thebes/gfxImageSurface.cpp b/gfx/thebes/gfxImageSurface.cpp index 2b8a4091d1b..e0462e7cac6 100644 --- a/gfx/thebes/gfxImageSurface.cpp +++ b/gfx/thebes/gfxImageSurface.cpp @@ -109,7 +109,7 @@ static void* TryAllocAlignedBytes(size_t aSize) { // Use fallible allocators here -#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN) +#if defined(HAVE_POSIX_MEMALIGN) void* ptr; // Try to align for fast alpha recovery. This should only help // cairo too, can't hurt. diff --git a/ipc/chromium/src/base/process_util_linux.cc b/ipc/chromium/src/base/process_util_linux.cc index 858e3c54f6d..cf420763c73 100644 --- a/ipc/chromium/src/base/process_util_linux.cc +++ b/ipc/chromium/src/base/process_util_linux.cc @@ -19,10 +19,6 @@ #include "base/string_tokenizer.h" #include "base/string_util.h" -#ifdef MOZ_MEMORY_ANDROID -#include "jemalloc.h" -#endif - namespace { enum ParsingState { diff --git a/js/src/configure.in b/js/src/configure.in index 46acb0fe42f..01eecff76d3 100644 --- a/js/src/configure.in +++ b/js/src/configure.in @@ -4088,13 +4088,6 @@ if test "$MOZ_MEMORY"; then AC_MSG_ERROR([--enable-jemalloc not supported on ${target}]) ;; esac - - if test "$OS_ARCH" != "Darwin"; then - dnl NB: this must be kept in sync with jemalloc.h - AC_DEFINE(HAVE_JEMALLOC_VALLOC) - AC_DEFINE(HAVE_JEMALLOC_POSIX_MEMALIGN) - AC_DEFINE(HAVE_JEMALLOC_MEMALIGN) - fi fi AC_SUBST(MOZ_MEMORY) AC_SUBST(MOZ_GLUE_LDFLAGS) diff --git a/memory/jemalloc/jemalloc.c b/memory/jemalloc/jemalloc.c index 078f6a813d3..9ed208d4ad8 100644 --- a/memory/jemalloc/jemalloc.c +++ b/memory/jemalloc/jemalloc.c @@ -123,6 +123,8 @@ * jemalloc_purge_freed_pages(), which will force the OS to release those * MADV_FREE'd pages, making the process's RSS reflect its true memory usage. * + * The jemalloc_purge_freed_pages definition in jemalloc.h needs to be + * adjusted if MALLOC_DOUBLE_PURGE is ever enabled on Linux. */ #ifdef MOZ_MEMORY_DARWIN #define MALLOC_DOUBLE_PURGE @@ -374,7 +376,7 @@ __FBSDID("$FreeBSD: head/lib/libc/stdlib/malloc.c 180599 2008-07-18 19:35:44Z ja #endif -#include "jemalloc.h" +#include "jemalloc_types.h" #include "linkedlist.h" /* Some tools, such as /dev/dsp wrappers, LD_PRELOAD libraries that @@ -6550,8 +6552,11 @@ free(void *ptr) */ /* This was added by Mozilla for use by SQLite. */ +#ifdef MOZ_MEMORY_DARWIN +static +#endif size_t -je_malloc_usable_size_in_advance(size_t size) +je_malloc_good_size(size_t size) { /* * This duplicates the logic in imalloc(), arena_malloc() and @@ -6581,7 +6586,7 @@ je_malloc_usable_size_in_advance(size_t size) * Huge. We use PAGE_CEILING to get psize, instead of using * CHUNK_CEILING to get csize. This ensures that this * malloc_usable_size(malloc(n)) always matches - * je_malloc_usable_size_in_advance(n). + * je_malloc_good_size(n). */ size = PAGE_CEILING(size); } @@ -6915,7 +6920,7 @@ zone_destroy(malloc_zone_t *zone) static size_t zone_good_size(malloc_zone_t *zone, size_t size) { - return je_malloc_usable_size_in_advance(size); + return je_malloc_good_size(size); } static size_t diff --git a/memory/jemalloc/jemalloc.h b/memory/jemalloc/jemalloc.h index 27a2b2af3be..dc9a448f59d 100644 --- a/memory/jemalloc/jemalloc.h +++ b/memory/jemalloc/jemalloc.h @@ -32,52 +32,38 @@ #ifndef _JEMALLOC_H_ #define _JEMALLOC_H_ +#if defined(MOZ_MEMORY_DARWIN) +#include +#endif #include "jemalloc_types.h" #ifdef __cplusplus extern "C" { #endif -extern const char *_malloc_options; - -/* Darwin and Linux already have memory allocation functions */ -#if (!defined(MOZ_MEMORY_DARWIN) && !defined(MOZ_MEMORY_LINUX)) -void *malloc(size_t size); -void *valloc(size_t size); -void *calloc(size_t num, size_t size); -void *realloc(void *ptr, size_t size); -void free(void *ptr); -int posix_memalign(void **memptr, size_t alignment, size_t size); -#endif /* MOZ_MEMORY_DARWIN, MOZ_MEMORY_LINUX */ - -/* Android doesn't have posix_memalign */ -#ifdef MOZ_MEMORY_ANDROID -int posix_memalign(void **memptr, size_t alignment, size_t size); +#if defined(MOZ_MEMORY_LINUX) +__attribute__((weak)) #endif - -#if defined(MOZ_MEMORY_DARWIN) || defined(MOZ_MEMORY_WINDOWS) -void *je_malloc(size_t size); -void *je_valloc(size_t size); -void *je_calloc(size_t num, size_t size); -void *je_realloc(void *ptr, size_t size); -void je_free(void *ptr); -void *je_memalign(size_t alignment, size_t size); -int je_posix_memalign(void **memptr, size_t alignment, size_t size); -char *je_strndup(const char *src, size_t len); -char *je_strdup(const char *src); -size_t je_malloc_usable_size(const void *ptr); -#endif - -/* Linux has memalign and malloc_usable_size */ -#if !defined(MOZ_MEMORY_LINUX) -void *memalign(size_t alignment, size_t size); -size_t malloc_usable_size(const void *ptr); -#endif /* MOZ_MEMORY_LINUX */ - void jemalloc_stats(jemalloc_stats_t *stats); /* Computes the usable size in advance. */ -size_t je_malloc_usable_size_in_advance(size_t size); +#if !defined(MOZ_MEMORY_DARWIN) +#if defined(MOZ_MEMORY_LINUX) +__attribute__((weak)) +#endif +size_t je_malloc_good_size(size_t size); +#endif + +static inline size_t je_malloc_usable_size_in_advance(size_t size) { +#if defined(MOZ_MEMORY_DARWIN) + return malloc_good_size(size); +#else + if (je_malloc_good_size) + return je_malloc_good_size(size); + else + return size; +#endif +} /* * On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages @@ -104,7 +90,11 @@ size_t je_malloc_usable_size_in_advance(size_t size); * * If MALLOC_DOUBLE_PURGE is not defined, this function does nothing. */ +#if defined(MOZ_MEMORY_LINUX) +static inline void jemalloc_purge_freed_pages() { } +#else void jemalloc_purge_freed_pages(); +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/memory/mozalloc/mozalloc.cpp b/memory/mozalloc/mozalloc.cpp index c8796f049cf..8ec3cbe6ed0 100644 --- a/memory/mozalloc/mozalloc.cpp +++ b/memory/mozalloc/mozalloc.cpp @@ -53,11 +53,6 @@ # include // for valloc on *BSD #endif //if defined(XP_UNIX) -#if defined(MOZ_MEMORY) -// jemalloc.h doesn't redeclare symbols if they're provided by the OS -# include "jemalloc.h" -#endif - #if defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec)) # define MOZALLOC_EXPORT __declspec(dllexport) #endif @@ -67,6 +62,10 @@ #include "mozilla/mozalloc.h" #include "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom +/* Windows doesn't have malloc_usable_size, but jemalloc has */ +#if defined(MOZ_MEMORY_WINDOWS) +extern "C" size_t malloc_usable_size(const void *ptr); +#endif #if defined(__GNUC__) && (__GNUC__ > 2) #define LIKELY(x) (__builtin_expect(!!(x), 1)) @@ -76,21 +75,6 @@ #define UNLIKELY(x) (x) #endif -#ifdef MOZ_MEMORY_DARWIN -#include "jemalloc.h" -#define malloc(a) je_malloc(a) -#define posix_memalign(a, b, c) je_posix_memalign(a, b, c) -#define valloc(a) je_valloc(a) -#define calloc(a, b) je_calloc(a, b) -#define memalign(a, b) je_memalign(a, b) -#define strdup(a) je_strdup(a) -#define strndup(a, b) je_strndup(a, b) -/* We omit functions which could be passed a memory region that was not - * allocated by jemalloc (realloc, free and malloc_usable_size). Instead, - * we use the system-provided functions, which will in turn call the - * jemalloc versions when appropriate */ -#endif - void moz_free(void* ptr) { @@ -179,7 +163,7 @@ moz_strndup(const char* str, size_t strsize) } #endif // if defined(HAVE_STRNDUP) -#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN) +#if defined(HAVE_POSIX_MEMALIGN) int moz_xposix_memalign(void **ptr, size_t alignment, size_t size) { @@ -214,7 +198,7 @@ moz_posix_memalign(void **ptr, size_t alignment, size_t size) } #endif // if defined(HAVE_POSIX_MEMALIGN) -#if defined(HAVE_MEMALIGN) || defined(HAVE_JEMALLOC_MEMALIGN) +#if defined(HAVE_MEMALIGN) void* moz_xmemalign(size_t boundary, size_t size) { @@ -233,7 +217,7 @@ moz_memalign(size_t boundary, size_t size) } #endif // if defined(HAVE_MEMALIGN) -#if defined(HAVE_VALLOC) || defined(HAVE_JEMALLOC_VALLOC) +#if defined(HAVE_VALLOC) void* moz_xvalloc(size_t size) { diff --git a/memory/mozalloc/mozalloc.h b/memory/mozalloc/mozalloc.h index 1f14056f489..ff185bb3c41 100644 --- a/memory/mozalloc/mozalloc.h +++ b/memory/mozalloc/mozalloc.h @@ -146,7 +146,7 @@ MOZALLOC_EXPORT char* moz_strndup(const char* str, size_t strsize) #endif /* if defined(HAVE_STRNDUP) */ -#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN) +#if defined(HAVE_POSIX_MEMALIGN) MOZALLOC_EXPORT int moz_xposix_memalign(void **ptr, size_t alignment, size_t size) NS_WARN_UNUSED_RESULT; @@ -155,7 +155,7 @@ MOZALLOC_EXPORT int moz_posix_memalign(void **ptr, size_t alignment, size_t size #endif /* if defined(HAVE_POSIX_MEMALIGN) */ -#if defined(HAVE_MEMALIGN) || defined(HAVE_JEMALLOC_MEMALIGN) +#if defined(HAVE_MEMALIGN) MOZALLOC_EXPORT void* moz_xmemalign(size_t boundary, size_t size) NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; @@ -164,7 +164,7 @@ MOZALLOC_EXPORT void* moz_memalign(size_t boundary, size_t size) #endif /* if defined(HAVE_MEMALIGN) */ -#if defined(HAVE_VALLOC) || defined(HAVE_JEMALLOC_VALLOC) +#if defined(HAVE_VALLOC) MOZALLOC_EXPORT void* moz_xvalloc(size_t size) NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; diff --git a/memory/mozalloc/mozalloc_macro_wrappers.h b/memory/mozalloc/mozalloc_macro_wrappers.h index 257973e4c68..e66026442b4 100644 --- a/memory/mozalloc/mozalloc_macro_wrappers.h +++ b/memory/mozalloc/mozalloc_macro_wrappers.h @@ -63,11 +63,11 @@ #define strndup(_, __) moz_strndup(_, __) #endif -#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN) +#if defined(HAVE_POSIX_MEMALIGN) #define posix_memalign(_, __, ___) moz_posix_memalign(_, __, ___) #endif -#if defined(HAVE_MEMALIGN) || defined(HAVE_JEMALLOC_MEMALIGN) +#if defined(HAVE_MEMALIGN) #define memalign(_, __) moz_memalign(_, __) #endif diff --git a/memory/mozalloc/mozalloc_undef_macro_wrappers.h b/memory/mozalloc/mozalloc_undef_macro_wrappers.h index 2c79c380fa1..b64856fd511 100644 --- a/memory/mozalloc/mozalloc_undef_macro_wrappers.h +++ b/memory/mozalloc/mozalloc_undef_macro_wrappers.h @@ -64,11 +64,11 @@ # undef strndup #endif -#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN) +#if defined(HAVE_POSIX_MEMALIGN) # undef posix_memalign #endif -#if defined(HAVE_MEMALIGN) || defined(HAVE_JEMALLOC_MEMALIGN) +#if defined(HAVE_MEMALIGN) # undef memalign #endif diff --git a/mozglue/build/Makefile.in b/mozglue/build/Makefile.in index 5d8b7cfe94b..9e1802bdfe3 100644 --- a/mozglue/build/Makefile.in +++ b/mozglue/build/Makefile.in @@ -119,10 +119,6 @@ endif include $(topsrcdir)/config/rules.mk ifdef MOZ_MEMORY -ifeq (Darwin,$(OS_TARGET)) -LDFLAGS += -init _jemalloc_darwin_init -endif - ifeq (WINNT,$(OS_TARGET)) # Roll our own custom logic here for the import library diff --git a/mozglue/build/mozglue.def.in b/mozglue/build/mozglue.def.in index 97afb411828..cda2540751b 100644 --- a/mozglue/build/mozglue.def.in +++ b/mozglue/build/mozglue.def.in @@ -51,7 +51,7 @@ EXPORTS wcsdup=je_wcsdup _wcsdup=je_wcsdup malloc_usable_size=je_malloc_usable_size - je_malloc_usable_size_in_advance + je_malloc_good_size jemalloc_stats ; A hack to work around the CRT (see giant comment in Makefile.in) frex=je_dumb_free_thunk diff --git a/storage/src/mozStorageService.cpp b/storage/src/mozStorageService.cpp index ece763bffae..55ef9d996da 100644 --- a/storage/src/mozStorageService.cpp +++ b/storage/src/mozStorageService.cpp @@ -530,19 +530,7 @@ Service::shutdown() sqlite3_vfs *ConstructTelemetryVFS(); #ifdef MOZ_MEMORY - -# if defined(XP_WIN) || defined(SOLARIS) || defined(ANDROID) || defined(XP_MACOSX) -# include "jemalloc.h" -# elif defined(XP_LINUX) -// jemalloc is directly linked into firefox-bin; libxul doesn't link -// with it. So if we tried to use je_malloc_usable_size_in_advance directly -// here, it wouldn't be defined. Instead, we don't include the jemalloc header -// and weakly link against je_malloc_usable_size_in_advance. -extern "C" { -extern size_t je_malloc_usable_size_in_advance(size_t size) - NS_VISIBILITY_DEFAULT __attribute__((weak)); -} -# endif // XP_LINUX +# include "jemalloc.h" namespace { diff --git a/xpcom/base/nsMemoryReporterManager.cpp b/xpcom/base/nsMemoryReporterManager.cpp index 0b6e5142647..35d596c6608 100644 --- a/xpcom/base/nsMemoryReporterManager.cpp +++ b/xpcom/base/nsMemoryReporterManager.cpp @@ -62,21 +62,8 @@ static PRInt64 GetExplicit() } #if defined(MOZ_MEMORY) -# if defined(XP_WIN) || defined(SOLARIS) || defined(ANDROID) || defined(XP_MACOSX) -# define HAVE_JEMALLOC_STATS 1 -# include "jemalloc.h" -# elif defined(XP_LINUX) -# define HAVE_JEMALLOC_STATS 1 -# include "jemalloc_types.h" -// jemalloc is directly linked into firefox-bin; libxul doesn't link -// with it. So if we tried to use jemalloc_stats directly here, it -// wouldn't be defined. Instead, we don't include the jemalloc header -// and weakly link against jemalloc_stats. -extern "C" { -extern void jemalloc_stats(jemalloc_stats_t* stats) - NS_VISIBILITY_DEFAULT __attribute__((weak)); -} -# endif // XP_LINUX +# define HAVE_JEMALLOC_STATS 1 +# include "jemalloc.h" #endif // MOZ_MEMORY #if defined(XP_LINUX) || defined(XP_MACOSX) || defined(SOLARIS)