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

Side by Side Diff: base/allocator/malloc_zone_functions_mac.cc

Issue 2703803004: macOS: Shim all malloc zones. (Closed)
Patch Set: Nits. 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/allocator/malloc_zone_functions_mac.h"
6
7 #include "base/atomicops.h"
8 #include "base/synchronization/lock.h"
9
10 namespace base {
11 namespace allocator {
12
13 MallocZoneFunctions g_malloc_zones[kMaxZoneCount];
14 MallocZoneFunctions::MallocZoneFunctions() {}
15
16 void StoreZoneFunctions(const ChromeMallocZone* zone,
17 MallocZoneFunctions* functions) {
18 functions->malloc = zone->malloc;
19 functions->calloc = zone->calloc;
20 functions->valloc = zone->valloc;
21 functions->free = zone->free;
22 functions->realloc = zone->realloc;
23 functions->size = zone->size;
24 CHECK(functions->malloc && functions->calloc && functions->valloc &&
25 functions->free && functions->realloc && functions->size);
26
27 // These functions might be nullptr.
28 functions->batch_malloc = zone->batch_malloc;
29 functions->batch_free = zone->batch_free;
30
31 if (zone->version >= 5) {
32 // Not all custom malloc zones have a memalign.
33 functions->memalign = zone->memalign;
34 }
35 if (zone->version >= 6) {
36 // This may be nullptr.
37 functions->free_definite_size = zone->free_definite_size;
38 }
39
40 functions->context = zone;
41 }
42
43 namespace {
44
45 // All modifications to g_malloc_zones are gated behind this lock.
46 // Dispatch to a malloc zone does not need to acquire this lock.
47 base::Lock* GetLock() {
Mark Mentovai 2017/02/23 02:20:12 I know I told you that I didn’t care which, but no
erikchen 2017/02/23 20:06:44 Done.
48 static base::Lock* g_lock = new base::Lock;
49 return g_lock;
50 }
51
52 int g_zone_count = 0;
53
54 bool IsMallocZoneAlreadyStoredLocked(ChromeMallocZone* zone) {
55 GetLock()->AssertAcquired();
56 for (int i = 0; i < g_zone_count; ++i) {
57 if (g_malloc_zones[i].context == reinterpret_cast<void*>(zone))
58 return true;
59 }
60 return false;
61 }
62
63 } // namespace
64
65 void StoreMallocZone(ChromeMallocZone* zone) {
66 base::AutoLock l(*GetLock());
67 if (IsMallocZoneAlreadyStoredLocked(zone))
68 return;
69
70 if (g_zone_count == kMaxZoneCount)
71 return;
72
73 StoreZoneFunctions(zone, &g_malloc_zones[g_zone_count]);
74 ++g_zone_count;
75 base::subtle::MemoryBarrier();
76 }
77
78 bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone) {
79 base::AutoLock l(*GetLock());
80 return IsMallocZoneAlreadyStoredLocked(zone);
81 }
82
83 int GetMallocZoneCountForTesting() {
84 base::AutoLock l(*GetLock());
85 return g_zone_count;
86 }
87
88 void ClearAllMallocZonesForTesting() {
89 base::AutoLock l(*GetLock());
90 memset(g_malloc_zones, 0, sizeof(g_malloc_zones));
91 g_zone_count = 0;
92 }
93
94 } // namespace allocator
95 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698