OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 TraceWrapperMember_h | 5 // This file has been moved to platform/bindings/TraceWrapperMember.h. |
6 #define TraceWrapperMember_h | 6 // TODO(adithyas): Remove this file. |
7 | 7 #include "platform/bindings/TraceWrapperMember.h" |
8 #include "bindings/core/v8/ScriptWrappableVisitor.h" | |
9 #include "platform/heap/HeapAllocator.h" | |
10 | |
11 namespace blink { | |
12 | |
13 class HeapObjectHeader; | |
14 template <typename T> | |
15 class Member; | |
16 | |
17 /** | |
18 * TraceWrapperMember is used for Member fields that should participate in | |
19 * wrapper tracing, i.e., strongly hold a ScriptWrappable alive. All | |
20 * TraceWrapperMember fields must be traced in the class' traceWrappers method. | |
21 */ | |
22 template <class T> | |
23 class TraceWrapperMember : public Member<T> { | |
24 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | |
25 | |
26 public: | |
27 TraceWrapperMember(void* parent, T* raw) : Member<T>(raw), parent_(parent) { | |
28 #if DCHECK_IS_ON() | |
29 if (parent_) { | |
30 HeapObjectHeader::CheckFromPayload(parent_); | |
31 } | |
32 #endif | |
33 // We don't require a write barrier here as TraceWrapperMember is used for | |
34 // the following scenarios: | |
35 // - Initial initialization: The write barrier will not fire as the parent | |
36 // is initially white. | |
37 // - Wrapping when inserting into a container: The write barrier will fire | |
38 // upon establishing the move into the container. | |
39 // - Assignment to a field: The regular assignment operator will fire the | |
40 // write barrier. | |
41 // Note that support for black allocation would require a barrier here. | |
42 } | |
43 TraceWrapperMember(WTF::HashTableDeletedValueType x) | |
44 : Member<T>(x), parent_(nullptr) {} | |
45 | |
46 /** | |
47 * Copying a TraceWrapperMember means that its backpointer will also be | |
48 * copied. | |
49 */ | |
50 TraceWrapperMember(const TraceWrapperMember& other) { *this = other; } | |
51 | |
52 TraceWrapperMember& operator=(const TraceWrapperMember& other) { | |
53 DCHECK(!other.raw_ || other.parent_); | |
54 parent_ = other.parent_; | |
55 Member<T>::operator=(other); | |
56 ScriptWrappableVisitor::WriteBarrier(parent_, other); | |
57 return *this; | |
58 } | |
59 | |
60 TraceWrapperMember& operator=(const Member<T>& other) { | |
61 DCHECK(!TraceWrapperMemberIsNotInitialized()); | |
62 Member<T>::operator=(other); | |
63 ScriptWrappableVisitor::WriteBarrier(parent_, other); | |
64 return *this; | |
65 } | |
66 | |
67 TraceWrapperMember& operator=(T* other) { | |
68 DCHECK(!TraceWrapperMemberIsNotInitialized()); | |
69 Member<T>::operator=(other); | |
70 ScriptWrappableVisitor::WriteBarrier(parent_, other); | |
71 return *this; | |
72 } | |
73 | |
74 TraceWrapperMember& operator=(std::nullptr_t) { | |
75 // No need for a write barrier when assigning nullptr. | |
76 Member<T>::operator=(nullptr); | |
77 return *this; | |
78 } | |
79 | |
80 void* Parent() { return parent_; } | |
81 | |
82 private: | |
83 bool TraceWrapperMemberIsNotInitialized() { return !parent_; } | |
84 | |
85 /** | |
86 * The parent object holding strongly onto the actual Member. | |
87 */ | |
88 void* parent_; | |
89 }; | |
90 | |
91 /** | |
92 * Swaps two HeapVectors specialized for TraceWrapperMember. The custom swap | |
93 * function is required as TraceWrapperMember contains ownership information | |
94 * which is not copyable but has to be explicitly specified. | |
95 */ | |
96 template <typename T> | |
97 void swap(HeapVector<TraceWrapperMember<T>>& a, | |
98 HeapVector<TraceWrapperMember<T>>& b, | |
99 void* parent_for_a, | |
100 void* parent_for_b) { | |
101 HeapVector<TraceWrapperMember<T>> temp; | |
102 temp.ReserveCapacity(a.size()); | |
103 for (auto item : a) { | |
104 temp.push_back(TraceWrapperMember<T>(parent_for_b, item.Get())); | |
105 } | |
106 a.clear(); | |
107 a.ReserveCapacity(b.size()); | |
108 for (auto item : b) { | |
109 a.push_back(TraceWrapperMember<T>(parent_for_a, item.Get())); | |
110 } | |
111 b.clear(); | |
112 b.ReserveCapacity(temp.size()); | |
113 for (auto item : temp) { | |
114 b.push_back(TraceWrapperMember<T>(parent_for_b, item.Get())); | |
115 } | |
116 } | |
117 | |
118 /** | |
119 * Swaps two HeapVectors, one containing TraceWrapperMember and one with | |
120 * regular Members. The custom swap function is required as | |
121 * TraceWrapperMember contains ownership information which is not copyable | |
122 * but has to be explicitly specified. | |
123 */ | |
124 template <typename T> | |
125 void swap(HeapVector<TraceWrapperMember<T>>& a, | |
126 HeapVector<Member<T>>& b, | |
127 void* parent_for_a) { | |
128 HeapVector<TraceWrapperMember<T>> temp; | |
129 temp.ReserveCapacity(a.size()); | |
130 for (auto item : a) { | |
131 temp.push_back(TraceWrapperMember<T>(item.Parent(), item.Get())); | |
132 } | |
133 a.clear(); | |
134 a.ReserveCapacity(b.size()); | |
135 for (auto item : b) { | |
136 a.push_back(TraceWrapperMember<T>(parent_for_a, item.Get())); | |
137 } | |
138 b.clear(); | |
139 b.ReserveCapacity(temp.size()); | |
140 for (auto item : temp) { | |
141 b.push_back(item.Get()); | |
142 } | |
143 } | |
144 | |
145 } // namespace blink | |
146 | |
147 #endif // TraceWrapperMember_h | |
OLD | NEW |