OLD | NEW |
(Empty) | |
| 1 // 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_MARKING_H |
| 6 #define V8_MARKING_H |
| 7 |
| 8 #include "src/utils.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 |
| 13 class MarkBit { |
| 14 public: |
| 15 typedef uint32_t CellType; |
| 16 |
| 17 inline MarkBit(CellType* cell, CellType mask) : cell_(cell), mask_(mask) {} |
| 18 |
| 19 #ifdef DEBUG |
| 20 bool operator==(const MarkBit& other) { |
| 21 return cell_ == other.cell_ && mask_ == other.mask_; |
| 22 } |
| 23 #endif |
| 24 |
| 25 private: |
| 26 inline CellType* cell() { return cell_; } |
| 27 inline CellType mask() { return mask_; } |
| 28 |
| 29 inline MarkBit Next() { |
| 30 CellType new_mask = mask_ << 1; |
| 31 if (new_mask == 0) { |
| 32 return MarkBit(cell_ + 1, 1); |
| 33 } else { |
| 34 return MarkBit(cell_, new_mask); |
| 35 } |
| 36 } |
| 37 |
| 38 inline void Set() { *cell_ |= mask_; } |
| 39 inline bool Get() { return (*cell_ & mask_) != 0; } |
| 40 inline void Clear() { *cell_ &= ~mask_; } |
| 41 |
| 42 CellType* cell_; |
| 43 CellType mask_; |
| 44 |
| 45 friend class IncrementalMarking; |
| 46 friend class Marking; |
| 47 }; |
| 48 |
| 49 // Bitmap is a sequence of cells each containing fixed number of bits. |
| 50 class Bitmap { |
| 51 public: |
| 52 static const uint32_t kBitsPerCell = 32; |
| 53 static const uint32_t kBitsPerCellLog2 = 5; |
| 54 static const uint32_t kBitIndexMask = kBitsPerCell - 1; |
| 55 static const uint32_t kBytesPerCell = kBitsPerCell / kBitsPerByte; |
| 56 static const uint32_t kBytesPerCellLog2 = kBitsPerCellLog2 - kBitsPerByteLog2; |
| 57 |
| 58 static const size_t kLength = (1 << kPageSizeBits) >> (kPointerSizeLog2); |
| 59 |
| 60 static const size_t kSize = (1 << kPageSizeBits) >> |
| 61 (kPointerSizeLog2 + kBitsPerByteLog2); |
| 62 |
| 63 static int CellsForLength(int length) { |
| 64 return (length + kBitsPerCell - 1) >> kBitsPerCellLog2; |
| 65 } |
| 66 |
| 67 int CellsCount() { return CellsForLength(kLength); } |
| 68 |
| 69 static int SizeFor(int cells_count) { |
| 70 return sizeof(MarkBit::CellType) * cells_count; |
| 71 } |
| 72 |
| 73 INLINE(static uint32_t IndexToCell(uint32_t index)) { |
| 74 return index >> kBitsPerCellLog2; |
| 75 } |
| 76 |
| 77 V8_INLINE static uint32_t IndexInCell(uint32_t index) { |
| 78 return index & kBitIndexMask; |
| 79 } |
| 80 |
| 81 INLINE(static uint32_t CellToIndex(uint32_t index)) { |
| 82 return index << kBitsPerCellLog2; |
| 83 } |
| 84 |
| 85 INLINE(static uint32_t CellAlignIndex(uint32_t index)) { |
| 86 return (index + kBitIndexMask) & ~kBitIndexMask; |
| 87 } |
| 88 |
| 89 INLINE(MarkBit::CellType* cells()) { |
| 90 return reinterpret_cast<MarkBit::CellType*>(this); |
| 91 } |
| 92 |
| 93 INLINE(Address address()) { return reinterpret_cast<Address>(this); } |
| 94 |
| 95 INLINE(static Bitmap* FromAddress(Address addr)) { |
| 96 return reinterpret_cast<Bitmap*>(addr); |
| 97 } |
| 98 |
| 99 inline MarkBit MarkBitFromIndex(uint32_t index) { |
| 100 MarkBit::CellType mask = 1u << IndexInCell(index); |
| 101 MarkBit::CellType* cell = this->cells() + (index >> kBitsPerCellLog2); |
| 102 return MarkBit(cell, mask); |
| 103 } |
| 104 |
| 105 void Clear() { |
| 106 for (int i = 0; i < CellsCount(); i++) cells()[i] = 0; |
| 107 } |
| 108 |
| 109 void SetAllBits() { |
| 110 for (int i = 0; i < CellsCount(); i++) cells()[i] = 0xffffffff; |
| 111 } |
| 112 |
| 113 static void PrintWord(uint32_t word, uint32_t himask = 0) { |
| 114 for (uint32_t mask = 1; mask != 0; mask <<= 1) { |
| 115 if ((mask & himask) != 0) PrintF("["); |
| 116 PrintF((mask & word) ? "1" : "0"); |
| 117 if ((mask & himask) != 0) PrintF("]"); |
| 118 } |
| 119 } |
| 120 |
| 121 class CellPrinter { |
| 122 public: |
| 123 CellPrinter() : seq_start(0), seq_type(0), seq_length(0) {} |
| 124 |
| 125 void Print(uint32_t pos, uint32_t cell) { |
| 126 if (cell == seq_type) { |
| 127 seq_length++; |
| 128 return; |
| 129 } |
| 130 |
| 131 Flush(); |
| 132 |
| 133 if (IsSeq(cell)) { |
| 134 seq_start = pos; |
| 135 seq_length = 0; |
| 136 seq_type = cell; |
| 137 return; |
| 138 } |
| 139 |
| 140 PrintF("%d: ", pos); |
| 141 PrintWord(cell); |
| 142 PrintF("\n"); |
| 143 } |
| 144 |
| 145 void Flush() { |
| 146 if (seq_length > 0) { |
| 147 PrintF("%d: %dx%d\n", seq_start, seq_type == 0 ? 0 : 1, |
| 148 seq_length * kBitsPerCell); |
| 149 seq_length = 0; |
| 150 } |
| 151 } |
| 152 |
| 153 static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; } |
| 154 |
| 155 private: |
| 156 uint32_t seq_start; |
| 157 uint32_t seq_type; |
| 158 uint32_t seq_length; |
| 159 }; |
| 160 |
| 161 void Print() { |
| 162 CellPrinter printer; |
| 163 for (int i = 0; i < CellsCount(); i++) { |
| 164 printer.Print(i, cells()[i]); |
| 165 } |
| 166 printer.Flush(); |
| 167 PrintF("\n"); |
| 168 } |
| 169 |
| 170 bool IsClean() { |
| 171 for (int i = 0; i < CellsCount(); i++) { |
| 172 if (cells()[i] != 0) { |
| 173 return false; |
| 174 } |
| 175 } |
| 176 return true; |
| 177 } |
| 178 |
| 179 // Clears all bits starting from {cell_base_index} up to and excluding |
| 180 // {index}. Note that {cell_base_index} is required to be cell aligned. |
| 181 void ClearRange(uint32_t cell_base_index, uint32_t index) { |
| 182 DCHECK_EQ(IndexInCell(cell_base_index), 0u); |
| 183 DCHECK_GE(index, cell_base_index); |
| 184 uint32_t start_cell_index = IndexToCell(cell_base_index); |
| 185 uint32_t end_cell_index = IndexToCell(index); |
| 186 DCHECK_GE(end_cell_index, start_cell_index); |
| 187 // Clear all cells till the cell containing the last index. |
| 188 for (uint32_t i = start_cell_index; i < end_cell_index; i++) { |
| 189 cells()[i] = 0; |
| 190 } |
| 191 // Clear all bits in the last cell till the last bit before index. |
| 192 uint32_t clear_mask = ~((1u << IndexInCell(index)) - 1); |
| 193 cells()[end_cell_index] &= clear_mask; |
| 194 } |
| 195 }; |
| 196 |
| 197 class Marking : public AllStatic { |
| 198 public: |
| 199 // Impossible markbits: 01 |
| 200 static const char* kImpossibleBitPattern; |
| 201 INLINE(static bool IsImpossible(MarkBit mark_bit)) { |
| 202 return !mark_bit.Get() && mark_bit.Next().Get(); |
| 203 } |
| 204 |
| 205 // Black markbits: 11 |
| 206 static const char* kBlackBitPattern; |
| 207 INLINE(static bool IsBlack(MarkBit mark_bit)) { |
| 208 return mark_bit.Get() && mark_bit.Next().Get(); |
| 209 } |
| 210 |
| 211 // White markbits: 00 - this is required by the mark bit clearer. |
| 212 static const char* kWhiteBitPattern; |
| 213 INLINE(static bool IsWhite(MarkBit mark_bit)) { |
| 214 DCHECK(!IsImpossible(mark_bit)); |
| 215 return !mark_bit.Get(); |
| 216 } |
| 217 |
| 218 // Grey markbits: 10 |
| 219 static const char* kGreyBitPattern; |
| 220 INLINE(static bool IsGrey(MarkBit mark_bit)) { |
| 221 return mark_bit.Get() && !mark_bit.Next().Get(); |
| 222 } |
| 223 |
| 224 // IsBlackOrGrey assumes that the first bit is set for black or grey |
| 225 // objects. |
| 226 INLINE(static bool IsBlackOrGrey(MarkBit mark_bit)) { return mark_bit.Get(); } |
| 227 |
| 228 INLINE(static void MarkBlack(MarkBit mark_bit)) { |
| 229 mark_bit.Set(); |
| 230 mark_bit.Next().Set(); |
| 231 } |
| 232 |
| 233 INLINE(static void MarkWhite(MarkBit mark_bit)) { |
| 234 mark_bit.Clear(); |
| 235 mark_bit.Next().Clear(); |
| 236 } |
| 237 |
| 238 INLINE(static void BlackToWhite(MarkBit markbit)) { |
| 239 DCHECK(IsBlack(markbit)); |
| 240 markbit.Clear(); |
| 241 markbit.Next().Clear(); |
| 242 } |
| 243 |
| 244 INLINE(static void GreyToWhite(MarkBit markbit)) { |
| 245 DCHECK(IsGrey(markbit)); |
| 246 markbit.Clear(); |
| 247 markbit.Next().Clear(); |
| 248 } |
| 249 |
| 250 INLINE(static void BlackToGrey(MarkBit markbit)) { |
| 251 DCHECK(IsBlack(markbit)); |
| 252 markbit.Next().Clear(); |
| 253 } |
| 254 |
| 255 INLINE(static void WhiteToGrey(MarkBit markbit)) { |
| 256 DCHECK(IsWhite(markbit)); |
| 257 markbit.Set(); |
| 258 } |
| 259 |
| 260 INLINE(static void WhiteToBlack(MarkBit markbit)) { |
| 261 DCHECK(IsWhite(markbit)); |
| 262 markbit.Set(); |
| 263 markbit.Next().Set(); |
| 264 } |
| 265 |
| 266 INLINE(static void GreyToBlack(MarkBit markbit)) { |
| 267 DCHECK(IsGrey(markbit)); |
| 268 markbit.Next().Set(); |
| 269 } |
| 270 |
| 271 INLINE(static void AnyToGrey(MarkBit markbit)) { |
| 272 markbit.Set(); |
| 273 markbit.Next().Clear(); |
| 274 } |
| 275 |
| 276 #ifdef DEBUG |
| 277 enum ObjectColor { |
| 278 BLACK_OBJECT, |
| 279 WHITE_OBJECT, |
| 280 GREY_OBJECT, |
| 281 IMPOSSIBLE_COLOR |
| 282 }; |
| 283 |
| 284 static const char* ColorName(ObjectColor color) { |
| 285 switch (color) { |
| 286 case BLACK_OBJECT: |
| 287 return "black"; |
| 288 case WHITE_OBJECT: |
| 289 return "white"; |
| 290 case GREY_OBJECT: |
| 291 return "grey"; |
| 292 case IMPOSSIBLE_COLOR: |
| 293 return "impossible"; |
| 294 } |
| 295 return "error"; |
| 296 } |
| 297 |
| 298 static ObjectColor Color(MarkBit mark_bit) { |
| 299 if (IsBlack(mark_bit)) return BLACK_OBJECT; |
| 300 if (IsWhite(mark_bit)) return WHITE_OBJECT; |
| 301 if (IsGrey(mark_bit)) return GREY_OBJECT; |
| 302 UNREACHABLE(); |
| 303 return IMPOSSIBLE_COLOR; |
| 304 } |
| 305 #endif |
| 306 |
| 307 private: |
| 308 DISALLOW_IMPLICIT_CONSTRUCTORS(Marking); |
| 309 }; |
| 310 |
| 311 } // namespace internal |
| 312 } // namespace v8 |
| 313 |
| 314 #endif // V8_MARKING_H_ |
OLD | NEW |