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 20 matching lines...) Expand all Loading... | |
150 static ObjectColor Color(MarkBit mark_bit) { | 151 static ObjectColor Color(MarkBit mark_bit) { |
151 if (IsBlack(mark_bit)) return BLACK_OBJECT; | 152 if (IsBlack(mark_bit)) return BLACK_OBJECT; |
152 if (IsWhite(mark_bit)) return WHITE_OBJECT; | 153 if (IsWhite(mark_bit)) return WHITE_OBJECT; |
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)) { |
ulan
2016/01/05 14:45:23
Let's assert that the color of the _to_ is white.
Hannes Payer (out of office)
2016/01/07 10:30:32
Done.
| |
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 if (from_mark_bit.Get()) { | 164 if (from_mark_bit.Get()) { |
165 to_mark_bit.Set(); | 165 to_mark_bit.Set(); |
166 is_black = true; // Looks black so far. | 166 // Looks black so far. |
ulan
2016/01/05 14:45:23
This comment doesn't help anymore. Let's just remo
Hannes Payer (out of office)
2016/01/07 10:30:32
Done.
| |
167 if (from_mark_bit.Next().Get()) { | |
168 to_mark_bit.Next().Set(); | |
169 return true; // Is black. | |
Hannes Payer (out of office)
2016/01/07 10:30:32
I also removed "// Is black." comment.
| |
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 LiveObjectIterator(MemoryChunk* chunk, LiveObjectIterationMode mode) | |
862 : chunk_(chunk), | |
863 mode_(mode), | |
864 it_(chunk_), | |
865 cell_base_(it_.CurrentCellBase()), | |
866 current_cell_(*it_.CurrentCell()), | |
867 clear_overlapping_bit_(false) {} | |
868 | |
869 inline HeapObject* Next() { | |
870 while (!it_.Done()) { | |
ulan
2016/01/05 14:45:23
IUC, this condition will cause early return for th
Hannes Payer (out of office)
2016/01/07 10:30:32
Nope, last cell index is already invalid, so this
| |
871 HeapObject* object = NULL; | |
872 if (clear_overlapping_bit_) { | |
873 DCHECK(current_cell_ & 0x1); | |
874 current_cell_ &= 0xFFFFFFFE; | |
875 clear_overlapping_bit_ = false; | |
876 } | |
877 | |
878 while (current_cell_ != 0) { | |
879 int trailing_zeros = base::bits::CountTrailingZeros32(current_cell_); | |
880 Address addr = cell_base_ + trailing_zeros * kPointerSize; | |
881 MarkBit mark_bit = Marking::MarkBitFrom(addr); | |
882 | |
883 if (IsValidLiveObject(mark_bit)) { | |
884 object = HeapObject::FromAddress(addr); | |
885 } | |
886 | |
887 current_cell_ &= ~(1u << trailing_zeros); | |
888 if (trailing_zeros < Bitmap::kBitIndexMask) { | |
889 current_cell_ &= ~(1u << (trailing_zeros + 1)); | |
890 } else if (Marking::IsBlack(mark_bit)) { | |
891 clear_overlapping_bit_ = true; | |
ulan
2016/01/05 14:45:23
We would need to fetch the next cell here anyway.
Hannes Payer (out of office)
2016/01/07 10:30:32
Done.
| |
892 } | |
893 | |
894 // We found a live object. | |
895 break; | |
896 } | |
897 if (current_cell_ == 0) { | |
898 it_.Advance(); | |
899 cell_base_ = it_.CurrentCellBase(); | |
900 current_cell_ = *it_.CurrentCell(); | |
901 } | |
902 if (object != NULL) return object; | |
903 } | |
904 return NULL; | |
905 } | |
906 | |
907 | |
908 private: | |
909 bool IsValidLiveObject(MarkBit mark_bit) { | |
910 return mode_ == kAllLiveObjects || | |
ulan
2016/01/05 14:45:23
As discussed we can templatize this.
Hannes Payer (out of office)
2016/01/07 10:30:32
Done.
| |
911 (mode_ == kBlackObjects && Marking::IsBlack(mark_bit)) || | |
912 (mode_ == kGreyObjects && Marking::IsGrey(mark_bit)); | |
913 } | |
914 | |
915 MemoryChunk* chunk_; | |
916 LiveObjectIterationMode mode_; | |
917 MarkBitCellIterator it_; | |
918 Address cell_base_; | |
919 MarkBit::CellType current_cell_; | |
920 bool clear_overlapping_bit_; | |
921 }; | |
922 | |
923 | |
857 class EvacuationScope BASE_EMBEDDED { | 924 class EvacuationScope BASE_EMBEDDED { |
858 public: | 925 public: |
859 explicit EvacuationScope(MarkCompactCollector* collector) | 926 explicit EvacuationScope(MarkCompactCollector* collector) |
860 : collector_(collector) { | 927 : collector_(collector) { |
861 collector_->set_evacuation(true); | 928 collector_->set_evacuation(true); |
862 } | 929 } |
863 | 930 |
864 ~EvacuationScope() { collector_->set_evacuation(false); } | 931 ~EvacuationScope() { collector_->set_evacuation(false); } |
865 | 932 |
866 private: | 933 private: |
867 MarkCompactCollector* collector_; | 934 MarkCompactCollector* collector_; |
868 }; | 935 }; |
869 | 936 |
870 | 937 |
871 const char* AllocationSpaceName(AllocationSpace space); | 938 const char* AllocationSpaceName(AllocationSpace space); |
872 } // namespace internal | 939 } // namespace internal |
873 } // namespace v8 | 940 } // namespace v8 |
874 | 941 |
875 #endif // V8_HEAP_MARK_COMPACT_H_ | 942 #endif // V8_HEAP_MARK_COMPACT_H_ |
OLD | NEW |