1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_MARKING_H
#define V8_MARKING_H
#include "src/base/atomic-utils.h"
#include "src/utils.h"
namespace v8 {
namespace internal {
class MarkBit {
public:
typedef uint32_t CellType;
STATIC_ASSERT(sizeof(CellType) == sizeof(base::Atomic32));
enum AccessMode { ATOMIC, NON_ATOMIC };
inline MarkBit(base::Atomic32* cell, CellType mask) : cell_(cell) {
mask_ = static_cast<base::Atomic32>(mask);
}
#ifdef DEBUG
bool operator==(const MarkBit& other) {
return cell_ == other.cell_ && mask_ == other.mask_;
}
#endif
private:
inline MarkBit Next() {
CellType new_mask = mask_ << 1;
if (new_mask == 0) {
return MarkBit(cell_ + 1, 1);
} else {
return MarkBit(cell_, new_mask);
}
}
// The function returns true if it succeeded to
// transition the bit from 0 to 1.
template <AccessMode mode = NON_ATOMIC>
inline bool Set();
template <AccessMode mode = NON_ATOMIC>
inline bool Get();
// The function returns true if it succeeded to
// transition the bit from 1 to 0.
template <AccessMode mode = NON_ATOMIC>
inline bool Clear();
base::Atomic32* cell_;
base::Atomic32 mask_;
friend class IncrementalMarking;
friend class ConcurrentMarkingMarkbits;
friend class Marking;
};
template <>
inline bool MarkBit::Set<MarkBit::NON_ATOMIC>() {
base::Atomic32 old_value = *cell_;
*cell_ = old_value | mask_;
return (old_value & mask_) == 0;
}
template <>
inline bool MarkBit::Set<MarkBit::ATOMIC>() {
base::Atomic32 old_value;
base::Atomic32 new_value;
do {
old_value = base::NoBarrier_Load(cell_);
if (old_value & mask_) return false;
new_value = old_value | mask_;
} while (base::Release_CompareAndSwap(cell_, old_value, new_value) !=
old_value);
return true;
}
template <>
inline bool MarkBit::Get<MarkBit::NON_ATOMIC>() {
return (base::NoBarrier_Load(cell_) & mask_) != 0;
}
template <>
inline bool MarkBit::Get<MarkBit::ATOMIC>() {
return (base::Acquire_Load(cell_) & mask_) != 0;
}
template <>
inline bool MarkBit::Clear<MarkBit::NON_ATOMIC>() {
base::Atomic32 old_value = *cell_;
*cell_ = old_value & ~mask_;
return (old_value & mask_) == mask_;
}
template <>
inline bool MarkBit::Clear<MarkBit::ATOMIC>() {
base::Atomic32 old_value;
base::Atomic32 new_value;
do {
old_value = base::NoBarrier_Load(cell_);
if (!(old_value & mask_)) return false;
new_value = old_value & ~mask_;
} while (base::Release_CompareAndSwap(cell_, old_value, new_value) !=
old_value);
return true;
}
// Bitmap is a sequence of cells each containing fixed number of bits.
class Bitmap {
public:
static const uint32_t kBitsPerCell = 32;
static const uint32_t kBitsPerCellLog2 = 5;
static const uint32_t kBitIndexMask = kBitsPerCell - 1;
static const uint32_t kBytesPerCell = kBitsPerCell / kBitsPerByte;
static const uint32_t kBytesPerCellLog2 = kBitsPerCellLog2 - kBitsPerByteLog2;
static const size_t kLength = (1 << kPageSizeBits) >> (kPointerSizeLog2);
static const size_t kSize = (1 << kPageSizeBits) >>
(kPointerSizeLog2 + kBitsPerByteLog2);
static int CellsForLength(int length) {
return (length + kBitsPerCell - 1) >> kBitsPerCellLog2;
}
int CellsCount() { return CellsForLength(kLength); }
static int SizeFor(int cells_count) {
return sizeof(MarkBit::CellType) * cells_count;
}
INLINE(static uint32_t IndexToCell(uint32_t index)) {
return index >> kBitsPerCellLog2;
}
V8_INLINE static uint32_t IndexInCell(uint32_t index) {
return index & kBitIndexMask;
}
INLINE(static uint32_t CellToIndex(uint32_t index)) {
return index << kBitsPerCellLog2;
}
INLINE(static uint32_t CellAlignIndex(uint32_t index)) {
return (index + kBitIndexMask) & ~kBitIndexMask;
}
INLINE(MarkBit::CellType* cells()) {
return reinterpret_cast<MarkBit::CellType*>(this);
}
INLINE(Address address()) { return reinterpret_cast<Address>(this); }
INLINE(static Bitmap* FromAddress(Address addr)) {
return reinterpret_cast<Bitmap*>(addr);
}
inline MarkBit MarkBitFromIndex(uint32_t index) {
MarkBit::CellType mask = 1u << IndexInCell(index);
MarkBit::CellType* cell = this->cells() + (index >> kBitsPerCellLog2);
return MarkBit(reinterpret_cast<base::Atomic32*>(cell), mask);
}
void Clear() {
for (int i = 0; i < CellsCount(); i++) cells()[i] = 0;
}
// Sets all bits in the range [start_index, end_index).
void SetRange(uint32_t start_index, uint32_t end_index) {
unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
if (start_cell_index != end_cell_index) {
// Firstly, fill all bits from the start address to the end of the first
// cell with 1s.
cells()[start_cell_index] |= ~(start_index_mask - 1);
// Then fill all in between cells with 1s.
for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
cells()[i] = ~0u;
}
// Finally, fill all bits until the end address in the last cell with 1s.
cells()[end_cell_index] |= (end_index_mask - 1);
} else {
cells()[start_cell_index] |= end_index_mask - start_index_mask;
}
}
// Clears all bits in the range [start_index, end_index).
void ClearRange(uint32_t start_index, uint32_t end_index) {
unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
if (start_cell_index != end_cell_index) {
// Firstly, fill all bits from the start address to the end of the first
// cell with 0s.
cells()[start_cell_index] &= (start_index_mask - 1);
// Then fill all in between cells with 0s.
for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
cells()[i] = 0;
}
// Finally, set all bits until the end address in the last cell with 0s.
cells()[end_cell_index] &= ~(end_index_mask - 1);
} else {
cells()[start_cell_index] &= ~(end_index_mask - start_index_mask);
}
}
// Returns true if all bits in the range [start_index, end_index) are set.
bool AllBitsSetInRange(uint32_t start_index, uint32_t end_index) {
unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
MarkBit::CellType matching_mask;
if (start_cell_index != end_cell_index) {
matching_mask = ~(start_index_mask - 1);
if ((cells()[start_cell_index] & matching_mask) != matching_mask) {
return false;
}
for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
if (cells()[i] != ~0u) return false;
}
matching_mask = (end_index_mask - 1);
// Check against a mask of 0 to avoid dereferencing the cell after the
// end of the bitmap.
return (matching_mask == 0) ||
((cells()[end_cell_index] & matching_mask) == matching_mask);
} else {
matching_mask = end_index_mask - start_index_mask;
// Check against a mask of 0 to avoid dereferencing the cell after the
// end of the bitmap.
return (matching_mask == 0) ||
(cells()[end_cell_index] & matching_mask) == matching_mask;
}
}
// Returns true if all bits in the range [start_index, end_index) are cleared.
bool AllBitsClearInRange(uint32_t start_index, uint32_t end_index) {
unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
MarkBit::CellType matching_mask;
if (start_cell_index != end_cell_index) {
matching_mask = ~(start_index_mask - 1);
if ((cells()[start_cell_index] & matching_mask)) return false;
for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
if (cells()[i]) return false;
}
matching_mask = (end_index_mask - 1);
// Check against a mask of 0 to avoid dereferencing the cell after the
// end of the bitmap.
return (matching_mask == 0) || !(cells()[end_cell_index] & matching_mask);
} else {
matching_mask = end_index_mask - start_index_mask;
// Check against a mask of 0 to avoid dereferencing the cell after the
// end of the bitmap.
return (matching_mask == 0) || !(cells()[end_cell_index] & matching_mask);
}
}
static void PrintWord(uint32_t word, uint32_t himask = 0) {
for (uint32_t mask = 1; mask != 0; mask <<= 1) {
if ((mask & himask) != 0) PrintF("[");
PrintF((mask & word) ? "1" : "0");
if ((mask & himask) != 0) PrintF("]");
}
}
class CellPrinter {
public:
CellPrinter() : seq_start(0), seq_type(0), seq_length(0) {}
void Print(uint32_t pos, uint32_t cell) {
if (cell == seq_type) {
seq_length++;
return;
}
Flush();
if (IsSeq(cell)) {
seq_start = pos;
seq_length = 0;
seq_type = cell;
return;
}
PrintF("%d: ", pos);
PrintWord(cell);
PrintF("\n");
}
void Flush() {
if (seq_length > 0) {
PrintF("%d: %dx%d\n", seq_start, seq_type == 0 ? 0 : 1,
seq_length * kBitsPerCell);
seq_length = 0;
}
}
static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; }
private:
uint32_t seq_start;
uint32_t seq_type;
uint32_t seq_length;
};
void Print() {
CellPrinter printer;
for (int i = 0; i < CellsCount(); i++) {
printer.Print(i, cells()[i]);
}
printer.Flush();
PrintF("\n");
}
bool IsClean() {
for (int i = 0; i < CellsCount(); i++) {
if (cells()[i] != 0) {
return false;
}
}
return true;
}
};
class Marking : public AllStatic {
public:
// TODO(hpayer): The current mark bit operations use as default NON_ATOMIC
// mode for access. We should remove the default value or switch it with
// ATOMIC as soon we add concurrency.
// Impossible markbits: 01
static const char* kImpossibleBitPattern;
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool IsImpossible(MarkBit mark_bit)) {
if (mode == MarkBit::NON_ATOMIC) {
return !mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
}
// If we are in concurrent mode we can only tell if an object has the
// impossible bit pattern if we read the first bit again after reading
// the first and the second bit. If the first bit is till zero and the
// second bit is one then the object has the impossible bit pattern.
bool is_impossible = !mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
if (is_impossible) {
return !mark_bit.Get<mode>();
}
return false;
}
// Black markbits: 11
static const char* kBlackBitPattern;
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool IsBlack(MarkBit mark_bit)) {
return mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
}
// White markbits: 00 - this is required by the mark bit clearer.
static const char* kWhiteBitPattern;
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool IsWhite(MarkBit mark_bit)) {
DCHECK(!IsImpossible(mark_bit));
return !mark_bit.Get<mode>();
}
// Grey markbits: 10
static const char* kGreyBitPattern;
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool IsGrey(MarkBit mark_bit)) {
return mark_bit.Get<mode>() && !mark_bit.Next().Get<mode>();
}
// IsBlackOrGrey assumes that the first bit is set for black or grey
// objects.
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool IsBlackOrGrey(MarkBit mark_bit)) {
return mark_bit.Get<mode>();
}
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static void MarkWhite(MarkBit markbit)) {
STATIC_ASSERT(mode == MarkBit::NON_ATOMIC);
markbit.Clear<mode>();
markbit.Next().Clear<mode>();
}
// Warning: this method is not safe in general in concurrent scenarios.
// If you know that nobody else will change the bits on the given location
// then you may use it.
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static void MarkBlack(MarkBit markbit)) {
markbit.Set<mode>();
markbit.Next().Set<mode>();
}
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool BlackToGrey(MarkBit markbit)) {
STATIC_ASSERT(mode == MarkBit::NON_ATOMIC);
DCHECK(IsBlack(markbit));
return markbit.Next().Clear<mode>();
}
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool WhiteToGrey(MarkBit markbit)) {
return markbit.Set<mode>();
}
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool WhiteToBlack(MarkBit markbit)) {
return markbit.Set<mode>() && markbit.Next().Set<mode>();
}
template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
INLINE(static bool GreyToBlack(MarkBit markbit)) {
return markbit.Get<mode>() && markbit.Next().Set<mode>();
}
enum ObjectColor {
BLACK_OBJECT,
WHITE_OBJECT,
GREY_OBJECT,
IMPOSSIBLE_COLOR
};
static const char* ColorName(ObjectColor color) {
switch (color) {
case BLACK_OBJECT:
return "black";
case WHITE_OBJECT:
return "white";
case GREY_OBJECT:
return "grey";
case IMPOSSIBLE_COLOR:
return "impossible";
}
return "error";
}
static ObjectColor Color(MarkBit mark_bit) {
if (IsBlack(mark_bit)) return BLACK_OBJECT;
if (IsWhite(mark_bit)) return WHITE_OBJECT;
if (IsGrey(mark_bit)) return GREY_OBJECT;
UNREACHABLE();
return IMPOSSIBLE_COLOR;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Marking);
};
} // namespace internal
} // namespace v8
#endif // V8_MARKING_H_
|