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_aggregator_mac.h" | |
6 | |
7 #include "base/atomicops.h" | |
8 | |
9 namespace base { | |
10 namespace allocator { | |
11 | |
12 MallocZoneFunctions::MallocZoneFunctions() {} | |
13 | |
14 void StoreZoneFunctions(ChromeMallocZone* zone, | |
15 MallocZoneFunctions* functions) { | |
16 functions->malloc = zone->malloc; | |
17 functions->calloc = zone->calloc; | |
18 functions->valloc = zone->valloc; | |
19 functions->free = zone->free; | |
20 functions->realloc = zone->realloc; | |
21 functions->size = zone->size; | |
22 CHECK(functions->malloc && functions->calloc && functions->valloc && | |
23 functions->free && functions->realloc && functions->size); | |
24 | |
25 // These functions might be nullptr. | |
26 functions->batch_malloc = zone->batch_malloc; | |
27 functions->batch_free = zone->batch_free; | |
28 | |
29 if (zone->version >= 5) { | |
30 // Not all custom malloc zones have a memalign. | |
31 functions->memalign = zone->memalign; | |
32 } | |
33 if (zone->version >= 6) { | |
34 // This may be nullptr. | |
35 functions->free_definite_size = zone->free_definite_size; | |
36 } | |
37 | |
38 functions->context = zone; | |
39 } | |
40 | |
41 // static | |
42 MallocZoneAggregator* MallocZoneAggregator::GetMallocZoneAggregator() { | |
43 static MallocZoneAggregator* malloc_zone_aggregator = | |
44 new MallocZoneAggregator(); | |
45 return malloc_zone_aggregator; | |
46 } | |
47 | |
48 MallocZoneAggregator::MallocZoneAggregator() { | |
49 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
| |
50 } | |
51 MallocZoneAggregator::~MallocZoneAggregator() {} | |
52 | |
53 void* MallocZoneAggregator::DispatchMallocToZone(void* zone, size_t size) { | |
54 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
55 return functions.malloc(reinterpret_cast<struct _malloc_zone_t*>(zone), size); | |
56 } | |
57 | |
58 void* MallocZoneAggregator::DispatchCallocToZone(void* zone, | |
59 size_t num_items, | |
60 size_t size) { | |
61 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
62 return functions.calloc(reinterpret_cast<struct _malloc_zone_t*>(zone), | |
63 num_items, size); | |
64 } | |
65 void* MallocZoneAggregator::DispatchVallocToZone(void* zone, size_t size) { | |
66 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
67 return functions.valloc(reinterpret_cast<struct _malloc_zone_t*>(zone), size); | |
68 } | |
69 | |
70 void MallocZoneAggregator::DispatchFreeToZone(void* zone, void* ptr) { | |
71 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
72 functions.free(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr); | |
73 } | |
74 | |
75 void* MallocZoneAggregator::DispatchReallocToZone(void* zone, | |
76 void* ptr, | |
77 size_t size) { | |
78 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
79 return functions.realloc(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr, | |
80 size); | |
81 } | |
82 | |
83 void* MallocZoneAggregator::DispatchMemalignToZone(void* zone, | |
84 size_t alignment, | |
85 size_t size) { | |
86 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
87 return functions.memalign(reinterpret_cast<struct _malloc_zone_t*>(zone), | |
88 alignment, size); | |
89 } | |
90 | |
91 unsigned MallocZoneAggregator::DispatchBatchMallocToZone( | |
92 void* zone, | |
93 size_t size, | |
94 void** results, | |
95 unsigned num_requested) { | |
96 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
97 return functions.batch_malloc(reinterpret_cast<struct _malloc_zone_t*>(zone), | |
98 size, results, num_requested); | |
99 } | |
100 | |
101 void MallocZoneAggregator::DispatchBatchFreeToZone(void* zone, | |
102 void** to_be_freed, | |
103 unsigned num_to_be_freed) { | |
104 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
105 functions.batch_free(reinterpret_cast<struct _malloc_zone_t*>(zone), | |
106 to_be_freed, num_to_be_freed); | |
107 } | |
108 | |
109 void MallocZoneAggregator::DispatchFreeDefiniteSizeToZone(void* zone, | |
110 void* ptr, | |
111 size_t size) { | |
112 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
113 functions.free_definite_size(reinterpret_cast<struct _malloc_zone_t*>(zone), | |
114 ptr, size); | |
115 } | |
116 | |
117 size_t MallocZoneAggregator::DispatchGetSizeEstimateToZone(void* zone, | |
118 void* ptr) { | |
119 MallocZoneFunctions& functions = GetFunctionsForZone(zone); | |
120 return functions.size(reinterpret_cast<struct _malloc_zone_t*>(zone), ptr); | |
121 } | |
122 | |
123 void MallocZoneAggregator::StoreZone(ChromeMallocZone* zone) { | |
124 base::AutoLock l(lock_); | |
125 if (IsZoneAlreadyStoredLockAcquired(zone)) | |
126 return; | |
127 | |
128 if (zone_count_ == kMaxZoneCount) | |
129 return; | |
130 | |
131 StoreZoneFunctions(zone, &zones_[zone_count_]); | |
132 ++zone_count_; | |
133 base::subtle::MemoryBarrier(); | |
134 } | |
135 | |
136 bool MallocZoneAggregator::IsZoneAlreadyStored(ChromeMallocZone* zone) { | |
137 base::AutoLock l(lock_); | |
138 return IsZoneAlreadyStoredLockAcquired(zone); | |
139 } | |
140 | |
141 int MallocZoneAggregator::GetZoneCount() { | |
142 base::AutoLock l(lock_); | |
143 return zone_count_; | |
144 } | |
145 | |
146 bool MallocZoneAggregator::IsZoneAlreadyStoredLockAcquired( | |
147 ChromeMallocZone* zone) { | |
148 lock_.AssertAcquired(); | |
149 for (int i = 0; i < zone_count_; ++i) { | |
150 if (zones_[i].context == reinterpret_cast<void*>(zone)) | |
151 return true; | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 MallocZoneFunctions& MallocZoneAggregator::GetFunctionsForZone(void* zone) { | |
157 for (unsigned int i = 0; i < arraysize(zones_); ++i) { | |
158 if (zones_[i].context == zone) | |
159 return zones_[i]; | |
160 } | |
161 CHECK(false); | |
162 __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();
| |
163 } | |
164 | |
165 } // namespace allocator | |
166 } // namespace base | |
OLD | NEW |