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