5 #ifndef V8_HEAP_REMEMBERED_SET_H_ 6 #define V8_HEAP_REMEMBERED_SET_H_ 8 #include "src/heap/heap.h" 9 #include "src/heap/slot-set.h" 10 #include "src/heap/spaces.h" 11 #include "src/reloc-info.h" 12 #include "src/v8memory.h" 17 enum RememberedSetIterationMode { SYNCHRONIZED, NON_SYNCHRONIZED };
20 template <RememberedSetType type>
25 template <AccessMode access_mode = AccessMode::ATOMIC>
27 DCHECK(chunk->Contains(slot_addr));
28 SlotSet* slot_set = chunk->slot_set<
type, access_mode>();
29 if (slot_set ==
nullptr) {
30 slot_set = chunk->AllocateSlotSet<
type>();
32 uintptr_t offset = slot_addr - chunk->address();
33 slot_set[offset / Page::kPageSize].Insert<access_mode>(offset %
40 DCHECK(chunk->Contains(slot_addr));
42 if (slot_set ==
nullptr) {
45 uintptr_t offset = slot_addr - chunk->address();
46 return slot_set[offset / Page::kPageSize].Contains(offset %
54 DCHECK(chunk->Contains(slot_addr));
56 if (slot_set !=
nullptr) {
57 uintptr_t offset = slot_addr - chunk->address();
58 slot_set[offset / Page::kPageSize].Remove(offset % Page::kPageSize);
65 SlotSet::EmptyBucketMode mode) {
67 if (slot_set !=
nullptr) {
68 uintptr_t start_offset = start - chunk->address();
69 uintptr_t end_offset = end - chunk->address();
70 DCHECK_LT(start_offset, end_offset);
71 if (end_offset < static_cast<uintptr_t>(Page::kPageSize)) {
72 slot_set->RemoveRange(static_cast<int>(start_offset),
73 static_cast<int>(end_offset), mode);
77 int start_chunk =
static_cast<int>(start_offset / Page::kPageSize);
78 int end_chunk =
static_cast<int>((end_offset - 1) / Page::kPageSize);
79 int offset_in_start_chunk =
80 static_cast<int>(start_offset % Page::kPageSize);
83 int offset_in_end_chunk =
static_cast<int>(
84 end_offset -
static_cast<uintptr_t>(end_chunk) * Page::kPageSize);
85 if (start_chunk == end_chunk) {
86 slot_set[start_chunk].RemoveRange(offset_in_start_chunk,
87 offset_in_end_chunk, mode);
90 slot_set[start_chunk].RemoveRange(offset_in_start_chunk,
91 Page::kPageSize, mode);
93 for (
int i = start_chunk + 1;
i < end_chunk;
i++) {
94 slot_set[
i].RemoveRange(0, Page::kPageSize, mode);
97 slot_set[end_chunk].RemoveRange(0, offset_in_end_chunk, mode);
105 template <
typename Callback>
106 static void Iterate(
Heap* heap, RememberedSetIterationMode mode,
108 IterateMemoryChunks(heap, [mode, callback](
MemoryChunk* chunk) {
109 if (mode == SYNCHRONIZED) chunk->mutex()->Lock();
110 Iterate(chunk, callback);
111 if (mode == SYNCHRONIZED) chunk->mutex()->Unlock();
117 template <
typename Callback>
118 static void IterateMemoryChunks(
Heap* heap, Callback callback) {
121 while ((chunk = it.next()) !=
nullptr) {
124 if (slots !=
nullptr || typed_slots !=
nullptr ||
125 chunk->invalidated_slots() !=
nullptr) {
137 template <
typename Callback>
138 static void Iterate(
MemoryChunk* chunk, Callback callback,
139 SlotSet::EmptyBucketMode mode) {
141 if (slots !=
nullptr) {
142 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize;
144 for (
size_t page = 0; page < pages; page++) {
145 new_count += slots[page].Iterate(callback, mode);
149 if (
type == OLD_TO_OLD && new_count == 0) {
150 chunk->ReleaseSlotSet<OLD_TO_OLD>();
155 static int NumberOfPreFreedEmptyBuckets(
MemoryChunk* chunk) {
156 DCHECK(
type == OLD_TO_NEW);
159 if (slots !=
nullptr) {
160 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize;
161 for (
size_t page = 0; page < pages; page++) {
162 result += slots[page].NumberOfPreFreedEmptyBuckets();
168 static void PreFreeEmptyBuckets(
MemoryChunk* chunk) {
169 DCHECK(
type == OLD_TO_NEW);
171 if (slots !=
nullptr) {
172 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize;
173 for (
size_t page = 0; page < pages; page++) {
174 slots[page].PreFreeEmptyBuckets();
180 DCHECK(
type == OLD_TO_NEW);
182 if (slots !=
nullptr) {
183 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize;
184 for (
size_t page = 0; page < pages; page++) {
185 slots[page].FreeEmptyBuckets();
186 slots[page].FreeToBeFreedBuckets();
193 static void InsertTyped(
Page* page,
Address host_addr, SlotType slot_type,
196 if (slot_set ==
nullptr) {
197 slot_set = page->AllocateTypedSlotSet<
type>();
199 if (host_addr == kNullAddress) {
200 host_addr = page->address();
202 uintptr_t offset = slot_addr - page->address();
203 uintptr_t host_offset = host_addr - page->address();
204 DCHECK_LT(offset, static_cast<uintptr_t>(TypedSlotSet::kMaxOffset));
205 DCHECK_LT(host_offset, static_cast<uintptr_t>(TypedSlotSet::kMaxOffset));
206 slot_set->Insert(slot_type, static_cast<uint32_t>(host_offset),
207 static_cast<uint32_t>(offset));
214 if (slots !=
nullptr) {
216 [start, end](SlotType slot_type,
Address host_addr,
218 return start <= slot_addr && slot_addr < end ? REMOVE_SLOT
221 TypedSlotSet::PREFREE_EMPTY_CHUNKS);
228 template <
typename Callback>
229 static void IterateTyped(
Heap* heap, RememberedSetIterationMode mode,
231 IterateMemoryChunks(heap, [mode, callback](
MemoryChunk* chunk) {
232 if (mode == SYNCHRONIZED) chunk->mutex()->Lock();
233 IterateTyped(chunk, callback);
234 if (mode == SYNCHRONIZED) chunk->mutex()->Unlock();
241 template <
typename Callback>
242 static void IterateTyped(
MemoryChunk* chunk, Callback callback) {
244 if (slots !=
nullptr) {
245 int new_count = slots->Iterate(callback, TypedSlotSet::KEEP_EMPTY_CHUNKS);
246 if (new_count == 0) {
247 chunk->ReleaseTypedSlotSet<
type>();
253 static void ClearAll(
Heap* heap) {
254 STATIC_ASSERT(
type == OLD_TO_OLD);
257 while ((chunk = it.next()) !=
nullptr) {
258 chunk->ReleaseSlotSet<OLD_TO_OLD>();
259 chunk->ReleaseTypedSlotSet<OLD_TO_OLD>();
260 chunk->ReleaseInvalidatedSlots();
272 template <
typename Callback>
273 static SlotCallbackResult UpdateCodeEntry(
Address entry_address,
275 Object* code = Code::GetObjectFromEntryAddress(entry_address);
278 DCHECK(!HasWeakHeapObjectTag(code));
279 if (code != old_code) {
280 Memory<Address>(entry_address) = Code::cast(code)->entry();
287 template <
typename Callback>
288 static SlotCallbackResult UpdateCodeTarget(
RelocInfo* rinfo,
290 DCHECK(RelocInfo::IsCodeTargetMode(rinfo->rmode()));
291 Code old_target = Code::GetCodeFromTargetAddress(rinfo->target_address());
292 Object* new_target = old_target;
294 DCHECK(!HasWeakHeapObjectTag(new_target));
295 if (new_target != old_target) {
296 rinfo->set_target_address(
297 Code::cast(new_target)->raw_instruction_start());
304 template <
typename Callback>
305 static SlotCallbackResult UpdateEmbeddedPointer(
Heap* heap,
RelocInfo* rinfo,
307 DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
308 HeapObject* old_target = rinfo->target_object();
309 Object* new_target = old_target;
311 DCHECK(!HasWeakHeapObjectTag(new_target));
312 if (new_target != old_target) {
313 rinfo->set_target_object(heap, HeapObject::cast(new_target));
320 template <
typename Callback>
321 static SlotCallbackResult UpdateTypedSlot(
Heap* heap, SlotType slot_type,
322 Address addr, Callback callback) {
324 case CODE_TARGET_SLOT: {
326 return UpdateCodeTarget(&rinfo, callback);
328 case CODE_ENTRY_SLOT: {
329 return UpdateCodeEntry(addr, callback);
331 case EMBEDDED_OBJECT_SLOT: {
332 RelocInfo rinfo(addr, RelocInfo::EMBEDDED_OBJECT, 0,
Code());
333 return UpdateEmbeddedPointer(heap, &rinfo, callback);
345 inline SlotType SlotTypeForRelocInfoMode(RelocInfo::Mode rmode) {
346 if (RelocInfo::IsCodeTargetMode(rmode)) {
347 return CODE_TARGET_SLOT;
348 }
else if (RelocInfo::IsEmbeddedObject(rmode)) {
349 return EMBEDDED_OBJECT_SLOT;
357 #endif // V8_HEAP_REMEMBERED_SET_H_