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