1 /*
  2  * Copyright (c) 2000, 2026, 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/inlineKlass.inline.hpp"
 34 #include "oops/layoutKind.hpp"
 35 #include "oops/objArrayOop.hpp"
 36 #include "oops/oop.hpp"
 37 #include "utilities/debug.hpp"
 38 
 39 template <DecoratorSet decorators, typename T>
 40 inline void CardTableBarrierSet::write_ref_field_post(T* field) {
 41   volatile CardValue* byte = card_table()->byte_for(field);
 42   *byte = CardTable::dirty_card_val();
 43 }
 44 
 45 class Klass;
 46 
 47 // count is number of array elements being written
 48 void CardTableBarrierSet::write_ref_array(HeapWord* start, size_t count) {
 49   HeapWord* end = (HeapWord*)((char*)start + (count*heapOopSize));
 50   // In the case of compressed oops, start and end may potentially be misaligned;
 51   // so we need to conservatively align the first downward (this is not
 52   // strictly necessary for current uses, but a case of good hygiene and,
 53   // if you will, aesthetics) and the second upward (this is essential for
 54   // current uses) to a HeapWord boundary, so we mark all cards overlapping
 55   // this write. If this evolves in the future to calling a
 56   // logging barrier of narrow oop granularity, like the pre-barrier for G1
 57   // (mentioned here merely by way of example), we will need to change this
 58   // interface, so it is "exactly precise" (if i may be allowed the adverbial
 59   // redundancy for emphasis) and does not include narrow oop slots not
 60   // included in the original write interval.
 61   HeapWord* aligned_start = align_down(start, HeapWordSize);
 62   HeapWord* aligned_end   = align_up  (end,   HeapWordSize);
 63   // If compressed oops were not being used, these should already be aligned
 64   assert(UseCompressedOops || (aligned_start == start && aligned_end == end),
 65          "Expected heap word alignment of start and end");
 66   write_region(MemRegion(aligned_start, aligned_end));
 67 }
 68 
 69 template <DecoratorSet decorators, typename BarrierSetT>
 70 template <typename T>
 71 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 72 oop_store_in_heap(T* addr, oop value) {
 73   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 74   bs->template write_ref_field_pre<decorators>(addr);
 75   Raw::oop_store(addr, value);
 76   bs->template write_ref_field_post<decorators>(addr);
 77 }
 78 
 79 template <DecoratorSet decorators, typename BarrierSetT>
 80 template <typename T>
 81 inline oop CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 82 oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
 83   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 84   bs->template write_ref_field_pre<decorators>(addr);
 85   oop result = Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);
 86   if (result == compare_value) {
 87     bs->template write_ref_field_post<decorators>(addr);
 88   }
 89   return result;
 90 }
 91 
 92 template <DecoratorSet decorators, typename BarrierSetT>
 93 template <typename T>
 94 inline oop CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
 95 oop_atomic_xchg_in_heap(T* addr, oop new_value) {
 96   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
 97   bs->template write_ref_field_pre<decorators>(addr);
 98   oop result = Raw::oop_atomic_xchg(addr, new_value);
 99   bs->template write_ref_field_post<decorators>(addr);
100   return result;
101 }
102 
103 template <DecoratorSet decorators, typename BarrierSetT>
104 template <typename T>
105 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
106 oop_arraycopy_partial_barrier(BarrierSetT *bs, T* dst_raw, T* p) {
107   const size_t pd = pointer_delta(p, dst_raw, (size_t)heapOopSize);
108   // pointer delta is scaled to number of elements (length field in
109   // objArrayOop) which we assume is 32 bit.
110   assert(pd == (size_t)(int)pd, "length field overflow");
111   bs->write_ref_array((HeapWord*)dst_raw, pd);
112 }
113 
114 template <DecoratorSet decorators, typename BarrierSetT>
115 template <typename T>
116 inline OopCopyResult CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
117 oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
118                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
119                       size_t length) {
120   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
121 
122   src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
123   dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
124 
125   if ((!HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value) &&
126       (!HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value)) {
127     // Optimized covariant case
128     bs->write_ref_array_pre(dst_raw, length,
129                             HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value);
130     Raw::oop_arraycopy(nullptr, 0, src_raw, nullptr, 0, dst_raw, length);
131     bs->write_ref_array((HeapWord*)dst_raw, length);
132   } else {
133     assert(dst_obj != nullptr, "better have an actual oop");
134     Klass* bound = objArrayOop(dst_obj)->element_klass();
135     T* from = const_cast<T*>(src_raw);
136     T* end = from + length;
137     for (T* p = dst_raw; from < end; from++, p++) {
138       T element = *from;
139       // Apply any required checks
140       if (HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value && CompressedOops::is_null(element)) {
141         oop_arraycopy_partial_barrier(bs, dst_raw, p);
142         return OopCopyResult::failed_check_null;
143       }
144       if (HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value &&
145           (!oopDesc::is_instanceof_or_null(CompressedOops::decode(element), bound))) {
146         oop_arraycopy_partial_barrier(bs, dst_raw, p);
147         return OopCopyResult::failed_check_class_cast;
148       }
149       // write
150       bs->template write_ref_field_pre<decorators>(p);
151       *p = element;
152     }
153     bs->write_ref_array((HeapWord*)dst_raw, length);
154   }
155 
156   return OopCopyResult::ok;
157 }
158 
159 template <DecoratorSet decorators, typename BarrierSetT>
160 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
161 clone_in_heap(oop src, oop dst, size_t size) {
162   Raw::clone(src, dst, size);
163   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
164   bs->write_region(MemRegion((HeapWord*)(void*)dst, size));
165 }
166 
167 template <DecoratorSet decorators, typename BarrierSetT>
168 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
169 value_copy_in_heap(const ValuePayload& src, const ValuePayload& dst) {
170   precond(src.klass() == dst.klass());
171 
172   const InlineKlass* md = src.klass();
173   if (!md->contains_oops()) {
174     // If we do not have oops in the flat array, we can just do a raw copy.
175     Raw::value_copy(src, dst);
176   } else {
177     BarrierSetT* bs = barrier_set_cast<BarrierSetT>(BarrierSet::barrier_set());
178     // addr() points at the payload start, the oop map offset are relative to
179     // the object header, adjust address to account for this discrepancy.
180     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
181     typedef typename ValueOopType<decorators>::type OopType;
182 
183     // Pre-barriers...
184     OopMapBlock* map = md->start_of_nonstatic_oop_maps();
185     OopMapBlock* const end = map + md->nonstatic_oop_map_count();
186     bool is_uninitialized = HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value;
187     while (map != end) {
188       address doop_address = oop_map_adjusted_dst_addr + map->offset();
189       // The pre-barrier only impacts G1, which will emit a barrier if the destination is
190       // initialized. Note that we should not emit a barrier if the destination is uninitialized,
191       // as doing so will fill the SATB queue with garbage data.
192       bs->write_ref_array_pre((OopType*) doop_address, map->count(), is_uninitialized);
193       map++;
194     }
195 
196     Raw::value_copy(src, dst);
197 
198     // Post-barriers...
199     map = md->start_of_nonstatic_oop_maps();
200     while (map != end) {
201       address doop_address = oop_map_adjusted_dst_addr + map->offset();
202       // The post-barrier needs to be called for initialized and uninitialized destinations.
203       bs->write_ref_array((HeapWord*) doop_address, map->count());
204       map++;
205     }
206   }
207 }
208 
209 template <DecoratorSet decorators, typename BarrierSetT>
210 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
211 value_store_null_in_heap(const ValuePayload& dst) {
212   const InlineKlass* md = dst.klass();
213   if (!md->contains_oops()) {
214     // If we do not have oops in the flat array, we can just do a raw clear.
215     Raw::value_store_null(dst);
216   } else {
217     BarrierSetT* bs = barrier_set_cast<BarrierSetT>(BarrierSet::barrier_set());
218     // addr() points at the payload start, the oop map offset are relative to
219     // the object header, adjust address to account for this discrepancy.
220     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
221     typedef typename ValueOopType<decorators>::type OopType;
222 
223     // Pre-barriers...
224     OopMapBlock* map = md->start_of_nonstatic_oop_maps();
225     OopMapBlock* const end = map + md->nonstatic_oop_map_count();
226     bool is_uninitialized = HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value;
227     while (map != end) {
228       address doop_address = oop_map_adjusted_dst_addr + map->offset();
229       // The pre-barrier only impacts G1, which will emit a barrier if the destination is
230       // initialized. Note that we should not emit a barrier if the destination is uninitialized,
231       // as doing so will fill the SATB queue with garbage data.
232       bs->write_ref_array_pre((OopType*) doop_address, map->count(), is_uninitialized);
233       map++;
234     }
235 
236     Raw::value_store_null(dst);
237 
238     // Post-barriers...
239     map = md->start_of_nonstatic_oop_maps();
240     while (map != end) {
241       address doop_address = oop_map_adjusted_dst_addr + map->offset();
242       // The post-barrier needs to be called for initialized and uninitialized destinations.
243       bs->write_ref_array((HeapWord*) doop_address, map->count());
244       map++;
245     }
246   }
247 }
248 
249 #endif // SHARE_GC_SHARED_CARDTABLEBARRIERSET_INLINE_HPP