1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "src/global-handles.h"
10 #include "src/vm-state-inl.h"
16 ObjectGroup::~ObjectGroup() {
17 if (info != NULL) info->Dispose();
22 ImplicitRefGroup::~ImplicitRefGroup() {
27 class GlobalHandles::Node {
29 // State transition diagram:
30 // FREE -> NORMAL <-> WEAK -> PENDING -> NEAR_DEATH -> { NORMAL, WEAK, FREE }
33 NORMAL, // Normal global handle.
34 WEAK, // Flagged as weak but not yet finalized.
35 PENDING, // Has been recognized as only reachable by weak handles.
36 NEAR_DEATH // Callback has informed the handle is near death.
39 // Maps handle location (slot) to the containing node.
40 static Node* FromLocation(Object** location) {
41 DCHECK(OFFSET_OF(Node, object_) == 0);
42 return reinterpret_cast<Node*>(location);
46 DCHECK(OFFSET_OF(Node, class_id_) == Internals::kNodeClassIdOffset);
47 DCHECK(OFFSET_OF(Node, flags_) == Internals::kNodeFlagsOffset);
48 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);
59 #ifdef ENABLE_HANDLE_ZAPPING
61 // TODO(1428): if it's a weak handle we should have invoked its callback.
62 // Zap the values for eager trapping.
63 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
64 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
66 set_independent(false);
67 set_partially_dependent(false);
68 set_in_new_space_list(false);
69 parameter_or_next_free_.next_free = NULL;
70 weak_callback_ = NULL;
74 void Initialize(int index, Node** first_free) {
75 index_ = static_cast<uint8_t>(index);
76 DCHECK(static_cast<int>(index_) == index);
78 set_in_new_space_list(false);
79 parameter_or_next_free_.next_free = *first_free;
83 void Acquire(Object* object) {
84 DCHECK(state() == FREE);
86 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
87 set_independent(false);
88 set_partially_dependent(false);
90 parameter_or_next_free_.parameter = NULL;
91 weak_callback_ = NULL;
96 DCHECK(state() != FREE);
98 // Zap the values for eager trapping.
99 object_ = reinterpret_cast<Object*>(kGlobalHandleZapValue);
100 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
101 set_independent(false);
102 set_partially_dependent(false);
103 weak_callback_ = NULL;
107 // Object slot accessors.
108 Object* object() const { return object_; }
109 Object** location() { return &object_; }
110 Handle<Object> handle() { return Handle<Object>(location()); }
112 // Wrapper class ID accessors.
113 bool has_wrapper_class_id() const {
114 return class_id_ != v8::HeapProfiler::kPersistentHandleNoClassId;
117 uint16_t wrapper_class_id() const { return class_id_; }
119 // State and flag accessors.
121 State state() const {
122 return NodeState::decode(flags_);
124 void set_state(State state) {
125 flags_ = NodeState::update(flags_, state);
128 bool is_independent() {
129 return IsIndependent::decode(flags_);
131 void set_independent(bool v) {
132 flags_ = IsIndependent::update(flags_, v);
135 bool is_partially_dependent() {
136 return IsPartiallyDependent::decode(flags_);
138 void set_partially_dependent(bool v) {
139 flags_ = IsPartiallyDependent::update(flags_, v);
142 bool is_in_new_space_list() {
143 return IsInNewSpaceList::decode(flags_);
145 void set_in_new_space_list(bool v) {
146 flags_ = IsInNewSpaceList::update(flags_, v);
149 bool IsNearDeath() const {
150 // Check for PENDING to ensure correct answer when processing callbacks.
151 return state() == PENDING || state() == NEAR_DEATH;
154 bool IsWeak() const { return state() == WEAK; }
156 bool IsRetainer() const { return state() != FREE; }
158 bool IsStrongRetainer() const { return state() == NORMAL; }
160 bool IsWeakRetainer() const {
161 return state() == WEAK || state() == PENDING || state() == NEAR_DEATH;
165 DCHECK(state() == WEAK);
169 // Independent flag accessors.
170 void MarkIndependent() {
171 DCHECK(state() != FREE);
172 set_independent(true);
175 void MarkPartiallyDependent() {
176 DCHECK(state() != FREE);
177 if (GetGlobalHandles()->isolate()->heap()->InNewSpace(object_)) {
178 set_partially_dependent(true);
181 void clear_partially_dependent() { set_partially_dependent(false); }
183 // Callback accessor.
184 // TODO(svenpanne) Re-enable or nuke later.
185 // WeakReferenceCallback callback() { return callback_; }
187 // Callback parameter accessors.
188 void set_parameter(void* parameter) {
189 DCHECK(state() != FREE);
190 parameter_or_next_free_.parameter = parameter;
192 void* parameter() const {
193 DCHECK(state() != FREE);
194 return parameter_or_next_free_.parameter;
197 // Accessors for next free node in the free list.
199 DCHECK(state() == FREE);
200 return parameter_or_next_free_.next_free;
202 void set_next_free(Node* value) {
203 DCHECK(state() == FREE);
204 parameter_or_next_free_.next_free = value;
207 void MakeWeak(void* parameter, WeakCallback weak_callback) {
208 DCHECK(weak_callback != NULL);
209 DCHECK(state() != FREE);
210 CHECK(object_ != NULL);
212 set_parameter(parameter);
213 weak_callback_ = weak_callback;
216 void* ClearWeakness() {
217 DCHECK(state() != FREE);
218 void* p = parameter();
224 bool PostGarbageCollectionProcessing(Isolate* isolate) {
225 if (state() != Node::PENDING) return false;
226 if (weak_callback_ == NULL) {
230 void* par = parameter();
231 set_state(NEAR_DEATH);
234 Object** object = location();
236 // Check that we are not passing a finalized external string to
238 DCHECK(!object_->IsExternalAsciiString() ||
239 ExternalAsciiString::cast(object_)->resource() != NULL);
240 DCHECK(!object_->IsExternalTwoByteString() ||
241 ExternalTwoByteString::cast(object_)->resource() != NULL);
243 VMState<EXTERNAL> state(isolate);
244 HandleScope handle_scope(isolate);
245 Handle<Object> handle(*object, isolate);
246 v8::WeakCallbackData<v8::Value, void> data(
247 reinterpret_cast<v8::Isolate*>(isolate),
248 v8::Utils::ToLocal(handle),
250 weak_callback_(data);
252 // Absence of explicit cleanup or revival of weak handle
253 // in most of the cases would lead to memory leak.
254 CHECK(state() != NEAR_DEATH);
258 inline GlobalHandles* GetGlobalHandles();
261 inline NodeBlock* FindBlock();
262 inline void IncreaseBlockUses();
263 inline void DecreaseBlockUses();
265 // Storage for object pointer.
266 // Placed first to avoid offset computation.
269 // Next word stores class_id, index, state, and independent.
270 // Note: the most aligned fields should go first.
275 // Index in the containing handle block.
278 // This stores three flags (independent, partially_dependent and
279 // in_new_space_list) and a State.
280 class NodeState: public BitField<State, 0, 4> {};
281 class IsIndependent: public BitField<bool, 4, 1> {};
282 class IsPartiallyDependent: public BitField<bool, 5, 1> {};
283 class IsInNewSpaceList: public BitField<bool, 6, 1> {};
287 // Handle specific callback - might be a weak reference in disguise.
288 WeakCallback weak_callback_;
290 // Provided data for callback. In FREE state, this is used for
291 // the free list link.
295 } parameter_or_next_free_;
297 DISALLOW_COPY_AND_ASSIGN(Node);
301 class GlobalHandles::NodeBlock {
303 static const int kSize = 256;
305 explicit NodeBlock(GlobalHandles* global_handles, NodeBlock* next)
310 global_handles_(global_handles) {}
312 void PutNodesOnFreeList(Node** first_free) {
313 for (int i = kSize - 1; i >= 0; --i) {
314 nodes_[i].Initialize(i, first_free);
318 Node* node_at(int index) {
319 DCHECK(0 <= index && index < kSize);
320 return &nodes_[index];
323 void IncreaseUses() {
324 DCHECK(used_nodes_ < kSize);
325 if (used_nodes_++ == 0) {
326 NodeBlock* old_first = global_handles_->first_used_block_;
327 global_handles_->first_used_block_ = this;
328 next_used_ = old_first;
330 if (old_first == NULL) return;
331 old_first->prev_used_ = this;
335 void DecreaseUses() {
336 DCHECK(used_nodes_ > 0);
337 if (--used_nodes_ == 0) {
338 if (next_used_ != NULL) next_used_->prev_used_ = prev_used_;
339 if (prev_used_ != NULL) prev_used_->next_used_ = next_used_;
340 if (this == global_handles_->first_used_block_) {
341 global_handles_->first_used_block_ = next_used_;
346 GlobalHandles* global_handles() { return global_handles_; }
348 // Next block in the list of all blocks.
349 NodeBlock* next() const { return next_; }
351 // Next/previous block in the list of blocks with used nodes.
352 NodeBlock* next_used() const { return next_used_; }
353 NodeBlock* prev_used() const { return prev_used_; }
357 NodeBlock* const next_;
359 NodeBlock* next_used_;
360 NodeBlock* prev_used_;
361 GlobalHandles* global_handles_;
365 GlobalHandles* GlobalHandles::Node::GetGlobalHandles() {
366 return FindBlock()->global_handles();
370 GlobalHandles::NodeBlock* GlobalHandles::Node::FindBlock() {
371 intptr_t ptr = reinterpret_cast<intptr_t>(this);
372 ptr = ptr - index_ * sizeof(Node);
373 NodeBlock* block = reinterpret_cast<NodeBlock*>(ptr);
374 DCHECK(block->node_at(index_) == this);
379 void GlobalHandles::Node::IncreaseBlockUses() {
380 NodeBlock* node_block = FindBlock();
381 node_block->IncreaseUses();
382 GlobalHandles* global_handles = node_block->global_handles();
383 global_handles->isolate()->counters()->global_handles()->Increment();
384 global_handles->number_of_global_handles_++;
388 void GlobalHandles::Node::DecreaseBlockUses() {
389 NodeBlock* node_block = FindBlock();
390 GlobalHandles* global_handles = node_block->global_handles();
391 parameter_or_next_free_.next_free = global_handles->first_free_;
392 global_handles->first_free_ = this;
393 node_block->DecreaseUses();
394 global_handles->isolate()->counters()->global_handles()->Decrement();
395 global_handles->number_of_global_handles_--;
399 class GlobalHandles::NodeIterator {
401 explicit NodeIterator(GlobalHandles* global_handles)
402 : block_(global_handles->first_used_block_),
405 bool done() const { return block_ == NULL; }
409 return block_->node_at(index_);
414 if (++index_ < NodeBlock::kSize) return;
416 block_ = block_->next_used();
423 DISALLOW_COPY_AND_ASSIGN(NodeIterator);
427 GlobalHandles::GlobalHandles(Isolate* isolate)
429 number_of_global_handles_(0),
431 first_used_block_(NULL),
433 post_gc_processing_count_(0),
434 object_group_connections_(kObjectGroupConnectionsCapacity) {}
437 GlobalHandles::~GlobalHandles() {
438 NodeBlock* block = first_block_;
439 while (block != NULL) {
440 NodeBlock* tmp = block->next();
448 Handle<Object> GlobalHandles::Create(Object* value) {
449 if (first_free_ == NULL) {
450 first_block_ = new NodeBlock(this, first_block_);
451 first_block_->PutNodesOnFreeList(&first_free_);
453 DCHECK(first_free_ != NULL);
454 // Take the first node in the free list.
455 Node* result = first_free_;
456 first_free_ = result->next_free();
457 result->Acquire(value);
458 if (isolate_->heap()->InNewSpace(value) &&
459 !result->is_in_new_space_list()) {
460 new_space_nodes_.Add(result);
461 result->set_in_new_space_list(true);
463 return result->handle();
467 Handle<Object> GlobalHandles::CopyGlobal(Object** location) {
468 DCHECK(location != NULL);
469 return Node::FromLocation(location)->GetGlobalHandles()->Create(*location);
473 void GlobalHandles::Destroy(Object** location) {
474 if (location != NULL) Node::FromLocation(location)->Release();
478 void GlobalHandles::MakeWeak(Object** location,
480 WeakCallback weak_callback) {
481 Node::FromLocation(location)->MakeWeak(parameter, weak_callback);
485 void* GlobalHandles::ClearWeakness(Object** location) {
486 return Node::FromLocation(location)->ClearWeakness();
490 void GlobalHandles::MarkIndependent(Object** location) {
491 Node::FromLocation(location)->MarkIndependent();
495 void GlobalHandles::MarkPartiallyDependent(Object** location) {
496 Node::FromLocation(location)->MarkPartiallyDependent();
500 bool GlobalHandles::IsIndependent(Object** location) {
501 return Node::FromLocation(location)->is_independent();
505 bool GlobalHandles::IsNearDeath(Object** location) {
506 return Node::FromLocation(location)->IsNearDeath();
510 bool GlobalHandles::IsWeak(Object** location) {
511 return Node::FromLocation(location)->IsWeak();
515 void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
516 for (NodeIterator it(this); !it.done(); it.Advance()) {
517 if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location());
522 void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
523 for (NodeIterator it(this); !it.done(); it.Advance()) {
524 if (it.node()->IsWeak() && f(it.node()->location())) {
525 it.node()->MarkPending();
531 void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
532 for (int i = 0; i < new_space_nodes_.length(); ++i) {
533 Node* node = new_space_nodes_[i];
534 if (node->IsStrongRetainer() ||
535 (node->IsWeakRetainer() && !node->is_independent() &&
536 !node->is_partially_dependent())) {
537 v->VisitPointer(node->location());
543 void GlobalHandles::IdentifyNewSpaceWeakIndependentHandles(
544 WeakSlotCallbackWithHeap f) {
545 for (int i = 0; i < new_space_nodes_.length(); ++i) {
546 Node* node = new_space_nodes_[i];
547 DCHECK(node->is_in_new_space_list());
548 if ((node->is_independent() || node->is_partially_dependent()) &&
549 node->IsWeak() && f(isolate_->heap(), node->location())) {
556 void GlobalHandles::IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v) {
557 for (int i = 0; i < new_space_nodes_.length(); ++i) {
558 Node* node = new_space_nodes_[i];
559 DCHECK(node->is_in_new_space_list());
560 if ((node->is_independent() || node->is_partially_dependent()) &&
561 node->IsWeakRetainer()) {
562 v->VisitPointer(node->location());
568 bool GlobalHandles::IterateObjectGroups(ObjectVisitor* v,
569 WeakSlotCallbackWithHeap can_skip) {
570 ComputeObjectGroupsAndImplicitReferences();
572 bool any_group_was_visited = false;
573 for (int i = 0; i < object_groups_.length(); i++) {
574 ObjectGroup* entry = object_groups_.at(i);
575 DCHECK(entry != NULL);
577 Object*** objects = entry->objects;
578 bool group_should_be_visited = false;
579 for (size_t j = 0; j < entry->length; j++) {
580 Object* object = *objects[j];
581 if (object->IsHeapObject()) {
582 if (!can_skip(isolate_->heap(), &object)) {
583 group_should_be_visited = true;
589 if (!group_should_be_visited) {
590 object_groups_[last++] = entry;
594 // An object in the group requires visiting, so iterate over all
595 // objects in the group.
596 for (size_t j = 0; j < entry->length; ++j) {
597 Object* object = *objects[j];
598 if (object->IsHeapObject()) {
599 v->VisitPointer(&object);
600 any_group_was_visited = true;
604 // Once the entire group has been iterated over, set the object
605 // group to NULL so it won't be processed again.
607 object_groups_.at(i) = NULL;
609 object_groups_.Rewind(last);
610 return any_group_was_visited;
614 int GlobalHandles::PostGarbageCollectionProcessing(
615 GarbageCollector collector) {
616 // Process weak global handle callbacks. This must be done after the
617 // GC is completely done, because the callbacks may invoke arbitrary
619 DCHECK(isolate_->heap()->gc_state() == Heap::NOT_IN_GC);
620 const int initial_post_gc_processing_count = ++post_gc_processing_count_;
622 if (collector == SCAVENGER) {
623 for (int i = 0; i < new_space_nodes_.length(); ++i) {
624 Node* node = new_space_nodes_[i];
625 DCHECK(node->is_in_new_space_list());
626 if (!node->IsRetainer()) {
627 // Free nodes do not have weak callbacks. Do not use them to compute
631 // Skip dependent handles. Their weak callbacks might expect to be
632 // called between two global garbage collection callbacks which
633 // are not called for minor collections.
634 if (!node->is_independent() && !node->is_partially_dependent()) {
637 node->clear_partially_dependent();
638 if (node->PostGarbageCollectionProcessing(isolate_)) {
639 if (initial_post_gc_processing_count != post_gc_processing_count_) {
640 // Weak callback triggered another GC and another round of
641 // PostGarbageCollection processing. The current node might
642 // have been deleted in that round, so we need to bail out (or
643 // restart the processing).
647 if (!node->IsRetainer()) {
652 for (NodeIterator it(this); !it.done(); it.Advance()) {
653 if (!it.node()->IsRetainer()) {
654 // Free nodes do not have weak callbacks. Do not use them to compute
658 it.node()->clear_partially_dependent();
659 if (it.node()->PostGarbageCollectionProcessing(isolate_)) {
660 if (initial_post_gc_processing_count != post_gc_processing_count_) {
661 // See the comment above.
665 if (!it.node()->IsRetainer()) {
670 // Update the list of new space nodes.
672 for (int i = 0; i < new_space_nodes_.length(); ++i) {
673 Node* node = new_space_nodes_[i];
674 DCHECK(node->is_in_new_space_list());
675 if (node->IsRetainer()) {
676 if (isolate_->heap()->InNewSpace(node->object())) {
677 new_space_nodes_[last++] = node;
678 isolate_->heap()->IncrementNodesCopiedInNewSpace();
680 node->set_in_new_space_list(false);
681 isolate_->heap()->IncrementNodesPromoted();
684 node->set_in_new_space_list(false);
685 isolate_->heap()->IncrementNodesDiedInNewSpace();
688 new_space_nodes_.Rewind(last);
693 void GlobalHandles::IterateStrongRoots(ObjectVisitor* v) {
694 for (NodeIterator it(this); !it.done(); it.Advance()) {
695 if (it.node()->IsStrongRetainer()) {
696 v->VisitPointer(it.node()->location());
702 void GlobalHandles::IterateAllRoots(ObjectVisitor* v) {
703 for (NodeIterator it(this); !it.done(); it.Advance()) {
704 if (it.node()->IsRetainer()) {
705 v->VisitPointer(it.node()->location());
711 void GlobalHandles::IterateAllRootsWithClassIds(ObjectVisitor* v) {
712 for (NodeIterator it(this); !it.done(); it.Advance()) {
713 if (it.node()->IsRetainer() && it.node()->has_wrapper_class_id()) {
714 v->VisitEmbedderReference(it.node()->location(),
715 it.node()->wrapper_class_id());
721 void GlobalHandles::IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v) {
722 for (int i = 0; i < new_space_nodes_.length(); ++i) {
723 Node* node = new_space_nodes_[i];
724 if (node->IsRetainer() && node->has_wrapper_class_id()) {
725 v->VisitEmbedderReference(node->location(),
726 node->wrapper_class_id());
732 int GlobalHandles::NumberOfWeakHandles() {
734 for (NodeIterator it(this); !it.done(); it.Advance()) {
735 if (it.node()->IsWeakRetainer()) {
743 int GlobalHandles::NumberOfGlobalObjectWeakHandles() {
745 for (NodeIterator it(this); !it.done(); it.Advance()) {
746 if (it.node()->IsWeakRetainer() &&
747 it.node()->object()->IsJSGlobalObject()) {
755 void GlobalHandles::RecordStats(HeapStats* stats) {
756 *stats->global_handle_count = 0;
757 *stats->weak_global_handle_count = 0;
758 *stats->pending_global_handle_count = 0;
759 *stats->near_death_global_handle_count = 0;
760 *stats->free_global_handle_count = 0;
761 for (NodeIterator it(this); !it.done(); it.Advance()) {
762 *stats->global_handle_count += 1;
763 if (it.node()->state() == Node::WEAK) {
764 *stats->weak_global_handle_count += 1;
765 } else if (it.node()->state() == Node::PENDING) {
766 *stats->pending_global_handle_count += 1;
767 } else if (it.node()->state() == Node::NEAR_DEATH) {
768 *stats->near_death_global_handle_count += 1;
769 } else if (it.node()->state() == Node::FREE) {
770 *stats->free_global_handle_count += 1;
777 void GlobalHandles::PrintStats() {
784 for (NodeIterator it(this); !it.done(); it.Advance()) {
786 if (it.node()->state() == Node::WEAK) weak++;
787 if (it.node()->state() == Node::PENDING) pending++;
788 if (it.node()->state() == Node::NEAR_DEATH) near_death++;
789 if (it.node()->state() == Node::FREE) destroyed++;
792 PrintF("Global Handle Statistics:\n");
793 PrintF(" allocated memory = %" V8_PTR_PREFIX "dB\n", sizeof(Node) * total);
794 PrintF(" # weak = %d\n", weak);
795 PrintF(" # pending = %d\n", pending);
796 PrintF(" # near_death = %d\n", near_death);
797 PrintF(" # free = %d\n", destroyed);
798 PrintF(" # total = %d\n", total);
802 void GlobalHandles::Print() {
803 PrintF("Global handles:\n");
804 for (NodeIterator it(this); !it.done(); it.Advance()) {
805 PrintF(" handle %p to %p%s\n",
806 reinterpret_cast<void*>(it.node()->location()),
807 reinterpret_cast<void*>(it.node()->object()),
808 it.node()->IsWeak() ? " (weak)" : "");
816 void GlobalHandles::AddObjectGroup(Object*** handles,
818 v8::RetainedObjectInfo* info) {
820 for (size_t i = 0; i < length; ++i) {
821 DCHECK(!Node::FromLocation(handles[i])->is_independent());
825 if (info != NULL) info->Dispose();
828 ObjectGroup* group = new ObjectGroup(length);
829 for (size_t i = 0; i < length; ++i)
830 group->objects[i] = handles[i];
832 object_groups_.Add(group);
836 void GlobalHandles::SetObjectGroupId(Object** handle,
838 object_group_connections_.Add(ObjectGroupConnection(id, handle));
842 void GlobalHandles::SetRetainedObjectInfo(UniqueId id,
843 RetainedObjectInfo* info) {
844 retainer_infos_.Add(ObjectGroupRetainerInfo(id, info));
848 void GlobalHandles::AddImplicitReferences(HeapObject** parent,
852 DCHECK(!Node::FromLocation(BitCast<Object**>(parent))->is_independent());
853 for (size_t i = 0; i < length; ++i) {
854 DCHECK(!Node::FromLocation(children[i])->is_independent());
857 if (length == 0) return;
858 ImplicitRefGroup* group = new ImplicitRefGroup(parent, length);
859 for (size_t i = 0; i < length; ++i)
860 group->children[i] = children[i];
861 implicit_ref_groups_.Add(group);
865 void GlobalHandles::SetReferenceFromGroup(UniqueId id, Object** child) {
866 DCHECK(!Node::FromLocation(child)->is_independent());
867 implicit_ref_connections_.Add(ObjectGroupConnection(id, child));
871 void GlobalHandles::SetReference(HeapObject** parent, Object** child) {
872 DCHECK(!Node::FromLocation(child)->is_independent());
873 ImplicitRefGroup* group = new ImplicitRefGroup(parent, 1);
874 group->children[0] = child;
875 implicit_ref_groups_.Add(group);
879 void GlobalHandles::RemoveObjectGroups() {
880 for (int i = 0; i < object_groups_.length(); i++)
881 delete object_groups_.at(i);
882 object_groups_.Clear();
883 for (int i = 0; i < retainer_infos_.length(); ++i)
884 retainer_infos_[i].info->Dispose();
885 retainer_infos_.Clear();
886 object_group_connections_.Clear();
887 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
891 void GlobalHandles::RemoveImplicitRefGroups() {
892 for (int i = 0; i < implicit_ref_groups_.length(); i++) {
893 delete implicit_ref_groups_.at(i);
895 implicit_ref_groups_.Clear();
896 implicit_ref_connections_.Clear();
900 void GlobalHandles::TearDown() {
901 // TODO(1428): invoke weak callbacks.
905 void GlobalHandles::ComputeObjectGroupsAndImplicitReferences() {
906 if (object_group_connections_.length() == 0) {
907 for (int i = 0; i < retainer_infos_.length(); ++i)
908 retainer_infos_[i].info->Dispose();
909 retainer_infos_.Clear();
910 implicit_ref_connections_.Clear();
914 object_group_connections_.Sort();
915 retainer_infos_.Sort();
916 implicit_ref_connections_.Sort();
918 int info_index = 0; // For iterating retainer_infos_.
919 UniqueId current_group_id(0);
920 int current_group_start = 0;
922 int current_implicit_refs_start = 0;
923 int current_implicit_refs_end = 0;
924 for (int i = 0; i <= object_group_connections_.length(); ++i) {
926 current_group_id = object_group_connections_[i].id;
927 if (i == object_group_connections_.length() ||
928 current_group_id != object_group_connections_[i].id) {
929 // Group detected: objects in indices [current_group_start, i[.
931 // Find out which implicit references are related to this group. (We want
932 // to ignore object groups which only have 1 object, but that object is
933 // needed as a representative object for the implicit refrerence group.)
934 while (current_implicit_refs_start < implicit_ref_connections_.length() &&
935 implicit_ref_connections_[current_implicit_refs_start].id <
937 ++current_implicit_refs_start;
938 current_implicit_refs_end = current_implicit_refs_start;
939 while (current_implicit_refs_end < implicit_ref_connections_.length() &&
940 implicit_ref_connections_[current_implicit_refs_end].id ==
942 ++current_implicit_refs_end;
944 if (current_implicit_refs_end > current_implicit_refs_start) {
945 // Find a representative object for the implicit references.
946 HeapObject** representative = NULL;
947 for (int j = current_group_start; j < i; ++j) {
948 Object** object = object_group_connections_[j].object;
949 if ((*object)->IsHeapObject()) {
950 representative = reinterpret_cast<HeapObject**>(object);
954 if (representative) {
955 ImplicitRefGroup* group = new ImplicitRefGroup(
957 current_implicit_refs_end - current_implicit_refs_start);
958 for (int j = current_implicit_refs_start;
959 j < current_implicit_refs_end;
961 group->children[j - current_implicit_refs_start] =
962 implicit_ref_connections_[j].object;
964 implicit_ref_groups_.Add(group);
966 current_implicit_refs_start = current_implicit_refs_end;
969 // Find a RetainedObjectInfo for the group.
970 RetainedObjectInfo* info = NULL;
971 while (info_index < retainer_infos_.length() &&
972 retainer_infos_[info_index].id < current_group_id) {
973 retainer_infos_[info_index].info->Dispose();
976 if (info_index < retainer_infos_.length() &&
977 retainer_infos_[info_index].id == current_group_id) {
978 // This object group has an associated ObjectGroupRetainerInfo.
979 info = retainer_infos_[info_index].info;
983 // Ignore groups which only contain one object.
984 if (i > current_group_start + 1) {
985 ObjectGroup* group = new ObjectGroup(i - current_group_start);
986 for (int j = current_group_start; j < i; ++j) {
987 group->objects[j - current_group_start] =
988 object_group_connections_[j].object;
991 object_groups_.Add(group);
996 if (i < object_group_connections_.length()) {
997 current_group_id = object_group_connections_[i].id;
998 current_group_start = i;
1002 object_group_connections_.Clear();
1003 object_group_connections_.Initialize(kObjectGroupConnectionsCapacity);
1004 retainer_infos_.Clear();
1005 implicit_ref_connections_.Clear();
1009 EternalHandles::EternalHandles() : size_(0) {
1010 for (unsigned i = 0; i < ARRAY_SIZE(singleton_handles_); i++) {
1011 singleton_handles_[i] = kInvalidIndex;
1016 EternalHandles::~EternalHandles() {
1017 for (int i = 0; i < blocks_.length(); i++) delete[] blocks_[i];
1021 void EternalHandles::IterateAllRoots(ObjectVisitor* visitor) {
1023 for (int i = 0; i < blocks_.length(); i++) {
1025 Object** block = blocks_[i];
1026 visitor->VisitPointers(block, block + Min(limit, kSize));
1032 void EternalHandles::IterateNewSpaceRoots(ObjectVisitor* visitor) {
1033 for (int i = 0; i < new_space_indices_.length(); i++) {
1034 visitor->VisitPointer(GetLocation(new_space_indices_[i]));
1039 void EternalHandles::PostGarbageCollectionProcessing(Heap* heap) {
1041 for (int i = 0; i < new_space_indices_.length(); i++) {
1042 int index = new_space_indices_[i];
1043 if (heap->InNewSpace(*GetLocation(index))) {
1044 new_space_indices_[last++] = index;
1047 new_space_indices_.Rewind(last);
1051 void EternalHandles::Create(Isolate* isolate, Object* object, int* index) {
1052 DCHECK_EQ(kInvalidIndex, *index);
1053 if (object == NULL) return;
1054 DCHECK_NE(isolate->heap()->the_hole_value(), object);
1055 int block = size_ >> kShift;
1056 int offset = size_ & kMask;
1059 Object** next_block = new Object*[kSize];
1060 Object* the_hole = isolate->heap()->the_hole_value();
1061 MemsetPointer(next_block, the_hole, kSize);
1062 blocks_.Add(next_block);
1064 DCHECK_EQ(isolate->heap()->the_hole_value(), blocks_[block][offset]);
1065 blocks_[block][offset] = object;
1066 if (isolate->heap()->InNewSpace(object)) {
1067 new_space_indices_.Add(size_);
1073 } } // namespace v8::internal