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