Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: third_party/WebKit/Source/platform/heap/Member.h

Issue 1999363002: Split out Members, Persistents and SelfKeepAlive in separate headers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sof-gc-type
Patch Set: rebased Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #ifndef Member_h
6 #define Member_h
7
8 #include "wtf/Allocator.h"
9 #include "wtf/HashFunctions.h"
10
11 namespace blink {
12
13 template<typename T> class Persistent;
14
15 // Members are used in classes to contain strong pointers to other oilpan heap
16 // allocated objects.
17 // All Member fields of a class must be traced in the class' trace method.
18 // During the mark phase of the GC all live objects are marked as live and
19 // all Member fields of a live object will be traced marked as live as well.
20 template<typename T>
21 class Member {
22 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
23 public:
24 Member() : m_raw(nullptr)
25 {
26 }
27
28 Member(std::nullptr_t) : m_raw(nullptr)
29 {
30 }
31
32 Member(T* raw) : m_raw(raw)
33 {
34 checkPointer();
35 }
36
37 explicit Member(T& raw) : m_raw(&raw)
38 {
39 checkPointer();
40 }
41
42 Member(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(-1))
43 {
44 }
45
46 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>( -1); }
47
48 Member(const Member& other) : m_raw(other)
49 {
50 checkPointer();
51 }
52
53 template<typename U>
54 Member(const Persistent<U>& other)
55 {
56 m_raw = other;
57 checkPointer();
58 }
59
60 template<typename U>
61 Member(const Member<U>& other) : m_raw(other)
62 {
63 checkPointer();
64 }
65
66 T* release()
67 {
68 T* result = m_raw;
69 m_raw = nullptr;
70 return result;
71 }
72
73 explicit operator bool() const { return m_raw; }
74
75 operator T*() const { return m_raw; }
76
77 T* operator->() const { return m_raw; }
78 T& operator*() const { return *m_raw; }
79
80 template<typename U>
81 Member& operator=(const Persistent<U>& other)
82 {
83 m_raw = other;
84 checkPointer();
85 return *this;
86 }
87
88 template<typename U>
89 Member& operator=(const Member<U>& other)
90 {
91 m_raw = other;
92 checkPointer();
93 return *this;
94 }
95
96 template<typename U>
97 Member& operator=(U* other)
98 {
99 m_raw = other;
100 checkPointer();
101 return *this;
102 }
103
104 Member& operator=(std::nullptr_t)
105 {
106 m_raw = nullptr;
107 return *this;
108 }
109
110 void swap(Member<T>& other)
111 {
112 std::swap(m_raw, other.m_raw);
113 checkPointer();
114 }
115
116 T* get() const { return m_raw; }
117
118 void clear() { m_raw = nullptr; }
119
120
121 protected:
122 void checkPointer()
123 {
124 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER)
125 if (!m_raw)
126 return;
127 // HashTable can store a special value (which is not aligned to the
128 // allocation granularity) to Member<> to represent a deleted entry.
129 // Thus we treat a pointer that is not aligned to the granularity
130 // as a valid pointer.
131 if (reinterpret_cast<intptr_t>(m_raw) % allocationGranularity)
132 return;
133
134 // TODO(haraken): What we really want to check here is that the pointer
135 // is a traceable object. In other words, the pointer is either of:
136 //
137 // (a) a pointer to the head of an on-heap object.
138 // (b) a pointer to the head of an on-heap mixin object.
139 //
140 // We can check it by calling ThreadHeap::isHeapObjectAlive(m_raw),
141 // but we cannot call it here because it requires to include T.h.
142 // So we currently only try to implement the check for (a), but do
143 // not insist that T's definition is in scope.
144 if (IsFullyDefined<T>::value && !IsGarbageCollectedMixin<T>::value)
145 ASSERT(HeapObjectHeader::fromPayload(m_raw)->checkHeader());
146 #endif
147 }
148
149 T* m_raw;
150
151 template<bool x, WTF::WeakHandlingFlag y, WTF::ShouldWeakPointersBeMarkedStr ongly z, typename U, typename V> friend struct CollectionBackingTraceTrait;
152 friend class Visitor;
153
154 };
155
156 // WeakMember is similar to Member in that it is used to point to other oilpan
157 // heap allocated objects.
158 // However instead of creating a strong pointer to the object, the WeakMember cr eates
159 // a weak pointer, which does not keep the pointee alive. Hence if all pointers to
160 // to a heap allocated object are weak the object will be garbage collected. At the
161 // time of GC the weak pointers will automatically be set to null.
162 template<typename T>
163 class WeakMember : public Member<T> {
164 public:
165 WeakMember() : Member<T>() { }
166
167 WeakMember(std::nullptr_t) : Member<T>(nullptr) { }
168
169 WeakMember(T* raw) : Member<T>(raw) { }
170
171 WeakMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { }
172
173 template<typename U>
174 WeakMember(const Persistent<U>& other) : Member<T>(other) { }
175
176 template<typename U>
177 WeakMember(const Member<U>& other) : Member<T>(other) { }
178
179 template<typename U>
180 WeakMember& operator=(const Persistent<U>& other)
181 {
182 this->m_raw = other;
183 this->checkPointer();
184 return *this;
185 }
186
187 template<typename U>
188 WeakMember& operator=(const Member<U>& other)
189 {
190 this->m_raw = other;
191 this->checkPointer();
192 return *this;
193 }
194
195 template<typename U>
196 WeakMember& operator=(U* other)
197 {
198 this->m_raw = other;
199 this->checkPointer();
200 return *this;
201 }
202
203 WeakMember& operator=(std::nullptr_t)
204 {
205 this->m_raw = nullptr;
206 return *this;
207 }
208
209 private:
210 T** cell() const { return const_cast<T**>(&this->m_raw); }
211
212 template<typename Derived> friend class VisitorHelper;
213 };
214
215 // UntracedMember is a pointer to an on-heap object that is not traced for some
216 // reason. Please don't use this unless you understand what you're doing.
217 // Basically, all pointers to on-heap objects must be stored in either of
218 // Persistent, Member or WeakMember. It is not allowed to leave raw pointers to
219 // on-heap objects. However, there can be scenarios where you have to use raw
220 // pointers for some reason, and in that case you can use UntracedMember. Of
221 // course, it must be guaranteed that the pointing on-heap object is kept alive
222 // while the raw pointer is pointing to the object.
223 template<typename T>
224 class UntracedMember final : public Member<T> {
225 public:
226 UntracedMember() : Member<T>() { }
227
228 UntracedMember(std::nullptr_t) : Member<T>(nullptr) { }
229
230 UntracedMember(T* raw) : Member<T>(raw) { }
231
232 template<typename U>
233 UntracedMember(const Persistent<U>& other) : Member<T>(other) { }
234
235 template<typename U>
236 UntracedMember(const Member<U>& other) : Member<T>(other) { }
237
238 UntracedMember(WTF::HashTableDeletedValueType x) : Member<T>(x) { }
239
240 template<typename U>
241 UntracedMember& operator=(const Persistent<U>& other)
242 {
243 this->m_raw = other;
244 this->checkPointer();
245 return *this;
246 }
247
248 template<typename U>
249 UntracedMember& operator=(const Member<U>& other)
250 {
251 this->m_raw = other;
252 this->checkPointer();
253 return *this;
254 }
255
256 template<typename U>
257 UntracedMember& operator=(U* other)
258 {
259 this->m_raw = other;
260 this->checkPointer();
261 return *this;
262 }
263
264 UntracedMember& operator=(std::nullptr_t)
265 {
266 this->m_raw = nullptr;
267 return *this;
268 }
269 };
270
271 } // namespace blink
272
273 namespace WTF {
274
275 template <typename T>
276 struct MemberHash : PtrHash<T> {
277 STATIC_ONLY(MemberHash);
278 template <typename U>
279 static unsigned hash(const U& key) { return PtrHash<T>::hash(key); }
280 template <typename U, typename V>
281 static bool equal(const U& a, const V& b) { return a == b; }
282 };
283
284 template <typename T>
285 struct WeakMemberHash : MemberHash<T> {
286 STATIC_ONLY(WeakMemberHash);
287 };
288
289 template <typename T>
290 struct UntracedMemberHash : MemberHash<T> {
291 STATIC_ONLY(UntracedMemberHash);
292 };
293
294 // PtrHash is the default hash for hash tables with members.
295 template <typename T>
296 struct DefaultHash<blink::Member<T>> {
297 STATIC_ONLY(DefaultHash);
298 using Hash = MemberHash<T>;
299 };
300
301 template <typename T>
302 struct DefaultHash<blink::WeakMember<T>> {
303 STATIC_ONLY(DefaultHash);
304 using Hash = WeakMemberHash<T>;
305 };
306
307 template <typename T>
308 struct DefaultHash<blink::UntracedMember<T>> {
309 STATIC_ONLY(DefaultHash);
310 using Hash = UntracedMemberHash<T>;
311 };
312
313 template<typename T>
314 struct NeedsTracing<blink::Member<T>> {
315 STATIC_ONLY(NeedsTracing);
316 static const bool value = true;
317 };
318
319 template<typename T>
320 struct IsWeak<blink::WeakMember<T>> {
321 STATIC_ONLY(IsWeak);
322 static const bool value = true;
323 };
324
325 } // namespace WTF
326
327 #endif // Member_h
328
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapTest.cpp ('k') | third_party/WebKit/Source/platform/heap/Persistent.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698