Bug 804303 - Part 3: Build jemalloc3 as a replace-malloc library when building with mozjemalloc. r=khuey, a=jlebar
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 07 Dec 2012 09:32:24 +0100
changeset 122100 08b8b44a0813d483854f1210888da793cb04d006
parent 122099 e8e8d414a28b036a7f9cc015c5f76c9c199689f1
child 122101 051bca12fd697052086750b4088b0f8a6cde5f39
push id1997
push userakeybl@mozilla.com
push dateMon, 07 Jan 2013 21:25:26 +0000
treeherdermozilla-beta@4baf45cdcf21 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey, jlebar
bugs804303
milestone19.0a2
Bug 804303 - Part 3: Build jemalloc3 as a replace-malloc library when building with mozjemalloc. r=khuey, a=jlebar
allmakefiles.sh
configure.in
memory/Makefile.in
memory/build/mozmemory_wrap.h
memory/replace/Makefile.in
memory/replace/jemalloc/Makefile.in
memory/replace/jemalloc/pthread_atfork.c
toolkit/toolkit-tiers.mk
--- a/allmakefiles.sh
+++ b/allmakefiles.sh
@@ -54,28 +54,38 @@ if [ ! "$LIBXUL_SDK" ]; then
       build/stlport/stl/config/_android.h
     "
   fi
   add_makefiles "
     memory/mozalloc/Makefile
     mozglue/Makefile
     mozglue/build/Makefile
   "
-  if [ "$MOZ_JEMALLOC3" -a -z "$MOZ_NATIVE_JEMALLOC" ]; then
+  if [ "$MOZ_JEMALLOC3" -o "$MOZ_REPLACE_MALLOC" ] && [ -z "$MOZ_NATIVE_JEMALLOC" ]; then
     add_makefiles "
       memory/jemalloc/Makefile
     "
   fi
   if [ "$MOZ_MEMORY" ]; then
     add_makefiles "
       memory/Makefile
       memory/mozjemalloc/Makefile
       memory/build/Makefile
     "
   fi
+  if [ "$MOZ_REPLACE_MALLOC" ]; then
+    add_makefiles "
+      memory/replace/Makefile
+    "
+    if [ -z "$MOZ_JEMALLOC3" ]; then
+      add_makefiles "
+        memory/replace/jemalloc/Makefile
+      "
+    fi
+  fi
   if [ "$MOZ_REPLACE_MALLOC_LINKAGE" = "dummy library" ]; then
     add_makefiles "
       memory/replace/dummy/Makefile
     "
   fi
   if [ "$MOZ_WIDGET_TOOLKIT" = "android" ]; then
     add_makefiles "
       other-licenses/android/Makefile
--- a/configure.in
+++ b/configure.in
@@ -8959,41 +8959,51 @@ MOZ_CRASHREPORTER=${MOZ_CRASHREPORTER} \
 if cmp -s ./mozinfo.json.tmp ./mozinfo.json; then
   rm ./mozinfo.json.tmp
 else
   mv -f ./mozinfo.json.tmp ./mozinfo.json
 fi
 
 # Run jemalloc configure script
 
-if test -z "$MOZ_NATIVE_JEMALLOC" -a "$MOZ_MEMORY" -a "$MOZ_JEMALLOC3"; then
+if test -z "$MOZ_NATIVE_JEMALLOC" -a "$MOZ_MEMORY" && test -n "$MOZ_JEMALLOC3" -o -n "$MOZ_REPLACE_MALLOC"; then
   ac_configure_args="$_SUBDIR_CONFIG_ARGS --build=$build --host=$target --enable-stats --with-jemalloc-prefix=je_"
   if test -n "$MOZ_REPLACE_MALLOC"; then
     # When using replace_malloc, we always want memalign and valloc exported from jemalloc.
     ac_configure_args="$ac_configure_args ac_cv_func_memalign=yes"
     ac_configure_args="$ac_configure_args ac_cv_func_valloc=yes"
   fi
-  case "${OS_ARCH}" in
-    WINNT|Darwin)
-      # We want jemalloc functions to be kept hidden on both Mac and Windows
-      # See memory/build/mozmemory_wrap.h for details.
-      ac_configure_args="$ac_configure_args --without-export"
-      ;;
-  esac
+  if test -n "$MOZ_JEMALLOC3"; then
+    case "${OS_ARCH}" in
+      WINNT|Darwin)
+        # We want jemalloc functions to be kept hidden on both Mac and Windows
+        # See memory/build/mozmemory_wrap.h for details.
+        ac_configure_args="$ac_configure_args --without-export"
+        ;;
+    esac
+  elif test "${OS_ARCH}" = Darwin; then
+    # When building as a replace-malloc lib, disabling the zone allocator
+    # forces to use pthread_atfork.
+    ac_configure_args="$ac_configure_args --disable-zone-allocator"
+  fi
+  _MANGLE="malloc posix_memalign aligned_alloc calloc realloc free memalign valloc malloc_usable_size"
+  JEMALLOC_WRAPPER=
   if test -z "$MOZ_REPLACE_MALLOC"; then
     case "$OS_ARCH" in
       Linux|DragonFly|FreeBSD|NetBSD|OpenBSD)
-        MANGLE="malloc posix_memalign aligned_alloc calloc realloc free memalign valloc malloc_usable_size"
+        MANGLE=$_MANGLE
         ;;
     esac
+  elif test -z "$MOZ_JEMALLOC3"; then
+    MANGLE=$_MANGLE
+    JEMALLOC_WRAPPER=replace_
   fi
   if test -n "$MANGLE"; then
     MANGLED=
-    JEMALLOC_WRAPPER=
-    if test -n "$_WRAP_MALLOC"; then
+    if test -n "$_WRAP_MALLOC" -a -z "$JEMALLOC_WRAPPER"; then
       JEMALLOC_WRAPPER=__wrap_
     fi
     for mangle in ${MANGLE}; do
       if test -n "$MANGLED"; then
         MANGLED="$mangle:$JEMALLOC_WRAPPER$mangle,$MANGLED"
       else
         MANGLED="$mangle:$JEMALLOC_WRAPPER$mangle"
       fi
--- a/memory/Makefile.in
+++ b/memory/Makefile.in
@@ -6,17 +6,17 @@ DEPTH           = @DEPTH@
 topsrcdir       = @top_srcdir@
 srcdir          = @srcdir@
 VPATH           = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 DIRS += mozjemalloc
 
-ifdef MOZ_JEMALLOC3
+ifneq (,$(MOZ_JEMALLOC3)$(MOZ_REPLACE_MALLOC))
 ifndef MOZ_NATIVE_JEMALLOC
 DIRS += jemalloc
 endif
 endif
 
 DIRS += build
 
 ifeq ($(MOZ_REPLACE_MALLOC_LINKAGE),dummy library)
--- a/memory/build/mozmemory_wrap.h
+++ b/memory/build/mozmemory_wrap.h
@@ -130,17 +130,21 @@
 
 #if !defined(MOZ_NATIVE_JEMALLOC)
 #  ifdef MOZ_MEMORY_IMPL
 #    if defined(MOZ_JEMALLOC_IMPL) && defined(MOZ_REPLACE_MALLOC)
 #      define mozmem_malloc_impl(a)     je_ ## a
 #      define mozmem_jemalloc_impl(a)   je_ ## a
 #    else
 #      define MOZ_JEMALLOC_API MFBT_API
-#      if (defined(XP_WIN) || defined(XP_DARWIN))
+#      ifdef MOZ_REPLACE_JEMALLOC
+#        define MOZ_MEMORY_API MFBT_API
+#        define mozmem_malloc_impl(a)     replace_ ## a
+#        define mozmem_jemalloc_impl(a)   replace_ ## a
+#      elif (defined(XP_WIN) || defined(XP_DARWIN))
 #        if defined(MOZ_REPLACE_MALLOC)
 #          define mozmem_malloc_impl(a)   a ## _impl
 #        else
 #          define mozmem_malloc_impl(a)   je_ ## a
 #        endif
 #      else
 #        define MOZ_MEMORY_API MFBT_API
 #        if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK)
new file mode 100644
--- /dev/null
+++ b/memory/replace/Makefile.in
@@ -0,0 +1,17 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+DEPTH           = @DEPTH@
+topsrcdir       = @top_srcdir@
+srcdir          = @srcdir@
+VPATH           = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+# Build jemalloc3 as a replace-malloc lib when building with mozjemalloc
+ifndef MOZ_JEMALLOC
+DIRS += jemalloc
+endif
+
+include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/memory/replace/jemalloc/Makefile.in
@@ -0,0 +1,41 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+DEPTH           = @DEPTH@
+topsrcdir       = @top_srcdir@
+srcdir          = @srcdir@
+VPATH           = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE = memory
+LIBRARY_NAME = replace_jemalloc
+FORCE_SHARED_LIB = 1
+NO_DIST_INSTALL = 1
+
+SHARED_LIBRARY_LIBS = $(call EXPAND_LIBNAME_PATH,jemalloc,$(DEPTH)/memory/jemalloc)
+DEFINES += -DMOZ_JEMALLOC3 -DMOZ_REPLACE_JEMALLOC
+LOCAL_INCLUDES += -I../../jemalloc/src/include
+ifdef _MSC_VER
+LOCAL_INCLUDES += -I$(topsrcdir)/memory/jemalloc/src/include/msvc_compat
+endif
+
+VPATH += $(topsrcdir)/memory/build
+
+CSRCS = \
+  mozjemalloc_compat.c \
+  jemalloc_config.c \
+  $(NULL)
+
+MOZ_GLUE_LDFLAGS = # Don't link against mozglue
+WRAP_LDFLAGS = # Never wrap malloc function calls with -Wl,--wrap
+
+# Android doesn't have pthread_atfork, so just implement a dummy function.
+# It shouldn't make much problem, as the use of fork is pretty limited on
+# Android.
+ifeq ($(MOZ_WIDGET_TOOLKIT),android)
+CSRCS += pthread_atfork.c
+endif
+
+include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/memory/replace/jemalloc/pthread_atfork.c
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+int pthread_atfork(void (*prefork)(void),
+                   void (*postfork_parent)(void),
+                   void (*postfork_child)(void))
+{
+  return 0;
+}
--- a/toolkit/toolkit-tiers.mk
+++ b/toolkit/toolkit-tiers.mk
@@ -266,16 +266,20 @@ ifdef APP_LIBXUL_DIRS
 # built before libxul so it can be linked into libxul.
 tier_platform_dirs += $(APP_LIBXUL_DIRS)
 endif
 
 tier_platform_dirs += toolkit/library
 
 tier_platform_dirs += xpcom/stub
 
+ifdef MOZ_REPLACE_MALLOC
+tier_platform_dirs += memory/replace
+endif
+
 ifdef NS_TRACE_MALLOC
 tier_platform_dirs += tools/trace-malloc
 endif
 
 ifdef MOZ_ENABLE_GNOME_COMPONENT
 tier_platform_dirs    += toolkit/system/gnome
 endif