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 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 namespace base { | 24 namespace base { |
25 namespace debug { | 25 namespace debug { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 using base::allocator::AllocatorDispatch; | 29 using base::allocator::AllocatorDispatch; |
30 | 30 |
31 ThreadLocalStorage::StaticSlot g_thread_allocator_usage = TLS_INITIALIZER; | 31 ThreadLocalStorage::StaticSlot g_thread_allocator_usage = TLS_INITIALIZER; |
32 | 32 |
33 ThreadHeapUsage* const kInitializingSentinel = | 33 ThreadHeapUsage* const kInitializationOrTeardownSentinel = |
Sigurður Ásgeirsson
2017/02/01 22:21:27
I'd be tempted to use two sentinels here, as you h
Primiano Tucci (use gerrit)
2017/02/01 22:39:05
Yeah I thought to this. Honestly the only reason I
| |
34 reinterpret_cast<ThreadHeapUsage*>(-1); | 34 reinterpret_cast<ThreadHeapUsage*>(-1); |
35 | 35 |
36 bool g_heap_tracking_enabled = false; | 36 bool g_heap_tracking_enabled = false; |
37 | 37 |
38 // Forward declared as it needs to delegate memory allocation to the next | 38 // Forward declared as it needs to delegate memory allocation to the next |
39 // lower shim. | 39 // lower shim. |
40 ThreadHeapUsage* GetOrCreateThreadUsage(); | 40 ThreadHeapUsage* GetOrCreateThreadUsage(); |
41 | 41 |
42 size_t GetAllocSizeEstimate(const AllocatorDispatch* next, void* ptr) { | 42 size_t GetAllocSizeEstimate(const AllocatorDispatch* next, void* ptr) { |
43 if (ptr == nullptr) | 43 if (ptr == nullptr) |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 &FreeFn, | 167 &FreeFn, |
168 &GetSizeEstimateFn, | 168 &GetSizeEstimateFn, |
169 &BatchMallocFn, | 169 &BatchMallocFn, |
170 &BatchFreeFn, | 170 &BatchFreeFn, |
171 &FreeDefiniteSizeFn, | 171 &FreeDefiniteSizeFn, |
172 nullptr}; | 172 nullptr}; |
173 | 173 |
174 ThreadHeapUsage* GetOrCreateThreadUsage() { | 174 ThreadHeapUsage* GetOrCreateThreadUsage() { |
175 ThreadHeapUsage* allocator_usage = | 175 ThreadHeapUsage* allocator_usage = |
176 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); | 176 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); |
177 if (allocator_usage == kInitializingSentinel) | 177 if (allocator_usage == kInitializationOrTeardownSentinel) |
178 return nullptr; // Re-entrancy case. | 178 return nullptr; // Re-entrancy case. |
179 | 179 |
180 if (allocator_usage == nullptr) { | 180 if (allocator_usage == nullptr) { |
181 // Prevent reentrancy due to the allocation below. | 181 // Prevent reentrancy due to the allocation below. |
182 g_thread_allocator_usage.Set(kInitializingSentinel); | 182 g_thread_allocator_usage.Set(kInitializationOrTeardownSentinel); |
183 | 183 |
184 // Delegate the allocation of the per-thread structure to the underlying | 184 allocator_usage = new ThreadHeapUsage(); |
Sigurður Ásgeirsson
2017/02/01 22:21:27
comically this'll lead to the shim accounting its
Primiano Tucci (use gerrit)
2017/02/01 22:39:05
Hmm how? This shouldn't happen because we set the
Sigurður Ásgeirsson
2017/02/01 22:46:16
Ah, true :).
| |
185 // heap shim, for symmetry with the deallocation. Otherwise interposing | |
186 // shims may mis-attribute or mis-direct this allocation. | |
187 const AllocatorDispatch* next = allocator_dispatch.next; | |
188 allocator_usage = new (next->alloc_function(next, sizeof(ThreadHeapUsage))) | |
189 ThreadHeapUsage(); | |
190 static_assert(std::is_pod<ThreadHeapUsage>::value, | 185 static_assert(std::is_pod<ThreadHeapUsage>::value, |
191 "AllocatorDispatch must be POD"); | 186 "AllocatorDispatch must be POD"); |
192 memset(allocator_usage, 0, sizeof(*allocator_usage)); | 187 memset(allocator_usage, 0, sizeof(*allocator_usage)); |
193 g_thread_allocator_usage.Set(allocator_usage); | 188 g_thread_allocator_usage.Set(allocator_usage); |
194 } | 189 } |
195 | 190 |
196 return allocator_usage; | 191 return allocator_usage; |
197 } | 192 } |
198 | 193 |
199 } // namespace | 194 } // namespace |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 g_heap_tracking_enabled = false; | 285 g_heap_tracking_enabled = false; |
291 } | 286 } |
292 | 287 |
293 base::allocator::AllocatorDispatch* | 288 base::allocator::AllocatorDispatch* |
294 ThreadHeapUsageTracker::GetDispatchForTesting() { | 289 ThreadHeapUsageTracker::GetDispatchForTesting() { |
295 return &allocator_dispatch; | 290 return &allocator_dispatch; |
296 } | 291 } |
297 | 292 |
298 void ThreadHeapUsageTracker::EnsureTLSInitialized() { | 293 void ThreadHeapUsageTracker::EnsureTLSInitialized() { |
299 if (!g_thread_allocator_usage.initialized()) { | 294 if (!g_thread_allocator_usage.initialized()) { |
300 g_thread_allocator_usage.Initialize([](void* allocator_usage) { | 295 g_thread_allocator_usage.Initialize([](void* thread_heap_usage) { |
301 // Delegate the freeing of the per-thread structure to the next-lower | 296 // Deleting the ThreadHeapUsage TLS object will re-enter the shim and hit |
302 // heap shim. Otherwise this free will re-initialize the TLS on thread | 297 // RecordFree() above. The sentinel prevents RecordFree() from re-creating |
303 // exit. | 298 // another ThreadHeapUsage object. |
304 allocator_dispatch.next->free_function(allocator_dispatch.next, | 299 if (thread_heap_usage == kInitializationOrTeardownSentinel) |
Sigurður Ásgeirsson
2017/02/01 22:21:27
How would this ever happen?
Primiano Tucci (use gerrit)
2017/02/01 22:39:05
Ha! this is tricky but will happen all the times.
Sigurður Ásgeirsson
2017/02/01 22:46:16
Ah - perhaps this warrants a quick comment, as it'
Primiano Tucci (use gerrit)
2017/02/02 00:38:27
Done.
| |
305 allocator_usage); | 300 return; |
301 g_thread_allocator_usage.Set(kInitializationOrTeardownSentinel); | |
302 delete static_cast<ThreadHeapUsage*>(thread_heap_usage); | |
306 }); | 303 }); |
307 } | 304 } |
308 } | 305 } |
309 | 306 |
310 } // namespace debug | 307 } // namespace debug |
311 } // namespace base | 308 } // namespace base |
OLD | NEW |