xref: /third_party/node/deps/v8/src/heap/marking.cc (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/heap/marking.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cinamespace v8 {
81cb0ef41Sopenharmony_cinamespace internal {
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst size_t Bitmap::kSize = Bitmap::CellsCount() * Bitmap::kBytesPerCell;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_citemplate <>
131cb0ef41Sopenharmony_cibool ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsSetInRange(
141cb0ef41Sopenharmony_ci    uint32_t start_index, uint32_t end_index) {
151cb0ef41Sopenharmony_ci  if (start_index >= end_index) return false;
161cb0ef41Sopenharmony_ci  end_index--;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
191cb0ef41Sopenharmony_ci  MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
221cb0ef41Sopenharmony_ci  MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  MarkBit::CellType matching_mask;
251cb0ef41Sopenharmony_ci  if (start_cell_index != end_cell_index) {
261cb0ef41Sopenharmony_ci    matching_mask = ~(start_index_mask - 1);
271cb0ef41Sopenharmony_ci    if ((cells()[start_cell_index] & matching_mask) != matching_mask) {
281cb0ef41Sopenharmony_ci      return false;
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci    for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
311cb0ef41Sopenharmony_ci      if (cells()[i] != ~0u) return false;
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci    matching_mask = end_index_mask | (end_index_mask - 1);
341cb0ef41Sopenharmony_ci    return ((cells()[end_cell_index] & matching_mask) == matching_mask);
351cb0ef41Sopenharmony_ci  } else {
361cb0ef41Sopenharmony_ci    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
371cb0ef41Sopenharmony_ci    return (cells()[end_cell_index] & matching_mask) == matching_mask;
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_citemplate <>
421cb0ef41Sopenharmony_cibool ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsClearInRange(
431cb0ef41Sopenharmony_ci    uint32_t start_index, uint32_t end_index) {
441cb0ef41Sopenharmony_ci  if (start_index >= end_index) return true;
451cb0ef41Sopenharmony_ci  end_index--;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
481cb0ef41Sopenharmony_ci  MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
511cb0ef41Sopenharmony_ci  MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  MarkBit::CellType matching_mask;
541cb0ef41Sopenharmony_ci  if (start_cell_index != end_cell_index) {
551cb0ef41Sopenharmony_ci    matching_mask = ~(start_index_mask - 1);
561cb0ef41Sopenharmony_ci    if ((cells()[start_cell_index] & matching_mask)) return false;
571cb0ef41Sopenharmony_ci    for (unsigned int i = start_cell_index + 1; i < end_cell_index; i++) {
581cb0ef41Sopenharmony_ci      if (cells()[i]) return false;
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci    matching_mask = end_index_mask | (end_index_mask - 1);
611cb0ef41Sopenharmony_ci    return !(cells()[end_cell_index] & matching_mask);
621cb0ef41Sopenharmony_ci  } else {
631cb0ef41Sopenharmony_ci    matching_mask = end_index_mask | (end_index_mask - start_index_mask);
641cb0ef41Sopenharmony_ci    return !(cells()[end_cell_index] & matching_mask);
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cinamespace {
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_civoid PrintWord(uint32_t word, uint32_t himask = 0) {
711cb0ef41Sopenharmony_ci  for (uint32_t mask = 1; mask != 0; mask <<= 1) {
721cb0ef41Sopenharmony_ci    if ((mask & himask) != 0) PrintF("[");
731cb0ef41Sopenharmony_ci    PrintF((mask & word) ? "1" : "0");
741cb0ef41Sopenharmony_ci    if ((mask & himask) != 0) PrintF("]");
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciclass CellPrinter {
791cb0ef41Sopenharmony_ci public:
801cb0ef41Sopenharmony_ci  CellPrinter() : seq_start(0), seq_type(0), seq_length(0) {}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  void Print(size_t pos, uint32_t cell) {
831cb0ef41Sopenharmony_ci    if (cell == seq_type) {
841cb0ef41Sopenharmony_ci      seq_length++;
851cb0ef41Sopenharmony_ci      return;
861cb0ef41Sopenharmony_ci    }
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci    Flush();
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    if (IsSeq(cell)) {
911cb0ef41Sopenharmony_ci      seq_start = pos;
921cb0ef41Sopenharmony_ci      seq_length = 0;
931cb0ef41Sopenharmony_ci      seq_type = cell;
941cb0ef41Sopenharmony_ci      return;
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci    PrintF("%zu: ", pos);
981cb0ef41Sopenharmony_ci    PrintWord(cell);
991cb0ef41Sopenharmony_ci    PrintF("\n");
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  void Flush() {
1031cb0ef41Sopenharmony_ci    if (seq_length > 0) {
1041cb0ef41Sopenharmony_ci      PrintF("%zu: %dx%zu\n", seq_start, seq_type == 0 ? 0 : 1,
1051cb0ef41Sopenharmony_ci             seq_length * Bitmap::kBitsPerCell);
1061cb0ef41Sopenharmony_ci      seq_length = 0;
1071cb0ef41Sopenharmony_ci    }
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  static bool IsSeq(uint32_t cell) { return cell == 0 || cell == 0xFFFFFFFF; }
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci private:
1131cb0ef41Sopenharmony_ci  size_t seq_start;
1141cb0ef41Sopenharmony_ci  uint32_t seq_type;
1151cb0ef41Sopenharmony_ci  size_t seq_length;
1161cb0ef41Sopenharmony_ci};
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci}  // anonymous namespace
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_citemplate <>
1211cb0ef41Sopenharmony_civoid ConcurrentBitmap<AccessMode::NON_ATOMIC>::Print() {
1221cb0ef41Sopenharmony_ci  CellPrinter printer;
1231cb0ef41Sopenharmony_ci  for (size_t i = 0; i < CellsCount(); i++) {
1241cb0ef41Sopenharmony_ci    printer.Print(i, cells()[i]);
1251cb0ef41Sopenharmony_ci  }
1261cb0ef41Sopenharmony_ci  printer.Flush();
1271cb0ef41Sopenharmony_ci  PrintF("\n");
1281cb0ef41Sopenharmony_ci}
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_citemplate <>
1311cb0ef41Sopenharmony_cibool ConcurrentBitmap<AccessMode::NON_ATOMIC>::IsClean() {
1321cb0ef41Sopenharmony_ci  for (size_t i = 0; i < CellsCount(); i++) {
1331cb0ef41Sopenharmony_ci    if (cells()[i] != 0) {
1341cb0ef41Sopenharmony_ci      return false;
1351cb0ef41Sopenharmony_ci    }
1361cb0ef41Sopenharmony_ci  }
1371cb0ef41Sopenharmony_ci  return true;
1381cb0ef41Sopenharmony_ci}
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci}  // namespace internal
1411cb0ef41Sopenharmony_ci}  // namespace v8
142