1/*
2 * Copyright (C) 2011, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WriteBarrier_h
27#define WriteBarrier_h
28
29#include "GCAssertions.h"
30#include "HandleTypes.h"
31#include "Heap.h"
32#include "SamplingCounter.h"
33
34namespace JSC {
35
36namespace DFG {
37class DesiredWriteBarrier;
38}
39
40class JSCell;
41class VM;
42class JSGlobalObject;
43
44template<class T> class WriteBarrierBase;
45template<> class WriteBarrierBase<JSValue>;
46
47JS_EXPORT_PRIVATE void slowValidateCell(JSCell*);
48JS_EXPORT_PRIVATE void slowValidateCell(JSGlobalObject*);
49
50#if ENABLE(GC_VALIDATION)
51template<class T> inline void validateCell(T cell)
52{
53 ASSERT_GC_OBJECT_INHERITS(cell, std::remove_pointer<T>::type::info());
54}
55
56template<> inline void validateCell<JSCell*>(JSCell* cell)
57{
58 slowValidateCell(cell);
59}
60
61template<> inline void validateCell<JSGlobalObject*>(JSGlobalObject* globalObject)
62{
63 slowValidateCell(globalObject);
64}
65#else
66template<class T> inline void validateCell(T)
67{
68}
69#endif
70
71// We have a separate base class with no constructors for use in Unions.
72template <typename T> class WriteBarrierBase {
73public:
74 void set(VM&, const JSCell* owner, T* value);
75
76 // This is meant to be used like operator=, but is called copyFrom instead, in
77 // order to kindly inform the C++ compiler that its advice is not appreciated.
78 void copyFrom(const WriteBarrierBase<T>& other)
79 {
80 m_cell = other.m_cell;
81 }
82
83 void setMayBeNull(VM&, const JSCell* owner, T* value);
84
85 // Should only be used by JSCell during early initialisation
86 // when some basic types aren't yet completely instantiated
87 void setEarlyValue(VM&, const JSCell* owner, T* value);
88
89 T* get() const
90 {
91 // Copy m_cell to a local to avoid multiple-read issues. (See <http://webkit.org/b/110854>)
92 JSCell* cell = m_cell;
93 if (cell)
94 validateCell(cell);
95 return reinterpret_cast<T*>(static_cast<void*>(cell));
96 }
97
98 T* operator*() const
99 {
100 ASSERT(m_cell);
101 validateCell<T>(static_cast<T*>(m_cell));
102 return static_cast<T*>(m_cell);
103 }
104
105 T* operator->() const
106 {
107 ASSERT(m_cell);
108 validateCell(static_cast<T*>(m_cell));
109 return static_cast<T*>(m_cell);
110 }
111
112 void clear() { m_cell = 0; }
113
114 T** slot() { return reinterpret_cast<T**>(&m_cell); }
115
116 explicit operator bool() const { return m_cell; }
117
118 bool operator!() const { return !m_cell; }
119
120 void setWithoutWriteBarrier(T* value)
121 {
122#if ENABLE(WRITE_BARRIER_PROFILING)
123 WriteBarrierCounters::usesWithoutBarrierFromCpp.count();
124#endif
125 this->m_cell = reinterpret_cast<JSCell*>(value);
126 }
127
128 T* unvalidatedGet() const { return reinterpret_cast<T*>(static_cast<void*>(m_cell)); }
129
130private:
131 JSCell* m_cell;
132};
133
134template <> class WriteBarrierBase<Unknown> {
135public:
136 void set(VM&, const JSCell* owner, JSValue);
137 void setWithoutWriteBarrier(JSValue value)
138 {
139 m_value = JSValue::encode(value);
140 }
141
142 JSValue get() const
143 {
144 return JSValue::decode(m_value);
145 }
146 void clear() { m_value = JSValue::encode(JSValue()); }
147 void setUndefined() { m_value = JSValue::encode(jsUndefined()); }
148 void setStartingValue(JSValue value) { m_value = JSValue::encode(value); }
149 bool isNumber() const { return get().isNumber(); }
150 bool isObject() const { return get().isObject(); }
151 bool isNull() const { return get().isNull(); }
152 bool isGetterSetter() const { return get().isGetterSetter(); }
153 bool isCustomGetterSetter() const { return get().isCustomGetterSetter(); }
154
155 JSValue* slot() const
156 {
157 return bitwise_cast<JSValue*>(&m_value);
158 }
159
160 int32_t* tagPointer() { return &bitwise_cast<EncodedValueDescriptor*>(&m_value)->asBits.tag; }
161 int32_t* payloadPointer() { return &bitwise_cast<EncodedValueDescriptor*>(&m_value)->asBits.payload; }
162
163 explicit operator bool() const { return !!get(); }
164 bool operator!() const { return !get(); }
165
166private:
167 EncodedJSValue m_value;
168};
169
170template <typename T> class WriteBarrier : public WriteBarrierBase<T> {
171 WTF_MAKE_FAST_ALLOCATED;
172public:
173 WriteBarrier()
174 {
175 this->setWithoutWriteBarrier(0);
176 }
177
178 WriteBarrier(VM& vm, const JSCell* owner, T* value)
179 {
180 this->set(vm, owner, value);
181 }
182
183 WriteBarrier(DFG::DesiredWriteBarrier&, T* value)
184 {
185 ASSERT(isCompilationThread());
186 this->setWithoutWriteBarrier(value);
187 }
188
189 enum MayBeNullTag { MayBeNull };
190 WriteBarrier(VM& vm, const JSCell* owner, T* value, MayBeNullTag)
191 {
192 this->setMayBeNull(vm, owner, value);
193 }
194};
195
196enum UndefinedWriteBarrierTagType { UndefinedWriteBarrierTag };
197template <> class WriteBarrier<Unknown> : public WriteBarrierBase<Unknown> {
198 WTF_MAKE_FAST_ALLOCATED;
199public:
200 WriteBarrier()
201 {
202 this->setWithoutWriteBarrier(JSValue());
203 }
204 WriteBarrier(UndefinedWriteBarrierTagType)
205 {
206 this->setWithoutWriteBarrier(jsUndefined());
207 }
208
209 WriteBarrier(VM& vm, const JSCell* owner, JSValue value)
210 {
211 this->set(vm, owner, value);
212 }
213
214 WriteBarrier(DFG::DesiredWriteBarrier&, JSValue value)
215 {
216 ASSERT(isCompilationThread());
217 this->setWithoutWriteBarrier(value);
218 }
219};
220
221template <typename U, typename V> inline bool operator==(const WriteBarrierBase<U>& lhs, const WriteBarrierBase<V>& rhs)
222{
223 return lhs.get() == rhs.get();
224}
225
226} // namespace JSC
227
228#endif // WriteBarrier_h
229