Skip to content

Commit

Permalink
allocator: add use_experimental_allocator_shim build flag
Browse files Browse the repository at this point in the history
This CL introduces a new GYP/GN build flag
use_experimental_allocator_shim (default: false).
After this CL, the only side-effect of that flag is that tcmalloc
does not override anymore the libc symbols (malloc, new etc.).
This is in preparation of a unified shim layer, which will come
separately in upcoming CLs, which will take care of overriding
those symbols in Chrome.

Design doc: http://bit.ly/allocator-shim

BUG=550886

Review URL: https://codereview.chromium.org/1678893002

Cr-Commit-Position: refs/heads/master@{#374449}
  • Loading branch information
primiano authored and Commit bot committed Feb 9, 2016
1 parent fc37513 commit 3217078
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion base/allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ config("allocator_shim_define") {
}

config("tcmalloc_flags") {
defines = []
if (enable_debugallocation) {
defines = [
defines += [
# Use debugallocation for Debug builds to catch problems early
# and cleanly, http://crbug.com/30715 .
"TCMALLOC_FOR_DEBUGALLOCATION",
]
}
if (use_experimental_allocator_shim) {
defines += [ "TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC" ]
}
if (is_clang) {
cflags = [
# tcmalloc initializes some fields in the wrong order.
Expand Down
6 changes: 6 additions & 0 deletions base/allocator/allocator.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# e.g. for profiling (it's more rare to profile Debug builds,
# but people sometimes need to do that).
'disable_debugallocation%': 0,
'use_experimental_allocator_shim%': 0,
},
'targets': [
# The only targets that should depend on allocator are 'base' and
Expand Down Expand Up @@ -340,6 +341,11 @@
'<(tcmalloc_dir)/src/profiler.cc',
],
}],
['use_experimental_allocator_shim==1', {
'defines': [
'TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC',
],
}]
],
'configurations': {
'Debug_Base': {
Expand Down
7 changes: 7 additions & 0 deletions build/config/allocator.gni
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ if (is_android || current_cpu == "mipsel" || is_mac || is_ios || is_asan ||
declare_args() {
# Memory allocator to use. Set to "none" to use default allocator.
use_allocator = _default_allocator

# TODO(primiano): this should just become the default without having a flag,
# but we need to get there first. http://crbug.com/550886 .
# Causes all the allocations to be routed via allocator_shim.cc.
use_experimental_allocator_shim = false
}

assert(use_allocator == "none" || use_allocator == "tcmalloc")

assert(!is_win || use_allocator == "none", "Tcmalloc doesn't work on Windows.")
assert(!use_experimental_allocator_shim || is_linux,
"use_experimental_allocator_shim supported only on Linux")
1 change: 1 addition & 0 deletions third_party/tcmalloc/README.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ Modifications:
- Conditionally define HAVE_VDSO_SUPPORT only on linux_x86 to avoid static initializers
- Add TC_MALLOPT_IS_OVERRIDDEN_BY_TCMALLOC mallopt() arg
- Added tc_malloc_skip_new_handler.
- Added TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC which bypasses the libc_override logic.
12 changes: 11 additions & 1 deletion third_party/tcmalloc/chromium/src/libc_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,22 @@

static void ReplaceSystemAlloc(); // defined in the .h files below

#if defined(TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC)
// TCMALLOC_DONT_REPLACE_SYSTEM_ALLOC has the following semantic:
// - tcmalloc with all its tc_* (tc_malloc, tc_free) symbols is being built
// and linked as usual.
// - the default system allocator symbols (malloc, free, operator new) are NOT
// overridden. The embedded must take care of routing them to tc_* symbols.
// This no-op #if block effectively prevents the inclusion of the
// libc_override_* headers below.
static void ReplaceSystemAlloc() {}

// For windows, there are two ways to get tcmalloc. If we're
// patching, then src/windows/patch_function.cc will do the necessary
// overriding here. Otherwise, we doing the 'redefine' trick, where
// we remove malloc/new/etc from mscvcrt.dll, and just need to define
// them now.
#if defined(_WIN32) && defined(WIN32_DO_PATCHING)
#elif defined(_WIN32) && defined(WIN32_DO_PATCHING)
void PatchWindowsFunctions(); // in src/windows/patch_function.cc
static void ReplaceSystemAlloc() { PatchWindowsFunctions(); }

Expand Down

0 comments on commit 3217078

Please sign in to comment.