| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/allocator/allocator_shim.h" | 5 #include "base/allocator/allocator_shim.h" |
| 6 | 6 |
| 7 #include <config.h> | 7 #include <config.h> |
| 8 #include "base/profiler/alternate_timer.h" |
| 8 #include "base/sysinfo.h" | 9 #include "base/sysinfo.h" |
| 9 | 10 |
| 10 // When defined, different heap allocators can be used via an environment | 11 // When defined, different heap allocators can be used via an environment |
| 11 // variable set before running the program. This may reduce the amount | 12 // variable set before running the program. This may reduce the amount |
| 12 // of inlining that we get with malloc/free/etc. Disabling makes it | 13 // of inlining that we get with malloc/free/etc. Disabling makes it |
| 13 // so that only tcmalloc can be used. | 14 // so that only tcmalloc can be used. |
| 14 #define ENABLE_DYNAMIC_ALLOCATOR_SWITCHING | 15 #define ENABLE_DYNAMIC_ALLOCATOR_SWITCHING |
| 15 | 16 |
| 16 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth | 17 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth |
| 17 // from the "user code" so that debugging tools (HeapChecker) can work. | 18 // from the "user code" so that debugging tools (HeapChecker) can work. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 41 // specifying environment variables shown below it. | 42 // specifying environment variables shown below it. |
| 42 // See SetupSubprocessAllocator() to specify a default secondary (subprocess) | 43 // See SetupSubprocessAllocator() to specify a default secondary (subprocess) |
| 43 // allocator. | 44 // allocator. |
| 44 // TODO(jar): Switch to using TCMALLOC for the renderer as well. | 45 // TODO(jar): Switch to using TCMALLOC for the renderer as well. |
| 45 static Allocator allocator = TCMALLOC; | 46 static Allocator allocator = TCMALLOC; |
| 46 | 47 |
| 47 // The names of the environment variables that can optionally control the | 48 // The names of the environment variables that can optionally control the |
| 48 // selection of the allocator. The primary may be used to control overall | 49 // selection of the allocator. The primary may be used to control overall |
| 49 // allocator selection, and the secondary can be used to specify an allocator | 50 // allocator selection, and the secondary can be used to specify an allocator |
| 50 // to use in sub-processes. | 51 // to use in sub-processes. |
| 51 static const char* primary_name = "CHROME_ALLOCATOR"; | 52 static const char primary_name[] = "CHROME_ALLOCATOR"; |
| 52 static const char* secondary_name = "CHROME_ALLOCATOR_2"; | 53 static const char secondary_name[] = "CHROME_ALLOCATOR_2"; |
| 53 | 54 |
| 54 // We include tcmalloc and the win_allocator to get as much inlining as | 55 // We include tcmalloc and the win_allocator to get as much inlining as |
| 55 // possible. | 56 // possible. |
| 56 #include "tcmalloc.cc" | 57 #include "tcmalloc.cc" |
| 57 #include "win_allocator.cc" | 58 #include "win_allocator.cc" |
| 58 | 59 |
| 59 // Forward declarations from jemalloc. | 60 // Forward declarations from jemalloc. |
| 60 extern "C" { | 61 extern "C" { |
| 61 void* je_malloc(size_t s); | 62 void* je_malloc(size_t s); |
| 62 void* je_realloc(void* p, size_t s); | 63 void* je_realloc(void* p, size_t s); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 default: | 255 default: |
| 255 // fall through | 256 // fall through |
| 256 break; | 257 break; |
| 257 } | 258 } |
| 258 #endif | 259 #endif |
| 259 // Initializing tcmalloc. | 260 // Initializing tcmalloc. |
| 260 // We intentionally leak this object. It lasts for the process | 261 // We intentionally leak this object. It lasts for the process |
| 261 // lifetime. Trying to teardown at _heap_term() is so late that | 262 // lifetime. Trying to teardown at _heap_term() is so late that |
| 262 // you can't do anything useful anyway. | 263 // you can't do anything useful anyway. |
| 263 new TCMallocGuard(); | 264 new TCMallocGuard(); |
| 265 |
| 266 // Provide optional hook for monitoring allocation quantities on a per-thread |
| 267 // basis. Only set the hook if the environment indicates this needs to be |
| 268 // enabled. |
| 269 const char* profiling = |
| 270 GetenvBeforeMain(tracked_objects::kAlternateProfilerTime); |
| 271 if (profiling && *profiling == '1') { |
| 272 tracked_objects::SetAlternateTimeSource( |
| 273 tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread); |
| 274 } |
| 275 |
| 264 return 1; | 276 return 1; |
| 265 } | 277 } |
| 266 | 278 |
| 267 // The CRT heap cleanup stub. | 279 // The CRT heap cleanup stub. |
| 268 extern "C" void _heap_term() {} | 280 extern "C" void _heap_term() {} |
| 269 | 281 |
| 270 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 | 282 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 |
| 271 // to test whether the CRT has been initialized. Once we've ripped out | 283 // to test whether the CRT has been initialized. Once we've ripped out |
| 272 // the allocators from libcmt, we need to provide this definition so that | 284 // the allocators from libcmt, we need to provide this definition so that |
| 273 // the rest of the CRT is still usable. | 285 // the rest of the CRT is still usable. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 295 | 307 |
| 296 if (secondary_length || !primary_length) { | 308 if (secondary_length || !primary_length) { |
| 297 const char* secondary_value = secondary_length ? buffer : "TCMALLOC"; | 309 const char* secondary_value = secondary_length ? buffer : "TCMALLOC"; |
| 298 // Force renderer (or other subprocesses) to use secondary_value. | 310 // Force renderer (or other subprocesses) to use secondary_value. |
| 299 int ret_val = _putenv_s(primary_name, secondary_value); | 311 int ret_val = _putenv_s(primary_name, secondary_value); |
| 300 DCHECK_EQ(0, ret_val); | 312 DCHECK_EQ(0, ret_val); |
| 301 } | 313 } |
| 302 #endif // ENABLE_DYNAMIC_ALLOCATOR_SWITCHING | 314 #endif // ENABLE_DYNAMIC_ALLOCATOR_SWITCHING |
| 303 } | 315 } |
| 304 | 316 |
| 317 } // namespace allocator. |
| 305 } // namespace base. | 318 } // namespace base. |
| 306 } // namespace allocator. | |
| OLD | NEW |