summaryrefslogtreecommitdiffstats
path: root/base/allocator
diff options
context:
space:
mode:
authorjar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-08 04:22:17 +0000
committerjar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-08 04:22:17 +0000
commit0df2b9dbbd732ef2bfdc9833e567ce2148b8804f (patch)
treed66b67a1d07653881bcc3459186ff29f6c175230 /base/allocator
parent7c1fbf8ec98637d818daf4e7a902152de0ac3697 (diff)
downloadchromium_src-0df2b9dbbd732ef2bfdc9833e567ce2148b8804f.zip
chromium_src-0df2b9dbbd732ef2bfdc9833e567ce2148b8804f.tar.gz
chromium_src-0df2b9dbbd732ef2bfdc9833e567ce2148b8804f.tar.bz2
Support mixed allocation in browser vs subprocesses
[This is a re-landing of CL 6623059, which broke the shared library build on windows in revision 77207. An ifdef has been added to avoid the unresolved variable when allocator_shim.cc is never even compiled] Continue to support selection of a browser allocator selection via the environment variable CHROME_ALLOCATOR, and also add CHROME_ALLOCATOR_2 that can select the allocator to use in subprocesses, such as a renderer. Temporarilly set the browser default to the default windows heap allocator, and the subprocess allocator to TCMalloc to help detect memory corruption in a cannary for a few days. This may illuminate some flakiness in the tree, as double frees (hidden by races) may cause crashes. Add minor cleanup (removing reserved word "override" etc.). BUG=74901 TBR=mbelshe Review URL: http://codereview.chromium.org/6623072 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77245 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/allocator')
-rw-r--r--base/allocator/allocator.gyp1
-rw-r--r--base/allocator/allocator_shim.cc75
-rw-r--r--base/allocator/allocator_shim.h20
3 files changed, 78 insertions, 18 deletions
diff --git a/base/allocator/allocator.gyp b/base/allocator/allocator.gyp
index 9eb96de..065cbf4 100644
--- a/base/allocator/allocator.gyp
+++ b/base/allocator/allocator.gyp
@@ -176,6 +176,7 @@
'<(jemalloc_dir)/rb.h',
'allocator_shim.cc',
+ 'allocator_shim.h',
'generic_allocators.cc',
'win_allocator.cc',
],
diff --git a/base/allocator/allocator_shim.cc b/base/allocator/allocator_shim.cc
index 3935737..f11164c 100644
--- a/base/allocator/allocator_shim.cc
+++ b/base/allocator/allocator_shim.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/allocator/allocator_shim.h"
+
#include <config.h>
// When defined, different heap allocators can be used via an environment
@@ -29,13 +31,24 @@ static int new_mode = 0;
typedef enum {
TCMALLOC, // TCMalloc is the default allocator.
- JEMALLOC, // JEMalloc
- WINDEFAULT, // Windows Heap
- WINLFH, // Windows LFH Heap
+ JEMALLOC, // JEMalloc.
+ WINHEAP, // Windows Heap (standard Windows allocator).
+ WINLFH, // Windows LFH Heap.
} Allocator;
-// This is the default allocator.
-static Allocator allocator = TCMALLOC;
+// This is the default allocator. This value can be changed at startup by
+// specifying environment variables shown below it.
+// See SetupSubprocessAllocator() to specify a default secondary (subprocess)
+// allocator.
+// TODO(jar): Switch to using TCMALLOC for the renderer as well.
+static Allocator allocator = WINHEAP;
+
+// The names of the environment variables that can optionally control the
+// selection of the allocator. The primary may be used to control overall
+// allocator selection, and the secondary can be used to specify an allocator
+// to use in sub-processes.
+static const char* primary_name = "CHROME_ALLOCATOR";
+static const char* secondary_name = "CHROME_ALLOCATOR_2";
// We include tcmalloc and the win_allocator to get as much inlining as
// possible.
@@ -101,7 +114,7 @@ void* malloc(size_t size) __THROW {
case JEMALLOC:
ptr = je_malloc(size);
break;
- case WINDEFAULT:
+ case WINHEAP:
case WINLFH:
ptr = win_heap_malloc(size);
break;
@@ -129,7 +142,7 @@ void free(void* p) __THROW {
case JEMALLOC:
je_free(p);
return;
- case WINDEFAULT:
+ case WINHEAP:
case WINLFH:
win_heap_free(p);
return;
@@ -153,7 +166,7 @@ void* realloc(void* ptr, size_t size) __THROW {
case JEMALLOC:
new_ptr = je_realloc(ptr, size);
break;
- case WINDEFAULT:
+ case WINHEAP:
case WINLFH:
new_ptr = win_heap_realloc(ptr, size);
break;
@@ -185,7 +198,7 @@ void malloc_stats(void) __THROW {
case JEMALLOC:
// No stats.
return;
- case WINDEFAULT:
+ case WINHEAP:
case WINLFH:
// No stats.
return;
@@ -201,7 +214,7 @@ extern "C" size_t _msize(void* p) {
switch (allocator) {
case JEMALLOC:
return je_msize(p);
- case WINDEFAULT:
+ case WINHEAP:
case WINLFH:
return win_heap_msize(p);
}
@@ -217,22 +230,22 @@ extern "C" intptr_t _get_heap_handle() {
// The CRT heap initialization stub.
extern "C" int _heap_init() {
#ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
- const char* override = GetenvBeforeMain("CHROME_ALLOCATOR");
- if (override) {
- if (!stricmp(override, "jemalloc"))
+ const char* environment_value = GetenvBeforeMain(primary_name);
+ if (environment_value) {
+ if (!stricmp(environment_value, "jemalloc"))
allocator = JEMALLOC;
- else if (!stricmp(override, "winheap"))
- allocator = WINDEFAULT;
- else if (!stricmp(override, "winlfh"))
+ else if (!stricmp(environment_value, "winheap"))
+ allocator = WINHEAP;
+ else if (!stricmp(environment_value, "winlfh"))
allocator = WINLFH;
- else if (!stricmp(override, "tcmalloc"))
+ else if (!stricmp(environment_value, "tcmalloc"))
allocator = TCMALLOC;
}
switch (allocator) {
case JEMALLOC:
return je_malloc_init_hard() ? 0 : 1;
- case WINDEFAULT:
+ case WINHEAP:
return win_heap_init(false) ? 1 : 0;
case WINLFH:
return win_heap_init(true) ? 1 : 0;
@@ -264,3 +277,29 @@ extern "C" void* _crtheap = reinterpret_cast<void*>(1);
#include "generic_allocators.cc"
} // extern C
+
+namespace base {
+namespace allocator {
+
+void SetupSubprocessAllocator() {
+#ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
+ size_t primary_length = 0;
+ getenv_s(&primary_length, NULL, 0, primary_name);
+
+ size_t secondary_length = 0;
+ char buffer[20];
+ getenv_s(&secondary_length, buffer, sizeof(buffer), secondary_name);
+ DCHECK_GT(sizeof(buffer), secondary_length);
+ buffer[sizeof(buffer) - 1] = '\0';
+
+ if (secondary_length || !primary_length) {
+ char* secondary_value = secondary_length ? buffer : "TCMALLOC";
+ // Force renderer (or other subprocesses) to use secondary_value.
+ int ret_val = _putenv_s(primary_name, secondary_value);
+ CHECK_EQ(0, ret_val);
+ }
+#endif // ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
+}
+
+} // namespace base.
+} // namespace allocator.
diff --git a/base/allocator/allocator_shim.h b/base/allocator/allocator_shim.h
new file mode 100644
index 0000000..342710f
--- /dev/null
+++ b/base/allocator/allocator_shim.h
@@ -0,0 +1,20 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
+#define BASE_ALLOCATOR_ALLOCATOR_SHIM_H_
+
+namespace base {
+namespace allocator {
+
+// Resets the environment variable CHROME_ALLOCATOR to specify the choice to
+// be used by subprocesses. Priority is given to the current value of
+// CHROME_ALLOCATOR_2 (if specified), then CHROME_ALLOCATOR (if specified), and
+// then a default value (typically set to TCMALLOC).
+void SetupSubprocessAllocator();
+
+} // namespace base.
+} // namespace allocator.
+
+#endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_H_