| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/trace_event/malloc_dump_provider.h" | 5 #include "base/trace_event/malloc_dump_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/allocator/allocator_extension.h" | 9 #include "base/allocator/allocator_extension.h" |
| 10 #include "base/allocator/allocator_shim.h" | 10 #include "base/allocator/allocator_shim.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace base { | 30 namespace base { |
| 31 namespace trace_event { | 31 namespace trace_event { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 34 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 35 | 35 |
| 36 using allocator::AllocatorDispatch; | 36 using allocator::AllocatorDispatch; |
| 37 | 37 |
| 38 void* HookAlloc(const AllocatorDispatch* self, size_t size) { | 38 void* HookAlloc(const AllocatorDispatch* self, size_t size, void* context) { |
| 39 const AllocatorDispatch* const next = self->next; | 39 const AllocatorDispatch* const next = self->next; |
| 40 void* ptr = next->alloc_function(next, size); | 40 void* ptr = next->alloc_function(next, size, context); |
| 41 if (ptr) | 41 if (ptr) |
| 42 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); | 42 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 43 return ptr; | 43 return ptr; |
| 44 } | 44 } |
| 45 | 45 |
| 46 void* HookZeroInitAlloc(const AllocatorDispatch* self, size_t n, size_t size) { | 46 void* HookZeroInitAlloc(const AllocatorDispatch* self, |
| 47 size_t n, |
| 48 size_t size, |
| 49 void* context) { |
| 47 const AllocatorDispatch* const next = self->next; | 50 const AllocatorDispatch* const next = self->next; |
| 48 void* ptr = next->alloc_zero_initialized_function(next, n, size); | 51 void* ptr = next->alloc_zero_initialized_function(next, n, size, context); |
| 49 if (ptr) | 52 if (ptr) |
| 50 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, n * size); | 53 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, n * size); |
| 51 return ptr; | 54 return ptr; |
| 52 } | 55 } |
| 53 | 56 |
| 54 void* HookllocAligned(const AllocatorDispatch* self, | 57 void* HookllocAligned(const AllocatorDispatch* self, |
| 55 size_t alignment, | 58 size_t alignment, |
| 56 size_t size) { | 59 size_t size, |
| 60 void* context) { |
| 57 const AllocatorDispatch* const next = self->next; | 61 const AllocatorDispatch* const next = self->next; |
| 58 void* ptr = next->alloc_aligned_function(next, alignment, size); | 62 void* ptr = next->alloc_aligned_function(next, alignment, size, context); |
| 59 if (ptr) | 63 if (ptr) |
| 60 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); | 64 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 61 return ptr; | 65 return ptr; |
| 62 } | 66 } |
| 63 | 67 |
| 64 void* HookRealloc(const AllocatorDispatch* self, void* address, size_t size) { | 68 void* HookRealloc(const AllocatorDispatch* self, |
| 69 void* address, |
| 70 size_t size, |
| 71 void* context) { |
| 65 const AllocatorDispatch* const next = self->next; | 72 const AllocatorDispatch* const next = self->next; |
| 66 void* ptr = next->realloc_function(next, address, size); | 73 void* ptr = next->realloc_function(next, address, size, context); |
| 67 MallocDumpProvider::GetInstance()->RemoveAllocation(address); | 74 MallocDumpProvider::GetInstance()->RemoveAllocation(address); |
| 68 if (size > 0) // realloc(size == 0) means free(). | 75 if (size > 0) // realloc(size == 0) means free(). |
| 69 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); | 76 MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 70 return ptr; | 77 return ptr; |
| 71 } | 78 } |
| 72 | 79 |
| 73 void HookFree(const AllocatorDispatch* self, void* address) { | 80 void HookFree(const AllocatorDispatch* self, void* address, void* context) { |
| 74 if (address) | 81 if (address) |
| 75 MallocDumpProvider::GetInstance()->RemoveAllocation(address); | 82 MallocDumpProvider::GetInstance()->RemoveAllocation(address); |
| 76 const AllocatorDispatch* const next = self->next; | 83 const AllocatorDispatch* const next = self->next; |
| 77 next->free_function(next, address); | 84 next->free_function(next, address, context); |
| 78 } | 85 } |
| 79 | 86 |
| 80 size_t HookGetSizeEstimate(const AllocatorDispatch* self, void* address) { | 87 size_t HookGetSizeEstimate(const AllocatorDispatch* self, |
| 88 void* address, |
| 89 void* context) { |
| 81 const AllocatorDispatch* const next = self->next; | 90 const AllocatorDispatch* const next = self->next; |
| 82 return next->get_size_estimate_function(next, address); | 91 return next->get_size_estimate_function(next, address, context); |
| 83 } | 92 } |
| 84 | 93 |
| 85 unsigned HookBatchMalloc(const AllocatorDispatch* self, | 94 unsigned HookBatchMalloc(const AllocatorDispatch* self, |
| 86 size_t size, | 95 size_t size, |
| 87 void** results, | 96 void** results, |
| 88 unsigned num_requested) { | 97 unsigned num_requested, |
| 98 void* context) { |
| 89 const AllocatorDispatch* const next = self->next; | 99 const AllocatorDispatch* const next = self->next; |
| 90 unsigned count = | 100 unsigned count = |
| 91 next->batch_malloc_function(next, size, results, num_requested); | 101 next->batch_malloc_function(next, size, results, num_requested, context); |
| 92 for (unsigned i = 0; i < count; ++i) { | 102 for (unsigned i = 0; i < count; ++i) { |
| 93 MallocDumpProvider::GetInstance()->InsertAllocation(results[i], size); | 103 MallocDumpProvider::GetInstance()->InsertAllocation(results[i], size); |
| 94 } | 104 } |
| 95 return count; | 105 return count; |
| 96 } | 106 } |
| 97 | 107 |
| 98 void HookBatchFree(const AllocatorDispatch* self, | 108 void HookBatchFree(const AllocatorDispatch* self, |
| 99 void** to_be_freed, | 109 void** to_be_freed, |
| 100 unsigned num_to_be_freed) { | 110 unsigned num_to_be_freed, |
| 111 void* context) { |
| 101 const AllocatorDispatch* const next = self->next; | 112 const AllocatorDispatch* const next = self->next; |
| 102 for (unsigned i = 0; i < num_to_be_freed; ++i) { | 113 for (unsigned i = 0; i < num_to_be_freed; ++i) { |
| 103 MallocDumpProvider::GetInstance()->RemoveAllocation(to_be_freed[i]); | 114 MallocDumpProvider::GetInstance()->RemoveAllocation(to_be_freed[i]); |
| 104 } | 115 } |
| 105 next->batch_free_function(next, to_be_freed, num_to_be_freed); | 116 next->batch_free_function(next, to_be_freed, num_to_be_freed, context); |
| 106 } | 117 } |
| 107 | 118 |
| 108 void HookFreeDefiniteSize(const AllocatorDispatch* self, | 119 void HookFreeDefiniteSize(const AllocatorDispatch* self, |
| 109 void* ptr, | 120 void* ptr, |
| 110 size_t size) { | 121 size_t size, |
| 122 void* context) { |
| 111 if (ptr) | 123 if (ptr) |
| 112 MallocDumpProvider::GetInstance()->RemoveAllocation(ptr); | 124 MallocDumpProvider::GetInstance()->RemoveAllocation(ptr); |
| 113 const AllocatorDispatch* const next = self->next; | 125 const AllocatorDispatch* const next = self->next; |
| 114 next->free_definite_size_function(next, ptr, size); | 126 next->free_definite_size_function(next, ptr, size, context); |
| 115 } | 127 } |
| 116 | 128 |
| 117 AllocatorDispatch g_allocator_hooks = { | 129 AllocatorDispatch g_allocator_hooks = { |
| 118 &HookAlloc, /* alloc_function */ | 130 &HookAlloc, /* alloc_function */ |
| 119 &HookZeroInitAlloc, /* alloc_zero_initialized_function */ | 131 &HookZeroInitAlloc, /* alloc_zero_initialized_function */ |
| 120 &HookllocAligned, /* alloc_aligned_function */ | 132 &HookllocAligned, /* alloc_aligned_function */ |
| 121 &HookRealloc, /* realloc_function */ | 133 &HookRealloc, /* realloc_function */ |
| 122 &HookFree, /* free_function */ | 134 &HookFree, /* free_function */ |
| 123 &HookGetSizeEstimate, /* get_size_estimate_function */ | 135 &HookGetSizeEstimate, /* get_size_estimate_function */ |
| 124 &HookBatchMalloc, /* batch_malloc_function */ | 136 &HookBatchMalloc, /* batch_malloc_function */ |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 tid_dumping_heap_ == PlatformThread::CurrentId()) | 363 tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 352 return; | 364 return; |
| 353 AutoLock lock(allocation_register_lock_); | 365 AutoLock lock(allocation_register_lock_); |
| 354 if (!allocation_register_) | 366 if (!allocation_register_) |
| 355 return; | 367 return; |
| 356 allocation_register_->Remove(address); | 368 allocation_register_->Remove(address); |
| 357 } | 369 } |
| 358 | 370 |
| 359 } // namespace trace_event | 371 } // namespace trace_event |
| 360 } // namespace base | 372 } // namespace base |
| OLD | NEW |