Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1651)

Unified Diff: base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc

Issue 2658723007: Hook up allocator shim on mac. (Closed)
Patch Set: Comments from primiano. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..267861d53adaebfd7735b66004422c91ebaead07
--- /dev/null
+++ b/base/allocator/allocator_shim_default_dispatch_to_mac_zoned_malloc.cc
@@ -0,0 +1,123 @@
+// 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_mac_zoned_malloc.h"
+
+#include <utility>
+
+#include "base/allocator/allocator_interception_mac.h"
+#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)
+ -> 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);
+}
+
+void FreeDefiniteSizeImpl(const AllocatorDispatch* self,
+ void* ptr,
+ size_t size) {
+ if (g_default_zone.free_definite_size) {
+ g_default_zone.free_definite_size(malloc_default_zone(), ptr, size);
+ return;
+ }
+ CallUnshimmed(&ChromeMallocZone::free_definite_size, ptr, size);
+}
+
+} // namespace
+
+void InitializeDefaultDispatchToMacAllocator() {
+ StoreFunctionsForDefaultZone(&g_default_zone);
+}
+
+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 */
+ &FreeDefiniteSizeImpl, /* free_definite_size_function */
+ nullptr, /* next */
+};
+
+} // namespace allocator
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698