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 
30 #include "gc/g1/heapRegion.hpp"
31 
region_idx_for(CardValue * p)32 inline uint G1CardTable::region_idx_for(CardValue* p) {
33   size_t const card_idx = pointer_delta(p, _byte_map, sizeof(CardValue));
34   return (uint)(card_idx >> (HeapRegion::LogOfHRGrainBytes - card_shift));
35 }
36 
mark_clean_as_dirty(CardValue * card)37 inline bool G1CardTable::mark_clean_as_dirty(CardValue* card) {
38   CardValue value = *card;
39   if (value == clean_card_val()) {
40     *card = dirty_card_val();
41     return true;
42   }
43   return false;
44 }
45 
mark_region_dirty(size_t start_card_index,size_t num_cards)46 inline size_t G1CardTable::mark_region_dirty(size_t start_card_index, size_t num_cards) {
47   assert(is_aligned(start_card_index, sizeof(size_t)), "Start card index must be aligned.");
48   assert(is_aligned(num_cards, sizeof(size_t)), "Number of cards to change must be evenly divisible.");
49 
50   size_t result = 0;
51 
52   size_t const num_chunks = num_cards / sizeof(size_t);
53 
54   size_t* cur_word = (size_t*)&_byte_map[start_card_index];
55   size_t* const end_word_map = cur_word + num_chunks;
56   while (cur_word < end_word_map) {
57     size_t value = *cur_word;
58     if (value == WordAllClean) {
59       *cur_word = WordAllDirty;
60       result += sizeof(value);
61     } else if (value == WordAllDirty) {
62       // do nothing.
63     } else {
64       // There is a mix of cards in there. Tread slowly.
65       CardValue* cur = (CardValue*)cur_word;
66       for (size_t i = 0; i < sizeof(size_t); i++) {
67         CardValue value = *cur;
68         if (value == clean_card_val()) {
69           *cur = dirty_card_val();
70           result++;
71         }
72         cur++;
73       }
74     }
75     cur_word++;
76   }
77 
78   return result;
79 }
80 
change_dirty_cards_to(size_t start_card_index,size_t num_cards,CardValue which)81 inline void G1CardTable::change_dirty_cards_to(size_t start_card_index, size_t num_cards, CardValue which) {
82   CardValue* start = &_byte_map[start_card_index];
83   CardValue* const end = start + num_cards;
84   while (start < end) {
85     CardValue value = *start;
86     assert(value == dirty_card_val(),
87            "Must have been dirty %d start " PTR_FORMAT " " PTR_FORMAT, value, p2i(start), p2i(end));
88     *start++ = which;
89   }
90 }
91 
92 #endif /* SHARE_GC_G1_G1CARDTABLE_INLINE_HPP */
93