1 /*
2  * Copyright (c) 2000, 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_HPP
26 #define SHARE_GC_SHARED_MODREFBARRIERSET_HPP
27 
28 #include "gc/shared/barrierSet.hpp"
29 #include "memory/memRegion.hpp"
30 
31 class Klass;
32 
33 class ModRefBarrierSet: public BarrierSet {
34 protected:
ModRefBarrierSet(BarrierSetAssembler * barrier_set_assembler,BarrierSetC1 * barrier_set_c1,BarrierSetC2 * barrier_set_c2,const BarrierSet::FakeRtti & fake_rtti)35   ModRefBarrierSet(BarrierSetAssembler* barrier_set_assembler,
36                    BarrierSetC1* barrier_set_c1,
37                    BarrierSetC2* barrier_set_c2,
38                    const BarrierSet::FakeRtti& fake_rtti)
39     : BarrierSet(barrier_set_assembler,
40                  barrier_set_c1,
41                  barrier_set_c2,
42                  NULL /* barrier_set_nmethod */,
43                  fake_rtti.add_tag(BarrierSet::ModRef)) { }
~ModRefBarrierSet()44   ~ModRefBarrierSet() { }
45 
46 public:
47   template <DecoratorSet decorators, typename T>
write_ref_field_pre(T * addr)48   inline void write_ref_field_pre(T* addr) {}
49 
50   template <DecoratorSet decorators, typename T>
write_ref_field_post(T * addr,oop new_value)51   inline void write_ref_field_post(T *addr, oop new_value) {}
52 
53   // Causes all refs in "mr" to be assumed to be modified.
54   virtual void invalidate(MemRegion mr) = 0;
55   virtual void write_region(MemRegion mr) = 0;
56 
57   // Operations on arrays, or general regions (e.g., for "clone") may be
58   // optimized by some barriers.
59 
60   // Below length is the # array elements being written
write_ref_array_pre(oop * dst,size_t length,bool dest_uninitialized=false)61   virtual void write_ref_array_pre(oop* dst, size_t length,
62                                    bool dest_uninitialized = false) {}
write_ref_array_pre(narrowOop * dst,size_t length,bool dest_uninitialized=false)63   virtual void write_ref_array_pre(narrowOop* dst, size_t length,
64                                    bool dest_uninitialized = false) {}
65   // Below count is the # array elements being written, starting
66   // at the address "start", which may not necessarily be HeapWord-aligned
67   inline void write_ref_array(HeapWord* start, size_t count);
68 
69  protected:
70   virtual void write_ref_array_work(MemRegion mr) = 0;
71 
72  public:
73   // The ModRef abstraction introduces pre and post barriers
74   template <DecoratorSet decorators, typename BarrierSetT>
75   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
76     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
77 
78   public:
79     template <typename T>
80     static void oop_store_in_heap(T* addr, oop value);
81     template <typename T>
82     static oop oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value);
83     template <typename T>
84     static oop oop_atomic_xchg_in_heap(oop new_value, T* addr);
85 
86     template <typename T>
87     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
88                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
89                                       size_t length);
90 
91     static void clone_in_heap(oop src, oop dst, size_t size);
92 
oop_store_in_heap_at(oop base,ptrdiff_t offset,oop value)93     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
94       oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
95     }
96 
oop_atomic_xchg_in_heap_at(oop new_value,oop base,ptrdiff_t offset)97     static oop oop_atomic_xchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset) {
98       return oop_atomic_xchg_in_heap(new_value, AccessInternal::oop_field_addr<decorators>(base, offset));
99     }
100 
oop_atomic_cmpxchg_in_heap_at(oop new_value,oop base,ptrdiff_t offset,oop compare_value)101     static oop oop_atomic_cmpxchg_in_heap_at(oop new_value, oop base, ptrdiff_t offset, oop compare_value) {
102       return oop_atomic_cmpxchg_in_heap(new_value, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value);
103     }
104   };
105 };
106 
107 template<>
108 struct BarrierSet::GetName<ModRefBarrierSet> {
109   static const BarrierSet::Name value = BarrierSet::ModRef;
110 };
111 
112 #endif // SHARE_GC_SHARED_MODREFBARRIERSET_HPP
113