OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/heap/store-buffer.h" | 5 #include "src/heap/store-buffer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "src/counters.h" | 9 #include "src/counters.h" |
10 #include "src/heap/incremental-marking.h" | 10 #include "src/heap/incremental-marking.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 | 53 |
54 void StoreBuffer::TearDown() { | 54 void StoreBuffer::TearDown() { |
55 delete virtual_memory_; | 55 delete virtual_memory_; |
56 start_ = limit_ = NULL; | 56 start_ = limit_ = NULL; |
57 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_)); | 57 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_)); |
58 } | 58 } |
59 | 59 |
60 | 60 |
61 void StoreBuffer::StoreBufferOverflow(Isolate* isolate) { | 61 void StoreBuffer::StoreBufferOverflow(Isolate* isolate) { |
62 isolate->heap()->store_buffer()->InsertEntriesFromBuffer(); | 62 isolate->heap()->store_buffer()->MoveEntriesToRememberedSet(); |
63 isolate->counters()->store_buffer_overflows()->Increment(); | 63 isolate->counters()->store_buffer_overflows()->Increment(); |
64 } | 64 } |
65 | 65 |
66 void StoreBuffer::Remove(Address addr) { | 66 void StoreBuffer::MoveEntriesToRememberedSet() { |
67 InsertEntriesFromBuffer(); | |
68 MemoryChunk* chunk = MemoryChunk::FromAddress(addr); | |
69 DCHECK_EQ(chunk->owner()->identity(), OLD_SPACE); | |
70 uintptr_t offset = addr - chunk->address(); | |
71 DCHECK_LT(offset, static_cast<uintptr_t>(Page::kPageSize)); | |
72 if (chunk->old_to_new_slots() == nullptr) return; | |
73 chunk->old_to_new_slots()->Remove(static_cast<uint32_t>(offset)); | |
74 } | |
75 | |
76 #ifdef VERIFY_HEAP | |
77 void StoreBuffer::VerifyPointers(LargeObjectSpace* space) { | |
78 LargeObjectIterator it(space); | |
79 for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) { | |
80 if (object->IsFixedArray()) { | |
81 Address slot_address = object->address(); | |
82 Address end = object->address() + object->Size(); | |
83 while (slot_address < end) { | |
84 HeapObject** slot = reinterpret_cast<HeapObject**>(slot_address); | |
85 // When we are not in GC the Heap::InNewSpace() predicate | |
86 // checks that pointers which satisfy predicate point into | |
87 // the active semispace. | |
88 Object* object = *slot; | |
89 heap_->InNewSpace(object); | |
90 slot_address += kPointerSize; | |
91 } | |
92 } | |
93 } | |
94 } | |
95 #endif | |
96 | |
97 | |
98 void StoreBuffer::Verify() { | |
99 #ifdef VERIFY_HEAP | |
100 VerifyPointers(heap_->lo_space()); | |
101 #endif | |
102 } | |
103 | |
104 void StoreBuffer::InsertEntriesFromBuffer() { | |
105 Address* top = reinterpret_cast<Address*>(heap_->store_buffer_top()); | 67 Address* top = reinterpret_cast<Address*>(heap_->store_buffer_top()); |
106 if (top == start_) return; | 68 if (top == start_) return; |
107 // There's no check of the limit in the loop below so we check here for | |
108 // the worst case (compaction doesn't eliminate any pointers). | |
109 DCHECK(top <= limit_); | 69 DCHECK(top <= limit_); |
110 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_)); | 70 heap_->set_store_buffer_top(reinterpret_cast<Smi*>(start_)); |
111 Page* last_page = nullptr; | |
112 SlotSet* last_slot_set = nullptr; | |
113 for (Address* current = start_; current < top; current++) { | 71 for (Address* current = start_; current < top; current++) { |
114 DCHECK(!heap_->code_space()->Contains(*current)); | 72 DCHECK(!heap_->code_space()->Contains(*current)); |
115 Address addr = *current; | 73 Address addr = *current; |
116 Page* page = Page::FromAddress(addr); | 74 Page* page = Page::FromAnyPointerAddress(heap_, addr); |
117 SlotSet* slot_set; | 75 RememberedSet<OLD_TO_NEW>::Insert(page, addr); |
118 uint32_t offset; | |
119 if (page == last_page) { | |
120 slot_set = last_slot_set; | |
121 offset = static_cast<uint32_t>(addr - page->address()); | |
122 } else { | |
123 offset = AddressToSlotSetAndOffset(addr, &slot_set); | |
124 last_page = page; | |
125 last_slot_set = slot_set; | |
126 } | |
127 slot_set->Insert(offset); | |
128 } | 76 } |
129 } | 77 } |
130 | 78 |
131 static SlotSet::CallbackResult ProcessOldToNewSlot( | |
132 Heap* heap, Address slot_address, ObjectSlotCallback slot_callback) { | |
133 Object** slot = reinterpret_cast<Object**>(slot_address); | |
134 Object* object = *slot; | |
135 if (heap->InFromSpace(object)) { | |
136 HeapObject* heap_object = reinterpret_cast<HeapObject*>(object); | |
137 DCHECK(heap_object->IsHeapObject()); | |
138 slot_callback(reinterpret_cast<HeapObject**>(slot), heap_object); | |
139 object = *slot; | |
140 // If the object was in from space before and is after executing the | |
141 // callback in to space, the object is still live. | |
142 // Unfortunately, we do not know about the slot. It could be in a | |
143 // just freed free space object. | |
144 if (heap->InToSpace(object)) { | |
145 return SlotSet::KEEP_SLOT; | |
146 } | |
147 } else { | |
148 DCHECK(!heap->InNewSpace(object)); | |
149 } | |
150 return SlotSet::REMOVE_SLOT; | |
151 } | |
152 | |
153 void StoreBuffer::IteratePointersToNewSpace(ObjectSlotCallback slot_callback) { | |
154 Heap* heap = heap_; | |
155 Iterate([heap, slot_callback](Address addr) { | |
156 return ProcessOldToNewSlot(heap, addr, slot_callback); | |
157 }); | |
158 } | |
159 | |
160 template <typename Callback> | |
161 void StoreBuffer::Iterate(Callback callback) { | |
162 InsertEntriesFromBuffer(); | |
163 PointerChunkIterator it(heap_); | |
164 MemoryChunk* chunk; | |
165 while ((chunk = it.next()) != nullptr) { | |
166 if (chunk->old_to_new_slots() != nullptr) { | |
167 SlotSet* slots = chunk->old_to_new_slots(); | |
168 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize; | |
169 for (size_t page = 0; page < pages; page++) { | |
170 slots[page].Iterate(callback); | |
171 } | |
172 } | |
173 } | |
174 } | |
175 | |
176 | |
177 void StoreBuffer::ClearInvalidStoreBufferEntries() { | |
178 InsertEntriesFromBuffer(); | |
179 | |
180 Heap* heap = heap_; | |
181 PageIterator it(heap->old_space()); | |
182 MemoryChunk* chunk; | |
183 while (it.has_next()) { | |
184 chunk = it.next(); | |
185 if (chunk->old_to_new_slots() != nullptr) { | |
186 SlotSet* slots = chunk->old_to_new_slots(); | |
187 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize; | |
188 if (pages > 1) { | |
189 // Large pages were processed above. | |
190 continue; | |
191 } | |
192 slots->Iterate([heap](Address addr) { | |
193 Object** slot = reinterpret_cast<Object**>(addr); | |
194 Object* object = *slot; | |
195 if (heap->InNewSpace(object)) { | |
196 DCHECK(object->IsHeapObject()); | |
197 // If the target object is not black, the source slot must be part | |
198 // of a non-black (dead) object. | |
199 HeapObject* heap_object = HeapObject::cast(object); | |
200 bool live = Marking::IsBlack(Marking::MarkBitFrom(heap_object)) && | |
201 heap->mark_compact_collector()->IsSlotInLiveObject(addr); | |
202 return live ? SlotSet::KEEP_SLOT : SlotSet::REMOVE_SLOT; | |
203 } | |
204 return SlotSet::REMOVE_SLOT; | |
205 }); | |
206 } | |
207 } | |
208 } | |
209 | |
210 | |
211 void StoreBuffer::VerifyValidStoreBufferEntries() { | |
212 Heap* heap = heap_; | |
213 Iterate([heap](Address addr) { | |
214 Object** slot = reinterpret_cast<Object**>(addr); | |
215 Object* object = *slot; | |
216 if (Page::FromAddress(addr)->owner() != nullptr && | |
217 Page::FromAddress(addr)->owner()->identity() == OLD_SPACE) { | |
218 CHECK(object->IsHeapObject()); | |
219 CHECK(heap->InNewSpace(object)); | |
220 heap->mark_compact_collector()->VerifyIsSlotInLiveObject( | |
221 reinterpret_cast<Address>(slot), HeapObject::cast(object)); | |
222 } | |
223 return SlotSet::KEEP_SLOT; | |
224 }); | |
225 } | |
226 | |
227 } // namespace internal | 79 } // namespace internal |
228 } // namespace v8 | 80 } // namespace v8 |
OLD | NEW |