blob: 281ece4cc8d693601e26bd5a6df454064dccfa5a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Murdochda12d292016-06-02 14:46:10 +01009#include "src/heap/remembered-set.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/isolate.h"
11
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012namespace v8 {
13namespace internal {
14
Ben Murdoch097c5b22016-05-18 11:27:45 +010015inline 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 Murdoch4a90d5f2016-03-22 12:00:34 +000026void 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 Murdochb8a8cc12014-11-26 15:28:44 +000033}
34
35
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036void 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 Murdochb8a8cc12014-11-26 15:28:44 +000042}
43
44
45void MarkCompactCollector::MarkObject(HeapObject* obj, MarkBit mark_bit) {
46 DCHECK(Marking::MarkBitFrom(obj) == mark_bit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 if (Marking::IsWhite(mark_bit)) {
48 Marking::WhiteToBlack(mark_bit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 DCHECK(obj->GetIsolate()->heap()->Contains(obj));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050 PushBlack(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 }
52}
53
54
55void MarkCompactCollector::SetMark(HeapObject* obj, MarkBit mark_bit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 DCHECK(Marking::IsWhite(mark_bit));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 DCHECK(Marking::MarkBitFrom(obj) == mark_bit);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 Marking::WhiteToBlack(mark_bit);
59 MemoryChunk::IncrementLiveBytesFromGC(obj, obj->Size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060}
61
62
63bool MarkCompactCollector::IsMarked(Object* obj) {
64 DCHECK(obj->IsHeapObject());
65 HeapObject* heap_object = HeapObject::cast(obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 return Marking::IsBlackOrGrey(Marking::MarkBitFrom(heap_object));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067}
68
69
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070void MarkCompactCollector::RecordSlot(HeapObject* object, Object** slot,
71 Object* target) {
72 Page* target_page = Page::FromAddress(reinterpret_cast<Address>(target));
Ben Murdochda12d292016-06-02 14:46:10 +010073 Page* source_page = Page::FromAddress(reinterpret_cast<Address>(object));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 if (target_page->IsEvacuationCandidate() &&
75 !ShouldSkipEvacuationSlotRecording(object)) {
Ben Murdochda12d292016-06-02 14:46:10 +010076 DCHECK(Marking::IsBlackOrGrey(Marking::MarkBitFrom(object)));
77 RememberedSet<OLD_TO_OLD>::Insert(source_page,
78 reinterpret_cast<Address>(slot));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081
82
83void CodeFlusher::AddCandidate(SharedFunctionInfo* shared_info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010084 if (GetNextCandidate(shared_info) == nullptr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 SetNextCandidate(shared_info, shared_function_info_candidates_head_);
86 shared_function_info_candidates_head_ = shared_info;
87 }
88}
89
90
91void CodeFlusher::AddCandidate(JSFunction* function) {
92 DCHECK(function->code() == function->shared()->code());
Ben Murdoch097c5b22016-05-18 11:27:45 +010093 if (function->next_function_link()->IsUndefined()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 SetNextCandidate(function, jsfunction_candidates_head_);
95 jsfunction_candidates_head_ = function;
96 }
97}
98
99
100JSFunction** CodeFlusher::GetNextCandidateSlot(JSFunction* candidate) {
101 return reinterpret_cast<JSFunction**>(
102 HeapObject::RawField(candidate, JSFunction::kNextFunctionLinkOffset));
103}
104
105
106JSFunction* CodeFlusher::GetNextCandidate(JSFunction* candidate) {
107 Object* next_candidate = candidate->next_function_link();
108 return reinterpret_cast<JSFunction*>(next_candidate);
109}
110
111
112void CodeFlusher::SetNextCandidate(JSFunction* candidate,
113 JSFunction* next_candidate) {
114 candidate->set_next_function_link(next_candidate, UPDATE_WEAK_WRITE_BARRIER);
115}
116
117
118void CodeFlusher::ClearNextCandidate(JSFunction* candidate, Object* undefined) {
119 DCHECK(undefined->IsUndefined());
120 candidate->set_next_function_link(undefined, SKIP_WRITE_BARRIER);
121}
122
123
124SharedFunctionInfo* CodeFlusher::GetNextCandidate(
125 SharedFunctionInfo* candidate) {
126 Object* next_candidate = candidate->code()->gc_metadata();
127 return reinterpret_cast<SharedFunctionInfo*>(next_candidate);
128}
129
130
131void CodeFlusher::SetNextCandidate(SharedFunctionInfo* candidate,
132 SharedFunctionInfo* next_candidate) {
133 candidate->code()->set_gc_metadata(next_candidate);
134}
135
136
137void CodeFlusher::ClearNextCandidate(SharedFunctionInfo* candidate) {
138 candidate->code()->set_gc_metadata(NULL, SKIP_WRITE_BARRIER);
139}
140
141
142template <LiveObjectIterationMode T>
143HeapObject* 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 Murdochda12d292016-06-02 14:46:10 +0100172
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000193
194#endif // V8_HEAP_MARK_COMPACT_INL_H_