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     if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
129       bs->write_ref_array_pre(dst_raw, length);
130     }
131     Raw::oop_arraycopy(nullptr, 0, src_raw, nullptr, 0, dst_raw, length);
132     bs->write_ref_array((HeapWord*)dst_raw, length);
133   } else {
134     assert(dst_obj != nullptr, "better have an actual oop");
135     Klass* bound = objArrayOop(dst_obj)->element_klass();
136     T* from = const_cast<T*>(src_raw);
137     T* end = from + length;
138     for (T* p = dst_raw; from < end; from++, p++) {
139       T element = *from;
140       // Apply any required checks
141       if (HasDecorator<decorators, ARRAYCOPY_NOTNULL>::value && CompressedOops::is_null(element)) {
142         oop_arraycopy_partial_barrier(bs, dst_raw, p);
143         return OopCopyResult::failed_check_null;
144       }
145       if (HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value &&
146           (!oopDesc::is_instanceof_or_null(CompressedOops::decode(element), bound))) {
147         oop_arraycopy_partial_barrier(bs, dst_raw, p);
148         return OopCopyResult::failed_check_class_cast;
149       }
150       // write
151       bs->template write_ref_field_pre<decorators>(p);
152       *p = element;
153     }
154     bs->write_ref_array((HeapWord*)dst_raw, length);
155   }
156 
157   return OopCopyResult::ok;
158 }
159 
160 template <DecoratorSet decorators, typename BarrierSetT>
161 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
162 clone_in_heap(oop src, oop dst, size_t size) {
163   Raw::clone(src, dst, size);
164   BarrierSetT *bs = barrier_set_cast<BarrierSetT>(barrier_set());
165   bs->write_region(MemRegion((HeapWord*)(void*)dst, size));
166 }
167 
168 template <DecoratorSet decorators, typename BarrierSetT>
169 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
170 value_copy_in_heap(const ValuePayload& src, const ValuePayload& dst) {
171   precond(src.klass() == dst.klass());
172 
173   const InlineKlass* md = src.klass();
174   if (!md->contains_oops()) {
175     // If we do not have oops in the flat array, we can just do a raw copy.
176     Raw::value_copy(src, dst);
177   } else {
178     BarrierSetT* bs = barrier_set_cast<BarrierSetT>(BarrierSet::barrier_set());
179     // addr() points at the payload start, the oop map offset are relative to
180     // the object header, adjust address to account for this discrepancy.
181     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
182     typedef typename ValueOopType<decorators>::type OopType;
183 
184     // Pre-barriers...
185     if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
186       OopMapBlock* map = md->start_of_nonstatic_oop_maps();
187       OopMapBlock* const end = map + md->nonstatic_oop_map_count();
188 
189       while (map != end) {
190         address doop_address = oop_map_adjusted_dst_addr + map->offset();
191 
192         bs->write_ref_array_pre((OopType*) doop_address, map->count());
193         map++;
194       }
195     }
196 
197     Raw::value_copy(src, dst);
198 
199     // Post-barriers...
200     OopMapBlock* map = md->start_of_nonstatic_oop_maps();
201     OopMapBlock* const end = map + md->nonstatic_oop_map_count();
202     while (map != end) {
203       address doop_address = oop_map_adjusted_dst_addr + map->offset();
204       // The post-barrier needs to be called for initialized and uninitialized destinations.
205       bs->write_ref_array((HeapWord*) doop_address, map->count());
206       map++;
207     }
208   }
209 }
210 
211 template <DecoratorSet decorators, typename BarrierSetT>
212 inline void CardTableBarrierSet::AccessBarrier<decorators, BarrierSetT>::
213 value_store_null_in_heap(const ValuePayload& dst) {
214   const InlineKlass* md = dst.klass();
215   if (!md->contains_oops()) {
216     // If we do not have oops in the flat array, we can just do a raw clear.
217     Raw::value_store_null(dst);
218   } else {
219     BarrierSetT* bs = barrier_set_cast<BarrierSetT>(BarrierSet::barrier_set());
220     // addr() points at the payload start, the oop map offset are relative to
221     // the object header, adjust address to account for this discrepancy.
222     const address oop_map_adjusted_dst_addr = dst.addr() - md->payload_offset();
223     typedef typename ValueOopType<decorators>::type OopType;
224 
225     // Pre-barriers...
226     if (!HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value) {
227       OopMapBlock* map = md->start_of_nonstatic_oop_maps();
228       OopMapBlock* const end = map + md->nonstatic_oop_map_count();
229       while (map != end) {
230         address doop_address = oop_map_adjusted_dst_addr + map->offset();
231         bs->write_ref_array_pre((OopType*) doop_address, map->count());
232         map++;
233       }
234     }
235 
236     Raw::value_store_null(dst);
237 
238     // Storing null does not require post-barriers
239   }
240 }
241 
242 #endif // SHARE_GC_SHARED_CARDTABLEBARRIERSET_INLINE_HPP