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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 void FreeFn(const AllocatorDispatch* self, void* address) { | 123 void FreeFn(const AllocatorDispatch* self, void* address) { |
124 if (address != nullptr) | 124 if (address != nullptr) |
125 RecordFree(self->next, address); | 125 RecordFree(self->next, address); |
126 self->next->free_function(self->next, address); | 126 self->next->free_function(self->next, address); |
127 } | 127 } |
128 | 128 |
129 size_t GetSizeEstimateFn(const AllocatorDispatch* self, void* address) { | 129 size_t GetSizeEstimateFn(const AllocatorDispatch* self, void* address) { |
130 return self->next->get_size_estimate_function(self->next, address); | 130 return self->next->get_size_estimate_function(self->next, address); |
131 } | 131 } |
132 | 132 |
| 133 unsigned BatchMallocFn(const AllocatorDispatch* self, |
| 134 size_t size, |
| 135 void** results, |
| 136 unsigned num_requested) { |
| 137 unsigned count = self->next->batch_malloc_function(self->next, size, results, |
| 138 num_requested); |
| 139 for (unsigned i = 0; i < count; ++i) { |
| 140 RecordAlloc(self->next, results[i], size); |
| 141 } |
| 142 return count; |
| 143 } |
| 144 |
| 145 void BatchFreeFn(const AllocatorDispatch* self, |
| 146 void** to_be_freed, |
| 147 unsigned num_to_be_freed) { |
| 148 for (unsigned i = 0; i < num_to_be_freed; ++i) { |
| 149 if (to_be_freed[i] != nullptr) { |
| 150 RecordFree(self->next, to_be_freed[i]); |
| 151 } |
| 152 } |
| 153 self->next->batch_free_function(self->next, to_be_freed, num_to_be_freed); |
| 154 } |
| 155 |
| 156 void FreeDefiniteSizeFn(const AllocatorDispatch* self, void* ptr, size_t size) { |
| 157 if (ptr != nullptr) |
| 158 RecordFree(self->next, ptr); |
| 159 self->next->free_definite_size_function(self->next, ptr, size); |
| 160 } |
| 161 |
133 // The allocator dispatch used to intercept heap operations. | 162 // The allocator dispatch used to intercept heap operations. |
134 AllocatorDispatch allocator_dispatch = { | 163 AllocatorDispatch allocator_dispatch = {&AllocFn, |
135 &AllocFn, &AllocZeroInitializedFn, &AllocAlignedFn, &ReallocFn, | 164 &AllocZeroInitializedFn, |
136 &FreeFn, &GetSizeEstimateFn, nullptr}; | 165 &AllocAlignedFn, |
| 166 &ReallocFn, |
| 167 &FreeFn, |
| 168 &GetSizeEstimateFn, |
| 169 &BatchMallocFn, |
| 170 &BatchFreeFn, |
| 171 &FreeDefiniteSizeFn, |
| 172 nullptr}; |
137 | 173 |
138 ThreadHeapUsage* GetOrCreateThreadUsage() { | 174 ThreadHeapUsage* GetOrCreateThreadUsage() { |
139 ThreadHeapUsage* allocator_usage = | 175 ThreadHeapUsage* allocator_usage = |
140 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); | 176 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); |
141 if (allocator_usage == kInitializingSentinel) | 177 if (allocator_usage == kInitializingSentinel) |
142 return nullptr; // Re-entrancy case. | 178 return nullptr; // Re-entrancy case. |
143 | 179 |
144 if (allocator_usage == nullptr) { | 180 if (allocator_usage == nullptr) { |
145 // Prevent reentrancy due to the allocation below. | 181 // Prevent reentrancy due to the allocation below. |
146 g_thread_allocator_usage.Set(kInitializingSentinel); | 182 g_thread_allocator_usage.Set(kInitializingSentinel); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // heap shim. Otherwise this free will re-initialize the TLS on thread | 302 // heap shim. Otherwise this free will re-initialize the TLS on thread |
267 // exit. | 303 // exit. |
268 allocator_dispatch.next->free_function(allocator_dispatch.next, | 304 allocator_dispatch.next->free_function(allocator_dispatch.next, |
269 allocator_usage); | 305 allocator_usage); |
270 }); | 306 }); |
271 } | 307 } |
272 } | 308 } |
273 | 309 |
274 } // namespace debug | 310 } // namespace debug |
275 } // namespace base | 311 } // namespace base |
OLD | NEW |