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