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