marking.cc 3.88 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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 {

10 11
const size_t Bitmap::kSize = Bitmap::CellsCount() * Bitmap::kBytesPerCell;

12 13 14
template <>
bool ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsSetInRange(
    uint32_t start_index, uint32_t end_index) {
15 16 17
  if (start_index >= end_index) return false;
  end_index--;

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  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;
    }
33 34
    matching_mask = end_index_mask | (end_index_mask - 1);
    return ((cells()[end_cell_index] & matching_mask) == matching_mask);
35
  } else {
36 37
    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
    return (cells()[end_cell_index] & matching_mask) == matching_mask;
38 39 40
  }
}

41 42 43
template <>
bool ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsClearInRange(
    uint32_t start_index, uint32_t end_index) {
44 45 46
  if (start_index >= end_index) return true;
  end_index--;

47 48 49 50 51 52 53 54 55 56 57 58 59
  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;
    }
60 61
    matching_mask = end_index_mask | (end_index_mask - 1);
    return !(cells()[end_cell_index] & matching_mask);
62
  } else {
63 64
    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
    return !(cells()[end_cell_index] & matching_mask);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }
}

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) {}

82
  void Print(size_t pos, uint32_t cell) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if (cell == seq_type) {
      seq_length++;
      return;
    }

    Flush();

    if (IsSeq(cell)) {
      seq_start = pos;
      seq_length = 0;
      seq_type = cell;
      return;
    }

97
    PrintF("%zu: ", pos);
98 99 100 101 102 103
    PrintWord(cell);
    PrintF("\n");
  }

  void Flush() {
    if (seq_length > 0) {
104
      PrintF("%zu: %dx%zu\n", seq_start, seq_type == 0 ? 0 : 1,
105 106 107 108 109 110 111 112
             seq_length * Bitmap::kBitsPerCell);
      seq_length = 0;
    }
  }

  static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; }

 private:
113
  size_t seq_start;
114
  uint32_t seq_type;
115
  size_t seq_length;
116 117 118 119
};

}  // anonymous namespace

120 121
template <>
void ConcurrentBitmap<AccessMode::NON_ATOMIC>::Print() {
122
  CellPrinter printer;
123
  for (size_t i = 0; i < CellsCount(); i++) {
124 125 126 127 128 129
    printer.Print(i, cells()[i]);
  }
  printer.Flush();
  PrintF("\n");
}

130 131
template <>
bool ConcurrentBitmap<AccessMode::NON_ATOMIC>::IsClean() {
132
  for (size_t i = 0; i < CellsCount(); i++) {
133 134 135 136 137 138 139 140 141
    if (cells()[i] != 0) {
      return false;
    }
  }
  return true;
}

}  // namespace internal
}  // namespace v8