1 /*
2 * Copyright (c) 2000, 2025, 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_SHARED_CARDTABLEBARRIERSET_INLINE_HPP
26 #define SHARE_GC_SHARED_CARDTABLEBARRIERSET_INLINE_HPP
27
28 #include "gc/shared/cardTableBarrierSet.hpp"
29
30 #include "gc/shared/barrierSet.hpp"
31 #include "gc/shared/cardTable.hpp"
32 #include "oops/compressedOops.inline.hpp"
33 #include "oops/objArrayOop.hpp"
34 #include "oops/oop.hpp"
35
36 template <DecoratorSet decorators, typename T>
37 inline void CardTableBarrierSet::write_ref_field_post(T* field) {
38 volatile CardValue* byte = _card_table->byte_for(field);
39 *byte = CardTable::dirty_card_val();
40 }
41
42 class Klass;
43
44 // count is number of array elements being written
45 void CardTableBarrierSet::write_ref_array(HeapWord* start, size_t count) {
46 HeapWord* end = (HeapWord*)((char*)start + (count*heapOopSize));
47 // In the case of compressed oops, start and end may potentially be misaligned;
48 // so we need to conservatively align the first downward (this is not
49 // strictly necessary for current uses, but a case of good hygiene and,
50 // if you will, aesthetics) and the second upward (this is essential for
51 // current uses) to a HeapWord boundary, so we mark all cards overlapping
52 // this write. If this evolves in the future to calling a
53 // logging barrier of narrow oop granularity, like the pre-barrier for G1
54 // (mentioned here merely by way of example), we will need to change this
55 // interface, so it is "exactly precise" (if i may be allowed the adverbial
56 // redundancy for emphasis) and does not include narrow oop slots not
57 // included in the original write interval.
58 HeapWord* aligned_start = align_down(start, HeapWordSize);
59 HeapWord* aligned_end = align_up (end, HeapWordSize);
60 // If compressed oops were not being used, these should already be aligned
61 assert(UseCompressedOops || (aligned_start == start && aligned_end == end),
62 "Expected heap word alignment of start and end");
63 write_region(MemRegion(aligned_start, aligned_end));
64 }
65
66 template <DecoratorSet decorators, typename BarrierSetT>
67 template <typename T>
68 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
69 oop_store_in_heap(T* addr, oop value) {
70 BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
71 bs->template write_ref_field_pre<decorators>(addr);
72 Raw::oop_store(addr, value);
73 bs->template write_ref_field_post<decorators>(addr);
74 }
75
76 template <DecoratorSet decorators, typename BarrierSetT>
77 template <typename T>
78 inline oop CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
79 oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
80 BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
81 bs->template write_ref_field_pre<decorators>(addr);
82 oop result = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
83 if (result == compare_value) {
84 bs->template write_ref_field_post<decorators>(addr);
85 }
86 return result;
87 }
88
89 template <DecoratorSet decorators, typename BarrierSetT>
90 template <typename T>
91 inline oop CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
92 oop_atomic_xchg_in_heap(T* addr, oop new_value) {
93 BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
94 bs->template write_ref_field_pre<decorators>(addr);
95 oop result = Raw::oop_atomic_xchg(addr, new_value);
96 bs->template write_ref_field_post<decorators>(addr);
97 return result;
98 }
99
100 template <DecoratorSet decorators, typename BarrierSetT>
101 template <typename T>
102 inline bool CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
103 oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
104 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
105 size_t length) {
106 BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
107
108 src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
109 dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
110
111 if (!HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value) {
112 // Optimized covariant case
113 bs->write_ref_array_pre(dst_raw, length,
114 HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value);
115 Raw::oop_arraycopy(nullptr, 0, src_raw, nullptr, 0, dst_raw, length);
116 bs->write_ref_array((HeapWord*)dst_raw, length);
117 } else {
118 assert(dst_obj != nullptr, "better have an actual oop");
119 Klass* bound = objArrayOop(dst_obj)->element_klass();
120 T* from = const_cast<T*>(src_raw);
121 T* end = from + length;
122 for (T* p = dst_raw; from < end; from++, p++) {
123 T element = *from;
124 if (oopDesc::is_instanceof_or_null(CompressedOops::decode(element), bound)) {
125 bs->template write_ref_field_pre<decorators>(p);
126 *p = element;
127 } else {
128 // We must do a barrier to cover the partial copy.
129 const size_t pd = pointer_delta(p, dst_raw, (size_t)heapOopSize);
130 // pointer delta is scaled to number of elements (length field in
131 // objArrayOop) which we assume is 32 bit.
132 assert(pd == (size_t)(int)pd, "length field overflow");
133 bs->write_ref_array((HeapWord*)dst_raw, pd);
134 return false;
135 }
136 }
137 bs->write_ref_array((HeapWord*)dst_raw, length);
138 }
139 return true;
140 }
141
142 template <DecoratorSet decorators, typename BarrierSetT>
143 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
144 clone_in_heap(oop src, oop dst, size_t size) {
145 Raw::clone(src, dst, size);
146 BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
147 bs->write_region(MemRegion((HeapWord*)(void*)dst, size));
148 }
149
150 #endif // SHARE_GC_SHARED_CARDTABLEBARRIERSET_INLINE_HPP