blob: 351d76edb84c18706da7fede9003948d75562a59 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001// Copyright 2016 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_REMEMBERED_SET_H
6#define V8_REMEMBERED_SET_H
7
8#include "src/heap/heap.h"
9#include "src/heap/slot-set.h"
10#include "src/heap/spaces.h"
11
12namespace v8 {
13namespace internal {
14
15enum PointerDirection { OLD_TO_OLD, OLD_TO_NEW };
16
17template <PointerDirection direction>
18class RememberedSet {
19 public:
20 // Given a page and a slot in that page, this function adds the slot to the
21 // remembered set.
22 static void Insert(Page* page, Address slot_addr) {
23 DCHECK(page->Contains(slot_addr));
24 SlotSet* slot_set = GetSlotSet(page);
25 if (slot_set == nullptr) {
26 slot_set = AllocateSlotSet(page);
27 }
28 uintptr_t offset = slot_addr - page->address();
29 slot_set[offset / Page::kPageSize].Insert(offset % Page::kPageSize);
30 }
31
32 // Given a page and a slot in that page, this function removes the slot from
33 // the remembered set.
34 // If the slot was never added, then the function does nothing.
35 static void Remove(Page* page, Address slot_addr) {
36 DCHECK(page->Contains(slot_addr));
37 SlotSet* slot_set = GetSlotSet(page);
38 if (slot_set != nullptr) {
39 uintptr_t offset = slot_addr - page->address();
40 slot_set[offset / Page::kPageSize].Remove(offset % Page::kPageSize);
41 }
42 }
43
44 // Given a page and a range of slots in that page, this function removes the
45 // slots from the remembered set.
46 static void RemoveRange(Page* page, Address start, Address end) {
47 SlotSet* slot_set = GetSlotSet(page);
48 if (slot_set != nullptr) {
49 uintptr_t start_offset = start - page->address();
50 uintptr_t end_offset = end - page->address();
51 DCHECK_LT(start_offset, end_offset);
52 DCHECK_LE(end_offset, static_cast<uintptr_t>(Page::kPageSize));
53 slot_set->RemoveRange(static_cast<uint32_t>(start_offset),
54 static_cast<uint32_t>(end_offset));
55 }
56 }
57
58 // Iterates and filters the remembered set with the given callback.
59 // The callback should take (Address slot) and return SlotSet::CallbackResult.
60 template <typename Callback>
61 static void Iterate(Heap* heap, Callback callback) {
62 PointerChunkIterator it(heap);
63 MemoryChunk* chunk;
64 while ((chunk = it.next()) != nullptr) {
65 SlotSet* slots = GetSlotSet(chunk);
66 if (slots != nullptr) {
67 size_t pages = (chunk->size() + Page::kPageSize - 1) / Page::kPageSize;
68 int new_count = 0;
69 for (size_t page = 0; page < pages; page++) {
70 new_count += slots[page].Iterate(callback);
71 }
72 if (new_count == 0) {
73 ReleaseSlotSet(chunk);
74 }
75 }
76 }
77 }
78
79 // Iterates and filters the remembered set with the given callback.
80 // The callback should take (HeapObject** slot, HeapObject* target) and
81 // update the slot.
82 // A special wrapper takes care of filtering the slots based on their values.
83 // For OLD_TO_NEW case: slots that do not point to the ToSpace after
84 // callback invocation will be removed from the set.
85 template <typename Callback>
86 static void IterateWithWrapper(Heap* heap, Callback callback) {
87 Iterate(heap, [heap, callback](Address addr) {
88 return Wrapper(heap, addr, callback);
89 });
90 }
91
92 // Eliminates all stale slots from the remembered set, i.e.
93 // slots that are not part of live objects anymore. This method must be
94 // called after marking, when the whole transitive closure is known and
95 // must be called before sweeping when mark bits are still intact.
96 static void ClearInvalidSlots(Heap* heap);
97
98 static void VerifyValidSlots(Heap* heap);
99
100 private:
101 static SlotSet* GetSlotSet(MemoryChunk* chunk) {
102 if (direction == OLD_TO_OLD) {
103 return chunk->old_to_old_slots();
104 } else {
105 return chunk->old_to_new_slots();
106 }
107 }
108
109 static void ReleaseSlotSet(MemoryChunk* chunk) {
110 if (direction == OLD_TO_OLD) {
111 chunk->ReleaseOldToOldSlots();
112 } else {
113 chunk->ReleaseOldToNewSlots();
114 }
115 }
116
117 static SlotSet* AllocateSlotSet(MemoryChunk* chunk) {
118 if (direction == OLD_TO_OLD) {
119 chunk->AllocateOldToOldSlots();
120 return chunk->old_to_old_slots();
121 } else {
122 chunk->AllocateOldToNewSlots();
123 return chunk->old_to_new_slots();
124 }
125 }
126
127 template <typename Callback>
128 static SlotSet::CallbackResult Wrapper(Heap* heap, Address slot_address,
129 Callback slot_callback) {
130 STATIC_ASSERT(direction == OLD_TO_NEW);
131 Object** slot = reinterpret_cast<Object**>(slot_address);
132 Object* object = *slot;
133 if (heap->InFromSpace(object)) {
134 HeapObject* heap_object = reinterpret_cast<HeapObject*>(object);
135 DCHECK(heap_object->IsHeapObject());
136 slot_callback(reinterpret_cast<HeapObject**>(slot), heap_object);
137 object = *slot;
138 // If the object was in from space before and is after executing the
139 // callback in to space, the object is still live.
140 // Unfortunately, we do not know about the slot. It could be in a
141 // just freed free space object.
142 if (heap->InToSpace(object)) {
143 return SlotSet::KEEP_SLOT;
144 }
145 } else {
146 DCHECK(!heap->InNewSpace(object));
147 }
148 return SlotSet::REMOVE_SLOT;
149 }
150
151 static bool IsValidSlot(Heap* heap, Object** slot);
152};
153
154} // namespace internal
155} // namespace v8
156
157#endif // V8_REMEMBERED_SET_H