blob: ed9caa92a9dd412e055a63e9bfbf6c1acb03aca6 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/global-handles.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/api.h"
8#include "src/v8.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/vm-state-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010010
Steve Blocka7e24c12009-10-30 11:49:00 +000011namespace v8 {
12namespace internal {
13
Steve Block44f0eee2011-05-26 01:26:41 +010014
15ObjectGroup::~ObjectGroup() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016 if (info != NULL) info->Dispose();
17 delete[] objects;
18}
19
20
21ImplicitRefGroup::~ImplicitRefGroup() {
22 delete[] children;
Steve Block44f0eee2011-05-26 01:26:41 +010023}
24
25
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000026class GlobalHandles::Node {
Steve Blocka7e24c12009-10-30 11:49:00 +000027 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000028 // State transition diagram:
29 // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE }
30 enum State {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 FREE = 0,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040032 NORMAL, // Normal global handle.
33 WEAK, // Flagged as weak but not yet finalized.
34 PENDING, // Has been recognized as only reachable by weak handles.
35 NEAR_DEATH, // Callback has informed the handle is near death.
36 NUMBER_OF_NODE_STATES
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037 };
Steve Blocka7e24c12009-10-30 11:49:00 +000038
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000039 // Maps handle location (slot) to the containing node.
40 static Node* FromLocation(Object** location) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 DCHECK(offsetof(Node, object_) == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000042 return reinterpret_cast<Node*>(location);
43 }
44
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 Node() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 DCHECK(offsetof(Node, class_id_) == Internals::kNodeClassIdOffset);
47 DCHECK(offsetof(Node, flags_) == Internals::kNodeFlagsOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 STATIC_ASSERT(static_cast<int>(NodeState::kMask) ==
49 Internals::kNodeStateMask);
50 STATIC_ASSERT(WEAK == Internals::kNodeStateIsWeakValue);
51 STATIC_ASSERT(PENDING == Internals::kNodeStateIsPendingValue);
52 STATIC_ASSERT(NEAR_DEATH == Internals::kNodeStateIsNearDeathValue);
53 STATIC_ASSERT(static_cast<int>(IsIndependent::kShift) ==
54 Internals::kNodeIsIndependentShift);
55 STATIC_ASSERT(static_cast<int>(IsPartiallyDependent::kShift) ==
56 Internals::kNodeIsPartiallyDependentShift);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 STATIC_ASSERT(static_cast<int>(IsActive::kShift) ==
58 Internals::kNodeIsActiveShift);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000060
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061#ifdef ENABLE_HANDLE_ZAPPING
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000062 ~Node() {
63 // TODO(1428): if it's a weak handle we should have invoked its callback.
64 // Zap the values for eager trapping.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000066 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
67 index_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 set_independent(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 if (FLAG_scavenge_reclaim_unmodified_objects) {
70 set_active(false);
71 } else {
72 set_partially_dependent(false);
73 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 set_in_new_space_list(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000075 parameter_or_next_free_.next_free = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 weak_callback_ = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000077 }
78#endif
79
80 void Initialize(int index, Node** first_free) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000082 index_ = static_cast<uint8_t>(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 DCHECK(static_cast<int>(index_) == index);
84 set_state(FREE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 set_weakness_type(NORMAL_WEAK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 set_in_new_space_list(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000087 parameter_or_next_free_.next_free = *first_free;
88 *first_free = this;
89 }
90
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 void Acquire(Object* object) {
92 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +000093 object_ = object;
Steve Block44f0eee2011-05-26 01:26:41 +010094 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 set_independent(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 if (FLAG_scavenge_reclaim_unmodified_objects) {
97 set_active(false);
98 } else {
99 set_partially_dependent(false);
100 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 set_state(NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 parameter_or_next_free_.parameter = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 weak_callback_ = NULL;
104 IncreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 }
106
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400107 void Zap() {
108 DCHECK(IsInUse());
109 // Zap the values for eager trapping.
110 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
111 }
112
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 void Release() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400114 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 set_state(FREE);
116 // Zap the values for eager trapping.
117 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
118 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
119 set_independent(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 if (FLAG_scavenge_reclaim_unmodified_objects) {
121 set_active(false);
122 } else {
123 set_partially_dependent(false);
124 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125 weak_callback_ = NULL;
126 DecreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 }
128
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000129 // Object slot accessors.
130 Object* object() const { return object_; }
131 Object** location() { return &object_; }
132 Handle<Object> handle() { return Handle<Object>(location()); }
133
134 // Wrapper class ID accessors.
135 bool has_wrapper_class_id() const {
136 return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId;
137 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000139 uint16_t wrapper_class_id() const { return class_id_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140
141 // State and flag accessors.
142
143 State state() const {
144 return NodeState::decode(flags_);
145 }
146 void set_state(State state) {
147 flags_ = NodeState::update(flags_, state);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000148 }
149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 bool is_independent() {
151 return IsIndependent::decode(flags_);
152 }
153 void set_independent(bool v) {
154 flags_ = IsIndependent::update(flags_, v);
155 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000156
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 bool is_partially_dependent() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 CHECK(!FLAG_scavenge_reclaim_unmodified_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 return IsPartiallyDependent::decode(flags_);
160 }
161 void set_partially_dependent(bool v) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000162 CHECK(!FLAG_scavenge_reclaim_unmodified_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 flags_ = IsPartiallyDependent::update(flags_, v);
164 }
165
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 bool is_active() {
167 CHECK(FLAG_scavenge_reclaim_unmodified_objects);
168 return IsActive::decode(flags_);
169 }
170 void set_active(bool v) {
171 CHECK(FLAG_scavenge_reclaim_unmodified_objects);
172 flags_ = IsActive::update(flags_, v);
173 }
174
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000175 bool is_in_new_space_list() {
176 return IsInNewSpaceList::decode(flags_);
177 }
178 void set_in_new_space_list(bool v) {
179 flags_ = IsInNewSpaceList::update(flags_, v);
180 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000181
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400182 WeaknessType weakness_type() const {
183 return NodeWeaknessType::decode(flags_);
184 }
185 void set_weakness_type(WeaknessType weakness_type) {
186 flags_ = NodeWeaknessType::update(flags_, weakness_type);
187 }
188
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000189 bool IsNearDeath() const {
190 // Check for PENDING to ensure correct answer when processing callbacks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 return state() == PENDING || state() == NEAR_DEATH;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000192 }
193
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 bool IsWeak() const { return state() == WEAK; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000195
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400196 bool IsInUse() const { return state() != FREE; }
197
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198 bool IsRetainer() const {
199 return state() != FREE &&
200 !(state() == NEAR_DEATH && weakness_type() != NORMAL_WEAK);
201 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000202
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 bool IsStrongRetainer() const { return state() == NORMAL; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000204
205 bool IsWeakRetainer() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 return state() == WEAK || state() == PENDING ||
207 (state() == NEAR_DEATH && weakness_type() == NORMAL_WEAK);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000208 }
209
210 void MarkPending() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 DCHECK(state() == WEAK);
212 set_state(PENDING);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000213 }
214
215 // Independent flag accessors.
216 void MarkIndependent() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400217 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 set_independent(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000219 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 void MarkPartiallyDependent() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400222 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) {
224 set_partially_dependent(true);
225 }
226 }
227 void clear_partially_dependent() { set_partially_dependent(false); }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000228
229 // Callback accessor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 // TODO(svenpanne) Re-enable or nuke later.
231 // WeakReferenceCallback callback() { return callback_; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000232
233 // Callback parameter accessors.
234 void set_parameter(void* parameter) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400235 DCHECK(IsInUse());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000236 parameter_or_next_free_.parameter = parameter;
237 }
238 void* parameter() const {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400239 DCHECK(IsInUse());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000240 return parameter_or_next_free_.parameter;
241 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243 // Accessors for next free node in the free list.
244 Node* next_free() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 return parameter_or_next_free_.next_free;
247 }
248 void set_next_free(Node* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 parameter_or_next_free_.next_free = value;
251 }
252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 void MakeWeak(void* parameter, WeakCallback weak_callback) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 DCHECK(weak_callback != nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400255 DCHECK(IsInUse());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000256 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 set_state(WEAK);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400258 set_weakness_type(NORMAL_WEAK);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 set_parameter(parameter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 weak_callback_ = weak_callback;
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 }
262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 void MakeWeak(void* parameter,
264 WeakCallbackInfo<void>::Callback phantom_callback,
265 v8::WeakCallbackType type) {
266 DCHECK(phantom_callback != nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400267 DCHECK(IsInUse());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400269 set_state(WEAK);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000270 switch (type) {
271 case v8::WeakCallbackType::kParameter:
272 set_weakness_type(PHANTOM_WEAK);
273 break;
274 case v8::WeakCallbackType::kInternalFields:
275 set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS);
276 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400277 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000278 set_parameter(parameter);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400279 weak_callback_ = reinterpret_cast<WeakCallback>(phantom_callback);
280 }
281
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 void* ClearWeakness() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400283 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 void* p = parameter();
285 set_state(NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 set_parameter(NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 return p;
Steve Blocka7e24c12009-10-30 11:49:00 +0000288 }
289
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400290 void CollectPhantomCallbackData(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 Isolate* isolate,
292 List<PendingPhantomCallback>* pending_phantom_callbacks) {
293 DCHECK(weakness_type() == PHANTOM_WEAK ||
294 weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
295 DCHECK(state() == PENDING);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400296
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 void* internal_fields[v8::kInternalFieldsInWeakCallback] = {nullptr,
298 nullptr};
299 if (weakness_type() != PHANTOM_WEAK && object()->IsJSObject()) {
300 auto jsobject = JSObject::cast(object());
301 int field_count = jsobject->GetInternalFieldCount();
302 for (int i = 0; i < v8::kInternalFieldsInWeakCallback; ++i) {
303 if (field_count == i) break;
304 auto field = jsobject->GetInternalField(i);
305 if (field->IsSmi()) internal_fields[i] = field;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400306 }
307 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308
309 // Zap with something dangerous.
310 *location() = reinterpret_cast<Object*>(0x6057ca11);
311
312 typedef v8::WeakCallbackInfo<void> Data;
313 auto callback = reinterpret_cast<Data::Callback>(weak_callback_);
314 pending_phantom_callbacks->Add(
315 PendingPhantomCallback(this, callback, parameter(), internal_fields));
316 DCHECK(IsInUse());
317 set_state(NEAR_DEATH);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400318 }
319
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 bool PostGarbageCollectionProcessing(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321 // Handles only weak handles (not phantom) that are dying.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 if (state() != Node::PENDING) return false;
323 if (weak_callback_ == NULL) {
324 Release();
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100325 return false;
326 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327 set_state(NEAR_DEATH);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400329 // Check that we are not passing a finalized external string to
330 // the callback.
331 DCHECK(!object_->IsExternalOneByteString() ||
332 ExternalOneByteString::cast(object_)->resource() != NULL);
333 DCHECK(!object_->IsExternalTwoByteString() ||
334 ExternalTwoByteString::cast(object_)->resource() != NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 if (weakness_type() != NORMAL_WEAK) return false;
336
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400337 // Leaving V8.
338 VMState<EXTERNAL> vmstate(isolate);
339 HandleScope handle_scope(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 Object** object = location();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400341 Handle<Object> handle(*object, isolate);
342 v8::WeakCallbackData<v8::Value, void> data(
343 reinterpret_cast<v8::Isolate*>(isolate), parameter(),
344 v8::Utils::ToLocal(handle));
345 set_parameter(NULL);
346 weak_callback_(data);
347
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100348 // Absence of explicit cleanup or revival of weak handle
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100349 // in most of the cases would lead to memory leak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 CHECK(state() != NEAR_DEATH);
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 return true;
352 }
353
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354 inline GlobalHandles* GetGlobalHandles();
355
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000356 private:
357 inline NodeBlock* FindBlock();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358 inline void IncreaseBlockUses();
359 inline void DecreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000361 // Storage for object pointer.
362 // Placed first to avoid offset computation.
363 Object* object_;
364
365 // Next word stores class_id, index, state, and independent.
366 // Note: the most aligned fields should go first.
367
368 // Wrapper class ID.
Steve Block44f0eee2011-05-26 01:26:41 +0100369 uint16_t class_id_;
370
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000371 // Index in the containing handle block.
372 uint8_t index_;
373
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 // This stores three flags (independent, partially_dependent and
375 // in_new_space_list) and a State.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400376 class NodeState : public BitField<State, 0, 3> {};
377 class IsIndependent : public BitField<bool, 3, 1> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000378 // The following two fields are mutually exclusive
379 class IsActive : public BitField<bool, 4, 1> {};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400380 class IsPartiallyDependent : public BitField<bool, 4, 1> {};
381 class IsInNewSpaceList : public BitField<bool, 5, 1> {};
382 class NodeWeaknessType : public BitField<WeaknessType, 6, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000383
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 uint8_t flags_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 // Handle specific callback - might be a weak reference in disguise.
387 WeakCallback weak_callback_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000388
389 // Provided data for callback. In FREE state, this is used for
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 // the free list link.
391 union {
392 void* parameter;
393 Node* next_free;
394 } parameter_or_next_free_;
395
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000396 DISALLOW_COPY_AND_ASSIGN(Node);
Steve Blocka7e24c12009-10-30 11:49:00 +0000397};
398
399
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000400class GlobalHandles::NodeBlock {
401 public:
402 static const int kSize = 256;
Steve Blockd0582a62009-12-15 09:54:21 +0000403
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next)
405 : next_(next),
406 used_nodes_(0),
407 next_used_(NULL),
408 prev_used_(NULL),
409 global_handles_(global_handles) {}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000410
411 void PutNodesOnFreeList(Node** first_free) {
412 for (int i = kSize - 1; i >= 0; --i) {
413 nodes_[i].Initialize(i, first_free);
414 }
415 }
416
417 Node* node_at(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418 DCHECK(0 <= index && index < kSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000419 return &nodes_[index];
420 }
421
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 void IncreaseUses() {
423 DCHECK(used_nodes_ < kSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000424 if (used_nodes_++ == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 NodeBlock* old_first = global_handles_->first_used_block_;
426 global_handles_->first_used_block_ = this;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000427 next_used_ = old_first;
428 prev_used_ = NULL;
429 if (old_first == NULL) return;
430 old_first->prev_used_ = this;
431 }
432 }
433
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 void DecreaseUses() {
435 DCHECK(used_nodes_ > 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000436 if (--used_nodes_ == 0) {
437 if (next_used_ != NULL) next_used_->prev_used_ = prev_used_;
438 if (prev_used_ != NULL) prev_used_->next_used_ = next_used_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 if (this == global_handles_->first_used_block_) {
440 global_handles_->first_used_block_ = next_used_;
Ben Murdochbb769b22010-08-11 14:56:33 +0100441 }
442 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000443 }
Ben Murdochbb769b22010-08-11 14:56:33 +0100444
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 GlobalHandles* global_handles() { return global_handles_; }
446
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000447 // Next block in the list of all blocks.
448 NodeBlock* next() const { return next_; }
Steve Blockd0582a62009-12-15 09:54:21 +0000449
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000450 // Next/previous block in the list of blocks with used nodes.
451 NodeBlock* next_used() const { return next_used_; }
452 NodeBlock* prev_used() const { return prev_used_; }
Steve Blockd0582a62009-12-15 09:54:21 +0000453
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000454 private:
455 Node nodes_[kSize];
456 NodeBlock* const next_;
457 int used_nodes_;
458 NodeBlock* next_used_;
459 NodeBlock* prev_used_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 GlobalHandles* global_handles_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000461};
Steve Blockd0582a62009-12-15 09:54:21 +0000462
Steve Blockd0582a62009-12-15 09:54:21 +0000463
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464GlobalHandles* GlobalHandles::Node::GetGlobalHandles() {
465 return FindBlock()->global_handles();
466}
467
468
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000469GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
470 intptr_t ptr = reinterpret_cast<intptr_t>(this);
471 ptr = ptr - index_ * sizeof(Node);
472 NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 DCHECK(block->node_at(index_) == this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000474 return block;
475}
Steve Blockd0582a62009-12-15 09:54:21 +0000476
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000477
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478void GlobalHandles::Node::IncreaseBlockUses() {
479 NodeBlock* node_block = FindBlock();
480 node_block->IncreaseUses();
481 GlobalHandles* global_handles = node_block->global_handles();
482 global_handles->isolate()->counters()->global_handles()->Increment();
483 global_handles->number_of_global_handles_++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000484}
485
486
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487void GlobalHandles::Node::DecreaseBlockUses() {
488 NodeBlock* node_block = FindBlock();
489 GlobalHandles* global_handles = node_block->global_handles();
490 parameter_or_next_free_.next_free = global_handles->first_free_;
491 global_handles->first_free_ = this;
492 node_block->DecreaseUses();
493 global_handles->isolate()->counters()->global_handles()->Decrement();
494 global_handles->number_of_global_handles_--;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000495}
496
497
498class GlobalHandles::NodeIterator {
499 public:
500 explicit NodeIterator(GlobalHandles* global_handles)
501 : block_(global_handles->first_used_block_),
502 index_(0) {}
503
504 bool done() const { return block_ == NULL; }
505
506 Node* node() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 DCHECK(!done());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000508 return block_->node_at(index_);
509 }
510
511 void Advance() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 DCHECK(!done());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000513 if (++index_ < NodeBlock::kSize) return;
514 index_ = 0;
515 block_ = block_->next_used();
516 }
517
518 private:
519 NodeBlock* block_;
520 int index_;
521
522 DISALLOW_COPY_AND_ASSIGN(NodeIterator);
Steve Blockd0582a62009-12-15 09:54:21 +0000523};
524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525class GlobalHandles::PendingPhantomCallbacksSecondPassTask
526 : public v8::internal::CancelableTask {
527 public:
528 // Takes ownership of the contents of pending_phantom_callbacks, leaving it in
529 // the same state it would be after a call to Clear().
530 PendingPhantomCallbacksSecondPassTask(
531 List<PendingPhantomCallback>* pending_phantom_callbacks, Isolate* isolate)
532 : CancelableTask(isolate) {
533 pending_phantom_callbacks_.Swap(pending_phantom_callbacks);
534 }
535
536 void RunInternal() override {
537 isolate()->heap()->CallGCPrologueCallbacks(
538 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
539 InvokeSecondPassPhantomCallbacks(&pending_phantom_callbacks_, isolate());
540 isolate()->heap()->CallGCEpilogueCallbacks(
541 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
542 }
543
544 private:
545 List<PendingPhantomCallback> pending_phantom_callbacks_;
546
547 DISALLOW_COPY_AND_ASSIGN(PendingPhantomCallbacksSecondPassTask);
548};
549
Steve Blockd0582a62009-12-15 09:54:21 +0000550
Steve Block44f0eee2011-05-26 01:26:41 +0100551GlobalHandles::GlobalHandles(Isolate* isolate)
552 : isolate_(isolate),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100553 number_of_global_handles_(0),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000554 first_block_(NULL),
555 first_used_block_(NULL),
Steve Block44f0eee2011-05-26 01:26:41 +0100556 first_free_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 post_gc_processing_count_(0),
558 object_group_connections_(kObjectGroupConnectionsCapacity) {}
Steve Block44f0eee2011-05-26 01:26:41 +0100559
560
561GlobalHandles::~GlobalHandles() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000562 NodeBlock* block = first_block_;
563 while (block != NULL) {
564 NodeBlock* tmp = block->next();
565 delete block;
566 block = tmp;
567 }
568 first_block_ = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100569}
Steve Blockd0582a62009-12-15 09:54:21 +0000570
571
Steve Blocka7e24c12009-10-30 11:49:00 +0000572Handle<Object> GlobalHandles::Create(Object* value) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000573 if (first_free_ == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574 first_block_ = new NodeBlock(this, first_block_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000575 first_block_->PutNodesOnFreeList(&first_free_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 DCHECK(first_free_ != NULL);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000578 // Take the first node in the free list.
579 Node* result = first_free_;
580 first_free_ = result->next_free();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 result->Acquire(value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000582 if (isolate_->heap()->InNewSpace(value) &&
583 !result->is_in_new_space_list()) {
584 new_space_nodes_.Add(result);
585 result->set_in_new_space_list(true);
586 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 return result->handle();
588}
589
590
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591Handle<Object> GlobalHandles::CopyGlobal(Object** location) {
592 DCHECK(location != NULL);
593 return Node::FromLocation(location)->GetGlobalHandles()->Create(*location);
594}
595
596
Steve Blocka7e24c12009-10-30 11:49:00 +0000597void GlobalHandles::Destroy(Object** location) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 if (location != NULL) Node::FromLocation(location)->Release();
Steve Blocka7e24c12009-10-30 11:49:00 +0000599}
600
601
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400602void GlobalHandles::MakeWeak(Object** location, void* parameter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000603 WeakCallback weak_callback) {
604 Node::FromLocation(location)->MakeWeak(parameter, weak_callback);
Steve Blocka7e24c12009-10-30 11:49:00 +0000605}
606
607
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000608typedef v8::WeakCallbackInfo<void>::Callback GenericCallback;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400609
610
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611void GlobalHandles::MakeWeak(Object** location, void* parameter,
612 GenericCallback phantom_callback,
613 v8::WeakCallbackType type) {
614 Node::FromLocation(location)->MakeWeak(parameter, phantom_callback, type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400615}
616
617
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618void* GlobalHandles::ClearWeakness(Object** location) {
619 return Node::FromLocation(location)->ClearWeakness();
Steve Blocka7e24c12009-10-30 11:49:00 +0000620}
621
622
Ben Murdoch257744e2011-11-30 15:57:28 +0000623void GlobalHandles::MarkIndependent(Object** location) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000624 Node::FromLocation(location)->MarkIndependent();
Ben Murdoch257744e2011-11-30 15:57:28 +0000625}
626
627
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628void GlobalHandles::MarkPartiallyDependent(Object** location) {
629 Node::FromLocation(location)->MarkPartiallyDependent();
630}
631
632
633bool GlobalHandles::IsIndependent(Object** location) {
634 return Node::FromLocation(location)->is_independent();
635}
636
637
Steve Blocka7e24c12009-10-30 11:49:00 +0000638bool GlobalHandles::IsNearDeath(Object** location) {
639 return Node::FromLocation(location)->IsNearDeath();
640}
641
642
643bool GlobalHandles::IsWeak(Object** location) {
644 return Node::FromLocation(location)->IsWeak();
645}
646
Steve Blocka7e24c12009-10-30 11:49:00 +0000647void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000648 for (NodeIterator it(this); !it.done(); it.Advance()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400649 Node* node = it.node();
650 if (node->IsWeakRetainer()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000651 // Pending weak phantom handles die immediately. Everything else survives.
652 if (node->state() == Node::PENDING &&
653 node->weakness_type() != NORMAL_WEAK) {
654 node->CollectPhantomCallbackData(isolate(),
655 &pending_phantom_callbacks_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400656 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400657 v->VisitPointer(node->location());
658 }
659 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000660 }
661}
662
663
Steve Blocka7e24c12009-10-30 11:49:00 +0000664void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000665 for (NodeIterator it(this); !it.done(); it.Advance()) {
666 if (it.node()->IsWeak() && f(it.node()->location())) {
667 it.node()->MarkPending();
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 }
669 }
670}
671
672
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000673void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
674 for (int i = 0; i < new_space_nodes_.length(); ++i) {
675 Node* node = new_space_nodes_[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 if (FLAG_scavenge_reclaim_unmodified_objects) {
677 if (node->IsStrongRetainer() ||
678 (node->IsWeakRetainer() && !node->is_independent() &&
679 node->is_active())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 v->VisitPointer(node->location());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000681 }
682 } else {
683 if (node->IsStrongRetainer() ||
684 (node->IsWeakRetainer() && !node->is_independent() &&
685 !node->is_partially_dependent())) {
686 v->VisitPointer(node->location());
687 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000688 }
689 }
690}
691
692
693void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles(
694 WeakSlotCallbackWithHeap f) {
695 for (int i = 0; i < new_space_nodes_.length(); ++i) {
696 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697 DCHECK(node->is_in_new_space_list());
698 if ((node->is_independent() || node->is_partially_dependent()) &&
699 node->IsWeak() && f(isolate_->heap(), node->location())) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000700 node->MarkPending();
701 }
702 }
703}
704
705
706void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) {
707 for (int i = 0; i < new_space_nodes_.length(); ++i) {
708 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 DCHECK(node->is_in_new_space_list());
710 if ((node->is_independent() || node->is_partially_dependent()) &&
711 node->IsWeakRetainer()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000712 // Pending weak phantom handles die immediately. Everything else survives.
713 if (node->state() == Node::PENDING &&
714 node->weakness_type() != NORMAL_WEAK) {
715 node->CollectPhantomCallbackData(isolate(),
716 &pending_phantom_callbacks_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400717 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718 v->VisitPointer(node->location());
719 }
720 }
721 }
722}
723
724
725void GlobalHandles::IdentifyWeakUnmodifiedObjects(
726 WeakSlotCallback is_unmodified) {
727 for (int i = 0; i < new_space_nodes_.length(); ++i) {
728 Node* node = new_space_nodes_[i];
729 if (node->IsWeak() && !is_unmodified(node->location())) {
730 node->set_active(true);
731 }
732 }
733}
734
735
736void GlobalHandles::MarkNewSpaceWeakUnmodifiedObjectsPending(
737 WeakSlotCallbackWithHeap is_unscavenged) {
738 for (int i = 0; i < new_space_nodes_.length(); ++i) {
739 Node* node = new_space_nodes_[i];
740 DCHECK(node->is_in_new_space_list());
741 if ((node->is_independent() || !node->is_active()) && node->IsWeak() &&
742 is_unscavenged(isolate_->heap(), node->location())) {
743 node->MarkPending();
744 }
745 }
746}
747
748
749void GlobalHandles::IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v) {
750 for (int i = 0; i < new_space_nodes_.length(); ++i) {
751 Node* node = new_space_nodes_[i];
752 DCHECK(node->is_in_new_space_list());
753 if ((node->is_independent() || !node->is_active()) &&
754 node->IsWeakRetainer()) {
755 // Pending weak phantom handles die immediately. Everything else survives.
756 if (node->state() == Node::PENDING &&
757 node->weakness_type() != NORMAL_WEAK) {
758 node->CollectPhantomCallbackData(isolate(),
759 &pending_phantom_callbacks_);
760 } else {
761 v->VisitPointer(node->location());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400762 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000763 }
764 }
765}
766
767
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v,
769 WeakSlotCallbackWithHeap can_skip) {
770 ComputeObjectGroupsAndImplicitReferences();
771 int last = 0;
772 bool any_group_was_visited = false;
773 for (int i = 0; i < object_groups_.length(); i++) {
774 ObjectGroup* entry = object_groups_.at(i);
775 DCHECK(entry != NULL);
776
777 Object*** objects = entry->objects;
778 bool group_should_be_visited = false;
779 for (size_t j = 0; j < entry->length; j++) {
780 Object* object = *objects[j];
781 if (object->IsHeapObject()) {
782 if (!can_skip(isolate_->heap(), &object)) {
783 group_should_be_visited = true;
784 break;
785 }
786 }
787 }
788
789 if (!group_should_be_visited) {
790 object_groups_[last++] = entry;
791 continue;
792 }
793
794 // An object in the group requires visiting, so iterate over all
795 // objects in the group.
796 for (size_t j = 0; j < entry->length; ++j) {
797 Object* object = *objects[j];
798 if (object->IsHeapObject()) {
799 v->VisitPointer(&object);
800 any_group_was_visited = true;
801 }
802 }
803
804 // Once the entire group has been iterated over, set the object
805 // group to NULL so it won't be processed again.
806 delete entry;
807 object_groups_.at(i) = NULL;
808 }
809 object_groups_.Rewind(last);
810 return any_group_was_visited;
811}
812
Ben Murdochda12d292016-06-02 14:46:10 +0100813namespace {
814// Traces the information about object groups and implicit ref groups given by
815// the embedder to the V8 during each gc prologue.
816class ObjectGroupsTracer {
817 public:
818 explicit ObjectGroupsTracer(Isolate* isolate);
819 void Print();
820
821 private:
822 void PrintObjectGroup(ObjectGroup* group);
823 void PrintImplicitRefGroup(ImplicitRefGroup* group);
824 void PrintObject(Object* object);
825 void PrintConstructor(JSObject* js_object);
826 void PrintInternalFields(JSObject* js_object);
827 Isolate* isolate_;
828 DISALLOW_COPY_AND_ASSIGN(ObjectGroupsTracer);
829};
830
831ObjectGroupsTracer::ObjectGroupsTracer(Isolate* isolate) : isolate_(isolate) {}
832
833void ObjectGroupsTracer::Print() {
834 GlobalHandles* global_handles = isolate_->global_handles();
835
836 PrintIsolate(isolate_, "### Tracing object groups:\n");
837
838 for (auto group : *(global_handles->object_groups())) {
839 PrintObjectGroup(group);
840 }
841 for (auto group : *(global_handles->implicit_ref_groups())) {
842 PrintImplicitRefGroup(group);
843 }
844
845 PrintIsolate(isolate_, "### Tracing object groups finished.\n");
846}
847
848void ObjectGroupsTracer::PrintObject(Object* object) {
849 if (object->IsJSObject()) {
850 JSObject* js_object = JSObject::cast(object);
851
852 PrintF("{ constructor_name: ");
853 PrintConstructor(js_object);
854 PrintF(", hidden_fields: [ ");
855 PrintInternalFields(js_object);
856 PrintF(" ] }\n");
857 } else {
858 PrintF("object of unexpected type: %p\n", object);
859 }
860}
861
862void ObjectGroupsTracer::PrintConstructor(JSObject* js_object) {
863 Object* maybe_constructor = js_object->map()->GetConstructor();
864 if (maybe_constructor->IsJSFunction()) {
865 JSFunction* constructor = JSFunction::cast(maybe_constructor);
866 String* name = String::cast(constructor->shared()->name());
867 if (name->length() == 0) name = constructor->shared()->inferred_name();
868
869 PrintF("%s", name->ToCString().get());
870 } else if (maybe_constructor->IsNull()) {
871 if (js_object->IsOddball()) {
872 PrintF("<oddball>");
873 } else {
874 PrintF("<null>");
875 }
876 } else {
877 UNREACHABLE();
878 }
879}
880
881void ObjectGroupsTracer::PrintInternalFields(JSObject* js_object) {
882 for (int i = 0; i < js_object->GetInternalFieldCount(); ++i) {
883 if (i != 0) {
884 PrintF(", ");
885 }
886 PrintF("%p", js_object->GetInternalField(i));
887 }
888}
889
890void ObjectGroupsTracer::PrintObjectGroup(ObjectGroup* group) {
891 PrintIsolate(isolate_, "ObjectGroup (size: %lu)\n", group->length);
892 Object*** objects = group->objects;
893
894 for (size_t i = 0; i < group->length; ++i) {
895 PrintIsolate(isolate_, " - Member: ");
896 PrintObject(*objects[i]);
897 }
898}
899
900void ObjectGroupsTracer::PrintImplicitRefGroup(ImplicitRefGroup* group) {
901 PrintIsolate(isolate_, "ImplicitRefGroup (children count: %lu)\n",
902 group->length);
903 PrintIsolate(isolate_, " - Parent: ");
904 PrintObject(*(group->parent));
905
906 Object*** children = group->children;
907 for (size_t i = 0; i < group->length; ++i) {
908 PrintIsolate(isolate_, " - Child: ");
909 PrintObject(*children[i]);
910 }
911}
912
913} // namespace
914
915void GlobalHandles::PrintObjectGroups() {
916 ObjectGroupsTracer(isolate_).Print();
917}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000919void GlobalHandles::InvokeSecondPassPhantomCallbacks(
920 List<PendingPhantomCallback>* callbacks, Isolate* isolate) {
921 while (callbacks->length() != 0) {
922 auto callback = callbacks->RemoveLast();
923 DCHECK(callback.node() == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000924 // Fire second pass callback
925 callback.Invoke(isolate);
926 }
927}
928
929
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400930int GlobalHandles::PostScavengeProcessing(
931 const int initial_post_gc_processing_count) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 int freed_nodes = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400933 for (int i = 0; i < new_space_nodes_.length(); ++i) {
934 Node* node = new_space_nodes_[i];
935 DCHECK(node->is_in_new_space_list());
936 if (!node->IsRetainer()) {
937 // Free nodes do not have weak callbacks. Do not use them to compute
938 // the freed_nodes.
939 continue;
940 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000941 // Skip dependent or unmodified handles. Their weak callbacks might expect
942 // to be
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400943 // called between two global garbage collection callbacks which
944 // are not called for minor collections.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000945 if (FLAG_scavenge_reclaim_unmodified_objects) {
946 if (!node->is_independent() && (node->is_active())) {
947 node->set_active(false);
948 continue;
949 }
950 node->set_active(false);
951 } else {
952 if (!node->is_independent() && !node->is_partially_dependent()) {
953 continue;
954 }
955 node->clear_partially_dependent();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400956 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400958 if (node->PostGarbageCollectionProcessing(isolate_)) {
959 if (initial_post_gc_processing_count != post_gc_processing_count_) {
960 // Weak callback triggered another GC and another round of
961 // PostGarbageCollection processing. The current node might
962 // have been deleted in that round, so we need to bail out (or
963 // restart the processing).
964 return freed_nodes;
Steve Blocka7e24c12009-10-30 11:49:00 +0000965 }
966 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400967 if (!node->IsRetainer()) {
968 freed_nodes++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000969 }
970 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400971 return freed_nodes;
972}
973
974
975int GlobalHandles::PostMarkSweepProcessing(
976 const int initial_post_gc_processing_count) {
977 int freed_nodes = 0;
978 for (NodeIterator it(this); !it.done(); it.Advance()) {
979 if (!it.node()->IsRetainer()) {
980 // Free nodes do not have weak callbacks. Do not use them to compute
981 // the freed_nodes.
982 continue;
983 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000984 if (FLAG_scavenge_reclaim_unmodified_objects) {
985 it.node()->set_active(false);
986 } else {
987 it.node()->clear_partially_dependent();
988 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400989 if (it.node()->PostGarbageCollectionProcessing(isolate_)) {
990 if (initial_post_gc_processing_count != post_gc_processing_count_) {
991 // See the comment above.
992 return freed_nodes;
993 }
994 }
995 if (!it.node()->IsRetainer()) {
996 freed_nodes++;
997 }
998 }
999 return freed_nodes;
1000}
1001
1002
1003void GlobalHandles::UpdateListOfNewSpaceNodes() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001004 int last = 0;
1005 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1006 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001007 DCHECK(node->is_in_new_space_list());
1008 if (node->IsRetainer()) {
1009 if (isolate_->heap()->InNewSpace(node->object())) {
1010 new_space_nodes_[last++] = node;
1011 isolate_->heap()->IncrementNodesCopiedInNewSpace();
1012 } else {
1013 node->set_in_new_space_list(false);
1014 isolate_->heap()->IncrementNodesPromoted();
1015 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001016 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001017 node->set_in_new_space_list(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 isolate_->heap()->IncrementNodesDiedInNewSpace();
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 }
1020 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001021 new_space_nodes_.Rewind(last);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 new_space_nodes_.Trim();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001023}
1024
1025
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026int GlobalHandles::DispatchPendingPhantomCallbacks(
1027 bool synchronous_second_pass) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001028 int freed_nodes = 0;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001029 List<PendingPhantomCallback> second_pass_callbacks;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001030 {
1031 // The initial pass callbacks must simply clear the nodes.
1032 for (auto i = pending_phantom_callbacks_.begin();
1033 i != pending_phantom_callbacks_.end(); ++i) {
1034 auto callback = i;
1035 // Skip callbacks that have already been processed once.
1036 if (callback->node() == nullptr) continue;
1037 callback->Invoke(isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001038 if (callback->callback()) second_pass_callbacks.Add(*callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001039 freed_nodes++;
1040 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001041 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001042 pending_phantom_callbacks_.Clear();
1043 if (second_pass_callbacks.length() > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001044 if (FLAG_optimize_for_size || FLAG_predictable || synchronous_second_pass) {
1045 isolate()->heap()->CallGCPrologueCallbacks(
1046 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001047 InvokeSecondPassPhantomCallbacks(&second_pass_callbacks, isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001048 isolate()->heap()->CallGCEpilogueCallbacks(
1049 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
1050 } else {
1051 auto task = new PendingPhantomCallbacksSecondPassTask(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001052 &second_pass_callbacks, isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001053 V8::GetCurrentPlatform()->CallOnForegroundThread(
1054 reinterpret_cast<v8::Isolate*>(isolate()), task);
1055 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001056 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 return freed_nodes;
Steve Blocka7e24c12009-10-30 11:49:00 +00001058}
1059
1060
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001061void GlobalHandles::PendingPhantomCallback::Invoke(Isolate* isolate) {
1062 Data::Callback* callback_addr = nullptr;
1063 if (node_ != nullptr) {
1064 // Initialize for first pass callback.
1065 DCHECK(node_->state() == Node::NEAR_DEATH);
1066 callback_addr = &callback_;
1067 }
1068 Data data(reinterpret_cast<v8::Isolate*>(isolate), parameter_,
1069 internal_fields_, callback_addr);
1070 Data::Callback callback = callback_;
1071 callback_ = nullptr;
1072 callback(data);
1073 if (node_ != nullptr) {
1074 // Transition to second pass state.
1075 DCHECK(node_->state() == Node::FREE);
1076 node_ = nullptr;
1077 }
1078}
1079
1080
1081int GlobalHandles::PostGarbageCollectionProcessing(
1082 GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001083 // Process weak global handle callbacks. This must be done after the
1084 // GC is completely done, because the callbacks may invoke arbitrary
1085 // API functions.
1086 DCHECK(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
1087 const int initial_post_gc_processing_count = ++post_gc_processing_count_;
1088 int freed_nodes = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 bool synchronous_second_pass =
1090 (gc_callback_flags &
Ben Murdoch097c5b22016-05-18 11:27:45 +01001091 (kGCCallbackFlagForced | kGCCallbackFlagCollectAllAvailableGarbage |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001092 kGCCallbackFlagSynchronousPhantomCallbackProcessing)) != 0;
1093 freed_nodes += DispatchPendingPhantomCallbacks(synchronous_second_pass);
1094 if (initial_post_gc_processing_count != post_gc_processing_count_) {
1095 // If the callbacks caused a nested GC, then return. See comment in
1096 // PostScavengeProcessing.
1097 return freed_nodes;
1098 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001099 if (collector == SCAVENGER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001100 freed_nodes += PostScavengeProcessing(initial_post_gc_processing_count);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001101 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001102 freed_nodes += PostMarkSweepProcessing(initial_post_gc_processing_count);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001103 }
1104 if (initial_post_gc_processing_count != post_gc_processing_count_) {
1105 // If the callbacks caused a nested GC, then return. See comment in
1106 // PostScavengeProcessing.
1107 return freed_nodes;
1108 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001109 if (initial_post_gc_processing_count == post_gc_processing_count_) {
1110 UpdateListOfNewSpaceNodes();
1111 }
1112 return freed_nodes;
1113}
1114
1115
Steve Blockd0582a62009-12-15 09:54:21 +00001116void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001117 for (NodeIterator it(this); !it.done(); it.Advance()) {
1118 if (it.node()->IsStrongRetainer()) {
1119 v->VisitPointer(it.node()->location());
Steve Blocka7e24c12009-10-30 11:49:00 +00001120 }
1121 }
1122}
1123
Steve Blockd0582a62009-12-15 09:54:21 +00001124
1125void GlobalHandles::IterateAllRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001126 for (NodeIterator it(this); !it.done(); it.Advance()) {
1127 if (it.node()->IsRetainer()) {
1128 v->VisitPointer(it.node()->location());
Ben Murdoch257744e2011-11-30 15:57:28 +00001129 }
1130 }
1131}
1132
1133
Steve Block44f0eee2011-05-26 01:26:41 +01001134void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001135 for (NodeIterator it(this); !it.done(); it.Advance()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001136 if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001137 v->VisitEmbedderReference(it.node()->location(),
1138 it.node()->wrapper_class_id());
Steve Block44f0eee2011-05-26 01:26:41 +01001139 }
1140 }
1141}
1142
1143
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001144void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
1145 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1146 Node* node = new_space_nodes_[i];
1147 if (node->IsRetainer() && node->has_wrapper_class_id()) {
1148 v->VisitEmbedderReference(node->location(),
1149 node->wrapper_class_id());
1150 }
1151 }
1152}
1153
1154
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001155void GlobalHandles::IterateWeakRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
1156 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1157 Node* node = new_space_nodes_[i];
1158 if (node->has_wrapper_class_id() && node->IsWeak()) {
1159 v->VisitEmbedderReference(node->location(), node->wrapper_class_id());
1160 }
1161 }
1162}
1163
1164
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001165int GlobalHandles::NumberOfWeakHandles() {
1166 int count = 0;
1167 for (NodeIterator it(this); !it.done(); it.Advance()) {
1168 if (it.node()->IsWeakRetainer()) {
1169 count++;
1170 }
1171 }
1172 return count;
1173}
1174
1175
1176int GlobalHandles::NumberOfGlobalObjectWeakHandles() {
1177 int count = 0;
1178 for (NodeIterator it(this); !it.done(); it.Advance()) {
1179 if (it.node()->IsWeakRetainer() &&
1180 it.node()->object()->IsJSGlobalObject()) {
1181 count++;
1182 }
1183 }
1184 return count;
1185}
1186
1187
Steve Blockd0582a62009-12-15 09:54:21 +00001188void GlobalHandles::RecordStats(HeapStats* stats) {
1189 *stats->global_handle_count = 0;
1190 *stats->weak_global_handle_count = 0;
1191 *stats->pending_global_handle_count = 0;
1192 *stats->near_death_global_handle_count = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001193 *stats->free_global_handle_count = 0;
1194 for (NodeIterator it(this); !it.done(); it.Advance()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001195 *stats->global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001196 if (it.node()->state() == Node::WEAK) {
Steve Blockd0582a62009-12-15 09:54:21 +00001197 *stats->weak_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001198 } else if (it.node()->state() == Node::PENDING) {
Steve Blockd0582a62009-12-15 09:54:21 +00001199 *stats->pending_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001200 } else if (it.node()->state() == Node::NEAR_DEATH) {
Steve Blockd0582a62009-12-15 09:54:21 +00001201 *stats->near_death_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001202 } else if (it.node()->state() == Node::FREE) {
1203 *stats->free_global_handle_count += 1;
Steve Blockd0582a62009-12-15 09:54:21 +00001204 }
1205 }
1206}
Steve Blocka7e24c12009-10-30 11:49:00 +00001207
1208#ifdef DEBUG
1209
1210void GlobalHandles::PrintStats() {
1211 int total = 0;
1212 int weak = 0;
1213 int pending = 0;
1214 int near_death = 0;
1215 int destroyed = 0;
1216
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001217 for (NodeIterator it(this); !it.done(); it.Advance()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 total++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001219 if (it.node()->state() == Node::WEAK) weak++;
1220 if (it.node()->state() == Node::PENDING) pending++;
1221 if (it.node()->state() == Node::NEAR_DEATH) near_death++;
1222 if (it.node()->state() == Node::FREE) destroyed++;
Steve Blocka7e24c12009-10-30 11:49:00 +00001223 }
1224
1225 PrintF("Global Handle Statistics:\n");
Ben Murdochda12d292016-06-02 14:46:10 +01001226 PrintF(" allocated memory = %" V8_SIZET_PREFIX V8_PTR_PREFIX "dB\n",
1227 total * sizeof(Node));
Steve Blocka7e24c12009-10-30 11:49:00 +00001228 PrintF(" # weak = %d\n", weak);
1229 PrintF(" # pending = %d\n", pending);
1230 PrintF(" # near_death = %d\n", near_death);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001231 PrintF(" # free = %d\n", destroyed);
Steve Blocka7e24c12009-10-30 11:49:00 +00001232 PrintF(" # total = %d\n", total);
1233}
1234
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001235
Steve Blocka7e24c12009-10-30 11:49:00 +00001236void GlobalHandles::Print() {
1237 PrintF("Global handles:\n");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001238 for (NodeIterator it(this); !it.done(); it.Advance()) {
1239 PrintF(" handle %p to %p%s\n",
1240 reinterpret_cast<void*>(it.node()->location()),
1241 reinterpret_cast<void*>(it.node()->object()),
1242 it.node()->IsWeak() ? " (weak)" : "");
Steve Blocka7e24c12009-10-30 11:49:00 +00001243 }
1244}
1245
1246#endif
1247
Steve Block44f0eee2011-05-26 01:26:41 +01001248
1249
1250void GlobalHandles::AddObjectGroup(Object*** handles,
1251 size_t length,
1252 v8::RetainedObjectInfo* info) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001253#ifdef DEBUG
1254 for (size_t i = 0; i < length; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 DCHECK(!Node::FromLocation(handles[i])->is_independent());
Ben Murdoch257744e2011-11-30 15:57:28 +00001256 }
1257#endif
Ben Murdoch8b112d22011-06-08 16:22:53 +01001258 if (length == 0) {
1259 if (info != NULL) info->Dispose();
1260 return;
Steve Block44f0eee2011-05-26 01:26:41 +01001261 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001262 ObjectGroup* group = new ObjectGroup(length);
1263 for (size_t i = 0; i < length; ++i)
1264 group->objects[i] = handles[i];
1265 group->info = info;
1266 object_groups_.Add(group);
Steve Blocka7e24c12009-10-30 11:49:00 +00001267}
1268
Steve Block44f0eee2011-05-26 01:26:41 +01001269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270void GlobalHandles::SetObjectGroupId(Object** handle,
1271 UniqueId id) {
1272 object_group_connections_.Add(ObjectGroupConnection(id, handle));
1273}
1274
1275
1276void GlobalHandles::SetRetainedObjectInfo(UniqueId id,
1277 RetainedObjectInfo* info) {
1278 retainer_infos_.Add(ObjectGroupRetainerInfo(id, info));
1279}
1280
1281
1282void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) {
1283 DCHECK(!Node::FromLocation(child)->is_independent());
1284 implicit_ref_connections_.Add(ObjectGroupConnection(id, child));
1285}
1286
1287
1288void GlobalHandles::SetReference(HeapObject** parent, Object** child) {
1289 DCHECK(!Node::FromLocation(child)->is_independent());
1290 ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1);
1291 group->children[0] = child;
1292 implicit_ref_groups_.Add(group);
Steve Blocka7e24c12009-10-30 11:49:00 +00001293}
1294
1295
1296void GlobalHandles::RemoveObjectGroups() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297 for (int i = 0; i < object_groups_.length(); i++)
1298 delete object_groups_.at(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001299 object_groups_.Clear();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001300 for (int i = 0; i < retainer_infos_.length(); ++i)
1301 retainer_infos_[i].info->Dispose();
1302 retainer_infos_.Clear();
1303 object_group_connections_.Clear();
1304 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00001305}
1306
Steve Block44f0eee2011-05-26 01:26:41 +01001307
1308void GlobalHandles::RemoveImplicitRefGroups() {
1309 for (int i = 0; i < implicit_ref_groups_.length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001310 delete implicit_ref_groups_.at(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001311 }
1312 implicit_ref_groups_.Clear();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001313 implicit_ref_connections_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +01001314}
1315
1316
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001317void GlobalHandles::TearDown() {
1318 // TODO(1428): invoke weak callbacks.
1319}
1320
1321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() {
1323 if (object_group_connections_.length() == 0) {
1324 for (int i = 0; i < retainer_infos_.length(); ++i)
1325 retainer_infos_[i].info->Dispose();
1326 retainer_infos_.Clear();
1327 implicit_ref_connections_.Clear();
1328 return;
1329 }
1330
1331 object_group_connections_.Sort();
1332 retainer_infos_.Sort();
1333 implicit_ref_connections_.Sort();
1334
1335 int info_index = 0; // For iterating retainer_infos_.
1336 UniqueId current_group_id(0);
1337 int current_group_start = 0;
1338
1339 int current_implicit_refs_start = 0;
1340 int current_implicit_refs_end = 0;
1341 for (int i = 0; i <= object_group_connections_.length(); ++i) {
1342 if (i == 0)
1343 current_group_id = object_group_connections_[i].id;
1344 if (i == object_group_connections_.length() ||
1345 current_group_id != object_group_connections_[i].id) {
1346 // Group detected: objects in indices [current_group_start, i[.
1347
1348 // Find out which implicit references are related to this group. (We want
1349 // to ignore object groups which only have 1 object, but that object is
1350 // needed as a representative object for the implicit refrerence group.)
1351 while (current_implicit_refs_start < implicit_ref_connections_.length() &&
1352 implicit_ref_connections_[current_implicit_refs_start].id <
1353 current_group_id)
1354 ++current_implicit_refs_start;
1355 current_implicit_refs_end = current_implicit_refs_start;
1356 while (current_implicit_refs_end < implicit_ref_connections_.length() &&
1357 implicit_ref_connections_[current_implicit_refs_end].id ==
1358 current_group_id)
1359 ++current_implicit_refs_end;
1360
1361 if (current_implicit_refs_end > current_implicit_refs_start) {
1362 // Find a representative object for the implicit references.
1363 HeapObject** representative = NULL;
1364 for (int j = current_group_start; j < i; ++j) {
1365 Object** object = object_group_connections_[j].object;
1366 if ((*object)->IsHeapObject()) {
1367 representative = reinterpret_cast<HeapObject**>(object);
1368 break;
1369 }
1370 }
1371 if (representative) {
1372 ImplicitRefGroup* group = new ImplicitRefGroup(
1373 representative,
1374 current_implicit_refs_end - current_implicit_refs_start);
1375 for (int j = current_implicit_refs_start;
1376 j < current_implicit_refs_end;
1377 ++j) {
1378 group->children[j - current_implicit_refs_start] =
1379 implicit_ref_connections_[j].object;
1380 }
1381 implicit_ref_groups_.Add(group);
1382 }
1383 current_implicit_refs_start = current_implicit_refs_end;
1384 }
1385
1386 // Find a RetainedObjectInfo for the group.
1387 RetainedObjectInfo* info = NULL;
1388 while (info_index < retainer_infos_.length() &&
1389 retainer_infos_[info_index].id < current_group_id) {
1390 retainer_infos_[info_index].info->Dispose();
1391 ++info_index;
1392 }
1393 if (info_index < retainer_infos_.length() &&
1394 retainer_infos_[info_index].id == current_group_id) {
1395 // This object group has an associated ObjectGroupRetainerInfo.
1396 info = retainer_infos_[info_index].info;
1397 ++info_index;
1398 }
1399
1400 // Ignore groups which only contain one object.
1401 if (i > current_group_start + 1) {
1402 ObjectGroup* group = new ObjectGroup(i - current_group_start);
1403 for (int j = current_group_start; j < i; ++j) {
1404 group->objects[j - current_group_start] =
1405 object_group_connections_[j].object;
1406 }
1407 group->info = info;
1408 object_groups_.Add(group);
1409 } else if (info) {
1410 info->Dispose();
1411 }
1412
1413 if (i < object_group_connections_.length()) {
1414 current_group_id = object_group_connections_[i].id;
1415 current_group_start = i;
1416 }
1417 }
1418 }
1419 object_group_connections_.Clear();
1420 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
1421 retainer_infos_.Clear();
1422 implicit_ref_connections_.Clear();
1423}
1424
1425
1426EternalHandles::EternalHandles() : size_(0) {
1427 for (unsigned i = 0; i < arraysize(singleton_handles_); i++) {
1428 singleton_handles_[i] = kInvalidIndex;
1429 }
1430}
1431
1432
1433EternalHandles::~EternalHandles() {
1434 for (int i = 0; i < blocks_.length(); i++) delete[] blocks_[i];
1435}
1436
1437
1438void EternalHandles::IterateAllRoots(ObjectVisitor* visitor) {
1439 int limit = size_;
1440 for (int i = 0; i < blocks_.length(); i++) {
1441 DCHECK(limit > 0);
1442 Object** block = blocks_[i];
1443 visitor->VisitPointers(block, block + Min(limit, kSize));
1444 limit -= kSize;
1445 }
1446}
1447
1448
1449void EternalHandles::IterateNewSpaceRoots(ObjectVisitor* visitor) {
1450 for (int i = 0; i < new_space_indices_.length(); i++) {
1451 visitor->VisitPointer(GetLocation(new_space_indices_[i]));
1452 }
1453}
1454
1455
1456void EternalHandles::PostGarbageCollectionProcessing(Heap* heap) {
1457 int last = 0;
1458 for (int i = 0; i < new_space_indices_.length(); i++) {
1459 int index = new_space_indices_[i];
1460 if (heap->InNewSpace(*GetLocation(index))) {
1461 new_space_indices_[last++] = index;
1462 }
1463 }
1464 new_space_indices_.Rewind(last);
1465}
1466
1467
1468void EternalHandles::Create(Isolate* isolate, Object* object, int* index) {
1469 DCHECK_EQ(kInvalidIndex, *index);
1470 if (object == NULL) return;
1471 DCHECK_NE(isolate->heap()->the_hole_value(), object);
1472 int block = size_ >> kShift;
1473 int offset = size_ & kMask;
1474 // need to resize
1475 if (offset == 0) {
1476 Object** next_block = new Object*[kSize];
1477 Object* the_hole = isolate->heap()->the_hole_value();
1478 MemsetPointer(next_block, the_hole, kSize);
1479 blocks_.Add(next_block);
1480 }
1481 DCHECK_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]);
1482 blocks_[block][offset] = object;
1483 if (isolate->heap()->InNewSpace(object)) {
1484 new_space_indices_.Add(size_);
1485 }
1486 *index = size_++;
1487}
1488
1489
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001490} // namespace internal
1491} // namespace v8