marking.cc 7.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2017 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.

#include "src/heap/marking.h"

namespace v8 {
namespace internal {

void Bitmap::Clear() {
  base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
  for (int i = 0; i < CellsCount(); i++) {
    base::Relaxed_Store(cell_base + i, 0);
  }
  // This fence prevents re-ordering of publishing stores with the mark-bit
  // clearing stores.
17
  base::SeqCst_MemoryFence();
18 19
}

20 21 22 23 24 25 26 27 28 29
void Bitmap::MarkAllBits() {
  base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
  for (int i = 0; i < CellsCount(); i++) {
    base::Relaxed_Store(cell_base + i, 0xffffffff);
  }
  // This fence prevents re-ordering of publishing stores with the mark-bit
  // clearing stores.
  base::SeqCst_MemoryFence();
}

30
void Bitmap::SetRange(uint32_t start_index, uint32_t end_index) {
31 32 33
  if (start_index >= end_index) return;
  end_index--;

34 35 36 37 38 39 40
  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.
41 42
    SetBitsInCell<AccessMode::ATOMIC>(start_cell_index,
                                      ~(start_index_mask - 1));
43 44 45 46 47 48
    // Then fill all in between cells with 1s.
    base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
    for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
      base::Relaxed_Store(cell_base + i, ~0u);
    }
    // Finally, fill all bits until the end address in the last cell with 1s.
49 50
    SetBitsInCell<AccessMode::ATOMIC>(end_cell_index,
                                      end_index_mask | (end_index_mask - 1));
51
  } else {
52 53
    SetBitsInCell<AccessMode::ATOMIC>(
        start_cell_index, end_index_mask | (end_index_mask - start_index_mask));
54 55 56
  }
  // This fence prevents re-ordering of publishing stores with the mark-
  // bit setting stores.
57
  base::SeqCst_MemoryFence();
58 59 60
}

void Bitmap::ClearRange(uint32_t start_index, uint32_t end_index) {
61 62 63
  if (start_index >= end_index) return;
  end_index--;

64 65 66 67 68 69 70 71 72
  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.
73 74
    ClearBitsInCell<AccessMode::ATOMIC>(start_cell_index,
                                        ~(start_index_mask - 1));
75 76 77 78 79 80
    // Then fill all in between cells with 0s.
    base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
    for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
      base::Relaxed_Store(cell_base + i, 0);
    }
    // Finally, set all bits until the end address in the last cell with 0s.
81 82
    ClearBitsInCell<AccessMode::ATOMIC>(end_cell_index,
                                        end_index_mask | (end_index_mask - 1));
83
  } else {
84 85
    ClearBitsInCell<AccessMode::ATOMIC>(
        start_cell_index, end_index_mask | (end_index_mask - start_index_mask));
86 87 88
  }
  // This fence prevents re-ordering of publishing stores with the mark-
  // bit clearing stores.
89
  base::SeqCst_MemoryFence();
90 91 92
}

bool Bitmap::AllBitsSetInRange(uint32_t start_index, uint32_t end_index) {
93 94 95
  if (start_index >= end_index) return false;
  end_index--;

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  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;
    }
111 112
    matching_mask = end_index_mask | (end_index_mask - 1);
    return ((cells()[end_cell_index] & matching_mask) == matching_mask);
113
  } else {
114 115
    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
    return (cells()[end_cell_index] & matching_mask) == matching_mask;
116 117 118 119
  }
}

bool Bitmap::AllBitsClearInRange(uint32_t start_index, uint32_t end_index) {
120 121 122
  if (start_index >= end_index) return true;
  end_index--;

123 124 125 126 127 128 129 130 131 132 133 134 135
  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;
    }
136 137
    matching_mask = end_index_mask | (end_index_mask - 1);
    return !(cells()[end_cell_index] & matching_mask);
138
  } else {
139 140
    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
    return !(cells()[end_cell_index] & matching_mask);
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
  }
}

namespace {

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 * Bitmap::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;
};

}  // anonymous namespace

void Bitmap::Print() {
  CellPrinter printer;
  for (int i = 0; i < CellsCount(); i++) {
    printer.Print(i, cells()[i]);
  }
  printer.Flush();
  PrintF("\n");
}

bool Bitmap::IsClean() {
  for (int i = 0; i < CellsCount(); i++) {
    if (cells()[i] != 0) {
      return false;
    }
  }
  return true;
}

}  // namespace internal
}  // namespace v8