Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 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. |
| 4 | |
| 5 | #ifndef V8_HEAP_MARK_COMPACT_INL_H_ |
| 6 | #define V8_HEAP_MARK_COMPACT_INL_H_ |
| 7 | |
| 8 | #include "src/heap/mark-compact.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 9 | #include "src/heap/remembered-set.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/isolate.h" |
| 11 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 15 | inline std::vector<Page*>& MarkCompactCollector::sweeping_list(Space* space) { |
| 16 | if (space == heap()->old_space()) { |
| 17 | return sweeping_list_old_space_; |
| 18 | } else if (space == heap()->code_space()) { |
| 19 | return sweeping_list_code_space_; |
| 20 | } |
| 21 | DCHECK_EQ(space, heap()->map_space()); |
| 22 | return sweeping_list_map_space_; |
| 23 | } |
| 24 | |
| 25 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | void MarkCompactCollector::PushBlack(HeapObject* obj) { |
| 27 | DCHECK(Marking::IsBlack(Marking::MarkBitFrom(obj))); |
| 28 | if (marking_deque_.Push(obj)) { |
| 29 | MemoryChunk::IncrementLiveBytesFromGC(obj, obj->Size()); |
| 30 | } else { |
| 31 | Marking::BlackToGrey(obj); |
| 32 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | void MarkCompactCollector::UnshiftBlack(HeapObject* obj) { |
| 37 | DCHECK(Marking::IsBlack(Marking::MarkBitFrom(obj))); |
| 38 | if (!marking_deque_.Unshift(obj)) { |
| 39 | MemoryChunk::IncrementLiveBytesFromGC(obj, -obj->Size()); |
| 40 | Marking::BlackToGrey(obj); |
| 41 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | void MarkCompactCollector::MarkObject(HeapObject* obj, MarkBit mark_bit) { |
| 46 | DCHECK(Marking::MarkBitFrom(obj) == mark_bit); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | if (Marking::IsWhite(mark_bit)) { |
| 48 | Marking::WhiteToBlack(mark_bit); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 49 | DCHECK(obj->GetIsolate()->heap()->Contains(obj)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 50 | PushBlack(obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void MarkCompactCollector::SetMark(HeapObject* obj, MarkBit mark_bit) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | DCHECK(Marking::IsWhite(mark_bit)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | DCHECK(Marking::MarkBitFrom(obj) == mark_bit); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | Marking::WhiteToBlack(mark_bit); |
| 59 | MemoryChunk::IncrementLiveBytesFromGC(obj, obj->Size()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | bool MarkCompactCollector::IsMarked(Object* obj) { |
| 64 | DCHECK(obj->IsHeapObject()); |
| 65 | HeapObject* heap_object = HeapObject::cast(obj); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | return Marking::IsBlackOrGrey(Marking::MarkBitFrom(heap_object)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | void MarkCompactCollector::RecordSlot(HeapObject* object, Object** slot, |
| 71 | Object* target) { |
| 72 | Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 73 | Page* source_page = Page::FromAddress(reinterpret_cast<Address>(object)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | if (target_page->IsEvacuationCandidate() && |
| 75 | !ShouldSkipEvacuationSlotRecording(object)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 76 | DCHECK(Marking::IsBlackOrGrey(Marking::MarkBitFrom(object))); |
| 77 | RememberedSet<OLD_TO_OLD>::Insert(source_page, |
| 78 | reinterpret_cast<Address>(slot)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 79 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | void CodeFlusher::AddCandidate(SharedFunctionInfo* shared_info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 84 | if (GetNextCandidate(shared_info) == nullptr) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | SetNextCandidate(shared_info, shared_function_info_candidates_head_); |
| 86 | shared_function_info_candidates_head_ = shared_info; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void CodeFlusher::AddCandidate(JSFunction* function) { |
| 92 | DCHECK(function->code() == function->shared()->code()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 93 | if (function->next_function_link()->IsUndefined()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | SetNextCandidate(function, jsfunction_candidates_head_); |
| 95 | jsfunction_candidates_head_ = function; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | JSFunction** CodeFlusher::GetNextCandidateSlot(JSFunction* candidate) { |
| 101 | return reinterpret_cast<JSFunction**>( |
| 102 | HeapObject::RawField(candidate, JSFunction::kNextFunctionLinkOffset)); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | JSFunction* CodeFlusher::GetNextCandidate(JSFunction* candidate) { |
| 107 | Object* next_candidate = candidate->next_function_link(); |
| 108 | return reinterpret_cast<JSFunction*>(next_candidate); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void CodeFlusher::SetNextCandidate(JSFunction* candidate, |
| 113 | JSFunction* next_candidate) { |
| 114 | candidate->set_next_function_link(next_candidate, UPDATE_WEAK_WRITE_BARRIER); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void CodeFlusher::ClearNextCandidate(JSFunction* candidate, Object* undefined) { |
| 119 | DCHECK(undefined->IsUndefined()); |
| 120 | candidate->set_next_function_link(undefined, SKIP_WRITE_BARRIER); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | SharedFunctionInfo* CodeFlusher::GetNextCandidate( |
| 125 | SharedFunctionInfo* candidate) { |
| 126 | Object* next_candidate = candidate->code()->gc_metadata(); |
| 127 | return reinterpret_cast<SharedFunctionInfo*>(next_candidate); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void CodeFlusher::SetNextCandidate(SharedFunctionInfo* candidate, |
| 132 | SharedFunctionInfo* next_candidate) { |
| 133 | candidate->code()->set_gc_metadata(next_candidate); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void CodeFlusher::ClearNextCandidate(SharedFunctionInfo* candidate) { |
| 138 | candidate->code()->set_gc_metadata(NULL, SKIP_WRITE_BARRIER); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | template <LiveObjectIterationMode T> |
| 143 | HeapObject* LiveObjectIterator<T>::Next() { |
| 144 | while (!it_.Done()) { |
| 145 | HeapObject* object = nullptr; |
| 146 | while (current_cell_ != 0) { |
| 147 | uint32_t trailing_zeros = base::bits::CountTrailingZeros32(current_cell_); |
| 148 | Address addr = cell_base_ + trailing_zeros * kPointerSize; |
| 149 | |
| 150 | // Clear the first bit of the found object.. |
| 151 | current_cell_ &= ~(1u << trailing_zeros); |
| 152 | |
| 153 | uint32_t second_bit_index = 0; |
| 154 | if (trailing_zeros < Bitmap::kBitIndexMask) { |
| 155 | second_bit_index = 1u << (trailing_zeros + 1); |
| 156 | } else { |
| 157 | second_bit_index = 0x1; |
| 158 | // The overlapping case; there has to exist a cell after the current |
| 159 | // cell. |
| 160 | DCHECK(!it_.Done()); |
| 161 | it_.Advance(); |
| 162 | cell_base_ = it_.CurrentCellBase(); |
| 163 | current_cell_ = *it_.CurrentCell(); |
| 164 | } |
| 165 | if (T == kBlackObjects && (current_cell_ & second_bit_index)) { |
| 166 | object = HeapObject::FromAddress(addr); |
| 167 | } else if (T == kGreyObjects && !(current_cell_ & second_bit_index)) { |
| 168 | object = HeapObject::FromAddress(addr); |
| 169 | } else if (T == kAllLiveObjects) { |
| 170 | object = HeapObject::FromAddress(addr); |
| 171 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 172 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | // Clear the second bit of the found object. |
| 174 | current_cell_ &= ~second_bit_index; |
| 175 | |
| 176 | // We found a live object. |
| 177 | if (object != nullptr) break; |
| 178 | } |
| 179 | if (current_cell_ == 0) { |
| 180 | if (!it_.Done()) { |
| 181 | it_.Advance(); |
| 182 | cell_base_ = it_.CurrentCellBase(); |
| 183 | current_cell_ = *it_.CurrentCell(); |
| 184 | } |
| 185 | } |
| 186 | if (object != nullptr) return object; |
| 187 | } |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | } // namespace internal |
| 192 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | |
| 194 | #endif // V8_HEAP_MARK_COMPACT_INL_H_ |