1 /*
   2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_GC_G1_G1CARDTABLE_INLINE_HPP
  26 #define SHARE_GC_G1_G1CARDTABLE_INLINE_HPP
  27 
  28 #include "gc/g1/g1CardTable.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 
  31 inline uint G1CardTable::region_idx_for(CardValue* p) {
  32   size_t const card_idx = pointer_delta(p, _byte_map, sizeof(CardValue));
  33   return (uint)(card_idx >> (HeapRegion::LogOfHRGrainBytes - card_shift));
  34 }
  35 
  36 inline size_t G1CardTable::mark_clean_as_dirty(size_t card_index) {
  37   CardValue value = _byte_map[card_index];
  38   if (value == clean_card_val()) {
  39     _byte_map[card_index] = dirty_card_val();
  40     return 1;
  41   }
  42   return 0;
  43 }
  44 
  45 inline size_t G1CardTable::mark_region_dirty(size_t start_card_index, size_t num_cards) {
  46   assert(is_aligned(start_card_index, sizeof(size_t)), "Start card index must be aligned.");
  47   assert(is_aligned(num_cards, sizeof(size_t)), "Number of cards to change must be evenly divisible.");
  48 
  49   size_t result = 0;
  50 
  51   size_t const num_chunks = num_cards / sizeof(size_t);
  52 
  53   size_t* cur_word = (size_t*)&_byte_map[start_card_index];
  54   size_t* const end_word_map = cur_word + num_chunks;
  55   while (cur_word < end_word_map) {
  56     size_t value = *cur_word;
  57     if (value == WordAllClean) {
  58       *cur_word = WordAllDirty;
  59       result += sizeof(value);
  60     } else if (value == WordAllDirty) {
  61       // do nothing.
  62     } else {
  63       // There is a mix of cards in there. Tread slowly.
  64       CardValue* cur = (CardValue*)cur_word;
  65       for (size_t i = 0; i < sizeof(size_t); i++) {
  66         CardValue value = *cur;
  67         if (value == clean_card_val()) {
  68           *cur = dirty_card_val();
  69           result++;
  70         }
  71         cur++;
  72       }
  73     }
  74     cur_word++;
  75   }
  76 
  77   return result;
  78 }
  79 
  80 inline void G1CardTable::mark_as_scanned(size_t start_card_index, size_t num_cards) {
  81   CardValue* start = &_byte_map[start_card_index];
  82   CardValue* const end = start + num_cards;
  83   while (start < end) {
  84     CardValue value = *start;
  85     assert(value == dirty_card_val(),
  86            "Must have been dirty %d start " PTR_FORMAT " " PTR_FORMAT, value, p2i(start), p2i(end));
  87     *start++ = g1_card_already_scanned;
  88   }
  89 }
  90 
  91 #endif /* SHARE_GC_G1_G1CARDTABLE_INLINE_HPP */