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

Unified Diff: base/allocator/malloc_zone_aggregator_mac.cc

Issue 2703803004: macOS: Shim all malloc zones. (Closed)
Patch Set: Minor formatting. Created 3 years, 10 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/malloc_zone_aggregator_mac.cc
diff --git a/base/allocator/malloc_zone_aggregator_mac.cc b/base/allocator/malloc_zone_aggregator_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..221021775cbbe92610ae84542aa834c4c0158f6a
--- /dev/null
+++ b/base/allocator/malloc_zone_aggregator_mac.cc
@@ -0,0 +1,166 @@
+// 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/malloc_zone_aggregator_mac.h"
+
+#include "base/atomicops.h"
+
+namespace base {
+namespace allocator {
+
+MallocZoneFunctions::MallocZoneFunctions() {}
+
+void StoreZoneFunctions(ChromeMallocZone* zone,
+ MallocZoneFunctions* functions) {
+ functions->malloc = zone->malloc;
+ functions->calloc = zone->calloc;
+ functions->valloc = zone->valloc;
+ functions->free = zone->free;
+ functions->realloc = zone->realloc;
+ functions->size = zone->size;
+ CHECK(functions->malloc && functions->calloc && functions->valloc &&
+ functions->free && functions->realloc && functions->size);
+
+ // These functions might be nullptr.
+ functions->batch_malloc = zone->batch_malloc;
+ functions->batch_free = zone->batch_free;
+
+ if (zone->version >= 5) {
+ // Not all custom malloc zones have a memalign.
+ functions->memalign = zone->memalign;
+ }
+ if (zone->version >= 6) {
+ // This may be nullptr.
+ functions->free_definite_size = zone->free_definite_size;
+ }
+
+ functions->context = zone;
+}
+
+// static
+MallocZoneAggregator* MallocZoneAggregator::GetMallocZoneAggregator() {
+ static MallocZoneAggregator* malloc_zone_aggregator =
+ new MallocZoneAggregator();
+ return malloc_zone_aggregator;
+}
+
+MallocZoneAggregator::MallocZoneAggregator() {
+ memset(zones_, 0, sizeof(zones_));
Primiano Tucci (use gerrit) 2017/02/22 12:25:39 can you add a static_assert(is_pod(zones_[0])) ?
erikchen 2017/02/22 21:03:09 MallocZoneFunctions has a non-default constructor,
Primiano Tucci (use gerrit) 2017/02/23 11:19:41 I just realized now about that that default constr
+}
+MallocZoneAggregator::~MallocZoneAggregator() {}
+
+void* MallocZoneAggregator::DispatchMallocToZone(void* zone, size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(zone), size);
+}
+
+void* MallocZoneAggregator::DispatchCallocToZone(void* zone,
+ size_t num_items,
+ size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(zone),
+ num_items, size);
+}
+void* MallocZoneAggregator::DispatchVallocToZone(void* zone, size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.valloc(reinterpret_cast<struct _malloc_zone_t*>(zone), size);
+}
+
+void MallocZoneAggregator::DispatchFreeToZone(void* zone, void* ptr) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ functions.free(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr);
+}
+
+void* MallocZoneAggregator::DispatchReallocToZone(void* zone,
+ void* ptr,
+ size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr,
+ size);
+}
+
+void* MallocZoneAggregator::DispatchMemalignToZone(void* zone,
+ size_t alignment,
+ size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(zone),
+ alignment, size);
+}
+
+unsigned MallocZoneAggregator::DispatchBatchMallocToZone(
+ void* zone,
+ size_t size,
+ void** results,
+ unsigned num_requested) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.batch_malloc(reinterpret_cast<struct _malloc_zone_t*>(zone),
+ size, results, num_requested);
+}
+
+void MallocZoneAggregator::DispatchBatchFreeToZone(void* zone,
+ void** to_be_freed,
+ unsigned num_to_be_freed) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(zone),
+ to_be_freed, num_to_be_freed);
+}
+
+void MallocZoneAggregator::DispatchFreeDefiniteSizeToZone(void* zone,
+ void* ptr,
+ size_t size) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ functions.free_definite_size(reinterpret_cast<struct _malloc_zone_t*>(zone),
+ ptr, size);
+}
+
+size_t MallocZoneAggregator::DispatchGetSizeEstimateToZone(void* zone,
+ void* ptr) {
+ MallocZoneFunctions& functions = GetFunctionsForZone(zone);
+ return functions.size(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr);
+}
+
+void MallocZoneAggregator::StoreZone(ChromeMallocZone* zone) {
+ base::AutoLock l(lock_);
+ if (IsZoneAlreadyStoredLockAcquired(zone))
+ return;
+
+ if (zone_count_ == kMaxZoneCount)
+ return;
+
+ StoreZoneFunctions(zone, &zones_[zone_count_]);
+ ++zone_count_;
+ base::subtle::MemoryBarrier();
+}
+
+bool MallocZoneAggregator::IsZoneAlreadyStored(ChromeMallocZone* zone) {
+ base::AutoLock l(lock_);
+ return IsZoneAlreadyStoredLockAcquired(zone);
+}
+
+int MallocZoneAggregator::GetZoneCount() {
+ base::AutoLock l(lock_);
+ return zone_count_;
+}
+
+bool MallocZoneAggregator::IsZoneAlreadyStoredLockAcquired(
+ ChromeMallocZone* zone) {
+ lock_.AssertAcquired();
+ for (int i = 0; i < zone_count_; ++i) {
+ if (zones_[i].context == reinterpret_cast<void*>(zone))
+ return true;
+ }
+ return false;
+}
+
+MallocZoneFunctions& MallocZoneAggregator::GetFunctionsForZone(void* zone) {
+ for (unsigned int i = 0; i < arraysize(zones_); ++i) {
+ if (zones_[i].context == zone)
+ return zones_[i];
+ }
+ CHECK(false);
+ __builtin_unreachable();
Primiano Tucci (use gerrit) 2017/02/22 12:25:39 this seems redundant, CHECK(false) will already ex
erikchen 2017/02/22 21:03:09 Switched to IMMEDIATE_CRASH();
+}
+
+} // namespace allocator
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698