| 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/debug/thread_heap_usage_tracker.h" | 5 #include "base/debug/thread_heap_usage_tracker.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <new> | 9 #include <new> |
| 10 #include <type_traits> | 10 #include <type_traits> |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 DCHECK(g_thread_allocator_usage.initialized()); | 224 DCHECK(g_thread_allocator_usage.initialized()); |
| 225 | 225 |
| 226 ThreadHeapUsage* usage = GetOrCreateThreadUsage(); | 226 ThreadHeapUsage* usage = GetOrCreateThreadUsage(); |
| 227 DCHECK_NE(nullptr, usage); | 227 DCHECK_NE(nullptr, usage); |
| 228 return *usage; | 228 return *usage; |
| 229 } | 229 } |
| 230 | 230 |
| 231 void ThreadHeapUsageTracker::EnableHeapTracking() { | 231 void ThreadHeapUsageTracker::EnableHeapTracking() { |
| 232 EnsureTLSInitialized(); | 232 EnsureTLSInitialized(); |
| 233 | 233 |
| 234 CHECK_EQ(false, g_heap_tracking_enabled) << "No double-enabling."; | 234 // No double-enabling. |
| 235 CHECK_EQ(false, g_heap_tracking_enabled); |
| 235 g_heap_tracking_enabled = true; | 236 g_heap_tracking_enabled = true; |
| 236 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 237 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 237 base::allocator::InsertAllocatorDispatch(&allocator_dispatch); | 238 base::allocator::InsertAllocatorDispatch(&allocator_dispatch); |
| 238 #else | 239 #else |
| 239 CHECK(false) << "Can't enable heap tracking without the shim."; | 240 // Can't enable heap tracking without the shim. |
| 241 CHECK(false); |
| 240 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 242 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 241 } | 243 } |
| 242 | 244 |
| 243 bool ThreadHeapUsageTracker::IsHeapTrackingEnabled() { | 245 bool ThreadHeapUsageTracker::IsHeapTrackingEnabled() { |
| 244 return g_heap_tracking_enabled; | 246 return g_heap_tracking_enabled; |
| 245 } | 247 } |
| 246 | 248 |
| 247 void ThreadHeapUsageTracker::DisableHeapTrackingForTesting() { | 249 void ThreadHeapUsageTracker::DisableHeapTrackingForTesting() { |
| 248 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 250 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 249 base::allocator::RemoveAllocatorDispatchForTesting(&allocator_dispatch); | 251 base::allocator::RemoveAllocatorDispatchForTesting(&allocator_dispatch); |
| 250 #else | 252 #else |
| 251 CHECK(false) << "Can't disable heap tracking without the shim."; | 253 // Can't disable heap tracking without the shim. |
| 254 CHECK(false); |
| 252 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 255 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 253 DCHECK_EQ(true, g_heap_tracking_enabled) << "Heap tracking not enabled."; | 256 DCHECK_EQ(true, g_heap_tracking_enabled) << "Heap tracking not enabled."; |
| 254 g_heap_tracking_enabled = false; | 257 g_heap_tracking_enabled = false; |
| 255 } | 258 } |
| 256 | 259 |
| 257 base::allocator::AllocatorDispatch* | 260 base::allocator::AllocatorDispatch* |
| 258 ThreadHeapUsageTracker::GetDispatchForTesting() { | 261 ThreadHeapUsageTracker::GetDispatchForTesting() { |
| 259 return &allocator_dispatch; | 262 return &allocator_dispatch; |
| 260 } | 263 } |
| 261 | 264 |
| 262 void ThreadHeapUsageTracker::EnsureTLSInitialized() { | 265 void ThreadHeapUsageTracker::EnsureTLSInitialized() { |
| 263 if (!g_thread_allocator_usage.initialized()) { | 266 if (!g_thread_allocator_usage.initialized()) { |
| 264 g_thread_allocator_usage.Initialize([](void* allocator_usage) { | 267 g_thread_allocator_usage.Initialize([](void* allocator_usage) { |
| 265 // Delegate the freeing of the per-thread structure to the next-lower | 268 // Delegate the freeing of the per-thread structure to the next-lower |
| 266 // heap shim. Otherwise this free will re-initialize the TLS on thread | 269 // heap shim. Otherwise this free will re-initialize the TLS on thread |
| 267 // exit. | 270 // exit. |
| 268 allocator_dispatch.next->free_function(allocator_dispatch.next, | 271 allocator_dispatch.next->free_function(allocator_dispatch.next, |
| 269 allocator_usage); | 272 allocator_usage); |
| 270 }); | 273 }); |
| 271 } | 274 } |
| 272 } | 275 } |
| 273 | 276 |
| 274 } // namespace debug | 277 } // namespace debug |
| 275 } // namespace base | 278 } // namespace base |
| OLD | NEW |