Bug 833117 - Replace g_slice_set_config() with G_SLICE environment variable. r=nfroyd,r=karlt

Using g_slice_set_config() fails with newer glib because the slice allocator
now has a static constructor that runs when glib is loaded, consequently
emitting a noisy error message which confuses people into believing it's the
root of their problems.

The only way left to force the slice allocator to use "system" malloc (in
practice, jemalloc) is to set the G_SLICE environment variable to
always-malloc, and that needs to happen before glib is loaded.

Fortunately, the firefox and plugin-container executables don't depend on
glib. Unfortunately, webapprt does, so the problem remains for web apps
running through it. xpcshell and other executables that depend on libxul
directly (as opposed to loading it dynamically) are not covered either.
This commit is contained in:
Mike Hommey 2015-06-26 10:59:52 -07:00
parent 8bc87efb1e
commit 6535b630b6
3 changed files with 42 additions and 6 deletions

View File

@ -4325,12 +4325,6 @@ XREMain::XRE_main(int argc, char* argv[], const nsXREAppData* aAppData)
mozilla::IOInterposerInit ioInterposerGuard;
#if defined(MOZ_WIDGET_GTK)
#if defined(MOZ_MEMORY) || defined(__FreeBSD__) || defined(__NetBSD__)
// Disable the slice allocator, since jemalloc already uses similar layout
// algorithms, and using a sub-allocator tends to increase fragmentation.
// This must be done before g_thread_init() is called.
g_slice_set_config(G_SLICE_CONFIG_ALWAYS_MALLOC, 1);
#endif
g_thread_init(nullptr);
#endif

View File

@ -54,3 +54,6 @@ USE_LIBS += [
NO_EXPAND_LIBS = True
DIST_INSTALL = True
if CONFIG['MOZ_WIDGET_TOOLKIT'] in ('gtk2', 'gtk3'):
CXXFLAGS += CONFIG['GLIB_CFLAGS']

View File

@ -426,9 +426,48 @@ XPCOMGlueEnablePreload()
do_preload = true;
}
#ifdef MOZ_WIDGET_GTK
#include <glib.h>
class GSliceInit {
#if defined(MOZ_MEMORY) || defined(__FreeBSD__) || defined(__NetBSD__)
public:
GSliceInit() {
mHadGSlice = bool(getenv("G_SLICE"));
if (!mHadGSlice) {
// Disable the slice allocator, since jemalloc already uses similar layout
// algorithms, and using a sub-allocator tends to increase fragmentation.
// This must be done before g_thread_init() is called.
setenv("G_SLICE", "always-malloc", 1);
}
}
~GSliceInit() {
if (mHadGSlice) {
return;
}
if (sTop) {
auto g_thread_init = (void (*)(void*)) GetSymbol(sTop->libHandle,
"g_thread_init");
auto g_type_init = (void (*)()) GetSymbol(sTop->libHandle, "g_type_init");
g_thread_init(nullptr); // For GLib version < 2.32
g_type_init(); // For 2.32 <= GLib version < 2.36
}
unsetenv("G_SLICE");
}
private:
bool mHadGSlice;
#endif
};
#endif
nsresult
XPCOMGlueStartup(const char* aXPCOMFile)
{
#ifdef MOZ_WIDGET_GTK
GSliceInit gSliceInit;
#endif
xpcomFunctions.version = XPCOM_GLUE_VERSION;
xpcomFunctions.size = sizeof(XPCOMFunctions);