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..3ae6852363d3a6d9bdf532c59395e2db0e67447d 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; |
| +MallocZoneFunctions& GetFunctionsForZone(void* zone) { |
|
Primiano Tucci (use gerrit)
2017/02/23 00:40:17
IMHO it makes more sense for this to be an inline
erikchen
2017/02/23 01:43:14
inline function in malloc_zone_functions_mac.h doe
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
sorry, I really meant "allow us to not have to acc
erikchen
2017/02/23 20:06:44
Done.
|
| + 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(); |
|
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
out of curiosity, did you use this because the com
erikchen
2017/02/23 01:43:14
yes
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
got it, sg
|
| +} |
| 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 = { |