Bug 1368932 - Refactor such that there is only one definition of replace_malloc_init_funcs. r=njn
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 30 May 2017 15:57:28 +0900
changeset 361708 3f71138313da560bddc42152421cb529f4d54e0f
parent 361707 3d78fdcff608b2486ea7b75cb9b5fe31b75cc2ed
child 361709 ff2a02c2733bcb2280baf75911861321bb53d810
push id31939
push usercbook@mozilla.com
push dateThu, 01 Jun 2017 11:49:28 +0000
treeherdermozilla-central@d96110d76619 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1368932
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1368932 - Refactor such that there is only one definition of replace_malloc_init_funcs. r=njn We want, in a subsequent patch, to have replace_malloc_init_funcs be called on all platforms (including those relying on the replace-malloc library being loaded already) and perform more initialization. To prepare for that, we move the non-platform-specific pieces out.
memory/build/replace_malloc.c
--- a/memory/build/replace_malloc.c
+++ b/memory/build/replace_malloc.c
@@ -51,54 +51,68 @@ static const malloc_table_t malloc_table
 #  define MALLOC_DECL(name, return_type, ...) \
     typedef return_type (replace_ ## name ## _impl_t)(__VA_ARGS__); \
     replace_ ## name ## _impl_t *replace_ ## name = NULL;
 #  define MALLOC_FUNCS MALLOC_FUNCS_ALL
 #  include "malloc_decls.h"
 
 #  ifdef XP_WIN
 #    include <windows.h>
-static void
-replace_malloc_init_funcs()
+
+typedef HMODULE replace_malloc_handle_t;
+
+static replace_malloc_handle_t
+replace_malloc_handle()
 {
   char replace_malloc_lib[1024];
   if (GetEnvironmentVariableA("MOZ_REPLACE_MALLOC_LIB", (LPSTR)&replace_malloc_lib,
                               sizeof(replace_malloc_lib)) > 0) {
-    HMODULE handle = LoadLibraryA(replace_malloc_lib);
-    if (handle) {
-#define MALLOC_DECL(name, ...) \
-  replace_ ## name = (replace_ ## name ## _impl_t *) GetProcAddress(handle, "replace_" # name);
+    return LoadLibraryA(replace_malloc_lib);
+  }
+  return NULL;
+}
 
-#  define MALLOC_FUNCS MALLOC_FUNCS_ALL
-#include "malloc_decls.h"
-    }
-  }
-}
+#    define REPLACE_MALLOC_GET_FUNC(handle, name) \
+      (replace_ ## name ## _impl_t*) GetProcAddress(handle, "replace_" # name)
+
 #  elif defined(MOZ_WIDGET_ANDROID)
 #    include <dlfcn.h>
 #    include <stdlib.h>
+
+typedef void* replace_malloc_handle_t;
+
+static replace_malloc_handle_t
+replace_malloc_handle()
+{
+  const char *replace_malloc_lib = getenv("MOZ_REPLACE_MALLOC_LIB");
+  if (replace_malloc_lib && *replace_malloc_lib) {
+    return dlopen(replace_malloc_lib, RTLD_LAZY);
+  }
+  return NULL;
+}
+
+#    define REPLACE_MALLOC_GET_FUNC(handle, name) \
+      (replace_ ## name ## _impl_t*) dlsym(handle, "replace_" # name)
+
+#  else
+#    error No implementation for replace_malloc_handle()
+#  endif
+
 static void
 replace_malloc_init_funcs()
 {
-  const char *replace_malloc_lib = getenv("MOZ_REPLACE_MALLOC_LIB");
-  if (replace_malloc_lib && *replace_malloc_lib) {
-    void *handle = dlopen(replace_malloc_lib, RTLD_LAZY);
-    if (handle) {
-#define MALLOC_DECL(name, ...) \
-  replace_ ## name = (replace_ ## name ## _impl_t *) dlsym(handle, "replace_" # name);
+  replace_malloc_handle_t handle = replace_malloc_handle();
+  if (handle) {
+#  define MALLOC_DECL(name, ...) \
+    replace_ ## name = REPLACE_MALLOC_GET_FUNC(handle, name);
 
 #  define MALLOC_FUNCS MALLOC_FUNCS_ALL
-#include "malloc_decls.h"
-    }
+#  include "malloc_decls.h"
   }
 }
-#  else
-#    error No implementation for replace_malloc_init_funcs()
-#  endif
-
 #endif /* MOZ_NO_REPLACE_FUNC_DECL */
 
 /*
  * Below is the malloc implementation overriding jemalloc and calling the
  * replacement functions if they exist.
  */
 
 static int replace_malloc_initialized = 0;