1 /*
   2  * Copyright (c) 1997, 2010, 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 "oops/instanceRefKlass.hpp"
  29 #include "oops/oop.inline2.hpp"
  30 
  31 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  32 
  33 template <typename T, bool nv, typename OopClosureType>
  34 void InstanceRefKlass::specialized_oop_iterate(oop obj,
  35                                                OopClosureType* closure,
  36                                                MemRegion* mr) {
  37   T* disc_addr = (T*)java_lang_ref_Reference::discovered_addr(obj);
  38   if (closure->apply_to_weak_ref_discovered_field()) {
  39     Devirtualizer<nv, OopClosureType>::do_oop(closure, disc_addr);
  40   }
  41 
  42   T* referent_addr = (T*)java_lang_ref_Reference::referent_addr(obj);
  43   T heap_oop = oopDesc::load_heap_oop(referent_addr);
  44   ReferenceProcessor* rp = closure->_ref_processor;
  45   if (!oopDesc::is_null(heap_oop)) {
  46     oop referent = oopDesc::decode_heap_oop_not_null(heap_oop);
  47     if (!referent->is_gc_marked() && (rp != NULL) &&
  48         rp->discover_reference(obj, reference_type())) {
  49       return;
  50     } else if (mr == NULL || mr->contains(referent_addr)) {
  51       /* treat referent as normal oop */
  52       Devirtualizer<nv, OopClosureType>::do_oop(closure, referent_addr);
  53     }
  54   }
  55   T* next_addr = (T*)java_lang_ref_Reference::next_addr(obj);
  56   if (ReferenceProcessor::pending_list_uses_discovered_field()) {
  57     T next_oop  = oopDesc::load_heap_oop(next_addr);
  58     /* Treat discovered as normal oop, if ref is not "active" (next non-NULL) */
  59     if (!oopDesc::is_null(next_oop) && (mr == NULL || mr->contains(disc_addr))) {
  60         /* i.e. ref is not "active" */
  61       debug_only(
  62         if(TraceReferenceGC && PrintGCDetails) {
  63           gclog_or_tty->print_cr("   Process discovered as normal "
  64                                  INTPTR_FORMAT, disc_addr);
  65         }
  66       )
  67       Devirtualizer<nv, OopClosureType>::do_oop(closure, disc_addr);
  68     }
  69   } else {
  70     /* In the case of older JDKs which do not use the discovered field for  */
  71     /* the pending list, an inactive ref (next != NULL) must always have a  */
  72     /* NULL discovered field. */
  73     debug_only(
  74       T next_oop = oopDesc::load_heap_oop(next_addr);
  75       T disc_oop = oopDesc::load_heap_oop(disc_addr);
  76       assert(oopDesc::is_null(next_oop) || oopDesc::is_null(disc_oop),
  77            err_msg("Found an inactive reference " PTR_FORMAT " with a non-NULL"
  78                    "discovered field", (oopDesc*)obj));
  79     )
  80   }
  81   /* treat next as normal oop */
  82   if (mr == NULL || mr->contains(next_addr)) {
  83     Devirtualizer<nv, OopClosureType>::do_oop(closure, next_addr);
  84   }
  85 }
  86 
  87 // Macro to define InstanceRefKlass::oop_oop_iterate for virtual/nonvirtual for
  88 // all closures.  Macros calling macros above for each oop size.
  89 
  90 template <bool nv, typename OopClosureType>
  91 int InstanceRefKlass::
  92 oop_oop_iterate(oop obj, OopClosureType* closure) {
  93   /* Get size before changing pointers */
  94 
  95   int size = InstanceKlass::oop_oop_iterate<nv>(obj, closure);
  96 
  97   if (UseCompressedOops) {
  98     specialized_oop_iterate<narrowOop, nv>(obj, closure, NULL);
  99   } else {
 100     specialized_oop_iterate<oop, nv>(obj, closure, NULL);
 101   }
 102 
 103   return size;
 104 }
 105 
 106 #ifndef SERIALGC
 107 
 108 template <bool nv, typename OopClosureType>
 109 int InstanceRefKlass::
 110 oop_oop_iterate_backwards(oop obj, OopClosureType* closure) {
 111   /* Get size before changing pointers */
 112 
 113   int size = InstanceKlass::oop_oop_iterate_backwards<nv>(obj, closure);
 114 
 115   if (UseCompressedOops) {
 116     specialized_oop_iterate<narrowOop, nv>(obj, closure, NULL);
 117   } else {
 118     specialized_oop_iterate<oop, nv>(obj, closure, NULL);
 119   }
 120 
 121   return size;
 122 }
 123 #endif // !SERIALGC
 124 
 125 
 126 template <bool nv, typename OopClosureType>
 127 int InstanceRefKlass::
 128 oop_oop_iterate_m(oop obj,
 129                     OopClosureType* closure,
 130                     MemRegion mr) {
 131 
 132   int size = InstanceKlass::oop_oop_iterate_m<nv>(obj, closure, mr);
 133 
 134   if (UseCompressedOops) {
 135     specialized_oop_iterate<narrowOop, nv>(obj, closure, &mr);
 136   } else {
 137     specialized_oop_iterate<oop, nv>(obj, closure, &mr);
 138   }
 139 
 140   return size;
 141 }
 142 
 143 #endif // SHARE_VM_OOPS_INSTANCEREFKLASS_INLINE_HPP