diff --git a/memory/build/malloc_decls.h b/memory/build/malloc_decls.h index fee2b0792624..50c16176c57d 100644 --- a/memory/build/malloc_decls.h +++ b/memory/build/malloc_decls.h @@ -20,25 +20,19 @@ #define MALLOC_FUNCS_MALLOC \ (MALLOC_FUNCS_MALLOC_BASE | MALLOC_FUNCS_MALLOC_EXTRA) #define MALLOC_FUNCS_JEMALLOC 4 -#define MALLOC_FUNCS_INIT 8 -#define MALLOC_FUNCS_ARENA_BASE 16 -#define MALLOC_FUNCS_ARENA_ALLOC 32 +#define MALLOC_FUNCS_ARENA_BASE 8 +#define MALLOC_FUNCS_ARENA_ALLOC 16 #define MALLOC_FUNCS_ARENA (MALLOC_FUNCS_ARENA_BASE | MALLOC_FUNCS_ARENA_ALLOC) #define MALLOC_FUNCS_ALL \ - (MALLOC_FUNCS_INIT | MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | \ - MALLOC_FUNCS_ARENA) + (MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | MALLOC_FUNCS_ARENA) #endif // malloc_decls_h #ifndef MALLOC_FUNCS -#define MALLOC_FUNCS \ - (MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | MALLOC_FUNCS_ARENA) +#define MALLOC_FUNCS MALLOC_FUNCS_ALL #endif #ifdef MALLOC_DECL -#if MALLOC_FUNCS & MALLOC_FUNCS_INIT -MALLOC_DECL(init, void, malloc_table_t*, struct ReplaceMallocBridge**) -#endif #if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_BASE MALLOC_DECL(malloc, void*, size_t) MALLOC_DECL(calloc, void*, size_t, size_t) diff --git a/memory/build/mozjemalloc.cpp b/memory/build/mozjemalloc.cpp index faadfba45901..51d48088ed6e 100644 --- a/memory/build/mozjemalloc.cpp +++ b/memory/build/mozjemalloc.cpp @@ -4812,8 +4812,9 @@ static // the zygote. #ifdef XP_DARWIN #define MOZ_REPLACE_WEAK __attribute__((weak_import)) -#elif defined(XP_WIN) || defined(MOZ_WIDGET_ANDROID) -#define MOZ_NO_REPLACE_FUNC_DECL +#elif defined(XP_WIN) || defined(ANDROID) +#define MOZ_DYNAMIC_REPLACE_INIT +#define replace_init replace_init_decl #elif defined(__GNUC__) #define MOZ_REPLACE_WEAK __attribute__((weak)) #endif @@ -4826,12 +4827,10 @@ static malloc_table_t gReplaceMallocTable = { #include "malloc_decls.h" }; -#ifdef MOZ_NO_REPLACE_FUNC_DECL -#define MALLOC_DECL(name, return_type, ...) \ - typedef return_type(name##_impl_t)(__VA_ARGS__); \ - name##_impl_t* replace_##name = nullptr; -#define MALLOC_FUNCS MALLOC_FUNCS_INIT -#include "malloc_decls.h" +#ifdef MOZ_DYNAMIC_REPLACE_INIT +#undef replace_init +typedef decltype(replace_init_decl) replace_init_impl_t; +static replace_init_impl_t* replace_init = nullptr; #endif #ifdef XP_WIN @@ -4849,8 +4848,8 @@ replace_malloc_handle() return nullptr; } -#define REPLACE_MALLOC_GET_FUNC(handle, name) \ - (name##_impl_t*)GetProcAddress(handle, "replace_" #name) +#define REPLACE_MALLOC_GET_INIT_FUNC(handle) \ + (replace_init_impl_t*)GetProcAddress(handle, "replace_init") #elif defined(ANDROID) #include @@ -4867,8 +4866,8 @@ replace_malloc_handle() return nullptr; } -#define REPLACE_MALLOC_GET_FUNC(handle, name) \ - (name##_impl_t*)dlsym(handle, "replace_" #name) +#define REPLACE_MALLOC_GET_INIT_FUNC(handle) \ + (replace_init_impl_t*)dlsym(handle, "replace_init") #endif @@ -4882,14 +4881,10 @@ static ReplaceMallocBridge* gReplaceMallocBridge = nullptr; static void init() { -#ifdef MOZ_NO_REPLACE_FUNC_DECL +#ifdef MOZ_DYNAMIC_REPLACE_INIT 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_INIT -#include "malloc_decls.h" + replace_init = REPLACE_MALLOC_GET_INIT_FUNC(handle); } #endif diff --git a/memory/build/replace_malloc.h b/memory/build/replace_malloc.h index 8e541c4c3998..014ddfd7be08 100644 --- a/memory/build/replace_malloc.h +++ b/memory/build/replace_malloc.h @@ -80,29 +80,21 @@ MOZ_BEGIN_EXTERN_C -// MOZ_NO_REPLACE_FUNC_DECL and MOZ_REPLACE_WEAK are only defined in -// replace_malloc.c. Normally including this header will add function -// definitions. -#ifndef MOZ_NO_REPLACE_FUNC_DECL - +// MOZ_REPLACE_WEAK is only defined in mozjemalloc.cpp. Normally including +// this header will add function definitions. #ifndef MOZ_REPLACE_WEAK #define MOZ_REPLACE_WEAK #endif -// Export replace_init. -#define MALLOC_DECL(name, return_type, ...) \ - MOZ_EXPORT return_type replace_##name(__VA_ARGS__) MOZ_REPLACE_WEAK; +// Replace-malloc library initialization function. See top of this file +MOZ_EXPORT void +replace_init(malloc_table_t*, struct ReplaceMallocBridge**) MOZ_REPLACE_WEAK; -#define MALLOC_FUNCS MALLOC_FUNCS_INIT -#include "malloc_decls.h" - -// Define the remaining replace_* functions as not exported. +// Define the replace_* functions as not exported. #define MALLOC_DECL(name, return_type, ...) \ return_type replace_##name(__VA_ARGS__); #include "malloc_decls.h" -#endif // MOZ_NO_REPLACE_FUNC_DECL - MOZ_END_EXTERN_C #endif // replace_malloc_h