1 /*
 2  * Copyright (c) 2018, 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_VM_GC_G1_HEAPREGIONREMSET_INLINE_HPP
26 #define SHARE_VM_GC_G1_HEAPREGIONREMSET_INLINE_HPP
27 
28 #include "gc/g1/heapRegion.inline.hpp"
29 #include "gc/g1/heapRegionRemSet.hpp"
30 #include "gc/g1/sparsePRT.hpp"
31 #include "runtime/atomic.hpp"
32 #include "utilities/bitMap.inline.hpp"
33 
34 template <class Closure>
35 inline void HeapRegionRemSet::iterate_prts(Closure& cl) {
36   _other_regions.iterate(cl);
37 }
38 
39 inline bool PerRegionTable::add_card(CardIdx_t from_card_index) {
40   if (_bm.par_set_bit(from_card_index)) {
41     Atomic::inc(&_occupied, memory_order_relaxed);
42     return true;
43   }
44   return false;
45 }
46 
47 inline bool PerRegionTable::add_reference(OopOrNarrowOopStar from) {
48   // Must make this robust in case "from" is not in "_hr", because of
49   // concurrency.
50 
51   HeapRegion* loc_hr = hr();
52   // If the test below fails, then this table was reused concurrently
53   // with this operation.  This is OK, since the old table was coarsened,
54   // and adding a bit to the new table is never incorrect.
55   if (loc_hr->is_in_reserved(from)) {
56     CardIdx_t from_card = OtherRegionsTable::card_within_region(from, loc_hr);
57     return add_card(from_card);
58   }
59   return false;
60 }
61 
62 inline void PerRegionTable::init(HeapRegion* hr, bool clear_links_to_all_list) {
63   if (clear_links_to_all_list) {
64     set_next(NULL);
65   }
66   _collision_list_next = NULL;
67   _occupied = 0;
68   _bm.clear();
69   // Make sure that the bitmap clearing above has been finished before publishing
70   // this PRT to concurrent threads.
71   Atomic::release_store(&_hr, hr);
72 }
73 
74 template <class Closure>
75 void OtherRegionsTable::iterate(Closure& cl) {
76   if (_n_coarse_entries > 0) {
77     BitMap::idx_t cur = _coarse_map.get_next_one_offset(0);
78     while (cur != _coarse_map.size()) {
79       cl.next_coarse_prt((uint)cur);
80       cur = _coarse_map.get_next_one_offset(cur + 1);
81     }
82   }
83   {
84     PerRegionTable* cur = _first_all_fine_prts;
85     while (cur != NULL) {
86       cl.next_fine_prt(cur->hr()->hrm_index(), cur->bm());
87       cur = cur->next();
88     }
89   }
90   {
91     SparsePRTBucketIter iter(&_sparse_table);
92     SparsePRTEntry* cur;
93     while (iter.has_next(cur)) {
94       cl.next_sparse_prt(cur->r_ind(), cur->cards(), cur->num_valid_cards());
95     }
96   }
97 }
98 
99 #endif // SHARE_VM_GC_G1_HEAPREGIONREMSET_INLINE_HPP