1 /* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_OOPS_INSTANCEMIRRORKLASS_INLINE_HPP
  25 #define SHARE_VM_OOPS_INSTANCEMIRRORKLASS_INLINE_HPP
  26 
  27 #include "classfile/javaClasses.hpp"
  28 #include "oops/instanceKlass.inline.hpp"
  29 #include "oops/instanceMirrorKlass.hpp"
  30 #include "oops/klass.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 template <bool nv, typename T, class OopClosureType>
  37 void InstanceMirrorKlass::oop_oop_iterate_statics_specialized(oop obj, OopClosureType* closure) {
  38   T* p         = (T*)start_of_static_fields(obj);
  39   T* const end = p + java_lang_Class::static_oop_field_count(obj);
  40 
  41   for (; p < end; ++p) {
  42     Devirtualizer<nv>::do_oop(closure, p);
  43   }
  44 }
  45 
  46 template <bool nv, class OopClosureType>
  47 void InstanceMirrorKlass::oop_oop_iterate_statics(oop obj, OopClosureType* closure) {
  48   if (UseCompressedOops) {
  49     oop_oop_iterate_statics_specialized<nv, narrowOop>(obj, closure);
  50   } else {
  51     oop_oop_iterate_statics_specialized<nv, oop>(obj, closure);
  52   }
  53 }
  54 
  55 template <bool nv, class OopClosureType>
  56 int InstanceMirrorKlass::oop_oop_iterate(oop obj, OopClosureType* closure) {
  57   InstanceKlass::oop_oop_iterate<nv>(obj, closure);
  58 
  59   if (Devirtualizer<nv>::do_metadata(closure)) {
  60     Klass* klass = java_lang_Class::as_Klass(obj);
  61     // We'll get NULL for primitive mirrors.
  62     if (klass != NULL) {
  63       Devirtualizer<nv>::do_klass(closure, klass);
  64     }
  65   }
  66 
  67   oop_oop_iterate_statics<nv>(obj, closure);
  68 
  69   return oop_size(obj);
  70 }
  71 
  72 #if INCLUDE_ALL_GCS
  73 template <bool nv, class OopClosureType>
  74 int InstanceMirrorKlass::oop_oop_iterate_reverse(oop obj, OopClosureType* closure) {
  75   InstanceKlass::oop_oop_iterate_reverse<nv>(obj, closure);
  76 
  77   InstanceMirrorKlass::oop_oop_iterate_statics<nv>(obj, closure);
  78 
  79   return oop_size(obj);
  80 }
  81 #endif
  82 
  83 template <bool nv, typename T, class OopClosureType>
  84 void InstanceMirrorKlass::oop_oop_iterate_statics_specialized_bounded(oop obj,
  85                                                                      OopClosureType* closure,
  86                                                                      MemRegion mr) {
  87   T* p   = (T*)start_of_static_fields(obj);
  88   T* end = p + java_lang_Class::static_oop_field_count(obj);
  89 
  90   T* const l   = (T*)mr.start();
  91   T* const h   = (T*)mr.end();
  92   assert(mask_bits((intptr_t)l, sizeof(T)-1) == 0 &&
  93          mask_bits((intptr_t)h, sizeof(T)-1) == 0,
  94          "bounded region must be properly aligned");
  95 
  96   if (p < l) {
  97     p = l;
  98   }
  99   if (end > h) {
 100     end = h;
 101   }
 102 
 103   for (;p < end; ++p) {
 104     Devirtualizer<nv>::do_oop(closure, p);
 105   }
 106 }
 107 
 108 template <bool nv, class OopClosureType>
 109 void InstanceMirrorKlass::oop_oop_iterate_statics_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
 110   if (UseCompressedOops) {
 111     oop_oop_iterate_statics_specialized_bounded<nv, narrowOop>(obj, closure, mr);
 112   } else {
 113     oop_oop_iterate_statics_specialized_bounded<nv, oop>(obj, closure, mr);
 114   }
 115 }
 116 
 117 template <bool nv, class OopClosureType>
 118 int InstanceMirrorKlass::oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr) {
 119   InstanceKlass::oop_oop_iterate_bounded<nv>(obj, closure, mr);
 120 
 121   if (Devirtualizer<nv>::do_metadata(closure)) {
 122     if (mr.contains(obj)) {
 123       Klass* klass = java_lang_Class::as_Klass(obj);
 124       // We'll get NULL for primitive mirrors.
 125       if (klass != NULL) {
 126         Devirtualizer<nv>::do_klass(closure, klass);
 127       }
 128     }
 129   }
 130 
 131   oop_oop_iterate_statics_bounded<nv>(obj, closure, mr);
 132 
 133   return oop_size(obj);
 134 }
 135 
 136 #define ALL_INSTANCE_MIRROR_KLASS_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)  \
 137   OOP_OOP_ITERATE_DEFN(          InstanceMirrorKlass, OopClosureType, nv_suffix)   \
 138   OOP_OOP_ITERATE_DEFN_m(        InstanceMirrorKlass, OopClosureType, nv_suffix)   \
 139   OOP_OOP_ITERATE_BACKWARDS_DEFN(InstanceMirrorKlass, OopClosureType, nv_suffix)
 140 
 141 #endif // SHARE_VM_OOPS_INSTANCEMIRRORKLASS_INLINE_HPP