1 /*
   2  * Copyright (c) 2015, 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_OOPS_INSTANCEREFKLASS_INLINE_HPP
  26 #define SHARE_VM_OOPS_INSTANCEREFKLASS_INLINE_HPP
  27 
  28 #include "classfile/javaClasses.hpp"
  29 #include "gc/shared/referenceProcessor.hpp"

  30 #include "oops/instanceKlass.inline.hpp"
  31 #include "oops/instanceRefKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "utilities/debug.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/macros.hpp"
  36 
  37 template <bool nv, typename T, class OopClosureType, class Contains>
  38 void InstanceRefKlass::oop_oop_iterate_ref_processing_specialized(oop obj, OopClosureType* closure, Contains& contains) {
  39   T* disc_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
  40   if (closure->apply_to_weak_ref_discovered_field()) {
  41     Devirtualizer<nv>::do_oop(closure, disc_addr);
  42   }
  43 
  44   T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  45   T heap_oop = oopDesc::load_heap_oop(referent_addr);
  46   ReferenceProcessor* rp = closure->ref_processor();
  47   if (!oopDesc::is_null(heap_oop)) {
  48     oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);
  49     if (!referent->is_gc_marked() && (rp != NULL) &&
  50         rp->discover_reference(obj, reference_type())) {
  51       return;
  52     } else if (contains(referent_addr)) {
  53       // treat referent as normal oop
  54       Devirtualizer<nv>::do_oop(closure, referent_addr);
  55     }
  56   }
  57   T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
  58   T next_oop  = oopDesc::load_heap_oop(next_addr);
  59   // Treat discovered as normal oop, if ref is not "active" (next non-NULL)
  60   if (!oopDesc::is_null(next_oop) && contains(disc_addr)) {
  61     // i.e. ref is not "active"
  62     debug_only(
  63       if(TraceReferenceGC && PrintGCDetails) {
  64         gclog_or_tty->print_cr("   Process discovered as normal "
  65                                PTR_FORMAT, p2i(disc_addr));
  66       }
  67     )
  68     Devirtualizer<nv>::do_oop(closure, disc_addr);
  69   }
  70   // treat next as normal oop
  71   if (contains(next_addr)) {
  72     Devirtualizer<nv>::do_oop(closure, next_addr);
  73   }
  74 }
  75 
  76 class AlwaysContains {
  77  public:
  78   template <typename T> bool operator()(T* p) const { return true; }
  79 };
  80 
  81 template <bool nv, class OopClosureType>
  82 void InstanceRefKlass::oop_oop_iterate_ref_processing(oop obj, OopClosureType* closure) {
  83   AlwaysContains always_contains;
  84   if (UseCompressedOops) {
  85     oop_oop_iterate_ref_processing_specialized<nv, narrowOop>(obj, closure, always_contains);
  86   } else {
  87     oop_oop_iterate_ref_processing_specialized<nv, oop>(obj, closure, always_contains);
  88   }
  89 }
  90 
  91 class MrContains {
  92   const MemRegion _mr;
  93  public:
  94   MrContains(MemRegion mr) : _mr(mr) {}
  95   template <typename T> bool operator()(T* p) const { return _mr.contains(p); }
  96 };
  97 
  98 template <bool nv, class OopClosureType>
  99 void InstanceRefKlass::oop_oop_iterate_ref_processing_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
 100   const MrContains contains(mr);
 101   if (UseCompressedOops) {
 102     oop_oop_iterate_ref_processing_specialized<nv, narrowOop>(obj, closure, contains);
 103   } else {
 104     oop_oop_iterate_ref_processing_specialized<nv, oop>(obj, closure, contains);
 105   }
 106 }
 107 
 108 template <bool nv, class OopClosureType>
 109 void InstanceRefKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
 110   InstanceKlass::oop_oop_iterate<nv>(obj, closure);
 111 
 112   oop_oop_iterate_ref_processing<nv>(obj, closure);
 113 }
 114 
 115 #if INCLUDE_ALL_GCS
 116 template <bool nv, class OopClosureType>
 117 void InstanceRefKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
 118   InstanceKlass::oop_oop_iterate_reverse<nv>(obj, closure);
 119 
 120   oop_oop_iterate_ref_processing<nv>(obj, closure);
 121 }
 122 #endif // INCLUDE_ALL_GCS
 123 
 124 
 125 template <bool nv, class OopClosureType>
 126 void InstanceRefKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
 127   InstanceKlass::oop_oop_iterate_bounded<nv>(obj, closure, mr);
 128 
 129   oop_oop_iterate_ref_processing_bounded<nv>(obj, closure, mr);
 130 }
 131 
 132 // Macro to define InstanceRefKlass::oop_oop_iterate for virtual/nonvirtual for
 133 // all closures.  Macros calling macros above for each oop size.
 134 #define ALL_INSTANCE_REF_KLASS_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)  \
 135   OOP_OOP_ITERATE_DEFN(          InstanceRefKlass, OopClosureType, nv_suffix)   \
 136   OOP_OOP_ITERATE_DEFN_BOUNDED(  InstanceRefKlass, OopClosureType, nv_suffix)   \
 137   OOP_OOP_ITERATE_DEFN_BACKWARDS(InstanceRefKlass, OopClosureType, nv_suffix)
 138 
 139 #endif // SHARE_VM_OOPS_INSTANCEREFKLASS_INLINE_HPP
--- EOF ---