Chromium Code Reviews| Index: base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc |
| diff --git a/base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc b/base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc |
| index aabda1950cb290a5b2659e7a370551aea9613f1a..2851b4b77e4f9f5ef03e6a293de58f9bf52498cc 100644 |
| --- a/base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc |
| +++ b/base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc |
| @@ -8,54 +8,62 @@ |
| #include "base/allocator/allocator_interception_mac.h" |
| #include "base/allocator/allocator_shim.h" |
| +#include "base/allocator/malloc_zone_functions_mac.h" |
| #include "base/logging.h" |
| namespace base { |
| namespace allocator { |
| - |
| namespace { |
| -// This is the zone that the allocator shim will call to actually perform heap |
| -// allocations. It should be populated with the original, unintercepted default |
| -// malloc zone. |
| -MallocZoneFunctions g_default_zone; |
| +inline MallocZoneFunctions& GetFunctionsForZone(void* zone) { |
| + for (unsigned int i = 0; i < arraysize(g_malloc_zones); ++i) { |
| + if (g_malloc_zones[i].context == zone) |
| + return g_malloc_zones[i]; |
| + } |
| + IMMEDIATE_CRASH(); |
|
Mark Mentovai
2017/02/23 02:20:12
Too unsafe to log something if this goes bad?
erikchen
2017/02/23 20:06:44
right - we're about to cause a malloc-related func
|
| +} |
| void* MallocImpl(const AllocatorDispatch*, size_t size, void* context) { |
| - return g_default_zone.malloc( |
| - reinterpret_cast<struct _malloc_zone_t*>(context), size); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
| + size); |
| } |
| void* CallocImpl(const AllocatorDispatch*, |
| size_t n, |
| size_t size, |
| void* context) { |
| - return g_default_zone.calloc( |
| - reinterpret_cast<struct _malloc_zone_t*>(context), n, size); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(context), n, |
| + size); |
| } |
| void* MemalignImpl(const AllocatorDispatch*, |
| size_t alignment, |
| size_t size, |
| void* context) { |
| - return g_default_zone.memalign( |
| - reinterpret_cast<struct _malloc_zone_t*>(context), alignment, size); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(context), |
| + alignment, size); |
| } |
| void* ReallocImpl(const AllocatorDispatch*, |
| void* ptr, |
| size_t size, |
| void* context) { |
| - return g_default_zone.realloc( |
| - reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(context), |
| + ptr, size); |
| } |
| void FreeImpl(const AllocatorDispatch*, void* ptr, void* context) { |
| - g_default_zone.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + functions.free(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
| } |
| size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr, void* context) { |
| - return g_default_zone.size(reinterpret_cast<struct _malloc_zone_t*>(context), |
| - ptr); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.size(reinterpret_cast<struct _malloc_zone_t*>(context), ptr); |
| } |
| unsigned BatchMallocImpl(const AllocatorDispatch* self, |
| @@ -63,7 +71,8 @@ unsigned BatchMallocImpl(const AllocatorDispatch* self, |
| void** results, |
| unsigned num_requested, |
| void* context) { |
| - return g_default_zone.batch_malloc( |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + return functions.batch_malloc( |
| reinterpret_cast<struct _malloc_zone_t*>(context), size, results, |
| num_requested); |
| } |
| @@ -72,22 +81,24 @@ void BatchFreeImpl(const AllocatorDispatch* self, |
| void** to_be_freed, |
| unsigned num_to_be_freed, |
| void* context) { |
| - g_default_zone.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), |
| - to_be_freed, num_to_be_freed); |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(context), |
| + to_be_freed, num_to_be_freed); |
| } |
| void FreeDefiniteSizeImpl(const AllocatorDispatch* self, |
| void* ptr, |
| size_t size, |
| void* context) { |
| - g_default_zone.free_definite_size( |
| + MallocZoneFunctions& functions = GetFunctionsForZone(context); |
| + functions.free_definite_size( |
| reinterpret_cast<struct _malloc_zone_t*>(context), ptr, size); |
| } |
| } // namespace |
| void InitializeDefaultDispatchToMacAllocator() { |
| - StoreFunctionsForDefaultZone(&g_default_zone); |
| + StoreFunctionsForAllZones(); |
| } |
| const AllocatorDispatch AllocatorDispatch::default_dispatch = { |