blob: dffa2371136bf57eb05838dfcbb7c82622b6e1df [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);
85 set_in_new_space_list(false);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000086 parameter_or_next_free_.next_free = *first_free;
87 *first_free = this;
88 }
89
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 void Acquire(Object* object) {
91 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +000092 object_ = object;
Steve Block44f0eee2011-05-26 01:26:41 +010093 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 set_independent(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000095 if (FLAG_scavenge_reclaim_unmodified_objects) {
96 set_active(false);
97 } else {
98 set_partially_dependent(false);
99 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 set_state(NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 parameter_or_next_free_.parameter = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 weak_callback_ = NULL;
103 IncreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 }
105
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400106 void Zap() {
107 DCHECK(IsInUse());
108 // Zap the values for eager trapping.
109 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
110 }
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 void Release() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400113 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 set_state(FREE);
115 // Zap the values for eager trapping.
116 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
117 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
118 set_independent(false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 if (FLAG_scavenge_reclaim_unmodified_objects) {
120 set_active(false);
121 } else {
122 set_partially_dependent(false);
123 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 weak_callback_ = NULL;
125 DecreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 }
127
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 // Object slot accessors.
129 Object* object() const { return object_; }
130 Object** location() { return &object_; }
131 Handle<Object> handle() { return Handle<Object>(location()); }
132
133 // Wrapper class ID accessors.
134 bool has_wrapper_class_id() const {
135 return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId;
136 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 uint16_t wrapper_class_id() const { return class_id_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139
140 // State and flag accessors.
141
142 State state() const {
143 return NodeState::decode(flags_);
144 }
145 void set_state(State state) {
146 flags_ = NodeState::update(flags_, state);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000147 }
148
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 bool is_independent() {
150 return IsIndependent::decode(flags_);
151 }
152 void set_independent(bool v) {
153 flags_ = IsIndependent::update(flags_, v);
154 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000155
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 bool is_partially_dependent() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157 CHECK(!FLAG_scavenge_reclaim_unmodified_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 return IsPartiallyDependent::decode(flags_);
159 }
160 void set_partially_dependent(bool v) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 CHECK(!FLAG_scavenge_reclaim_unmodified_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 flags_ = IsPartiallyDependent::update(flags_, v);
163 }
164
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 bool is_active() {
166 CHECK(FLAG_scavenge_reclaim_unmodified_objects);
167 return IsActive::decode(flags_);
168 }
169 void set_active(bool v) {
170 CHECK(FLAG_scavenge_reclaim_unmodified_objects);
171 flags_ = IsActive::update(flags_, v);
172 }
173
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 bool is_in_new_space_list() {
175 return IsInNewSpaceList::decode(flags_);
176 }
177 void set_in_new_space_list(bool v) {
178 flags_ = IsInNewSpaceList::update(flags_, v);
179 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000180
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400181 WeaknessType weakness_type() const {
182 return NodeWeaknessType::decode(flags_);
183 }
184 void set_weakness_type(WeaknessType weakness_type) {
185 flags_ = NodeWeaknessType::update(flags_, weakness_type);
186 }
187
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188 bool IsNearDeath() const {
189 // Check for PENDING to ensure correct answer when processing callbacks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 return state() == PENDING || state() == NEAR_DEATH;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000191 }
192
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 bool IsWeak() const { return state() == WEAK; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000194
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400195 bool IsInUse() const { return state() != FREE; }
196
Ben Murdochc5610432016-08-08 18:44:38 +0100197 bool IsPendingPhantomCallback() const {
198 return state() == PENDING &&
199 (weakness_type() == PHANTOM_WEAK ||
200 weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
201 }
202
203 bool IsPendingPhantomResetHandle() const {
204 return state() == PENDING && weakness_type() == PHANTOM_WEAK_RESET_HANDLE;
205 }
206
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000207 bool IsRetainer() const {
208 return state() != FREE &&
Ben Murdochc5610432016-08-08 18:44:38 +0100209 !(state() == NEAR_DEATH && weakness_type() != FINALIZER_WEAK);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000210 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000211
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 bool IsStrongRetainer() const { return state() == NORMAL; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000213
214 bool IsWeakRetainer() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 return state() == WEAK || state() == PENDING ||
Ben Murdochc5610432016-08-08 18:44:38 +0100216 (state() == NEAR_DEATH && weakness_type() == FINALIZER_WEAK);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000217 }
218
219 void MarkPending() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220 DCHECK(state() == WEAK);
221 set_state(PENDING);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222 }
223
224 // Independent flag accessors.
225 void MarkIndependent() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 set_independent(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000228 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 void MarkPartiallyDependent() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400231 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) {
233 set_partially_dependent(true);
234 }
235 }
236 void clear_partially_dependent() { set_partially_dependent(false); }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000237
238 // Callback accessor.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 // TODO(svenpanne) Re-enable or nuke later.
240 // WeakReferenceCallback callback() { return callback_; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000241
242 // Callback parameter accessors.
243 void set_parameter(void* parameter) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400244 DCHECK(IsInUse());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000245 parameter_or_next_free_.parameter = parameter;
246 }
247 void* parameter() const {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400248 DCHECK(IsInUse());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000249 return parameter_or_next_free_.parameter;
250 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252 // Accessors for next free node in the free list.
253 Node* next_free() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 return parameter_or_next_free_.next_free;
256 }
257 void set_next_free(Node* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 DCHECK(state() == FREE);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 parameter_or_next_free_.next_free = value;
260 }
261
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000262 void MakeWeak(void* parameter,
263 WeakCallbackInfo<void>::Callback phantom_callback,
264 v8::WeakCallbackType type) {
265 DCHECK(phantom_callback != nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400266 DCHECK(IsInUse());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000267 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400268 set_state(WEAK);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000269 switch (type) {
270 case v8::WeakCallbackType::kParameter:
271 set_weakness_type(PHANTOM_WEAK);
272 break;
273 case v8::WeakCallbackType::kInternalFields:
Ben Murdochc5610432016-08-08 18:44:38 +0100274 set_weakness_type(PHANTOM_WEAK_2_INTERNAL_FIELDS);
275 break;
276 case v8::WeakCallbackType::kFinalizer:
277 set_weakness_type(FINALIZER_WEAK);
278 break;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400279 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 set_parameter(parameter);
Ben Murdochc5610432016-08-08 18:44:38 +0100281 weak_callback_ = phantom_callback;
282 }
283
284 void MakeWeak(Object*** location_addr) {
285 DCHECK(IsInUse());
286 CHECK_NE(object_, reinterpret_cast<Object*>(kGlobalHandleZapValue));
287 set_state(WEAK);
288 set_weakness_type(PHANTOM_WEAK_RESET_HANDLE);
289 set_parameter(location_addr);
290 weak_callback_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400291 }
292
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 void* ClearWeakness() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400294 DCHECK(IsInUse());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 void* p = parameter();
296 set_state(NORMAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 set_parameter(NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 return p;
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 }
300
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400301 void CollectPhantomCallbackData(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 Isolate* isolate,
303 List<PendingPhantomCallback>* pending_phantom_callbacks) {
304 DCHECK(weakness_type() == PHANTOM_WEAK ||
305 weakness_type() == PHANTOM_WEAK_2_INTERNAL_FIELDS);
306 DCHECK(state() == PENDING);
Ben Murdochc5610432016-08-08 18:44:38 +0100307 DCHECK(weak_callback_ != nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 void* internal_fields[v8::kInternalFieldsInWeakCallback] = {nullptr,
310 nullptr};
311 if (weakness_type() != PHANTOM_WEAK && object()->IsJSObject()) {
312 auto jsobject = JSObject::cast(object());
313 int field_count = jsobject->GetInternalFieldCount();
314 for (int i = 0; i < v8::kInternalFieldsInWeakCallback; ++i) {
315 if (field_count == i) break;
316 auto field = jsobject->GetInternalField(i);
317 if (field->IsSmi()) internal_fields[i] = field;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400318 }
319 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320
321 // Zap with something dangerous.
322 *location() = reinterpret_cast<Object*>(0x6057ca11);
323
324 typedef v8::WeakCallbackInfo<void> Data;
325 auto callback = reinterpret_cast<Data::Callback>(weak_callback_);
326 pending_phantom_callbacks->Add(
327 PendingPhantomCallback(this, callback, parameter(), internal_fields));
328 DCHECK(IsInUse());
329 set_state(NEAR_DEATH);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400330 }
331
Ben Murdochc5610432016-08-08 18:44:38 +0100332 void ResetPhantomHandle() {
333 DCHECK(weakness_type() == PHANTOM_WEAK_RESET_HANDLE);
334 DCHECK(state() == PENDING);
335 DCHECK(weak_callback_ == nullptr);
336 Object*** handle = reinterpret_cast<Object***>(parameter());
337 *handle = nullptr;
338 Release();
339 }
340
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 bool PostGarbageCollectionProcessing(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342 // Handles only weak handles (not phantom) that are dying.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 if (state() != Node::PENDING) return false;
344 if (weak_callback_ == NULL) {
345 Release();
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100346 return false;
347 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 set_state(NEAR_DEATH);
Steve Blocka7e24c12009-10-30 11:49:00 +0000349
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400350 // Check that we are not passing a finalized external string to
351 // the callback.
352 DCHECK(!object_->IsExternalOneByteString() ||
353 ExternalOneByteString::cast(object_)->resource() != NULL);
354 DCHECK(!object_->IsExternalTwoByteString() ||
355 ExternalTwoByteString::cast(object_)->resource() != NULL);
Ben Murdochc5610432016-08-08 18:44:38 +0100356 if (weakness_type() != FINALIZER_WEAK) {
357 return false;
358 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400360 // Leaving V8.
361 VMState<EXTERNAL> vmstate(isolate);
362 HandleScope handle_scope(isolate);
Ben Murdochc5610432016-08-08 18:44:38 +0100363 void* internal_fields[v8::kInternalFieldsInWeakCallback] = {nullptr,
364 nullptr};
365 v8::WeakCallbackInfo<void> data(reinterpret_cast<v8::Isolate*>(isolate),
366 parameter(), internal_fields, nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400367 weak_callback_(data);
368
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100369 // Absence of explicit cleanup or revival of weak handle
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100370 // in most of the cases would lead to memory leak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 CHECK(state() != NEAR_DEATH);
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 return true;
373 }
374
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 inline GlobalHandles* GetGlobalHandles();
376
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000377 private:
378 inline NodeBlock* FindBlock();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379 inline void IncreaseBlockUses();
380 inline void DecreaseBlockUses();
Steve Blocka7e24c12009-10-30 11:49:00 +0000381
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000382 // Storage for object pointer.
383 // Placed first to avoid offset computation.
384 Object* object_;
385
386 // Next word stores class_id, index, state, and independent.
387 // Note: the most aligned fields should go first.
388
389 // Wrapper class ID.
Steve Block44f0eee2011-05-26 01:26:41 +0100390 uint16_t class_id_;
391
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000392 // Index in the containing handle block.
393 uint8_t index_;
394
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 // This stores three flags (independent, partially_dependent and
396 // in_new_space_list) and a State.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397 class NodeState : public BitField<State, 0, 3> {};
398 class IsIndependent : public BitField<bool, 3, 1> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000399 // The following two fields are mutually exclusive
400 class IsActive : public BitField<bool, 4, 1> {};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400401 class IsPartiallyDependent : public BitField<bool, 4, 1> {};
402 class IsInNewSpaceList : public BitField<bool, 5, 1> {};
403 class NodeWeaknessType : public BitField<WeaknessType, 6, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000404
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405 uint8_t flags_;
Ben Murdoch257744e2011-11-30 15:57:28 +0000406
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 // Handle specific callback - might be a weak reference in disguise.
Ben Murdochc5610432016-08-08 18:44:38 +0100408 WeakCallbackInfo<void>::Callback weak_callback_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000409
410 // Provided data for callback. In FREE state, this is used for
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 // the free list link.
412 union {
413 void* parameter;
414 Node* next_free;
415 } parameter_or_next_free_;
416
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000417 DISALLOW_COPY_AND_ASSIGN(Node);
Steve Blocka7e24c12009-10-30 11:49:00 +0000418};
419
420
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000421class GlobalHandles::NodeBlock {
422 public:
423 static const int kSize = 256;
Steve Blockd0582a62009-12-15 09:54:21 +0000424
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next)
426 : next_(next),
427 used_nodes_(0),
428 next_used_(NULL),
429 prev_used_(NULL),
430 global_handles_(global_handles) {}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000431
432 void PutNodesOnFreeList(Node** first_free) {
433 for (int i = kSize - 1; i >= 0; --i) {
434 nodes_[i].Initialize(i, first_free);
435 }
436 }
437
438 Node* node_at(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 DCHECK(0 <= index && index < kSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000440 return &nodes_[index];
441 }
442
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 void IncreaseUses() {
444 DCHECK(used_nodes_ < kSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445 if (used_nodes_++ == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 NodeBlock* old_first = global_handles_->first_used_block_;
447 global_handles_->first_used_block_ = this;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000448 next_used_ = old_first;
449 prev_used_ = NULL;
450 if (old_first == NULL) return;
451 old_first->prev_used_ = this;
452 }
453 }
454
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 void DecreaseUses() {
456 DCHECK(used_nodes_ > 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000457 if (--used_nodes_ == 0) {
458 if (next_used_ != NULL) next_used_->prev_used_ = prev_used_;
459 if (prev_used_ != NULL) prev_used_->next_used_ = next_used_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 if (this == global_handles_->first_used_block_) {
461 global_handles_->first_used_block_ = next_used_;
Ben Murdochbb769b22010-08-11 14:56:33 +0100462 }
463 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000464 }
Ben Murdochbb769b22010-08-11 14:56:33 +0100465
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 GlobalHandles* global_handles() { return global_handles_; }
467
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000468 // Next block in the list of all blocks.
469 NodeBlock* next() const { return next_; }
Steve Blockd0582a62009-12-15 09:54:21 +0000470
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000471 // Next/previous block in the list of blocks with used nodes.
472 NodeBlock* next_used() const { return next_used_; }
473 NodeBlock* prev_used() const { return prev_used_; }
Steve Blockd0582a62009-12-15 09:54:21 +0000474
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000475 private:
476 Node nodes_[kSize];
477 NodeBlock* const next_;
478 int used_nodes_;
479 NodeBlock* next_used_;
480 NodeBlock* prev_used_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 GlobalHandles* global_handles_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000482};
Steve Blockd0582a62009-12-15 09:54:21 +0000483
Steve Blockd0582a62009-12-15 09:54:21 +0000484
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485GlobalHandles* GlobalHandles::Node::GetGlobalHandles() {
486 return FindBlock()->global_handles();
487}
488
489
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000490GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
491 intptr_t ptr = reinterpret_cast<intptr_t>(this);
492 ptr = ptr - index_ * sizeof(Node);
493 NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494 DCHECK(block->node_at(index_) == this);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000495 return block;
496}
Steve Blockd0582a62009-12-15 09:54:21 +0000497
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000498
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499void GlobalHandles::Node::IncreaseBlockUses() {
500 NodeBlock* node_block = FindBlock();
501 node_block->IncreaseUses();
502 GlobalHandles* global_handles = node_block->global_handles();
503 global_handles->isolate()->counters()->global_handles()->Increment();
504 global_handles->number_of_global_handles_++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000505}
506
507
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508void GlobalHandles::Node::DecreaseBlockUses() {
509 NodeBlock* node_block = FindBlock();
510 GlobalHandles* global_handles = node_block->global_handles();
511 parameter_or_next_free_.next_free = global_handles->first_free_;
512 global_handles->first_free_ = this;
513 node_block->DecreaseUses();
514 global_handles->isolate()->counters()->global_handles()->Decrement();
515 global_handles->number_of_global_handles_--;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000516}
517
518
519class GlobalHandles::NodeIterator {
520 public:
521 explicit NodeIterator(GlobalHandles* global_handles)
522 : block_(global_handles->first_used_block_),
523 index_(0) {}
524
525 bool done() const { return block_ == NULL; }
526
527 Node* node() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 DCHECK(!done());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000529 return block_->node_at(index_);
530 }
531
532 void Advance() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 DCHECK(!done());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000534 if (++index_ < NodeBlock::kSize) return;
535 index_ = 0;
536 block_ = block_->next_used();
537 }
538
539 private:
540 NodeBlock* block_;
541 int index_;
542
543 DISALLOW_COPY_AND_ASSIGN(NodeIterator);
Steve Blockd0582a62009-12-15 09:54:21 +0000544};
545
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000546class GlobalHandles::PendingPhantomCallbacksSecondPassTask
547 : public v8::internal::CancelableTask {
548 public:
549 // Takes ownership of the contents of pending_phantom_callbacks, leaving it in
550 // the same state it would be after a call to Clear().
551 PendingPhantomCallbacksSecondPassTask(
552 List<PendingPhantomCallback>* pending_phantom_callbacks, Isolate* isolate)
553 : CancelableTask(isolate) {
554 pending_phantom_callbacks_.Swap(pending_phantom_callbacks);
555 }
556
557 void RunInternal() override {
Ben Murdochc5610432016-08-08 18:44:38 +0100558 TRACE_EVENT0("v8", "V8.GCPhantomHandleProcessingCallback");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 isolate()->heap()->CallGCPrologueCallbacks(
560 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
561 InvokeSecondPassPhantomCallbacks(&pending_phantom_callbacks_, isolate());
562 isolate()->heap()->CallGCEpilogueCallbacks(
563 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
564 }
565
566 private:
567 List<PendingPhantomCallback> pending_phantom_callbacks_;
568
569 DISALLOW_COPY_AND_ASSIGN(PendingPhantomCallbacksSecondPassTask);
570};
571
Steve Block44f0eee2011-05-26 01:26:41 +0100572GlobalHandles::GlobalHandles(Isolate* isolate)
573 : isolate_(isolate),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100574 number_of_global_handles_(0),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000575 first_block_(NULL),
576 first_used_block_(NULL),
Steve Block44f0eee2011-05-26 01:26:41 +0100577 first_free_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 post_gc_processing_count_(0),
Ben Murdochc5610432016-08-08 18:44:38 +0100579 number_of_phantom_handle_resets_(0),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 object_group_connections_(kObjectGroupConnectionsCapacity) {}
Steve Block44f0eee2011-05-26 01:26:41 +0100581
Steve Block44f0eee2011-05-26 01:26:41 +0100582GlobalHandles::~GlobalHandles() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000583 NodeBlock* block = first_block_;
584 while (block != NULL) {
585 NodeBlock* tmp = block->next();
586 delete block;
587 block = tmp;
588 }
589 first_block_ = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +0100590}
Steve Blockd0582a62009-12-15 09:54:21 +0000591
592
Steve Blocka7e24c12009-10-30 11:49:00 +0000593Handle<Object> GlobalHandles::Create(Object* value) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000594 if (first_free_ == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 first_block_ = new NodeBlock(this, first_block_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000596 first_block_->PutNodesOnFreeList(&first_free_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000597 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 DCHECK(first_free_ != NULL);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000599 // Take the first node in the free list.
600 Node* result = first_free_;
601 first_free_ = result->next_free();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 result->Acquire(value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000603 if (isolate_->heap()->InNewSpace(value) &&
604 !result->is_in_new_space_list()) {
605 new_space_nodes_.Add(result);
606 result->set_in_new_space_list(true);
607 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 return result->handle();
609}
610
611
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612Handle<Object> GlobalHandles::CopyGlobal(Object** location) {
613 DCHECK(location != NULL);
614 return Node::FromLocation(location)->GetGlobalHandles()->Create(*location);
615}
616
617
Steve Blocka7e24c12009-10-30 11:49:00 +0000618void GlobalHandles::Destroy(Object** location) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619 if (location != NULL) Node::FromLocation(location)->Release();
Steve Blocka7e24c12009-10-30 11:49:00 +0000620}
621
622
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000623typedef v8::WeakCallbackInfo<void>::Callback GenericCallback;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400624
625
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000626void GlobalHandles::MakeWeak(Object** location, void* parameter,
627 GenericCallback phantom_callback,
628 v8::WeakCallbackType type) {
629 Node::FromLocation(location)->MakeWeak(parameter, phantom_callback, type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400630}
631
Ben Murdochc5610432016-08-08 18:44:38 +0100632void GlobalHandles::MakeWeak(Object*** location_addr) {
633 Node::FromLocation(*location_addr)->MakeWeak(location_addr);
634}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400635
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636void* GlobalHandles::ClearWeakness(Object** location) {
637 return Node::FromLocation(location)->ClearWeakness();
Steve Blocka7e24c12009-10-30 11:49:00 +0000638}
639
640
Ben Murdoch257744e2011-11-30 15:57:28 +0000641void GlobalHandles::MarkIndependent(Object** location) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000642 Node::FromLocation(location)->MarkIndependent();
Ben Murdoch257744e2011-11-30 15:57:28 +0000643}
644
645
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646void GlobalHandles::MarkPartiallyDependent(Object** location) {
647 Node::FromLocation(location)->MarkPartiallyDependent();
648}
649
650
651bool GlobalHandles::IsIndependent(Object** location) {
652 return Node::FromLocation(location)->is_independent();
653}
654
655
Steve Blocka7e24c12009-10-30 11:49:00 +0000656bool GlobalHandles::IsNearDeath(Object** location) {
657 return Node::FromLocation(location)->IsNearDeath();
658}
659
660
661bool GlobalHandles::IsWeak(Object** location) {
662 return Node::FromLocation(location)->IsWeak();
663}
664
Steve Blocka7e24c12009-10-30 11:49:00 +0000665void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000666 for (NodeIterator it(this); !it.done(); it.Advance()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400667 Node* node = it.node();
668 if (node->IsWeakRetainer()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000669 // Pending weak phantom handles die immediately. Everything else survives.
Ben Murdochc5610432016-08-08 18:44:38 +0100670 if (node->IsPendingPhantomResetHandle()) {
671 node->ResetPhantomHandle();
672 ++number_of_phantom_handle_resets_;
673 } else if (node->IsPendingPhantomCallback()) {
674 node->CollectPhantomCallbackData(isolate(),
675 &pending_phantom_callbacks_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400676 } else {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400677 v->VisitPointer(node->location());
678 }
679 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000680 }
681}
682
683
Steve Blocka7e24c12009-10-30 11:49:00 +0000684void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000685 for (NodeIterator it(this); !it.done(); it.Advance()) {
686 if (it.node()->IsWeak() && f(it.node()->location())) {
687 it.node()->MarkPending();
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 }
689 }
690}
691
692
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000693void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
694 for (int i = 0; i < new_space_nodes_.length(); ++i) {
695 Node* node = new_space_nodes_[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000696 if (FLAG_scavenge_reclaim_unmodified_objects) {
697 if (node->IsStrongRetainer() ||
698 (node->IsWeakRetainer() && !node->is_independent() &&
699 node->is_active())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 v->VisitPointer(node->location());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 }
702 } else {
703 if (node->IsStrongRetainer() ||
704 (node->IsWeakRetainer() && !node->is_independent() &&
705 !node->is_partially_dependent())) {
706 v->VisitPointer(node->location());
707 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000708 }
709 }
710}
711
712
713void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles(
714 WeakSlotCallbackWithHeap f) {
715 for (int i = 0; i < new_space_nodes_.length(); ++i) {
716 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 DCHECK(node->is_in_new_space_list());
718 if ((node->is_independent() || node->is_partially_dependent()) &&
719 node->IsWeak() && f(isolate_->heap(), node->location())) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000720 node->MarkPending();
721 }
722 }
723}
724
725
726void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) {
727 for (int i = 0; i < new_space_nodes_.length(); ++i) {
728 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 DCHECK(node->is_in_new_space_list());
730 if ((node->is_independent() || node->is_partially_dependent()) &&
731 node->IsWeakRetainer()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 // Pending weak phantom handles die immediately. Everything else survives.
Ben Murdochc5610432016-08-08 18:44:38 +0100733 if (node->IsPendingPhantomResetHandle()) {
734 node->ResetPhantomHandle();
735 ++number_of_phantom_handle_resets_;
736 } else if (node->IsPendingPhantomCallback()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000737 node->CollectPhantomCallbackData(isolate(),
738 &pending_phantom_callbacks_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400739 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000740 v->VisitPointer(node->location());
741 }
742 }
743 }
744}
745
746
747void GlobalHandles::IdentifyWeakUnmodifiedObjects(
748 WeakSlotCallback is_unmodified) {
749 for (int i = 0; i < new_space_nodes_.length(); ++i) {
750 Node* node = new_space_nodes_[i];
751 if (node->IsWeak() && !is_unmodified(node->location())) {
752 node->set_active(true);
753 }
754 }
755}
756
757
758void GlobalHandles::MarkNewSpaceWeakUnmodifiedObjectsPending(
759 WeakSlotCallbackWithHeap is_unscavenged) {
760 for (int i = 0; i < new_space_nodes_.length(); ++i) {
761 Node* node = new_space_nodes_[i];
762 DCHECK(node->is_in_new_space_list());
763 if ((node->is_independent() || !node->is_active()) && node->IsWeak() &&
764 is_unscavenged(isolate_->heap(), node->location())) {
765 node->MarkPending();
766 }
767 }
768}
769
770
771void GlobalHandles::IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v) {
772 for (int i = 0; i < new_space_nodes_.length(); ++i) {
773 Node* node = new_space_nodes_[i];
774 DCHECK(node->is_in_new_space_list());
775 if ((node->is_independent() || !node->is_active()) &&
776 node->IsWeakRetainer()) {
777 // Pending weak phantom handles die immediately. Everything else survives.
Ben Murdochc5610432016-08-08 18:44:38 +0100778 if (node->IsPendingPhantomResetHandle()) {
779 node->ResetPhantomHandle();
780 ++number_of_phantom_handle_resets_;
781 } else if (node->IsPendingPhantomCallback()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000782 node->CollectPhantomCallbackData(isolate(),
783 &pending_phantom_callbacks_);
784 } else {
785 v->VisitPointer(node->location());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400786 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000787 }
788 }
789}
790
791
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v,
793 WeakSlotCallbackWithHeap can_skip) {
794 ComputeObjectGroupsAndImplicitReferences();
795 int last = 0;
796 bool any_group_was_visited = false;
797 for (int i = 0; i < object_groups_.length(); i++) {
798 ObjectGroup* entry = object_groups_.at(i);
799 DCHECK(entry != NULL);
800
801 Object*** objects = entry->objects;
802 bool group_should_be_visited = false;
803 for (size_t j = 0; j < entry->length; j++) {
804 Object* object = *objects[j];
805 if (object->IsHeapObject()) {
806 if (!can_skip(isolate_->heap(), &object)) {
807 group_should_be_visited = true;
808 break;
809 }
810 }
811 }
812
813 if (!group_should_be_visited) {
814 object_groups_[last++] = entry;
815 continue;
816 }
817
818 // An object in the group requires visiting, so iterate over all
819 // objects in the group.
820 for (size_t j = 0; j < entry->length; ++j) {
821 Object* object = *objects[j];
822 if (object->IsHeapObject()) {
823 v->VisitPointer(&object);
824 any_group_was_visited = true;
825 }
826 }
827
828 // Once the entire group has been iterated over, set the object
829 // group to NULL so it won't be processed again.
830 delete entry;
831 object_groups_.at(i) = NULL;
832 }
833 object_groups_.Rewind(last);
834 return any_group_was_visited;
835}
836
Ben Murdochda12d292016-06-02 14:46:10 +0100837namespace {
838// Traces the information about object groups and implicit ref groups given by
839// the embedder to the V8 during each gc prologue.
840class ObjectGroupsTracer {
841 public:
842 explicit ObjectGroupsTracer(Isolate* isolate);
843 void Print();
844
845 private:
846 void PrintObjectGroup(ObjectGroup* group);
847 void PrintImplicitRefGroup(ImplicitRefGroup* group);
848 void PrintObject(Object* object);
849 void PrintConstructor(JSObject* js_object);
850 void PrintInternalFields(JSObject* js_object);
851 Isolate* isolate_;
852 DISALLOW_COPY_AND_ASSIGN(ObjectGroupsTracer);
853};
854
855ObjectGroupsTracer::ObjectGroupsTracer(Isolate* isolate) : isolate_(isolate) {}
856
857void ObjectGroupsTracer::Print() {
858 GlobalHandles* global_handles = isolate_->global_handles();
859
860 PrintIsolate(isolate_, "### Tracing object groups:\n");
861
862 for (auto group : *(global_handles->object_groups())) {
863 PrintObjectGroup(group);
864 }
865 for (auto group : *(global_handles->implicit_ref_groups())) {
866 PrintImplicitRefGroup(group);
867 }
868
869 PrintIsolate(isolate_, "### Tracing object groups finished.\n");
870}
871
872void ObjectGroupsTracer::PrintObject(Object* object) {
873 if (object->IsJSObject()) {
874 JSObject* js_object = JSObject::cast(object);
875
876 PrintF("{ constructor_name: ");
877 PrintConstructor(js_object);
878 PrintF(", hidden_fields: [ ");
879 PrintInternalFields(js_object);
880 PrintF(" ] }\n");
881 } else {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100882 PrintF("object of unexpected type: %p\n", static_cast<void*>(object));
Ben Murdochda12d292016-06-02 14:46:10 +0100883 }
884}
885
886void ObjectGroupsTracer::PrintConstructor(JSObject* js_object) {
887 Object* maybe_constructor = js_object->map()->GetConstructor();
888 if (maybe_constructor->IsJSFunction()) {
889 JSFunction* constructor = JSFunction::cast(maybe_constructor);
890 String* name = String::cast(constructor->shared()->name());
891 if (name->length() == 0) name = constructor->shared()->inferred_name();
892
893 PrintF("%s", name->ToCString().get());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100894 } else if (maybe_constructor->IsNull(isolate_)) {
Ben Murdochda12d292016-06-02 14:46:10 +0100895 if (js_object->IsOddball()) {
896 PrintF("<oddball>");
897 } else {
898 PrintF("<null>");
899 }
900 } else {
901 UNREACHABLE();
902 }
903}
904
905void ObjectGroupsTracer::PrintInternalFields(JSObject* js_object) {
906 for (int i = 0; i < js_object->GetInternalFieldCount(); ++i) {
907 if (i != 0) {
908 PrintF(", ");
909 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100910 PrintF("%p", static_cast<void*>(js_object->GetInternalField(i)));
Ben Murdochda12d292016-06-02 14:46:10 +0100911 }
912}
913
914void ObjectGroupsTracer::PrintObjectGroup(ObjectGroup* group) {
Ben Murdochc5610432016-08-08 18:44:38 +0100915 PrintIsolate(isolate_, "ObjectGroup (size: %" PRIuS ")\n", group->length);
Ben Murdochda12d292016-06-02 14:46:10 +0100916 Object*** objects = group->objects;
917
918 for (size_t i = 0; i < group->length; ++i) {
919 PrintIsolate(isolate_, " - Member: ");
920 PrintObject(*objects[i]);
921 }
922}
923
924void ObjectGroupsTracer::PrintImplicitRefGroup(ImplicitRefGroup* group) {
Ben Murdochc5610432016-08-08 18:44:38 +0100925 PrintIsolate(isolate_, "ImplicitRefGroup (children count: %" PRIuS ")\n",
Ben Murdochda12d292016-06-02 14:46:10 +0100926 group->length);
927 PrintIsolate(isolate_, " - Parent: ");
928 PrintObject(*(group->parent));
929
930 Object*** children = group->children;
931 for (size_t i = 0; i < group->length; ++i) {
932 PrintIsolate(isolate_, " - Child: ");
933 PrintObject(*children[i]);
934 }
935}
936
937} // namespace
938
939void GlobalHandles::PrintObjectGroups() {
940 ObjectGroupsTracer(isolate_).Print();
941}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000943void GlobalHandles::InvokeSecondPassPhantomCallbacks(
944 List<PendingPhantomCallback>* callbacks, Isolate* isolate) {
945 while (callbacks->length() != 0) {
946 auto callback = callbacks->RemoveLast();
947 DCHECK(callback.node() == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000948 // Fire second pass callback
949 callback.Invoke(isolate);
950 }
951}
952
953
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400954int GlobalHandles::PostScavengeProcessing(
955 const int initial_post_gc_processing_count) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956 int freed_nodes = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400957 for (int i = 0; i < new_space_nodes_.length(); ++i) {
958 Node* node = new_space_nodes_[i];
959 DCHECK(node->is_in_new_space_list());
960 if (!node->IsRetainer()) {
961 // Free nodes do not have weak callbacks. Do not use them to compute
962 // the freed_nodes.
963 continue;
964 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000965 // Skip dependent or unmodified handles. Their weak callbacks might expect
966 // to be
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400967 // called between two global garbage collection callbacks which
968 // are not called for minor collections.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000969 if (FLAG_scavenge_reclaim_unmodified_objects) {
970 if (!node->is_independent() && (node->is_active())) {
971 node->set_active(false);
972 continue;
973 }
974 node->set_active(false);
975 } else {
976 if (!node->is_independent() && !node->is_partially_dependent()) {
977 continue;
978 }
979 node->clear_partially_dependent();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400980 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400982 if (node->PostGarbageCollectionProcessing(isolate_)) {
983 if (initial_post_gc_processing_count != post_gc_processing_count_) {
984 // Weak callback triggered another GC and another round of
985 // PostGarbageCollection processing. The current node might
986 // have been deleted in that round, so we need to bail out (or
987 // restart the processing).
988 return freed_nodes;
Steve Blocka7e24c12009-10-30 11:49:00 +0000989 }
990 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400991 if (!node->IsRetainer()) {
992 freed_nodes++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000993 }
994 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400995 return freed_nodes;
996}
997
998
999int GlobalHandles::PostMarkSweepProcessing(
1000 const int initial_post_gc_processing_count) {
1001 int freed_nodes = 0;
1002 for (NodeIterator it(this); !it.done(); it.Advance()) {
1003 if (!it.node()->IsRetainer()) {
1004 // Free nodes do not have weak callbacks. Do not use them to compute
1005 // the freed_nodes.
1006 continue;
1007 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001008 if (FLAG_scavenge_reclaim_unmodified_objects) {
1009 it.node()->set_active(false);
1010 } else {
1011 it.node()->clear_partially_dependent();
1012 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001013 if (it.node()->PostGarbageCollectionProcessing(isolate_)) {
1014 if (initial_post_gc_processing_count != post_gc_processing_count_) {
1015 // See the comment above.
1016 return freed_nodes;
1017 }
1018 }
1019 if (!it.node()->IsRetainer()) {
1020 freed_nodes++;
1021 }
1022 }
1023 return freed_nodes;
1024}
1025
1026
1027void GlobalHandles::UpdateListOfNewSpaceNodes() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001028 int last = 0;
1029 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1030 Node* node = new_space_nodes_[i];
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001031 DCHECK(node->is_in_new_space_list());
1032 if (node->IsRetainer()) {
1033 if (isolate_->heap()->InNewSpace(node->object())) {
1034 new_space_nodes_[last++] = node;
1035 isolate_->heap()->IncrementNodesCopiedInNewSpace();
1036 } else {
1037 node->set_in_new_space_list(false);
1038 isolate_->heap()->IncrementNodesPromoted();
1039 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001041 node->set_in_new_space_list(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001042 isolate_->heap()->IncrementNodesDiedInNewSpace();
Steve Blocka7e24c12009-10-30 11:49:00 +00001043 }
1044 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001045 new_space_nodes_.Rewind(last);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001046 new_space_nodes_.Trim();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001047}
1048
1049
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001050int GlobalHandles::DispatchPendingPhantomCallbacks(
1051 bool synchronous_second_pass) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001052 int freed_nodes = 0;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001053 List<PendingPhantomCallback> second_pass_callbacks;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001054 {
1055 // The initial pass callbacks must simply clear the nodes.
1056 for (auto i = pending_phantom_callbacks_.begin();
1057 i != pending_phantom_callbacks_.end(); ++i) {
1058 auto callback = i;
1059 // Skip callbacks that have already been processed once.
1060 if (callback->node() == nullptr) continue;
1061 callback->Invoke(isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001062 if (callback->callback()) second_pass_callbacks.Add(*callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 freed_nodes++;
1064 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001065 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001066 pending_phantom_callbacks_.Clear();
1067 if (second_pass_callbacks.length() > 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001068 if (FLAG_optimize_for_size || FLAG_predictable || synchronous_second_pass) {
1069 isolate()->heap()->CallGCPrologueCallbacks(
1070 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001071 InvokeSecondPassPhantomCallbacks(&second_pass_callbacks, isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001072 isolate()->heap()->CallGCEpilogueCallbacks(
1073 GCType::kGCTypeProcessWeakCallbacks, kNoGCCallbackFlags);
1074 } else {
1075 auto task = new PendingPhantomCallbacksSecondPassTask(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001076 &second_pass_callbacks, isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001077 V8::GetCurrentPlatform()->CallOnForegroundThread(
1078 reinterpret_cast<v8::Isolate*>(isolate()), task);
1079 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001080 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081 return freed_nodes;
Steve Blocka7e24c12009-10-30 11:49:00 +00001082}
1083
1084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001085void GlobalHandles::PendingPhantomCallback::Invoke(Isolate* isolate) {
1086 Data::Callback* callback_addr = nullptr;
1087 if (node_ != nullptr) {
1088 // Initialize for first pass callback.
1089 DCHECK(node_->state() == Node::NEAR_DEATH);
1090 callback_addr = &callback_;
1091 }
1092 Data data(reinterpret_cast<v8::Isolate*>(isolate), parameter_,
1093 internal_fields_, callback_addr);
1094 Data::Callback callback = callback_;
1095 callback_ = nullptr;
1096 callback(data);
1097 if (node_ != nullptr) {
1098 // Transition to second pass state.
1099 DCHECK(node_->state() == Node::FREE);
1100 node_ = nullptr;
1101 }
1102}
1103
1104
1105int GlobalHandles::PostGarbageCollectionProcessing(
1106 GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001107 // Process weak global handle callbacks. This must be done after the
1108 // GC is completely done, because the callbacks may invoke arbitrary
1109 // API functions.
1110 DCHECK(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
1111 const int initial_post_gc_processing_count = ++post_gc_processing_count_;
1112 int freed_nodes = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001113 bool synchronous_second_pass =
1114 (gc_callback_flags &
Ben Murdoch097c5b22016-05-18 11:27:45 +01001115 (kGCCallbackFlagForced | kGCCallbackFlagCollectAllAvailableGarbage |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001116 kGCCallbackFlagSynchronousPhantomCallbackProcessing)) != 0;
1117 freed_nodes += DispatchPendingPhantomCallbacks(synchronous_second_pass);
1118 if (initial_post_gc_processing_count != post_gc_processing_count_) {
1119 // If the callbacks caused a nested GC, then return. See comment in
1120 // PostScavengeProcessing.
1121 return freed_nodes;
1122 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001123 if (collector == SCAVENGER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001124 freed_nodes += PostScavengeProcessing(initial_post_gc_processing_count);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001125 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001126 freed_nodes += PostMarkSweepProcessing(initial_post_gc_processing_count);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001127 }
1128 if (initial_post_gc_processing_count != post_gc_processing_count_) {
1129 // If the callbacks caused a nested GC, then return. See comment in
1130 // PostScavengeProcessing.
1131 return freed_nodes;
1132 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001133 if (initial_post_gc_processing_count == post_gc_processing_count_) {
1134 UpdateListOfNewSpaceNodes();
1135 }
1136 return freed_nodes;
1137}
1138
1139
Steve Blockd0582a62009-12-15 09:54:21 +00001140void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001141 for (NodeIterator it(this); !it.done(); it.Advance()) {
1142 if (it.node()->IsStrongRetainer()) {
1143 v->VisitPointer(it.node()->location());
Steve Blocka7e24c12009-10-30 11:49:00 +00001144 }
1145 }
1146}
1147
Steve Blockd0582a62009-12-15 09:54:21 +00001148
1149void GlobalHandles::IterateAllRoots(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001150 for (NodeIterator it(this); !it.done(); it.Advance()) {
1151 if (it.node()->IsRetainer()) {
1152 v->VisitPointer(it.node()->location());
Ben Murdoch257744e2011-11-30 15:57:28 +00001153 }
1154 }
1155}
1156
1157
Steve Block44f0eee2011-05-26 01:26:41 +01001158void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001159 for (NodeIterator it(this); !it.done(); it.Advance()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160 if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001161 v->VisitEmbedderReference(it.node()->location(),
1162 it.node()->wrapper_class_id());
Steve Block44f0eee2011-05-26 01:26:41 +01001163 }
1164 }
1165}
1166
1167
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001168void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
1169 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1170 Node* node = new_space_nodes_[i];
1171 if (node->IsRetainer() && node->has_wrapper_class_id()) {
1172 v->VisitEmbedderReference(node->location(),
1173 node->wrapper_class_id());
1174 }
1175 }
1176}
1177
1178
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001179void GlobalHandles::IterateWeakRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
1180 for (int i = 0; i < new_space_nodes_.length(); ++i) {
1181 Node* node = new_space_nodes_[i];
1182 if (node->has_wrapper_class_id() && node->IsWeak()) {
1183 v->VisitEmbedderReference(node->location(), node->wrapper_class_id());
1184 }
1185 }
1186}
1187
1188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001189int GlobalHandles::NumberOfWeakHandles() {
1190 int count = 0;
1191 for (NodeIterator it(this); !it.done(); it.Advance()) {
1192 if (it.node()->IsWeakRetainer()) {
1193 count++;
1194 }
1195 }
1196 return count;
1197}
1198
1199
1200int GlobalHandles::NumberOfGlobalObjectWeakHandles() {
1201 int count = 0;
1202 for (NodeIterator it(this); !it.done(); it.Advance()) {
1203 if (it.node()->IsWeakRetainer() &&
1204 it.node()->object()->IsJSGlobalObject()) {
1205 count++;
1206 }
1207 }
1208 return count;
1209}
1210
1211
Steve Blockd0582a62009-12-15 09:54:21 +00001212void GlobalHandles::RecordStats(HeapStats* stats) {
1213 *stats->global_handle_count = 0;
1214 *stats->weak_global_handle_count = 0;
1215 *stats->pending_global_handle_count = 0;
1216 *stats->near_death_global_handle_count = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001217 *stats->free_global_handle_count = 0;
1218 for (NodeIterator it(this); !it.done(); it.Advance()) {
Steve Blockd0582a62009-12-15 09:54:21 +00001219 *stats->global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001220 if (it.node()->state() == Node::WEAK) {
Steve Blockd0582a62009-12-15 09:54:21 +00001221 *stats->weak_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001222 } else if (it.node()->state() == Node::PENDING) {
Steve Blockd0582a62009-12-15 09:54:21 +00001223 *stats->pending_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001224 } else if (it.node()->state() == Node::NEAR_DEATH) {
Steve Blockd0582a62009-12-15 09:54:21 +00001225 *stats->near_death_global_handle_count += 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001226 } else if (it.node()->state() == Node::FREE) {
1227 *stats->free_global_handle_count += 1;
Steve Blockd0582a62009-12-15 09:54:21 +00001228 }
1229 }
1230}
Steve Blocka7e24c12009-10-30 11:49:00 +00001231
1232#ifdef DEBUG
1233
1234void GlobalHandles::PrintStats() {
1235 int total = 0;
1236 int weak = 0;
1237 int pending = 0;
1238 int near_death = 0;
1239 int destroyed = 0;
1240
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001241 for (NodeIterator it(this); !it.done(); it.Advance()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001242 total++;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001243 if (it.node()->state() == Node::WEAK) weak++;
1244 if (it.node()->state() == Node::PENDING) pending++;
1245 if (it.node()->state() == Node::NEAR_DEATH) near_death++;
1246 if (it.node()->state() == Node::FREE) destroyed++;
Steve Blocka7e24c12009-10-30 11:49:00 +00001247 }
1248
1249 PrintF("Global Handle Statistics:\n");
Ben Murdochc5610432016-08-08 18:44:38 +01001250 PrintF(" allocated memory = %" PRIuS "B\n", total * sizeof(Node));
Steve Blocka7e24c12009-10-30 11:49:00 +00001251 PrintF(" # weak = %d\n", weak);
1252 PrintF(" # pending = %d\n", pending);
1253 PrintF(" # near_death = %d\n", near_death);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001254 PrintF(" # free = %d\n", destroyed);
Steve Blocka7e24c12009-10-30 11:49:00 +00001255 PrintF(" # total = %d\n", total);
1256}
1257
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258
Steve Blocka7e24c12009-10-30 11:49:00 +00001259void GlobalHandles::Print() {
1260 PrintF("Global handles:\n");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001261 for (NodeIterator it(this); !it.done(); it.Advance()) {
1262 PrintF(" handle %p to %p%s\n",
1263 reinterpret_cast<void*>(it.node()->location()),
1264 reinterpret_cast<void*>(it.node()->object()),
1265 it.node()->IsWeak() ? " (weak)" : "");
Steve Blocka7e24c12009-10-30 11:49:00 +00001266 }
1267}
1268
1269#endif
1270
Steve Block44f0eee2011-05-26 01:26:41 +01001271
1272
1273void GlobalHandles::AddObjectGroup(Object*** handles,
1274 size_t length,
1275 v8::RetainedObjectInfo* info) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001276#ifdef DEBUG
1277 for (size_t i = 0; i < length; ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001278 DCHECK(!Node::FromLocation(handles[i])->is_independent());
Ben Murdoch257744e2011-11-30 15:57:28 +00001279 }
1280#endif
Ben Murdoch8b112d22011-06-08 16:22:53 +01001281 if (length == 0) {
1282 if (info != NULL) info->Dispose();
1283 return;
Steve Block44f0eee2011-05-26 01:26:41 +01001284 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 ObjectGroup* group = new ObjectGroup(length);
1286 for (size_t i = 0; i < length; ++i)
1287 group->objects[i] = handles[i];
1288 group->info = info;
1289 object_groups_.Add(group);
Steve Blocka7e24c12009-10-30 11:49:00 +00001290}
1291
Steve Block44f0eee2011-05-26 01:26:41 +01001292
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001293void GlobalHandles::SetObjectGroupId(Object** handle,
1294 UniqueId id) {
1295 object_group_connections_.Add(ObjectGroupConnection(id, handle));
1296}
1297
1298
1299void GlobalHandles::SetRetainedObjectInfo(UniqueId id,
1300 RetainedObjectInfo* info) {
1301 retainer_infos_.Add(ObjectGroupRetainerInfo(id, info));
1302}
1303
1304
1305void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) {
1306 DCHECK(!Node::FromLocation(child)->is_independent());
1307 implicit_ref_connections_.Add(ObjectGroupConnection(id, child));
1308}
1309
1310
1311void GlobalHandles::SetReference(HeapObject** parent, Object** child) {
1312 DCHECK(!Node::FromLocation(child)->is_independent());
1313 ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1);
1314 group->children[0] = child;
1315 implicit_ref_groups_.Add(group);
Steve Blocka7e24c12009-10-30 11:49:00 +00001316}
1317
1318
1319void GlobalHandles::RemoveObjectGroups() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 for (int i = 0; i < object_groups_.length(); i++)
1321 delete object_groups_.at(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001322 object_groups_.Clear();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 for (int i = 0; i < retainer_infos_.length(); ++i)
1324 retainer_infos_[i].info->Dispose();
1325 retainer_infos_.Clear();
1326 object_group_connections_.Clear();
1327 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00001328}
1329
Steve Block44f0eee2011-05-26 01:26:41 +01001330
1331void GlobalHandles::RemoveImplicitRefGroups() {
1332 for (int i = 0; i < implicit_ref_groups_.length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 delete implicit_ref_groups_.at(i);
Steve Block44f0eee2011-05-26 01:26:41 +01001334 }
1335 implicit_ref_groups_.Clear();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 implicit_ref_connections_.Clear();
Steve Block44f0eee2011-05-26 01:26:41 +01001337}
1338
1339
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001340void GlobalHandles::TearDown() {
1341 // TODO(1428): invoke weak callbacks.
1342}
1343
1344
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() {
1346 if (object_group_connections_.length() == 0) {
1347 for (int i = 0; i < retainer_infos_.length(); ++i)
1348 retainer_infos_[i].info->Dispose();
1349 retainer_infos_.Clear();
1350 implicit_ref_connections_.Clear();
1351 return;
1352 }
1353
1354 object_group_connections_.Sort();
1355 retainer_infos_.Sort();
1356 implicit_ref_connections_.Sort();
1357
1358 int info_index = 0; // For iterating retainer_infos_.
1359 UniqueId current_group_id(0);
1360 int current_group_start = 0;
1361
1362 int current_implicit_refs_start = 0;
1363 int current_implicit_refs_end = 0;
1364 for (int i = 0; i <= object_group_connections_.length(); ++i) {
1365 if (i == 0)
1366 current_group_id = object_group_connections_[i].id;
1367 if (i == object_group_connections_.length() ||
1368 current_group_id != object_group_connections_[i].id) {
1369 // Group detected: objects in indices [current_group_start, i[.
1370
1371 // Find out which implicit references are related to this group. (We want
1372 // to ignore object groups which only have 1 object, but that object is
1373 // needed as a representative object for the implicit refrerence group.)
1374 while (current_implicit_refs_start < implicit_ref_connections_.length() &&
1375 implicit_ref_connections_[current_implicit_refs_start].id <
1376 current_group_id)
1377 ++current_implicit_refs_start;
1378 current_implicit_refs_end = current_implicit_refs_start;
1379 while (current_implicit_refs_end < implicit_ref_connections_.length() &&
1380 implicit_ref_connections_[current_implicit_refs_end].id ==
1381 current_group_id)
1382 ++current_implicit_refs_end;
1383
1384 if (current_implicit_refs_end > current_implicit_refs_start) {
1385 // Find a representative object for the implicit references.
1386 HeapObject** representative = NULL;
1387 for (int j = current_group_start; j < i; ++j) {
1388 Object** object = object_group_connections_[j].object;
1389 if ((*object)->IsHeapObject()) {
1390 representative = reinterpret_cast<HeapObject**>(object);
1391 break;
1392 }
1393 }
1394 if (representative) {
1395 ImplicitRefGroup* group = new ImplicitRefGroup(
1396 representative,
1397 current_implicit_refs_end - current_implicit_refs_start);
1398 for (int j = current_implicit_refs_start;
1399 j < current_implicit_refs_end;
1400 ++j) {
1401 group->children[j - current_implicit_refs_start] =
1402 implicit_ref_connections_[j].object;
1403 }
1404 implicit_ref_groups_.Add(group);
1405 }
1406 current_implicit_refs_start = current_implicit_refs_end;
1407 }
1408
1409 // Find a RetainedObjectInfo for the group.
1410 RetainedObjectInfo* info = NULL;
1411 while (info_index < retainer_infos_.length() &&
1412 retainer_infos_[info_index].id < current_group_id) {
1413 retainer_infos_[info_index].info->Dispose();
1414 ++info_index;
1415 }
1416 if (info_index < retainer_infos_.length() &&
1417 retainer_infos_[info_index].id == current_group_id) {
1418 // This object group has an associated ObjectGroupRetainerInfo.
1419 info = retainer_infos_[info_index].info;
1420 ++info_index;
1421 }
1422
1423 // Ignore groups which only contain one object.
1424 if (i > current_group_start + 1) {
1425 ObjectGroup* group = new ObjectGroup(i - current_group_start);
1426 for (int j = current_group_start; j < i; ++j) {
1427 group->objects[j - current_group_start] =
1428 object_group_connections_[j].object;
1429 }
1430 group->info = info;
1431 object_groups_.Add(group);
1432 } else if (info) {
1433 info->Dispose();
1434 }
1435
1436 if (i < object_group_connections_.length()) {
1437 current_group_id = object_group_connections_[i].id;
1438 current_group_start = i;
1439 }
1440 }
1441 }
1442 object_group_connections_.Clear();
1443 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
1444 retainer_infos_.Clear();
1445 implicit_ref_connections_.Clear();
1446}
1447
1448
1449EternalHandles::EternalHandles() : size_(0) {
1450 for (unsigned i = 0; i < arraysize(singleton_handles_); i++) {
1451 singleton_handles_[i] = kInvalidIndex;
1452 }
1453}
1454
1455
1456EternalHandles::~EternalHandles() {
1457 for (int i = 0; i < blocks_.length(); i++) delete[] blocks_[i];
1458}
1459
1460
1461void EternalHandles::IterateAllRoots(ObjectVisitor* visitor) {
1462 int limit = size_;
1463 for (int i = 0; i < blocks_.length(); i++) {
1464 DCHECK(limit > 0);
1465 Object** block = blocks_[i];
1466 visitor->VisitPointers(block, block + Min(limit, kSize));
1467 limit -= kSize;
1468 }
1469}
1470
1471
1472void EternalHandles::IterateNewSpaceRoots(ObjectVisitor* visitor) {
1473 for (int i = 0; i < new_space_indices_.length(); i++) {
1474 visitor->VisitPointer(GetLocation(new_space_indices_[i]));
1475 }
1476}
1477
1478
1479void EternalHandles::PostGarbageCollectionProcessing(Heap* heap) {
1480 int last = 0;
1481 for (int i = 0; i < new_space_indices_.length(); i++) {
1482 int index = new_space_indices_[i];
1483 if (heap->InNewSpace(*GetLocation(index))) {
1484 new_space_indices_[last++] = index;
1485 }
1486 }
1487 new_space_indices_.Rewind(last);
1488}
1489
1490
1491void EternalHandles::Create(Isolate* isolate, Object* object, int* index) {
1492 DCHECK_EQ(kInvalidIndex, *index);
1493 if (object == NULL) return;
1494 DCHECK_NE(isolate->heap()->the_hole_value(), object);
1495 int block = size_ >> kShift;
1496 int offset = size_ & kMask;
1497 // need to resize
1498 if (offset == 0) {
1499 Object** next_block = new Object*[kSize];
1500 Object* the_hole = isolate->heap()->the_hole_value();
1501 MemsetPointer(next_block, the_hole, kSize);
1502 blocks_.Add(next_block);
1503 }
1504 DCHECK_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]);
1505 blocks_[block][offset] = object;
1506 if (isolate->heap()->InNewSpace(object)) {
1507 new_space_indices_.Add(size_);
1508 }
1509 *index = size_++;
1510}
1511
1512
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001513} // namespace internal
1514} // namespace v8