From 4859e2ab1dbf59c78454993b6eb1c144bc56966c Mon Sep 17 00:00:00 2001 From: Brad Lassey Date: Sat, 5 Jun 2010 13:15:50 -0400 Subject: [PATCH] bug 559263 - make jemalloc work with android, follow up patch r=ted --- configure.in | 14 ++- js/src/configure.in | 2 +- memory/jemalloc/jemalloc.c | 2 + memory/mozalloc/Makefile.in | 4 +- memory/mozalloc/ld_malloc_wrappers.c | 140 +++++++++++++++++++++++++++ memory/mozalloc/mozalloc.cpp | 3 +- memory/mozalloc/wraps.c | 102 ------------------- 7 files changed, 157 insertions(+), 110 deletions(-) create mode 100644 memory/mozalloc/ld_malloc_wrappers.c delete mode 100644 memory/mozalloc/wraps.c diff --git a/configure.in b/configure.in index 34f43904734..9e828cff9fe 100644 --- a/configure.in +++ b/configure.in @@ -7171,6 +7171,8 @@ if test "$MOZ_MEMORY"; then *-android*) AC_DEFINE(MOZ_MEMORY_LINUX) AC_DEFINE(MOZ_MEMORY_ANDROID) + _WRAP_MALLOC=1 + export WRAP_MALLOC_LIB="-L$_objdir/dist/lib -lmozalloc" ;; *) AC_MSG_ERROR([--enable-jemalloc not supported on ${target}]) @@ -7200,7 +7202,7 @@ MOZ_ARG_ENABLE_BOOL(wrap-malloc, if test -n "$_WRAP_MALLOC"; then if test "$GNU_CC"; then - WRAP_MALLOC_CFLAGS="${LDFLAGS} -Wl,--wrap -Wl,malloc -Wl,--wrap -Wl,calloc -Wl,--wrap -Wl,valloc -Wl,--wrap -Wl,free -Wl,--wrap -Wl,realloc -Wl,--wrap -Wl,memalign -Wl,--wrap -Wl,__builtin_new -Wl,--wrap -Wl,__builtin_vec_new -Wl,--wrap -Wl,__builtin_delete -Wl,--wrap -Wl,__builtin_vec_delete -Wl,--wrap -Wl,PR_Free -Wl,--wrap -Wl,PR_Malloc -Wl,--wrap -Wl,PR_Calloc -Wl,--wrap -Wl,PR_Realloc -Wl,--wrap -Wl,strdup -Wl,--wrap -Wl,strndup" + WRAP_MALLOC_CFLAGS="${LDFLAGS} -Wl,--wrap -Wl,malloc -Wl,--wrap -Wl,calloc -Wl,--wrap -Wl,valloc -Wl,--wrap -Wl,free -Wl,--wrap -Wl,realloc -Wl,--wrap -Wl,memalign -Wl,--wrap -Wl,__builtin_new -Wl,--wrap -Wl,__builtin_vec_new -Wl,--wrap -Wl,__builtin_delete -Wl,--wrap -Wl,__builtin_vec_delete -Wl,--wrap -Wl,PR_Free -Wl,--wrap -Wl,PR_Malloc -Wl,--wrap -Wl,PR_Calloc -Wl,--wrap -Wl,PR_Realloc -Wl,--wrap -Wl,strdup -Wl,--wrap -Wl,strndup -Wl,--wrap -Wl,posix_memalign" MKSHLIB='$(CXX) $(DSO_LDOPTS) $(WRAP_MALLOC_CFLAGS) $(WRAP_MALLOC_LIB) -o $@' fi fi @@ -9007,6 +9009,13 @@ unset CONFIG_FILES # No need to run subconfigures when building with LIBXUL_SDK_DIR if test "$COMPILE_ENVIRONMENT" -a -z "$LIBXUL_SDK_DIR"; then +if test -n "$MOZ_THUMB2"; then + _SUBDIR_CONFIG_ARGS="$_SUBDIR_CONFIG_ARGS --enable-thumb2" +fi +if test -n "$_WRAP_MALLOC"; then + _SUBDIR_CONFIG_ARGS="$_SUBDIR_CONFIG_ARGS --enable-wrap-malloc" +fi + if test -z "$MOZ_NATIVE_NSPR"; then ac_configure_args="$_SUBDIR_CONFIG_ARGS --with-dist-prefix=$MOZ_BUILD_ROOT/dist --with-mozilla" if test -z "$MOZ_DEBUG"; then @@ -9024,9 +9033,6 @@ if test -z "$MOZ_NATIVE_NSPR"; then if test -n "$USE_ARM_KUSER"; then ac_configure_args="$ac_configure_args --with-arm-kuser" fi - if test -n "$MOZ_THUMB2"; then - ac_configure_args="$ac_configure_args --enable-thumb2" - fi AC_OUTPUT_SUBDIRS(nsprpub) ac_configure_args="$_SUBDIR_CONFIG_ARGS" fi diff --git a/js/src/configure.in b/js/src/configure.in index 0d1bdeca9ae..7eed142e124 100644 --- a/js/src/configure.in +++ b/js/src/configure.in @@ -4368,7 +4368,7 @@ MOZ_ARG_ENABLE_BOOL(wrap-malloc, if test -n "$_WRAP_MALLOC"; then if test "$GNU_CC"; then - WRAP_MALLOC_CFLAGS="${LDFLAGS} -Wl,--wrap -Wl,malloc -Wl,--wrap -Wl,calloc -Wl,--wrap -Wl,valloc -Wl,--wrap -Wl,free -Wl,--wrap -Wl,realloc -Wl,--wrap -Wl,memalign -Wl,--wrap -Wl,__builtin_new -Wl,--wrap -Wl,__builtin_vec_new -Wl,--wrap -Wl,__builtin_delete -Wl,--wrap -Wl,__builtin_vec_delete -Wl,--wrap -Wl,PR_Free -Wl,--wrap -Wl,PR_Malloc -Wl,--wrap -Wl,PR_Calloc -Wl,--wrap -Wl,PR_Realloc -Wl,--wrap -Wl,strdup -Wl,--wrap -Wl,strndup" + WRAP_MALLOC_CFLAGS="${LDFLAGS} -Wl,--wrap -Wl,malloc -Wl,--wrap -Wl,calloc -Wl,--wrap -Wl,valloc -Wl,--wrap -Wl,free -Wl,--wrap -Wl,realloc -Wl,--wrap -Wl,memalign -Wl,--wrap -Wl,__builtin_new -Wl,--wrap -Wl,__builtin_vec_new -Wl,--wrap -Wl,__builtin_delete -Wl,--wrap -Wl,__builtin_vec_delete -Wl,--wrap -Wl,PR_Free -Wl,--wrap -Wl,PR_Malloc -Wl,--wrap -Wl,PR_Calloc -Wl,--wrap -Wl,PR_Realloc -Wl,--wrap -Wl,strdup -Wl,--wrap -Wl,strndup -Wl,--wrap -Wl,posix_memalign" MKSHLIB='$(CXX) $(DSO_LDOPTS) $(WRAP_MALLOC_CFLAGS) $(WRAP_MALLOC_LIB) -o $@' fi fi diff --git a/memory/jemalloc/jemalloc.c b/memory/jemalloc/jemalloc.c index b8cc352fd55..4b7c8e27fb7 100644 --- a/memory/jemalloc/jemalloc.c +++ b/memory/jemalloc/jemalloc.c @@ -5770,6 +5770,8 @@ inline void sys_free(void* ptr) {return free(ptr);} #define calloc(a, b) je_calloc(a, b) #define realloc(a, b) je_realloc(a, b) #define free(a) je_free(a) +#define posix_memalign(a, b, c) je_posix_memalign(a, b, c) + char *je_strndup(const char *src, size_t len) { char* dst = (char*)je_malloc(len + 1); if(dst) diff --git a/memory/mozalloc/Makefile.in b/memory/mozalloc/Makefile.in index 20348754e00..5d76d127d29 100644 --- a/memory/mozalloc/Makefile.in +++ b/memory/mozalloc/Makefile.in @@ -59,8 +59,8 @@ ifneq (,$(findstring mozalloc,$(WRAP_MALLOC_LIB))) EXTRA_DSO_LDOPTS += $(DIST)/lib/libjemalloc.a WRAP_MALLOC_LIB= WRAP_MALLOC_CFLAGS= -DEFINES += -DWRAP_MALLOC -CSRCS = wraps.c +DEFINES += -DWRAP_MALLOC_WITH_JEMALLOC +CSRCS = ld_malloc_wrappers.c ifeq (,$(filter-out Linux,$(OS_TARGET))) EXTRA_DSO_LDOPTS += -lpthread endif diff --git a/memory/mozalloc/ld_malloc_wrappers.c b/memory/mozalloc/ld_malloc_wrappers.c new file mode 100644 index 00000000000..7f9ced33d49 --- /dev/null +++ b/memory/mozalloc/ld_malloc_wrappers.c @@ -0,0 +1,140 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Brad Lassey + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include // for size_t +#include // for malloc, free +#include "mozalloc.h" +#include + +#ifdef __malloc_hook +static void* moz_malloc_hook(size_t size, const void *caller) +{ + return moz_malloc(size); +} + +static void* moz_realloc_hook(void *ptr, size_t size, const void *caller) +{ + return moz_realloc(ptr, size); +} + +static void moz_free_hook(void *ptr, const void *caller) +{ + moz_free(ptr); +} + +static void* moz_memalign_hook(size_t align, size_t size, const void *caller) +{ + return moz_memalign(align, size); +} + +static void +moz_malloc_init_hook (void) +{ + __malloc_hook = moz_malloc_hook; + __realloc_hook = moz_realloc_hook; + __free_hook = moz_free_hook; + __memalign_hook = moz_memalign_hook; +} + +/* Override initializing hook from the C library. */ +void (*__malloc_initialize_hook) (void) = moz_malloc_init_hook; +#endif + +inline void __wrap_free(void* ptr) +{ + moz_free(ptr); +} + +inline void* __wrap_malloc(size_t size) +{ + return moz_malloc(size); +} + +inline void* __wrap_realloc(void* ptr, size_t size) +{ + return moz_realloc(ptr, size); +} + +inline void* __wrap_calloc(size_t num, size_t size) +{ + return moz_calloc(num, size); +} + +inline void* __wrap_valloc(size_t size) +{ + return moz_valloc(size); +} + +inline void* __wrap_memalign(size_t align, size_t size) +{ + return moz_memalign(align, size); +} + +inline char* __wrap_strdup(char* str) +{ + return moz_strdup(str); +} + +inline char* __wrap_strndup(const char* str, size_t size) { + return moz_strndup(str, size); +} + + +inline void __wrap_PR_Free(void* ptr) +{ + moz_free(ptr); +} + +inline void* __wrap_PR_Malloc(size_t size) +{ + return moz_malloc(size); +} + +inline void* __wrap_PR_Realloc(void* ptr, size_t size) +{ + return moz_realloc(ptr, size); +} + +inline void* __wrap_PR_Calloc(size_t num, size_t size) +{ + return moz_calloc(num, size); +} + +inline int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size) +{ + return moz_posix_memalign(memptr, alignment, size); +} diff --git a/memory/mozalloc/mozalloc.cpp b/memory/mozalloc/mozalloc.cpp index 15ab270b6c9..5cd2a00c1d2 100644 --- a/memory/mozalloc/mozalloc.cpp +++ b/memory/mozalloc/mozalloc.cpp @@ -74,7 +74,7 @@ #define UNLIKELY(x) (x) #endif -#if defined(MOZ_MEMORY_ANDROID) || defined(WRAP_MALLOC) +#if defined(MOZ_MEMORY_ANDROID) || defined(WRAP_MALLOC_WITH_JEMALLOC) #include "jemalloc.h" #define malloc(a) je_malloc(a) #define valloc(a) je_valloc(a) @@ -83,6 +83,7 @@ #define free(a) je_free(a) #define strdup(a) je_strdup(a) #define strndup(a, b) je_strndup(a, b) +#define posix_memalign(a, b, c) je_posix_memalign(a, b, c) #endif void diff --git a/memory/mozalloc/wraps.c b/memory/mozalloc/wraps.c deleted file mode 100644 index ba780a76897..00000000000 --- a/memory/mozalloc/wraps.c +++ /dev/null @@ -1,102 +0,0 @@ -#include // for size_t -#include // for malloc, free -#include "mozalloc.h" -#ifdef MOZ_MEMORY_ANDROID -#include -#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "wrap", args) -#endif -#include - -#ifdef __malloc_hook -static void* moz_malloc_hook(size_t size, const void *caller) -{ - return moz_malloc(size); -} - -static void* moz_realloc_hook(void *ptr, size_t size, const void *caller) -{ - return moz_realloc(ptr, size); -} - -static void moz_free_hook(void *ptr, const void *caller) -{ - moz_free(ptr); -} - -static void* moz_memalign_hook(size_t align, size_t size, const void *caller) -{ - return moz_memalign(align, size); -} - -static void -moz_malloc_init_hook (void) -{ - __malloc_hook = moz_malloc_hook; - __realloc_hook = moz_realloc_hook; - __free_hook = moz_free_hook; - __memalign_hook = moz_memalign_hook; -} - -/* Override initializing hook from the C library. */ -void (*__malloc_initialize_hook) (void) = moz_malloc_init_hook; -#endif - -inline void __wrap_free(void* ptr) -{ - moz_free(ptr); -} - -inline void* __wrap_malloc(size_t size) -{ - return moz_malloc(size); -} - -inline void* __wrap_realloc(void* ptr, size_t size) -{ - return moz_realloc(ptr, size); -} - -inline void* __wrap_calloc(size_t num, size_t size) -{ - return moz_calloc(num, size); -} - -inline void* __wrap_valloc(size_t size) -{ - return moz_valloc(size); -} - -inline void* __wrap_memalign(size_t align, size_t size) -{ - return moz_memalign(align, size); -} - -inline char* __wrap_strdup(char* str) -{ - return moz_strdup(str); -} - -inline char* __wrap_strndup(const char* str, size_t size) { - return moz_strndup(str, size); -} - - -inline void __wrap_PR_Free(void* ptr) -{ - moz_free(ptr); -} - -inline void* __wrap_PR_Malloc(size_t size) -{ - return moz_malloc(size); -} - -inline void* __wrap_PR_Realloc(void* ptr, size_t size) -{ - return moz_realloc(ptr, size); -} - -inline void* __wrap_PR_Calloc(size_t num, size_t size) -{ - return moz_calloc(num, size); -}