OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/allocator/malloc_zone_functions_mac.h" | 5 #include "base/allocator/malloc_zone_functions_mac.h" |
6 | 6 |
7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
8 #include "base/synchronization/lock.h" | 8 #include "base/synchronization/lock.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace allocator { | 11 namespace allocator { |
12 | 12 |
13 MallocZoneFunctions* g_malloc_zones = nullptr; | 13 MallocZoneFunctions g_malloc_zones[kMaxZoneCount]; |
14 MallocZoneFunctions::MallocZoneFunctions() {} | 14 static_assert(std::is_pod<MallocZoneFunctions>::value, |
| 15 "MallocZoneFunctions must be POD"); |
15 | 16 |
16 void StoreZoneFunctions(const ChromeMallocZone* zone, | 17 void StoreZoneFunctions(const ChromeMallocZone* zone, |
17 MallocZoneFunctions* functions) { | 18 MallocZoneFunctions* functions) { |
| 19 memset(functions, 0, sizeof(MallocZoneFunctions)); |
18 functions->malloc = zone->malloc; | 20 functions->malloc = zone->malloc; |
19 functions->calloc = zone->calloc; | 21 functions->calloc = zone->calloc; |
20 functions->valloc = zone->valloc; | 22 functions->valloc = zone->valloc; |
21 functions->free = zone->free; | 23 functions->free = zone->free; |
22 functions->realloc = zone->realloc; | 24 functions->realloc = zone->realloc; |
23 functions->size = zone->size; | 25 functions->size = zone->size; |
24 CHECK(functions->malloc && functions->calloc && functions->valloc && | 26 CHECK(functions->malloc && functions->calloc && functions->valloc && |
25 functions->free && functions->realloc && functions->size); | 27 functions->free && functions->realloc && functions->size); |
26 | 28 |
27 // These functions might be nullptr. | 29 // These functions might be nullptr. |
(...skipping 16 matching lines...) Expand all Loading... |
44 | 46 |
45 // All modifications to g_malloc_zones are gated behind this lock. | 47 // All modifications to g_malloc_zones are gated behind this lock. |
46 // Dispatch to a malloc zone does not need to acquire this lock. | 48 // Dispatch to a malloc zone does not need to acquire this lock. |
47 base::Lock& GetLock() { | 49 base::Lock& GetLock() { |
48 static base::Lock* g_lock = new base::Lock; | 50 static base::Lock* g_lock = new base::Lock; |
49 return *g_lock; | 51 return *g_lock; |
50 } | 52 } |
51 | 53 |
52 void EnsureMallocZonesInitializedLocked() { | 54 void EnsureMallocZonesInitializedLocked() { |
53 GetLock().AssertAcquired(); | 55 GetLock().AssertAcquired(); |
54 if (!g_malloc_zones) { | |
55 g_malloc_zones = reinterpret_cast<base::allocator::MallocZoneFunctions*>( | |
56 calloc(kMaxZoneCount, sizeof(MallocZoneFunctions))); | |
57 } | |
58 } | 56 } |
59 | 57 |
60 int g_zone_count = 0; | 58 int g_zone_count = 0; |
61 | 59 |
62 bool IsMallocZoneAlreadyStoredLocked(ChromeMallocZone* zone) { | 60 bool IsMallocZoneAlreadyStoredLocked(ChromeMallocZone* zone) { |
63 EnsureMallocZonesInitializedLocked(); | 61 EnsureMallocZonesInitializedLocked(); |
64 GetLock().AssertAcquired(); | 62 GetLock().AssertAcquired(); |
65 for (int i = 0; i < g_zone_count; ++i) { | 63 for (int i = 0; i < g_zone_count; ++i) { |
66 if (g_malloc_zones[i].context == reinterpret_cast<void*>(zone)) | 64 if (g_malloc_zones[i].context == reinterpret_cast<void*>(zone)) |
67 return true; | 65 return true; |
68 } | 66 } |
69 return false; | 67 return false; |
70 } | 68 } |
71 | 69 |
72 } // namespace | 70 } // namespace |
73 | 71 |
74 void StoreMallocZone(ChromeMallocZone* zone) { | 72 bool StoreMallocZone(ChromeMallocZone* zone) { |
75 base::AutoLock l(GetLock()); | 73 base::AutoLock l(GetLock()); |
76 EnsureMallocZonesInitializedLocked(); | 74 EnsureMallocZonesInitializedLocked(); |
77 if (IsMallocZoneAlreadyStoredLocked(zone)) | 75 if (IsMallocZoneAlreadyStoredLocked(zone)) |
78 return; | 76 return false; |
79 | 77 |
80 if (g_zone_count == kMaxZoneCount) | 78 if (g_zone_count == kMaxZoneCount) |
81 return; | 79 return false; |
82 | 80 |
83 StoreZoneFunctions(zone, &g_malloc_zones[g_zone_count]); | 81 StoreZoneFunctions(zone, &g_malloc_zones[g_zone_count]); |
84 ++g_zone_count; | 82 ++g_zone_count; |
85 | 83 |
86 // No other thread can possibly see these stores at this point. The code that | 84 // No other thread can possibly see these stores at this point. The code that |
87 // reads these values is triggered after this function returns. so we want to | 85 // reads these values is triggered after this function returns. so we want to |
88 // guarantee that they are committed at this stage" | 86 // guarantee that they are committed at this stage" |
89 base::subtle::MemoryBarrier(); | 87 base::subtle::MemoryBarrier(); |
| 88 return true; |
90 } | 89 } |
91 | 90 |
92 bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone) { | 91 bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone) { |
93 base::AutoLock l(GetLock()); | 92 base::AutoLock l(GetLock()); |
94 return IsMallocZoneAlreadyStoredLocked(zone); | 93 return IsMallocZoneAlreadyStoredLocked(zone); |
95 } | 94 } |
96 | 95 |
| 96 bool DoesMallocZoneNeedReplacing(ChromeMallocZone* zone, |
| 97 const MallocZoneFunctions* functions) { |
| 98 return IsMallocZoneAlreadyStored(zone) && zone->malloc != functions->malloc; |
| 99 } |
| 100 |
97 int GetMallocZoneCountForTesting() { | 101 int GetMallocZoneCountForTesting() { |
98 base::AutoLock l(GetLock()); | 102 base::AutoLock l(GetLock()); |
99 return g_zone_count; | 103 return g_zone_count; |
100 } | 104 } |
101 | 105 |
102 void ClearAllMallocZonesForTesting() { | 106 void ClearAllMallocZonesForTesting() { |
103 base::AutoLock l(GetLock()); | 107 base::AutoLock l(GetLock()); |
104 EnsureMallocZonesInitializedLocked(); | 108 EnsureMallocZonesInitializedLocked(); |
105 memset(g_malloc_zones, 0, kMaxZoneCount * sizeof(MallocZoneFunctions)); | 109 memset(g_malloc_zones, 0, kMaxZoneCount * sizeof(MallocZoneFunctions)); |
106 g_zone_count = 0; | 110 g_zone_count = 0; |
107 } | 111 } |
108 | 112 |
109 } // namespace allocator | 113 } // namespace allocator |
110 } // namespace base | 114 } // namespace base |
OLD | NEW |