summaryrefslogtreecommitdiff
path: root/deps/v8/src/heap/marking.h
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/src/heap/marking.h')
-rw-r--r--deps/v8/src/heap/marking.h318
1 files changed, 87 insertions, 231 deletions
diff --git a/deps/v8/src/heap/marking.h b/deps/v8/src/heap/marking.h
index ab98a124bc..c76302218f 100644
--- a/deps/v8/src/heap/marking.h
+++ b/deps/v8/src/heap/marking.h
@@ -16,11 +16,7 @@ class MarkBit {
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);
- }
+ inline MarkBit(CellType* cell, CellType mask) : cell_(cell), mask_(mask) {}
#ifdef DEBUG
bool operator==(const MarkBit& other) {
@@ -40,19 +36,19 @@ class MarkBit {
// The function returns true if it succeeded to
// transition the bit from 0 to 1.
- template <AccessMode mode = NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
inline bool Set();
- template <AccessMode mode = NON_ATOMIC>
+ template <AccessMode mode = AccessMode::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>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
inline bool Clear();
- base::Atomic32* cell_;
- base::Atomic32 mask_;
+ CellType* cell_;
+ CellType mask_;
friend class IncrementalMarking;
friend class ConcurrentMarkingMarkbits;
@@ -60,57 +56,41 @@ class MarkBit {
};
template <>
-inline bool MarkBit::Set<MarkBit::NON_ATOMIC>() {
- base::Atomic32 old_value = *cell_;
+inline bool MarkBit::Set<AccessMode::NON_ATOMIC>() {
+ CellType 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;
+inline bool MarkBit::Set<AccessMode::ATOMIC>() {
+ return base::AsAtomic32::SetBits(cell_, mask_, mask_);
}
template <>
-inline bool MarkBit::Get<MarkBit::NON_ATOMIC>() {
- return (base::NoBarrier_Load(cell_) & mask_) != 0;
+inline bool MarkBit::Get<AccessMode::NON_ATOMIC>() {
+ return (*cell_ & mask_) != 0;
}
template <>
-inline bool MarkBit::Get<MarkBit::ATOMIC>() {
- return (base::Acquire_Load(cell_) & mask_) != 0;
+inline bool MarkBit::Get<AccessMode::ATOMIC>() {
+ return (base::AsAtomic32::Acquire_Load(cell_) & mask_) != 0;
}
template <>
-inline bool MarkBit::Clear<MarkBit::NON_ATOMIC>() {
- base::Atomic32 old_value = *cell_;
+inline bool MarkBit::Clear<AccessMode::NON_ATOMIC>() {
+ CellType 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;
+inline bool MarkBit::Clear<AccessMode::ATOMIC>() {
+ return base::AsAtomic32::SetBits(cell_, 0u, mask_);
}
// Bitmap is a sequence of cells each containing fixed number of bits.
-class Bitmap {
+class V8_EXPORT_PRIVATE Bitmap {
public:
static const uint32_t kBitsPerCell = 32;
static const uint32_t kBitsPerCellLog2 = 5;
@@ -129,11 +109,7 @@ class Bitmap {
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)) {
+ V8_INLINE static uint32_t IndexToCell(uint32_t index) {
return index >> kBitsPerCellLog2;
}
@@ -141,204 +117,85 @@ class Bitmap {
return index & kBitIndexMask;
}
- INLINE(static uint32_t CellToIndex(uint32_t index)) {
- return index << kBitsPerCellLog2;
+ // Retrieves the cell containing the provided markbit index.
+ V8_INLINE static uint32_t CellAlignIndex(uint32_t index) {
+ return index & ~kBitIndexMask;
}
- INLINE(static uint32_t CellAlignIndex(uint32_t index)) {
- return (index + kBitIndexMask) & ~kBitIndexMask;
+ V8_INLINE static bool IsCellAligned(uint32_t index) {
+ return (index & kBitIndexMask) == 0;
}
- INLINE(MarkBit::CellType* cells()) {
+ V8_INLINE MarkBit::CellType* cells() {
return reinterpret_cast<MarkBit::CellType*>(this);
}
- INLINE(Address address()) { return reinterpret_cast<Address>(this); }
-
- INLINE(static Bitmap* FromAddress(Address addr)) {
+ V8_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;
- }
+ return MarkBit(cell, 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);
- }
- }
+ void Clear();
- // 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);
- }
- }
+ // Clears bits in the given cell. The mask specifies bits to clear: if a
+ // bit is set in the mask then the corresponding bit is cleared in the cell.
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
+ void ClearBitsInCell(uint32_t cell_index, uint32_t 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("]");
- }
- }
+ // Sets bits in the given cell. The mask specifies bits to set: if a
+ // bit is set in the mask then the corresponding bit is set in the cell.
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
+ void SetBitsInCell(uint32_t cell_index, uint32_t mask);
- class CellPrinter {
- public:
- CellPrinter() : seq_start(0), seq_type(0), seq_length(0) {}
+ // Sets all bits in the range [start_index, end_index). The cells at the
+ // boundary of the range are updated with atomic compare and swap operation.
+ // The inner cells are updated with relaxed write.
+ void SetRange(uint32_t start_index, uint32_t end_index);
- void Print(uint32_t pos, uint32_t cell) {
- if (cell == seq_type) {
- seq_length++;
- return;
- }
+ // Clears all bits in the range [start_index, end_index). The cells at the
+ // boundary of the range are updated with atomic compare and swap operation.
+ // The inner cells are updated with relaxed write.
+ void ClearRange(uint32_t start_index, uint32_t end_index);
- Flush();
+ // Returns true if all bits in the range [start_index, end_index) are set.
+ bool AllBitsSetInRange(uint32_t start_index, uint32_t end_index);
- if (IsSeq(cell)) {
- seq_start = pos;
- seq_length = 0;
- seq_type = cell;
- return;
- }
+ // Returns true if all bits in the range [start_index, end_index) are cleared.
+ bool AllBitsClearInRange(uint32_t start_index, uint32_t end_index);
- PrintF("%d: ", pos);
- PrintWord(cell);
- PrintF("\n");
- }
+ void Print();
- void Flush() {
- if (seq_length > 0) {
- PrintF("%d: %dx%d\n", seq_start, seq_type == 0 ? 0 : 1,
- seq_length * kBitsPerCell);
- seq_length = 0;
- }
- }
+ bool IsClean();
+};
- static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; }
+template <>
+inline void Bitmap::SetBitsInCell<AccessMode::NON_ATOMIC>(uint32_t cell_index,
+ uint32_t mask) {
+ cells()[cell_index] |= mask;
+}
- private:
- uint32_t seq_start;
- uint32_t seq_type;
- uint32_t seq_length;
- };
+template <>
+inline void Bitmap::SetBitsInCell<AccessMode::ATOMIC>(uint32_t cell_index,
+ uint32_t mask) {
+ base::AsAtomic32::SetBits(cells() + cell_index, mask, mask);
+}
- void Print() {
- CellPrinter printer;
- for (int i = 0; i < CellsCount(); i++) {
- printer.Print(i, cells()[i]);
- }
- printer.Flush();
- PrintF("\n");
- }
+template <>
+inline void Bitmap::ClearBitsInCell<AccessMode::NON_ATOMIC>(uint32_t cell_index,
+ uint32_t mask) {
+ cells()[cell_index] &= ~mask;
+}
- bool IsClean() {
- for (int i = 0; i < CellsCount(); i++) {
- if (cells()[i] != 0) {
- return false;
- }
- }
- return true;
- }
-};
+template <>
+inline void Bitmap::ClearBitsInCell<AccessMode::ATOMIC>(uint32_t cell_index,
+ uint32_t mask) {
+ base::AsAtomic32::SetBits(cells() + cell_index, 0u, mask);
+}
class Marking : public AllStatic {
public:
@@ -348,9 +205,9 @@ class Marking : public AllStatic {
// Impossible markbits: 01
static const char* kImpossibleBitPattern;
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool IsImpossible(MarkBit mark_bit)) {
- if (mode == MarkBit::NON_ATOMIC) {
+ if (mode == AccessMode::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
@@ -366,36 +223,36 @@ class Marking : public AllStatic {
// Black markbits: 11
static const char* kBlackBitPattern;
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::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>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool IsWhite(MarkBit mark_bit)) {
- DCHECK(!IsImpossible(mark_bit));
+ DCHECK(!IsImpossible<mode>(mark_bit));
return !mark_bit.Get<mode>();
}
// Grey markbits: 10
static const char* kGreyBitPattern;
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::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>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool IsBlackOrGrey(MarkBit mark_bit)) {
return mark_bit.Get<mode>();
}
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static void MarkWhite(MarkBit markbit)) {
- STATIC_ASSERT(mode == MarkBit::NON_ATOMIC);
+ STATIC_ASSERT(mode == AccessMode::NON_ATOMIC);
markbit.Clear<mode>();
markbit.Next().Clear<mode>();
}
@@ -403,30 +260,30 @@ class Marking : public AllStatic {
// 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>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static void MarkBlack(MarkBit markbit)) {
markbit.Set<mode>();
markbit.Next().Set<mode>();
}
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool BlackToGrey(MarkBit markbit)) {
- STATIC_ASSERT(mode == MarkBit::NON_ATOMIC);
+ STATIC_ASSERT(mode == AccessMode::NON_ATOMIC);
DCHECK(IsBlack(markbit));
return markbit.Next().Clear<mode>();
}
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool WhiteToGrey(MarkBit markbit)) {
return markbit.Set<mode>();
}
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool WhiteToBlack(MarkBit markbit)) {
return markbit.Set<mode>() && markbit.Next().Set<mode>();
}
- template <MarkBit::AccessMode mode = MarkBit::NON_ATOMIC>
+ template <AccessMode mode = AccessMode::NON_ATOMIC>
INLINE(static bool GreyToBlack(MarkBit markbit)) {
return markbit.Get<mode>() && markbit.Next().Set<mode>();
}
@@ -457,7 +314,6 @@ class Marking : public AllStatic {
if (IsWhite(mark_bit)) return WHITE_OBJECT;
if (IsGrey(mark_bit)) return GREY_OBJECT;
UNREACHABLE();
- return IMPOSSIBLE_COLOR;
}
private: