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