OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 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 #ifndef V8_HEAP_MARK_COMPACT_H_ | 5 #ifndef V8_HEAP_MARK_COMPACT_H_ |
6 #define V8_HEAP_MARK_COMPACT_H_ | 6 #define V8_HEAP_MARK_COMPACT_H_ |
7 | 7 |
8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
9 #include "src/heap/spaces.h" | 9 #include "src/heap/spaces.h" |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... | |
38 INLINE(static MarkBit MarkBitFrom(HeapObject* obj)) { | 38 INLINE(static MarkBit MarkBitFrom(HeapObject* obj)) { |
39 return MarkBitFrom(reinterpret_cast<Address>(obj)); | 39 return MarkBitFrom(reinterpret_cast<Address>(obj)); |
40 } | 40 } |
41 | 41 |
42 // Impossible markbits: 01 | 42 // Impossible markbits: 01 |
43 static const char* kImpossibleBitPattern; | 43 static const char* kImpossibleBitPattern; |
44 INLINE(static bool IsImpossible(MarkBit mark_bit)) { | 44 INLINE(static bool IsImpossible(MarkBit mark_bit)) { |
45 return !mark_bit.Get() && mark_bit.Next().Get(); | 45 return !mark_bit.Get() && mark_bit.Next().Get(); |
46 } | 46 } |
47 | 47 |
48 // Black markbits: 10 - this is required by the sweeper. | 48 // Black markbits: 11 |
49 static const char* kBlackBitPattern; | 49 static const char* kBlackBitPattern; |
50 INLINE(static bool IsBlack(MarkBit mark_bit)) { | 50 INLINE(static bool IsBlack(MarkBit mark_bit)) { |
51 return mark_bit.Get() && !mark_bit.Next().Get(); | 51 return mark_bit.Get() && mark_bit.Next().Get(); |
52 } | 52 } |
53 | 53 |
54 // White markbits: 00 - this is required by the mark bit clearer. | 54 // White markbits: 00 - this is required by the mark bit clearer. |
55 static const char* kWhiteBitPattern; | 55 static const char* kWhiteBitPattern; |
56 INLINE(static bool IsWhite(MarkBit mark_bit)) { | 56 INLINE(static bool IsWhite(MarkBit mark_bit)) { |
57 DCHECK(!IsImpossible(mark_bit)); | 57 DCHECK(!IsImpossible(mark_bit)); |
58 return !mark_bit.Get(); | 58 return !mark_bit.Get(); |
59 } | 59 } |
60 | 60 |
61 // Grey markbits: 11 | 61 // Grey markbits: 10 |
62 static const char* kGreyBitPattern; | 62 static const char* kGreyBitPattern; |
63 INLINE(static bool IsGrey(MarkBit mark_bit)) { | 63 INLINE(static bool IsGrey(MarkBit mark_bit)) { |
64 return mark_bit.Get() && mark_bit.Next().Get(); | 64 return mark_bit.Get() && !mark_bit.Next().Get(); |
65 } | 65 } |
66 | 66 |
67 // IsBlackOrGrey assumes that the first bit is set for black or grey | 67 // IsBlackOrGrey assumes that the first bit is set for black or grey |
68 // objects. | 68 // objects. |
69 INLINE(static bool IsBlackOrGrey(MarkBit mark_bit)) { return mark_bit.Get(); } | 69 INLINE(static bool IsBlackOrGrey(MarkBit mark_bit)) { return mark_bit.Get(); } |
70 | 70 |
71 INLINE(static void MarkBlack(MarkBit mark_bit)) { | 71 INLINE(static void MarkBlack(MarkBit mark_bit)) { |
72 mark_bit.Set(); | 72 mark_bit.Set(); |
73 mark_bit.Next().Clear(); | 73 mark_bit.Next().Set(); |
74 } | 74 } |
75 | 75 |
76 INLINE(static void MarkWhite(MarkBit mark_bit)) { | 76 INLINE(static void MarkWhite(MarkBit mark_bit)) { |
77 mark_bit.Clear(); | 77 mark_bit.Clear(); |
78 mark_bit.Next().Clear(); | 78 mark_bit.Next().Clear(); |
79 } | 79 } |
80 | 80 |
81 INLINE(static void BlackToWhite(MarkBit markbit)) { | 81 INLINE(static void BlackToWhite(MarkBit markbit)) { |
82 DCHECK(IsBlack(markbit)); | 82 DCHECK(IsBlack(markbit)); |
83 markbit.Clear(); | 83 markbit.Clear(); |
84 markbit.Next().Clear(); | |
84 } | 85 } |
85 | 86 |
86 INLINE(static void GreyToWhite(MarkBit markbit)) { | 87 INLINE(static void GreyToWhite(MarkBit markbit)) { |
87 DCHECK(IsGrey(markbit)); | 88 DCHECK(IsGrey(markbit)); |
88 markbit.Clear(); | 89 markbit.Clear(); |
89 markbit.Next().Clear(); | 90 markbit.Next().Clear(); |
90 } | 91 } |
91 | 92 |
92 INLINE(static void BlackToGrey(MarkBit markbit)) { | 93 INLINE(static void BlackToGrey(MarkBit markbit)) { |
93 DCHECK(IsBlack(markbit)); | 94 DCHECK(IsBlack(markbit)); |
94 markbit.Next().Set(); | 95 markbit.Next().Clear(); |
95 } | 96 } |
96 | 97 |
97 INLINE(static void WhiteToGrey(MarkBit markbit)) { | 98 INLINE(static void WhiteToGrey(MarkBit markbit)) { |
98 DCHECK(IsWhite(markbit)); | 99 DCHECK(IsWhite(markbit)); |
99 markbit.Set(); | 100 markbit.Set(); |
100 markbit.Next().Set(); | |
101 } | 101 } |
102 | 102 |
103 INLINE(static void WhiteToBlack(MarkBit markbit)) { | 103 INLINE(static void WhiteToBlack(MarkBit markbit)) { |
104 DCHECK(IsWhite(markbit)); | 104 DCHECK(IsWhite(markbit)); |
105 markbit.Set(); | 105 markbit.Set(); |
106 markbit.Next().Set(); | |
106 } | 107 } |
107 | 108 |
108 INLINE(static void GreyToBlack(MarkBit markbit)) { | 109 INLINE(static void GreyToBlack(MarkBit markbit)) { |
109 DCHECK(IsGrey(markbit)); | 110 DCHECK(IsGrey(markbit)); |
110 markbit.Next().Clear(); | 111 markbit.Next().Set(); |
111 } | 112 } |
112 | 113 |
113 INLINE(static void BlackToGrey(HeapObject* obj)) { | 114 INLINE(static void BlackToGrey(HeapObject* obj)) { |
114 BlackToGrey(MarkBitFrom(obj)); | 115 BlackToGrey(MarkBitFrom(obj)); |
115 } | 116 } |
116 | 117 |
117 INLINE(static void AnyToGrey(MarkBit markbit)) { | 118 INLINE(static void AnyToGrey(MarkBit markbit)) { |
118 markbit.Set(); | 119 markbit.Set(); |
119 markbit.Next().Set(); | 120 markbit.Next().Clear(); |
120 } | 121 } |
121 | 122 |
122 static void TransferMark(Heap* heap, Address old_start, Address new_start); | 123 static void TransferMark(Heap* heap, Address old_start, Address new_start); |
123 | 124 |
124 #ifdef DEBUG | 125 #ifdef DEBUG |
125 enum ObjectColor { | 126 enum ObjectColor { |
126 BLACK_OBJECT, | 127 BLACK_OBJECT, |
127 WHITE_OBJECT, | 128 WHITE_OBJECT, |
128 GREY_OBJECT, | 129 GREY_OBJECT, |
129 IMPOSSIBLE_COLOR | 130 IMPOSSIBLE_COLOR |
(...skipping 23 matching lines...) Expand all Loading... | |
153 if (IsGrey(mark_bit)) return GREY_OBJECT; | 154 if (IsGrey(mark_bit)) return GREY_OBJECT; |
154 UNREACHABLE(); | 155 UNREACHABLE(); |
155 return IMPOSSIBLE_COLOR; | 156 return IMPOSSIBLE_COLOR; |
156 } | 157 } |
157 #endif | 158 #endif |
158 | 159 |
159 // Returns true if the transferred color is black. | 160 // Returns true if the transferred color is black. |
160 INLINE(static bool TransferColor(HeapObject* from, HeapObject* to)) { | 161 INLINE(static bool TransferColor(HeapObject* from, HeapObject* to)) { |
161 MarkBit from_mark_bit = MarkBitFrom(from); | 162 MarkBit from_mark_bit = MarkBitFrom(from); |
162 MarkBit to_mark_bit = MarkBitFrom(to); | 163 MarkBit to_mark_bit = MarkBitFrom(to); |
163 bool is_black = false; | 164 DCHECK(Marking::IsWhite(to_mark_bit)); |
164 if (from_mark_bit.Get()) { | 165 if (from_mark_bit.Get()) { |
165 to_mark_bit.Set(); | 166 to_mark_bit.Set(); |
166 is_black = true; // Looks black so far. | 167 if (from_mark_bit.Next().Get()) { |
168 to_mark_bit.Next().Set(); | |
169 return true; | |
170 } | |
167 } | 171 } |
168 if (from_mark_bit.Next().Get()) { | 172 return false; |
169 to_mark_bit.Next().Set(); | |
170 is_black = false; // Was actually gray. | |
171 } | |
172 return is_black; | |
173 } | 173 } |
174 | 174 |
175 private: | 175 private: |
176 DISALLOW_IMPLICIT_CONSTRUCTORS(Marking); | 176 DISALLOW_IMPLICIT_CONSTRUCTORS(Marking); |
177 }; | 177 }; |
178 | 178 |
179 // ---------------------------------------------------------------------------- | 179 // ---------------------------------------------------------------------------- |
180 // Marking deque for tracing live objects. | 180 // Marking deque for tracing live objects. |
181 class MarkingDeque { | 181 class MarkingDeque { |
182 public: | 182 public: |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
847 | 847 |
848 private: | 848 private: |
849 MemoryChunk* chunk_; | 849 MemoryChunk* chunk_; |
850 MarkBit::CellType* cells_; | 850 MarkBit::CellType* cells_; |
851 unsigned int last_cell_index_; | 851 unsigned int last_cell_index_; |
852 unsigned int cell_index_; | 852 unsigned int cell_index_; |
853 Address cell_base_; | 853 Address cell_base_; |
854 }; | 854 }; |
855 | 855 |
856 | 856 |
857 class LiveObjectIterator BASE_EMBEDDED { | |
858 public: | |
859 enum LiveObjectIterationMode { kBlackObjects, kGreyObjects, kAllLiveObjects }; | |
860 | |
861 explicit LiveObjectIterator(MemoryChunk* chunk) | |
862 : chunk_(chunk), | |
863 it_(chunk_), | |
864 cell_base_(it_.CurrentCellBase()), | |
865 current_cell_(*it_.CurrentCell()) {} | |
866 | |
867 template <LiveObjectIterationMode mode> | |
ulan
2016/01/07 16:39:11
The template parameter should be on the class, not
Hannes Payer (out of office)
2016/01/07 20:17:33
Done.
| |
868 inline HeapObject* Next() { | |
Michael Lippautz
2016/01/07 14:02:53
Maybe move the implementation to to mark-compact-i
Hannes Payer (out of office)
2016/01/07 16:26:16
Done.
| |
869 while (!it_.Done()) { | |
870 HeapObject* object = NULL; | |
Michael Lippautz
2016/01/07 14:02:53
nit:nullptr
Hannes Payer (out of office)
2016/01/07 16:26:16
Done.
| |
871 while (current_cell_ != 0) { | |
872 int trailing_zeros = base::bits::CountTrailingZeros32(current_cell_); | |
Michael Lippautz
2016/01/07 14:02:53
nit: Maybe add
DCHECK_NE(trailing_zeros, 32);
Hannes Payer (out of office)
2016/01/07 16:26:16
This is implied by current_cell_ != 0 and by the i
| |
873 Address addr = cell_base_ + trailing_zeros * kPointerSize; | |
874 | |
875 current_cell_ &= ~(1u << trailing_zeros); | |
ulan
2016/01/07 16:39:11
unsigned color = 0;
if (trailing_zeros < Bitmap::k
Hannes Payer (out of office)
2016/01/07 20:17:33
I refactored it the other way around.
| |
876 if (trailing_zeros < Bitmap::kBitIndexMask) { | |
Michael Lippautz
2016/01/07 14:02:53
You could do
uint32_t bit_idx = 0;
if (trailin
Hannes Payer (out of office)
2016/01/07 16:26:16
Done.
| |
877 if (mode == kBlackObjects && | |
878 (current_cell_ & 1u << (trailing_zeros + 1))) { | |
879 object = HeapObject::FromAddress(addr); | |
880 } else if (mode == kGreyObjects && | |
881 !(current_cell_ & 1u << (trailing_zeros + 1))) { | |
882 object = HeapObject::FromAddress(addr); | |
883 } else if (mode == kAllLiveObjects) { | |
884 object = HeapObject::FromAddress(addr); | |
885 } | |
886 current_cell_ &= ~(1u << (trailing_zeros + 1)); | |
887 } else { | |
888 // The overlapping case; there has to exist a cell after the current | |
889 // cell. | |
890 DCHECK(!it_.Done()); | |
891 it_.Advance(); | |
892 cell_base_ = it_.CurrentCellBase(); | |
893 current_cell_ = *it_.CurrentCell(); | |
894 if (mode == kBlackObjects && (current_cell_ & 0x1)) { | |
895 object = HeapObject::FromAddress(addr); | |
896 } else if (mode == kGreyObjects && !(current_cell_ & 0x1)) { | |
897 object = HeapObject::FromAddress(addr); | |
898 } else if (mode == kAllLiveObjects) { | |
899 object = HeapObject::FromAddress(addr); | |
900 } | |
901 // Clear overlapping bit. | |
902 current_cell_ &= 0xFFFFFFFE; | |
903 } | |
904 | |
905 // We found a live object. | |
906 if (object != NULL) break; | |
Michael Lippautz
2016/01/07 14:02:53
nit: nullptr, likewise below
Hannes Payer (out of office)
2016/01/07 16:26:16
Done.
| |
907 } | |
908 if (current_cell_ == 0) { | |
909 if (!it_.Done()) { | |
910 it_.Advance(); | |
911 cell_base_ = it_.CurrentCellBase(); | |
912 current_cell_ = *it_.CurrentCell(); | |
913 } | |
914 } | |
915 if (object != NULL) return object; | |
916 } | |
917 return NULL; | |
918 } | |
919 | |
920 | |
921 private: | |
922 MemoryChunk* chunk_; | |
923 MarkBitCellIterator it_; | |
924 Address cell_base_; | |
925 MarkBit::CellType current_cell_; | |
926 }; | |
927 | |
928 | |
857 class EvacuationScope BASE_EMBEDDED { | 929 class EvacuationScope BASE_EMBEDDED { |
858 public: | 930 public: |
859 explicit EvacuationScope(MarkCompactCollector* collector) | 931 explicit EvacuationScope(MarkCompactCollector* collector) |
860 : collector_(collector) { | 932 : collector_(collector) { |
861 collector_->set_evacuation(true); | 933 collector_->set_evacuation(true); |
862 } | 934 } |
863 | 935 |
864 ~EvacuationScope() { collector_->set_evacuation(false); } | 936 ~EvacuationScope() { collector_->set_evacuation(false); } |
865 | 937 |
866 private: | 938 private: |
867 MarkCompactCollector* collector_; | 939 MarkCompactCollector* collector_; |
868 }; | 940 }; |
869 | 941 |
870 | 942 |
871 const char* AllocationSpaceName(AllocationSpace space); | 943 const char* AllocationSpaceName(AllocationSpace space); |
872 } // namespace internal | 944 } // namespace internal |
873 } // namespace v8 | 945 } // namespace v8 |
874 | 946 |
875 #endif // V8_HEAP_MARK_COMPACT_H_ | 947 #endif // V8_HEAP_MARK_COMPACT_H_ |
OLD | NEW |