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 |
133 // The allocator dispatch used to intercept heap operations. | 156 // The allocator dispatch used to intercept heap operations. |
134 AllocatorDispatch allocator_dispatch = { | 157 AllocatorDispatch allocator_dispatch = { |
135 &AllocFn, &AllocZeroInitializedFn, &AllocAlignedFn, &ReallocFn, | 158 &AllocFn, &AllocZeroInitializedFn, &AllocAlignedFn, &ReallocFn, |
136 &FreeFn, &GetSizeEstimateFn, nullptr}; | 159 &FreeFn, &GetSizeEstimateFn, &BatchMallocFn, &BatchFreeFn, |
| 160 nullptr}; |
137 | 161 |
138 ThreadHeapUsage* GetOrCreateThreadUsage() { | 162 ThreadHeapUsage* GetOrCreateThreadUsage() { |
139 ThreadHeapUsage* allocator_usage = | 163 ThreadHeapUsage* allocator_usage = |
140 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); | 164 static_cast<ThreadHeapUsage*>(g_thread_allocator_usage.Get()); |
141 if (allocator_usage == kInitializingSentinel) | 165 if (allocator_usage == kInitializingSentinel) |
142 return nullptr; // Re-entrancy case. | 166 return nullptr; // Re-entrancy case. |
143 | 167 |
144 if (allocator_usage == nullptr) { | 168 if (allocator_usage == nullptr) { |
145 // Prevent reentrancy due to the allocation below. | 169 // Prevent reentrancy due to the allocation below. |
146 g_thread_allocator_usage.Set(kInitializingSentinel); | 170 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 | 290 // heap shim. Otherwise this free will re-initialize the TLS on thread |
267 // exit. | 291 // exit. |
268 allocator_dispatch.next->free_function(allocator_dispatch.next, | 292 allocator_dispatch.next->free_function(allocator_dispatch.next, |
269 allocator_usage); | 293 allocator_usage); |
270 }); | 294 }); |
271 } | 295 } |
272 } | 296 } |
273 | 297 |
274 } // namespace debug | 298 } // namespace debug |
275 } // namespace base | 299 } // namespace base |
OLD | NEW |