1 /*
  2  * Copyright (c) 2015, 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_OOPS_INSTANCEREFKLASS_INLINE_HPP
 26 #define SHARE_OOPS_INSTANCEREFKLASS_INLINE_HPP
 27 
 28 #include "oops/instanceRefKlass.hpp"
 29 
 30 #include "classfile/javaClasses.inline.hpp"
 31 #include "gc/shared/referenceProcessor.hpp"
 32 #include "logging/log.hpp"
 33 #include "oops/access.inline.hpp"
 34 #include "oops/instanceKlass.inline.hpp"
 35 #include "oops/oop.inline.hpp"
 36 #include "utilities/debug.hpp"
 37 #include "utilities/globalDefinitions.hpp"
 38 #include "utilities/macros.hpp"
 39 
 40 template <typename T, class OopClosureType, class Contains>
 41 void InstanceRefKlass::do_referent(oop obj, OopClosureType* closure, Contains& contains) {
 42   T* referent_addr = (T*)java_lang_ref_Reference::referent_addr_raw(obj);
 43   if (contains(referent_addr)) {
 44     Devirtualizer::do_oop(closure, referent_addr);
 45   }
 46 }
 47 
 48 template <typename T, class OopClosureType, class Contains>
 49 void InstanceRefKlass::do_discovered(oop obj, OopClosureType* closure, Contains& contains) {
 50   T* discovered_addr = (T*)java_lang_ref_Reference::discovered_addr_raw(obj);
 51   if (contains(discovered_addr)) {
 52     Devirtualizer::do_oop(closure, discovered_addr);
 53   }
 54 }
 55 
 56 static inline oop load_referent(oop obj, ReferenceType type) {
 57   if (type == REF_PHANTOM) {
 58     return HeapAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(java_lang_ref_Reference::referent_addr_raw(obj));
 59   } else {
 60     return HeapAccess<ON_WEAK_OOP_REF | AS_NO_KEEPALIVE>::oop_load(java_lang_ref_Reference::referent_addr_raw(obj));
 61   }
 62 }
 63 
 64 template <typename T, class OopClosureType>
 65 bool InstanceRefKlass::try_discover(oop obj, ReferenceType type, OopClosureType* closure) {
 66   ReferenceDiscoverer* rd = closure->ref_discoverer();
 67   if (rd != NULL) {
 68     oop referent = load_referent(obj, type);
 69     if (referent != NULL) {
 70       if (!referent->is_gc_marked()) {
 71         // Only try to discover if not yet marked.
 72         return rd->discover_reference(obj, type);
 73       }
 74     }
 75   }
 76   return false;
 77 }
 78 
 79 template <typename T, class OopClosureType, class Contains>
 80 void InstanceRefKlass::oop_oop_iterate_discovery(oop obj, ReferenceType type, OopClosureType* closure, Contains& contains) {
 81   // Try to discover reference and return if it succeeds.
 82   if (try_discover<T>(obj, type, closure)) {
 83     return;
 84   }
 85 
 86   // Treat referent and discovered as normal oops.
 87   do_referent<T>(obj, closure, contains);
 88   do_discovered<T>(obj, closure, contains);
 89 }
 90 
 91 template <typename T, class OopClosureType, class Contains>
 92 void InstanceRefKlass::oop_oop_iterate_discovered_and_discovery(oop obj, ReferenceType type, OopClosureType* closure, Contains& contains) {
 93   // Explicitly apply closure to the discovered field.
 94   do_discovered<T>(obj, closure, contains);
 95   // Then do normal reference processing with discovery.
 96   oop_oop_iterate_discovery<T>(obj, type, closure, contains);
 97 }
 98 
 99 template <typename T, class OopClosureType, class Contains>
100 void InstanceRefKlass::oop_oop_iterate_fields(oop obj, OopClosureType* closure, Contains& contains) {
101   assert(closure->ref_discoverer() == NULL, "ReferenceDiscoverer should not be set");
102   do_referent<T>(obj, closure, contains);
103   do_discovered<T>(obj, closure, contains);
104 }
105 
106 template <typename T, class OopClosureType, class Contains>
107 void InstanceRefKlass::oop_oop_iterate_fields_except_referent(oop obj, OopClosureType* closure, Contains& contains) {
108   assert(closure->ref_discoverer() == NULL, "ReferenceDiscoverer should not be set");
109   do_discovered<T>(obj, closure, contains);
110 }
111 
112 template <typename T, class OopClosureType, class Contains>
113 void InstanceRefKlass::oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure, Contains& contains) {
114   switch (closure->reference_iteration_mode()) {
115     case OopIterateClosure::DO_DISCOVERY:
116       trace_reference_gc<T>("do_discovery", obj);
117       oop_oop_iterate_discovery<T>(obj, reference_type(), closure, contains);
118       break;
119     case OopIterateClosure::DO_DISCOVERED_AND_DISCOVERY:
120       trace_reference_gc<T>("do_discovered_and_discovery", obj);
121       oop_oop_iterate_discovered_and_discovery<T>(obj, reference_type(), closure, contains);
122       break;
123     case OopIterateClosure::DO_FIELDS:
124       trace_reference_gc<T>("do_fields", obj);
125       oop_oop_iterate_fields<T>(obj, closure, contains);
126       break;
127     case OopIterateClosure::DO_FIELDS_EXCEPT_REFERENT:
128       trace_reference_gc<T>("do_fields_except_referent", obj);
129       oop_oop_iterate_fields_except_referent<T>(obj, closure, contains);
130       break;
131     default:
132       ShouldNotReachHere();
133   }
134 }
135 
136 class AlwaysContains {
137  public:
138   template <typename T> bool operator()(T* p) const { return true; }
139 };
140 
141 template <typename T, class OopClosureType>
142 void InstanceRefKlass::oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure) {
143   AlwaysContains always_contains;
144   oop_oop_iterate_ref_processing<T>(obj, closure, always_contains);
145 }
146 
147 class MrContains {
148   const MemRegion _mr;
149  public:
150   MrContains(MemRegion mr) : _mr(mr) {}
151   template <typename T> bool operator()(T* p) const { return _mr.contains(p); }
152 };
153 
154 template <typename T, class OopClosureType>
155 void InstanceRefKlass::oop_oop_iterate_ref_processing_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
156   const MrContains contains(mr);
157   oop_oop_iterate_ref_processing<T>(obj, closure, contains);
158 }
159 
160 template <typename T, class OopClosureType>
161 void InstanceRefKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
162   InstanceKlass::oop_oop_iterate<T>(obj, closure);
163 
164   oop_oop_iterate_ref_processing<T>(obj, closure);
165 }
166 
167 template <typename T, class OopClosureType>
168 void InstanceRefKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
169   InstanceKlass::oop_oop_iterate_reverse<T>(obj, closure);
170 
171   oop_oop_iterate_ref_processing<T>(obj, closure);
172 }
173 
174 template <typename T, class OopClosureType>
175 void InstanceRefKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
176   InstanceKlass::oop_oop_iterate_bounded<T>(obj, closure, mr);
177 
178   oop_oop_iterate_ref_processing_bounded<T>(obj, closure, mr);
179 }
180 
181 #ifdef ASSERT
182 template <typename T>
183 void InstanceRefKlass::trace_reference_gc(const char *s, oop obj) {
184   T* referent_addr   = (T*) java_lang_ref_Reference::referent_addr_raw(obj);
185   T* discovered_addr = (T*) java_lang_ref_Reference::discovered_addr_raw(obj);
186 
187   log_develop_trace(gc, ref)("InstanceRefKlass %s for obj " PTR_FORMAT, s, p2i(obj));
188   if (java_lang_ref_Reference::is_phantom(obj)) {
189     log_develop_trace(gc, ref)("     referent_addr/* " PTR_FORMAT " / " PTR_FORMAT,
190                                p2i(referent_addr), p2i((oop)HeapAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(referent_addr)));
191   } else {
192     log_develop_trace(gc, ref)("     referent_addr/* " PTR_FORMAT " / " PTR_FORMAT,
193                                p2i(referent_addr), p2i((oop)HeapAccess<ON_WEAK_OOP_REF | AS_NO_KEEPALIVE>::oop_load(referent_addr)));
194   }
195   log_develop_trace(gc, ref)("     discovered_addr/* " PTR_FORMAT " / " PTR_FORMAT,
196       p2i(discovered_addr), p2i((oop)HeapAccess<AS_NO_KEEPALIVE>::oop_load(discovered_addr)));
197 }
198 #endif
199 
200 #endif // SHARE_OOPS_INSTANCEREFKLASS_INLINE_HPP