OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <new> | 9 #include <new> |
10 | 10 |
11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/process/process_metrics.h" | 14 #include "base/process/process_metrics.h" |
15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 | 17 |
18 #if !defined(OS_WIN) | 18 #if !defined(OS_WIN) |
19 #include <unistd.h> | 19 #include <unistd.h> |
20 #else | 20 #else |
21 #include "base/allocator/winheap_stubs_win.h" | 21 #include "base/allocator/winheap_stubs_win.h" |
22 #endif | 22 #endif |
23 | 23 |
24 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
25 #include <malloc/malloc.h> | 25 #include <malloc/malloc.h> |
26 | 26 |
27 #include "base/allocator/allocator_interception_mac.h" | 27 #include "base/allocator/allocator_interception_mac.h" |
| 28 #include "base/base_switches.h" |
| 29 #include "base/command_line.h" |
28 #endif | 30 #endif |
29 | 31 |
30 // No calls to malloc / new in this file. They would would cause re-entrancy of | 32 // No calls to malloc / new in this file. They would would cause re-entrancy of |
31 // the shim, which is hard to deal with. Keep this code as simple as possible | 33 // the shim, which is hard to deal with. Keep this code as simple as possible |
32 // and don't use any external C++ object here, not even //base ones. Even if | 34 // and don't use any external C++ object here, not even //base ones. Even if |
33 // they are safe to use today, in future they might be refactored. | 35 // they are safe to use today, in future they might be refactored. |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 using namespace base; | 39 using namespace base; |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 #if defined(OS_MACOSX) | 323 #if defined(OS_MACOSX) |
322 namespace base { | 324 namespace base { |
323 namespace allocator { | 325 namespace allocator { |
324 void InitializeAllocatorShim() { | 326 void InitializeAllocatorShim() { |
325 // Prepares the default dispatch. After the intercepted malloc calls have | 327 // Prepares the default dispatch. After the intercepted malloc calls have |
326 // traversed the shim this will route them to the default malloc zone. | 328 // traversed the shim this will route them to the default malloc zone. |
327 InitializeDefaultDispatchToMacAllocator(); | 329 InitializeDefaultDispatchToMacAllocator(); |
328 | 330 |
329 MallocZoneFunctions functions = MallocZoneFunctionsToReplaceDefault(); | 331 MallocZoneFunctions functions = MallocZoneFunctionsToReplaceDefault(); |
330 | 332 |
| 333 // Intercept and override newly registered malloc zones. |
| 334 if (CommandLine::InitializedForCurrentProcess() && |
| 335 CommandLine::ForCurrentProcess()->HasSwitch( |
| 336 switches::kEnableHeapProfiling)) { |
| 337 InterceptNewlyRegisteredMallocZones(&functions); |
| 338 } |
| 339 |
331 // This replaces the default malloc zone, causing calls to malloc & friends | 340 // This replaces the default malloc zone, causing calls to malloc & friends |
332 // from the codebase to be routed to ShimMalloc() above. | 341 // from the codebase to be routed to ShimMalloc() above. |
333 base::allocator::ReplaceFunctionsForStoredZones(&functions); | 342 base::allocator::ReplaceFunctionsForStoredZones(&functions); |
334 } | 343 } |
335 } // namespace allocator | 344 } // namespace allocator |
336 } // namespace base | 345 } // namespace base |
337 #endif | 346 #endif |
338 | 347 |
339 // Cross-checks. | 348 // Cross-checks. |
340 | 349 |
341 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 350 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
342 #error The allocator shim should not be compiled when building for memory tools. | 351 #error The allocator shim should not be compiled when building for memory tools. |
343 #endif | 352 #endif |
344 | 353 |
345 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \ | 354 #if (defined(__GNUC__) && defined(__EXCEPTIONS)) || \ |
346 (defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS) | 355 (defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS) |
347 #error This code cannot be used when exceptions are turned on. | 356 #error This code cannot be used when exceptions are turned on. |
348 #endif | 357 #endif |
OLD | NEW |