Chromium Code Reviews| Index: base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.cc |
| diff --git a/base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.cc b/base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dc468ca413271fec22309541de7a1baa4a8e5ada |
| --- /dev/null |
| +++ b/base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/allocator/allocator_shim_default_dispatch_to_zoned_malloc.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/allocator/allocator_shim.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; |
| + |
| +template <class F, class... Args> |
| +auto CallUnshimmed(F ChromeMallocZone::*m, Args... args) |
|
Primiano Tucci (use gerrit)
2017/01/28 05:10:02
This is the part I am really missing here.
I am no
erikchen
2017/01/31 02:21:22
This is needed because ThreadHeapUsageTracker::Sta
DmitrySkiba
2017/01/31 02:33:37
Interesting. Originally I added this because in te
Primiano Tucci (use gerrit)
2017/01/31 17:40:05
Hmm. I can see how that would cause the test to fa
|
| + -> decltype(std::declval<F>()(nullptr, args...)) { |
| + struct _malloc_zone_t* zone = malloc_default_zone(); |
| + F f = reinterpret_cast<ChromeMallocZone*>(zone)->*m; |
| + return f(zone, args...); |
| +} |
| + |
| +void* MallocImpl(const AllocatorDispatch*, size_t size) { |
| + if (g_default_zone.malloc) |
| + return g_default_zone.malloc(malloc_default_zone(), size); |
| + return CallUnshimmed(&ChromeMallocZone::malloc, size); |
| +} |
| + |
| +void* CallocImpl(const AllocatorDispatch*, size_t n, size_t size) { |
| + if (g_default_zone.calloc) |
| + return g_default_zone.calloc(malloc_default_zone(), n, size); |
| + return CallUnshimmed(&ChromeMallocZone::calloc, n, size); |
| +} |
| + |
| +void* MemalignImpl(const AllocatorDispatch*, size_t alignment, size_t size) { |
| + if (g_default_zone.memalign) |
| + return g_default_zone.memalign(malloc_default_zone(), alignment, size); |
| + return CallUnshimmed(&ChromeMallocZone::memalign, alignment, size); |
| +} |
| + |
| +void* ReallocImpl(const AllocatorDispatch*, void* ptr, size_t size) { |
| + if (g_default_zone.realloc) |
| + return g_default_zone.realloc(malloc_default_zone(), ptr, size); |
| + return CallUnshimmed(&ChromeMallocZone::realloc, ptr, size); |
| +} |
| + |
| +void FreeImpl(const AllocatorDispatch*, void* ptr) { |
| + if (g_default_zone.free) { |
| + g_default_zone.free(malloc_default_zone(), ptr); |
| + return; |
| + } |
| + CallUnshimmed(&ChromeMallocZone::free, ptr); |
| +} |
| + |
| +size_t GetSizeEstimateImpl(const AllocatorDispatch*, void* ptr) { |
| + if (g_default_zone.size) { |
| + return g_default_zone.size(malloc_default_zone(), ptr); |
| + } |
| + return CallUnshimmed(&ChromeMallocZone::size, ptr); |
| +} |
| + |
| +unsigned BatchMallocImpl(const AllocatorDispatch* self, |
| + size_t size, |
| + void** results, |
| + unsigned num_requested) { |
| + if (g_default_zone.batch_malloc) |
| + return g_default_zone.batch_malloc(malloc_default_zone(), size, results, |
| + num_requested); |
| + return CallUnshimmed(&ChromeMallocZone::batch_malloc, size, results, |
| + num_requested); |
| +} |
| + |
| +void BatchFreeImpl(const AllocatorDispatch* self, |
| + void** to_be_freed, |
| + unsigned num_to_be_freed) { |
| + if (g_default_zone.batch_free) { |
| + g_default_zone.batch_free(malloc_default_zone(), to_be_freed, |
| + num_to_be_freed); |
| + return; |
| + } |
| + |
| + CallUnshimmed(&ChromeMallocZone::batch_free, to_be_freed, num_to_be_freed); |
| +} |
| + |
| +} // namespace |
| + |
| +void SetDefaultDispatchMallocZoneFunctions(MallocZoneFunctions* functions) { |
|
Primiano Tucci (use gerrit)
2017/01/28 05:10:02
this one should be just:
void InitializeDefaultDis
erikchen
2017/01/31 02:21:22
Done.
|
| + g_default_zone.malloc = functions->malloc; |
| + g_default_zone.calloc = functions->calloc; |
| + g_default_zone.memalign = functions->memalign; |
| + g_default_zone.realloc = functions->realloc; |
| + g_default_zone.free = functions->free; |
| + g_default_zone.size = functions->size; |
| + g_default_zone.batch_malloc = functions->batch_malloc; |
| + g_default_zone.batch_free = functions->batch_free; |
| +} |
| + |
| +const AllocatorDispatch AllocatorDispatch::default_dispatch = { |
| + &MallocImpl, /* alloc_function */ |
| + &CallocImpl, /* alloc_zero_initialized_function */ |
| + &MemalignImpl, /* alloc_aligned_function */ |
| + &ReallocImpl, /* realloc_function */ |
| + &FreeImpl, /* free_function */ |
| + &GetSizeEstimateImpl, /* get_size_estimate_function */ |
| + &BatchMallocImpl, /* batch_malloc_function */ |
| + &BatchFreeImpl, /* batch_free_function */ |
| + nullptr, /* next */ |
| +}; |
| + |
| +} // namespace allocator |
| +} // namespace base |