OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef GCInfo_h | 5 #ifndef GCInfo_h |
6 #define GCInfo_h | 6 #define GCInfo_h |
7 | 7 |
8 #include "platform/heap/Visitor.h" | 8 #include "platform/heap/Visitor.h" |
| 9 #include "wtf/Allocator.h" |
9 #include "wtf/Assertions.h" | 10 #include "wtf/Assertions.h" |
10 #include "wtf/Atomics.h" | 11 #include "wtf/Atomics.h" |
11 #include "wtf/Deque.h" | 12 #include "wtf/Deque.h" |
12 #include "wtf/HashCountedSet.h" | 13 #include "wtf/HashCountedSet.h" |
13 #include "wtf/HashMap.h" | 14 #include "wtf/HashMap.h" |
14 #include "wtf/HashSet.h" | 15 #include "wtf/HashSet.h" |
15 #include "wtf/HashTable.h" | 16 #include "wtf/HashTable.h" |
16 #include "wtf/LinkedHashSet.h" | 17 #include "wtf/LinkedHashSet.h" |
17 #include "wtf/ListHashSet.h" | 18 #include "wtf/ListHashSet.h" |
18 #include "wtf/TypeTraits.h" | 19 #include "wtf/TypeTraits.h" |
19 #include "wtf/Vector.h" | 20 #include "wtf/Vector.h" |
20 | 21 |
21 namespace blink { | 22 namespace blink { |
22 | 23 |
23 // The FinalizerTraitImpl specifies how to finalize objects. Object | 24 // The FinalizerTraitImpl specifies how to finalize objects. Object |
24 // that inherit from GarbageCollectedFinalized are finalized by | 25 // that inherit from GarbageCollectedFinalized are finalized by |
25 // calling their 'finalize' method which by default will call the | 26 // calling their 'finalize' method which by default will call the |
26 // destructor on the object. | 27 // destructor on the object. |
27 template<typename T, bool isGarbageCollectedFinalized> | 28 template<typename T, bool isGarbageCollectedFinalized> |
28 struct FinalizerTraitImpl; | 29 struct FinalizerTraitImpl; |
29 | 30 |
30 template<typename T> | 31 template<typename T> |
31 struct FinalizerTraitImpl<T, true> { | 32 struct FinalizerTraitImpl<T, true> { |
| 33 STATIC_ONLY(FinalizerTraitImpl); |
32 static void finalize(void* obj) | 34 static void finalize(void* obj) |
33 { | 35 { |
34 static_assert(sizeof(T), "T must be fully defined"); | 36 static_assert(sizeof(T), "T must be fully defined"); |
35 static_cast<T*>(obj)->finalizeGarbageCollectedObject(); | 37 static_cast<T*>(obj)->finalizeGarbageCollectedObject(); |
36 }; | 38 }; |
37 }; | 39 }; |
38 | 40 |
39 template<typename T> | 41 template<typename T> |
40 struct FinalizerTraitImpl<T, false> { | 42 struct FinalizerTraitImpl<T, false> { |
| 43 STATIC_ONLY(FinalizerTraitImpl); |
41 static void finalize(void* obj) | 44 static void finalize(void* obj) |
42 { | 45 { |
43 static_assert(sizeof(T), "T must be fully defined"); | 46 static_assert(sizeof(T), "T must be fully defined"); |
44 }; | 47 }; |
45 }; | 48 }; |
46 | 49 |
47 // The FinalizerTrait is used to determine if a type requires | 50 // The FinalizerTrait is used to determine if a type requires |
48 // finalization and what finalization means. | 51 // finalization and what finalization means. |
49 // | 52 // |
50 // By default classes that inherit from GarbageCollectedFinalized need | 53 // By default classes that inherit from GarbageCollectedFinalized need |
51 // finalization and finalization means calling the 'finalize' method | 54 // finalization and finalization means calling the 'finalize' method |
52 // of the object. The FinalizerTrait can be specialized if the default | 55 // of the object. The FinalizerTrait can be specialized if the default |
53 // behavior is not desired. | 56 // behavior is not desired. |
54 template<typename T> | 57 template<typename T> |
55 struct FinalizerTrait { | 58 struct FinalizerTrait { |
| 59 STATIC_ONLY(FinalizerTrait); |
56 static const bool nonTrivialFinalizer = WTF::IsSubclassOfTemplate<typename s
td::remove_const<T>::type, GarbageCollectedFinalized>::value; | 60 static const bool nonTrivialFinalizer = WTF::IsSubclassOfTemplate<typename s
td::remove_const<T>::type, GarbageCollectedFinalized>::value; |
57 static void finalize(void* obj) { FinalizerTraitImpl<T, nonTrivialFinalizer>
::finalize(obj); } | 61 static void finalize(void* obj) { FinalizerTraitImpl<T, nonTrivialFinalizer>
::finalize(obj); } |
58 }; | 62 }; |
59 | 63 |
60 class HeapAllocator; | 64 class HeapAllocator; |
61 template<typename ValueArg, size_t inlineCapacity> class HeapListHashSetAllocato
r; | 65 template<typename ValueArg, size_t inlineCapacity> class HeapListHashSetAllocato
r; |
62 template<typename T, typename Traits> class HeapVectorBacking; | 66 template<typename T, typename Traits> class HeapVectorBacking; |
63 template<typename Table> class HeapHashTableBacking; | 67 template<typename Table> class HeapHashTableBacking; |
64 | 68 |
65 template<typename T, typename U, typename V> | 69 template<typename T, typename U, typename V> |
66 struct FinalizerTrait<LinkedHashSet<T, U, V, HeapAllocator>> { | 70 struct FinalizerTrait<LinkedHashSet<T, U, V, HeapAllocator>> { |
| 71 STATIC_ONLY(FinalizerTrait); |
67 static const bool nonTrivialFinalizer = true; | 72 static const bool nonTrivialFinalizer = true; |
68 static void finalize(void* obj) { FinalizerTraitImpl<LinkedHashSet<T, U, V,
HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } | 73 static void finalize(void* obj) { FinalizerTraitImpl<LinkedHashSet<T, U, V,
HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } |
69 }; | 74 }; |
70 | 75 |
71 template<typename T, typename Allocator> | 76 template<typename T, typename Allocator> |
72 struct FinalizerTrait<WTF::ListHashSetNode<T, Allocator>> { | 77 struct FinalizerTrait<WTF::ListHashSetNode<T, Allocator>> { |
| 78 STATIC_ONLY(FinalizerTrait); |
73 static const bool nonTrivialFinalizer = !WTF::IsTriviallyDestructible<T>::va
lue; | 79 static const bool nonTrivialFinalizer = !WTF::IsTriviallyDestructible<T>::va
lue; |
74 static void finalize(void* obj) { FinalizerTraitImpl<WTF::ListHashSetNode<T,
Allocator>, nonTrivialFinalizer>::finalize(obj); } | 80 static void finalize(void* obj) { FinalizerTraitImpl<WTF::ListHashSetNode<T,
Allocator>, nonTrivialFinalizer>::finalize(obj); } |
75 }; | 81 }; |
76 | 82 |
77 template<typename T, size_t inlineCapacity> | 83 template<typename T, size_t inlineCapacity> |
78 struct FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator>> { | 84 struct FinalizerTrait<Vector<T, inlineCapacity, HeapAllocator>> { |
| 85 STATIC_ONLY(FinalizerTrait); |
79 static const bool nonTrivialFinalizer = inlineCapacity && VectorTraits<T>::n
eedsDestruction; | 86 static const bool nonTrivialFinalizer = inlineCapacity && VectorTraits<T>::n
eedsDestruction; |
80 static void finalize(void* obj) { FinalizerTraitImpl<Vector<T, inlineCapacit
y, HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } | 87 static void finalize(void* obj) { FinalizerTraitImpl<Vector<T, inlineCapacit
y, HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } |
81 }; | 88 }; |
82 | 89 |
83 template<typename T, size_t inlineCapacity> | 90 template<typename T, size_t inlineCapacity> |
84 struct FinalizerTrait<Deque<T, inlineCapacity, HeapAllocator>> { | 91 struct FinalizerTrait<Deque<T, inlineCapacity, HeapAllocator>> { |
| 92 STATIC_ONLY(FinalizerTrait); |
85 static const bool nonTrivialFinalizer = inlineCapacity && VectorTraits<T>::n
eedsDestruction; | 93 static const bool nonTrivialFinalizer = inlineCapacity && VectorTraits<T>::n
eedsDestruction; |
86 static void finalize(void* obj) { FinalizerTraitImpl<Deque<T, inlineCapacity
, HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } | 94 static void finalize(void* obj) { FinalizerTraitImpl<Deque<T, inlineCapacity
, HeapAllocator>, nonTrivialFinalizer>::finalize(obj); } |
87 }; | 95 }; |
88 | 96 |
89 template<typename Table> | 97 template<typename Table> |
90 struct FinalizerTrait<HeapHashTableBacking<Table>> { | 98 struct FinalizerTrait<HeapHashTableBacking<Table>> { |
| 99 STATIC_ONLY(FinalizerTrait); |
91 static const bool nonTrivialFinalizer = !WTF::IsTriviallyDestructible<typena
me Table::ValueType>::value; | 100 static const bool nonTrivialFinalizer = !WTF::IsTriviallyDestructible<typena
me Table::ValueType>::value; |
92 static void finalize(void* obj) { FinalizerTraitImpl<HeapHashTableBacking<Ta
ble>, nonTrivialFinalizer>::finalize(obj); } | 101 static void finalize(void* obj) { FinalizerTraitImpl<HeapHashTableBacking<Ta
ble>, nonTrivialFinalizer>::finalize(obj); } |
93 }; | 102 }; |
94 | 103 |
95 template<typename T, typename Traits> | 104 template<typename T, typename Traits> |
96 struct FinalizerTrait<HeapVectorBacking<T, Traits>> { | 105 struct FinalizerTrait<HeapVectorBacking<T, Traits>> { |
| 106 STATIC_ONLY(FinalizerTrait); |
97 static const bool nonTrivialFinalizer = Traits::needsDestruction; | 107 static const bool nonTrivialFinalizer = Traits::needsDestruction; |
98 static void finalize(void* obj) { FinalizerTraitImpl<HeapVectorBacking<T, Tr
aits>, nonTrivialFinalizer>::finalize(obj); } | 108 static void finalize(void* obj) { FinalizerTraitImpl<HeapVectorBacking<T, Tr
aits>, nonTrivialFinalizer>::finalize(obj); } |
99 }; | 109 }; |
100 | 110 |
101 // s_gcInfoTable holds the per-class GCInfo descriptors; each heap | 111 // s_gcInfoTable holds the per-class GCInfo descriptors; each heap |
102 // object header keeps its index into this table. | 112 // object header keeps its index into this table. |
103 extern PLATFORM_EXPORT GCInfo const** s_gcInfoTable; | 113 extern PLATFORM_EXPORT GCInfo const** s_gcInfoTable; |
104 | 114 |
105 // GCInfo contains meta-data associated with objects allocated in the | 115 // GCInfo contains meta-data associated with objects allocated in the |
106 // Blink heap. This meta-data consists of a function pointer used to | 116 // Blink heap. This meta-data consists of a function pointer used to |
(...skipping 16 matching lines...) Expand all Loading... |
123 const String className() const { return m_className(); } | 133 const String className() const { return m_className(); } |
124 GetClassNameCallback m_className; | 134 GetClassNameCallback m_className; |
125 #endif | 135 #endif |
126 }; | 136 }; |
127 | 137 |
128 #if ENABLE(ASSERT) | 138 #if ENABLE(ASSERT) |
129 PLATFORM_EXPORT void assertObjectHasGCInfo(const void*, size_t gcInfoIndex); | 139 PLATFORM_EXPORT void assertObjectHasGCInfo(const void*, size_t gcInfoIndex); |
130 #endif | 140 #endif |
131 | 141 |
132 class GCInfoTable { | 142 class GCInfoTable { |
| 143 STATIC_ONLY(GCInfoTable); |
133 public: | 144 public: |
134 PLATFORM_EXPORT static void ensureGCInfoIndex(const GCInfo*, size_t*); | 145 PLATFORM_EXPORT static void ensureGCInfoIndex(const GCInfo*, size_t*); |
135 | 146 |
136 static void init(); | 147 static void init(); |
137 static void shutdown(); | 148 static void shutdown(); |
138 | 149 |
139 static size_t gcInfoIndex() { return s_gcInfoIndex; } | 150 static size_t gcInfoIndex() { return s_gcInfoIndex; } |
140 | 151 |
141 // The (max + 1) GCInfo index supported. | 152 // The (max + 1) GCInfo index supported. |
142 // We assume that 14 bits is enough to represent all possible types: during | 153 // We assume that 14 bits is enough to represent all possible types: during |
143 // telemetry runs, we see about 1000 different types, looking at the output | 154 // telemetry runs, we see about 1000 different types, looking at the output |
144 // of the oilpan gc clang plugin, there appear to be at most about 6000 | 155 // of the oilpan gc clang plugin, there appear to be at most about 6000 |
145 // types, so 14 bits should be more than twice as many bits as we will ever | 156 // types, so 14 bits should be more than twice as many bits as we will ever |
146 // encounter. | 157 // encounter. |
147 static const size_t maxIndex = 1 << 14; | 158 static const size_t maxIndex = 1 << 14; |
148 | 159 |
149 private: | 160 private: |
150 static void resize(); | 161 static void resize(); |
151 | 162 |
152 static int s_gcInfoIndex; | 163 static int s_gcInfoIndex; |
153 static size_t s_gcInfoTableSize; | 164 static size_t s_gcInfoTableSize; |
154 }; | 165 }; |
155 | 166 |
156 // GCInfotAtBaseType should be used when returning a unique 14 bit integer | 167 // GCInfotAtBaseType should be used when returning a unique 14 bit integer |
157 // for a given gcInfo. | 168 // for a given gcInfo. |
158 template<typename T> | 169 template<typename T> |
159 struct GCInfoAtBaseType { | 170 struct GCInfoAtBaseType { |
| 171 STATIC_ONLY(GCInfoAtBaseType); |
160 static size_t index() | 172 static size_t index() |
161 { | 173 { |
162 static_assert(sizeof(T), "T must be fully defined"); | 174 static_assert(sizeof(T), "T must be fully defined"); |
163 static const GCInfo gcInfo = { | 175 static const GCInfo gcInfo = { |
164 TraceTrait<T>::trace, | 176 TraceTrait<T>::trace, |
165 FinalizerTrait<T>::finalize, | 177 FinalizerTrait<T>::finalize, |
166 FinalizerTrait<T>::nonTrivialFinalizer, | 178 FinalizerTrait<T>::nonTrivialFinalizer, |
167 std::is_polymorphic<T>::value, | 179 std::is_polymorphic<T>::value, |
168 #if ENABLE(DETAILED_MEMORY_INFRA) | 180 #if ENABLE(DETAILED_MEMORY_INFRA) |
169 TypenameStringTrait<T>::get | 181 TypenameStringTrait<T>::get |
170 #endif | 182 #endif |
171 }; | 183 }; |
172 | 184 |
173 static size_t gcInfoIndex = 0; | 185 static size_t gcInfoIndex = 0; |
174 ASSERT(s_gcInfoTable); | 186 ASSERT(s_gcInfoTable); |
175 if (!acquireLoad(&gcInfoIndex)) | 187 if (!acquireLoad(&gcInfoIndex)) |
176 GCInfoTable::ensureGCInfoIndex(&gcInfo, &gcInfoIndex); | 188 GCInfoTable::ensureGCInfoIndex(&gcInfo, &gcInfoIndex); |
177 ASSERT(gcInfoIndex >= 1); | 189 ASSERT(gcInfoIndex >= 1); |
178 ASSERT(gcInfoIndex < GCInfoTable::maxIndex); | 190 ASSERT(gcInfoIndex < GCInfoTable::maxIndex); |
179 return gcInfoIndex; | 191 return gcInfoIndex; |
180 } | 192 } |
181 }; | 193 }; |
182 | 194 |
183 template<typename T, bool = WTF::IsSubclassOfTemplate<typename std::remove_const
<T>::type, GarbageCollected>::value> struct GetGarbageCollectedType; | 195 template<typename T, bool = WTF::IsSubclassOfTemplate<typename std::remove_const
<T>::type, GarbageCollected>::value> struct GetGarbageCollectedType; |
184 | 196 |
185 template<typename T> | 197 template<typename T> |
186 struct GetGarbageCollectedType<T, true> { | 198 struct GetGarbageCollectedType<T, true> { |
| 199 STATIC_ONLY(GetGarbageCollectedType); |
187 using type = typename T::GarbageCollectedType; | 200 using type = typename T::GarbageCollectedType; |
188 }; | 201 }; |
189 | 202 |
190 template<typename T> | 203 template<typename T> |
191 struct GetGarbageCollectedType<T, false> { | 204 struct GetGarbageCollectedType<T, false> { |
| 205 STATIC_ONLY(GetGarbageCollectedType); |
192 using type = T; | 206 using type = T; |
193 }; | 207 }; |
194 | 208 |
195 template<typename T> | 209 template<typename T> |
196 struct GCInfoTrait { | 210 struct GCInfoTrait { |
| 211 STATIC_ONLY(GCInfoTrait); |
197 static size_t index() | 212 static size_t index() |
198 { | 213 { |
199 return GCInfoAtBaseType<typename GetGarbageCollectedType<T>::type>::inde
x(); | 214 return GCInfoAtBaseType<typename GetGarbageCollectedType<T>::type>::inde
x(); |
200 } | 215 } |
201 }; | 216 }; |
202 | 217 |
203 template<typename U> class GCInfoTrait<const U> : public GCInfoTrait<U> { }; | 218 template<typename U> class GCInfoTrait<const U> : public GCInfoTrait<U> { }; |
204 | 219 |
205 template<typename T, typename U, typename V, typename W, typename X> class HeapH
ashMap; | 220 template<typename T, typename U, typename V, typename W, typename X> class HeapH
ashMap; |
206 template<typename T, typename U, typename V> class HeapHashSet; | 221 template<typename T, typename U, typename V> class HeapHashSet; |
(...skipping 14 matching lines...) Expand all Loading... |
221 template<typename T, size_t inlineCapacity> | 236 template<typename T, size_t inlineCapacity> |
222 struct GCInfoTrait<HeapVector<T, inlineCapacity>> : public GCInfoTrait<Vector<T,
inlineCapacity, HeapAllocator>> { }; | 237 struct GCInfoTrait<HeapVector<T, inlineCapacity>> : public GCInfoTrait<Vector<T,
inlineCapacity, HeapAllocator>> { }; |
223 template<typename T, size_t inlineCapacity> | 238 template<typename T, size_t inlineCapacity> |
224 struct GCInfoTrait<HeapDeque<T, inlineCapacity>> : public GCInfoTrait<Deque<T, i
nlineCapacity, HeapAllocator>> { }; | 239 struct GCInfoTrait<HeapDeque<T, inlineCapacity>> : public GCInfoTrait<Deque<T, i
nlineCapacity, HeapAllocator>> { }; |
225 template<typename T, typename U, typename V> | 240 template<typename T, typename U, typename V> |
226 struct GCInfoTrait<HeapHashCountedSet<T, U, V>> : public GCInfoTrait<HashCounted
Set<T, U, V, HeapAllocator>> { }; | 241 struct GCInfoTrait<HeapHashCountedSet<T, U, V>> : public GCInfoTrait<HashCounted
Set<T, U, V, HeapAllocator>> { }; |
227 | 242 |
228 } // namespace blink | 243 } // namespace blink |
229 | 244 |
230 #endif | 245 #endif |
OLD | NEW |