OLD | NEW |
---|---|
(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(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() { | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
do you really need all this locking here? Isn't ea
erikchen
2017/02/23 01:43:14
Actually, the new Task Scheduler doesn't guarantee
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
Well depends wheteher you use SingleThreadTaskRunn
erikchen
2017/02/23 20:06:44
There's no way to guarantee things happen on a sin
Primiano Tucci (use gerrit)
2017/02/23 21:06:07
ThreadChecker (and SequenceChecker) have zero cost
| |
48 static base::Lock* g_lock = new base::Lock; | |
49 return g_lock; | |
50 } | |
51 | |
52 int g_zone_count = 0; | |
53 bool g_zeroed = false; | |
54 | |
55 bool IsMallocZoneAlreadyStoredLockAcquired(ChromeMallocZone* zone) { | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
I think the naming pattern in the rest of the code
erikchen
2017/02/23 01:43:14
Done.
| |
56 GetLock()->AssertAcquired(); | |
57 for (int i = 0; i < g_zone_count; ++i) { | |
58 if (g_malloc_zones[i].context == reinterpret_cast<void*>(zone)) | |
59 return true; | |
60 } | |
61 return false; | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 void StoreMallocZone(ChromeMallocZone* zone) { | |
67 base::AutoLock l(*GetLock()); | |
68 if (!g_zeroed) { | |
69 g_zeroed = true; | |
70 memset(g_malloc_zones, 0, sizeof(g_malloc_zones)); | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
why you need this? global structs and pods are zer
erikchen
2017/02/23 01:43:14
This isn't pod. Notice that MallocZoneFunctions ha
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
Uhm, this means that g_malloc_zones will likely ca
erikchen
2017/02/23 20:06:44
1. Our custom clang module forces us to make a def
Primiano Tucci (use gerrit)
2017/02/23 21:06:07
Alright I see, there seems to be a problem in our
| |
71 } | |
72 if (IsMallocZoneAlreadyStoredLockAcquired(zone)) | |
73 return; | |
74 | |
75 if (g_zone_count == kMaxZoneCount) | |
76 return; | |
77 | |
78 StoreZoneFunctions(zone, &g_malloc_zones[g_zone_count]); | |
79 ++g_zone_count; | |
80 base::subtle::MemoryBarrier(); | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
This memory barrier is not enough in theory and is
erikchen
2017/02/23 01:43:14
Sorry, but I disagree on both points. Comments in
Primiano Tucci (use gerrit)
2017/02/23 11:19:41
No need to be sorry :)
Primiano Tucci (use gerrit)
2017/02/23 17:47:16
Erik and I had a chat offline, summarizing here.
T
| |
81 } | |
82 | |
83 bool IsMallocZoneAlreadyStored(ChromeMallocZone* zone) { | |
84 base::AutoLock l(*GetLock()); | |
85 return IsMallocZoneAlreadyStoredLockAcquired(zone); | |
86 } | |
87 | |
88 int GetMallocZoneCountForTesting() { | |
89 base::AutoLock l(*GetLock()); | |
90 return g_zone_count; | |
91 } | |
92 | |
93 void ClearAllMallocZonesForTesting() { | |
94 base::AutoLock l(*GetLock()); | |
95 memset(g_malloc_zones, 0, sizeof(g_malloc_zones)); | |
96 g_zone_count = 0; | |
97 g_zeroed = false; | |
Primiano Tucci (use gerrit)
2017/02/23 00:40:18
as per the comment above I don't think you need th
erikchen
2017/02/23 01:43:14
removed.
| |
98 } | |
99 | |
100 } // namespace allocator | |
101 } // namespace base | |
OLD | NEW |