1 /*
   2  * Copyright (c) 2011, 2012, 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 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "gc_implementation/shared/markSweep.inline.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "memory/genOopClosures.inline.hpp"
  31 #include "memory/oopFactory.hpp"
  32 #include "oops/instanceKlass.hpp"
  33 #include "oops/instanceClassLoaderKlass.hpp"
  34 #include "oops/instanceMirrorKlass.hpp"
  35 #include "oops/instanceOop.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "oops/symbol.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "utilities/macros.hpp"
  40 #if INCLUDE_ALL_GCS
  41 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
  42 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
  43 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  44 #include "oops/oop.pcgc.inline.hpp"
  45 #endif // INCLUDE_ALL_GCS
  46 
  47 #define if_do_metadata_checked(closure, nv_suffix)                    \
  48   /* Make sure the non-virtual and the virtual versions match. */     \
  49   assert(closure->do_metadata##nv_suffix() == closure->do_metadata(), \
  50       "Inconsistency in do_metadata");                                \
  51   if (closure->do_metadata##nv_suffix())
  52 
  53 // Macro to define InstanceClassLoaderKlass::oop_oop_iterate for virtual/nonvirtual for
  54 // all closures.  Macros calling macros above for each oop size.
  55 // Since ClassLoader objects have only a pointer to the loader_data, they are not
  56 // compressed nor does the pointer move.
  57 
  58 #define InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)\
  59                                                                                 \
  60 int InstanceClassLoaderKlass::                                                  \
  61 oop_oop_iterate##nv_suffix(oop obj, OopClosureType* closure) {                  \
  62   /* Get size before changing pointers */                                       \
  63   SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
  64   int size = InstanceKlass::oop_oop_iterate##nv_suffix(obj, closure);           \
  65                                                                                 \
  66   if_do_metadata_checked(closure, nv_suffix) {                                  \
  67     ClassLoaderData* cld = java_lang_ClassLoader::loader_data(obj);             \
  68     /* cld can be null if we have a non-registered class loader. */             \
  69     if (cld != NULL) {                                                          \
  70       closure->do_class_loader_data(cld);                                       \
  71     }                                                                           \
  72   }                                                                             \
  73                                                                                 \
  74   return size;                                                                  \
  75 }
  76 
  77 #if INCLUDE_ALL_GCS
  78 #define InstanceClassLoaderKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN(OopClosureType, nv_suffix) \
  79                                                                                 \
  80 int InstanceClassLoaderKlass::                                                  \
  81 oop_oop_iterate_backwards##nv_suffix(oop obj, OopClosureType* closure) {        \
  82   /* Get size before changing pointers */                                       \
  83   SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
  84   int size = InstanceKlass::oop_oop_iterate_backwards##nv_suffix(obj, closure); \
  85   return size;                                                                  \
  86 }
  87 #endif // INCLUDE_ALL_GCS
  88 
  89 
  90 #define InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN_m(OopClosureType, nv_suffix)      \
  91                                                                                 \
  92 int InstanceClassLoaderKlass::                                                  \
  93 oop_oop_iterate##nv_suffix##_m(oop obj,                                         \
  94                                OopClosureType* closure,                         \
  95                                MemRegion mr) {                                  \
  96   SpecializationStats::record_iterate_call##nv_suffix(SpecializationStats::irk);\
  97                                                                                 \
  98   int size = InstanceKlass::oop_oop_iterate##nv_suffix##_m(obj, closure, mr);   \
  99                                                                                 \
 100   if_do_metadata_checked(closure, nv_suffix) {                                  \
 101     if (mr.contains(obj)) {                                                     \
 102       ClassLoaderData* cld = java_lang_ClassLoader::loader_data(obj);           \
 103       /* cld can be null if we have a non-registered class loader. */           \
 104       if (cld != NULL) {                                                        \
 105         closure->do_class_loader_data(cld);                                     \
 106       }                                                                         \
 107     }                                                                           \
 108   }                                                                             \
 109                                                                                 \
 110   return size;                                                                  \
 111 }
 112 
 113 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN)
 114 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN)
 115 #if INCLUDE_ALL_GCS
 116 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceClassLoaderKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
 117 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceClassLoaderKlass_OOP_OOP_ITERATE_BACKWARDS_DEFN)
 118 #endif // INCLUDE_ALL_GCS
 119 ALL_OOP_OOP_ITERATE_CLOSURES_1(InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN_m)
 120 ALL_OOP_OOP_ITERATE_CLOSURES_2(InstanceClassLoaderKlass_OOP_OOP_ITERATE_DEFN_m)
 121 
 122 void InstanceClassLoaderKlass::oop_follow_contents(oop obj) {
 123   InstanceKlass::oop_follow_contents(obj);
 124   ClassLoaderData * const loader_data = java_lang_ClassLoader::loader_data(obj);
 125 
 126   // We must NULL check here, since the class loader
 127   // can be found before the loader data has been set up.
 128   if(loader_data != NULL) {
 129     MarkSweep::follow_class_loader(loader_data);
 130   }
 131 }
 132 
 133 #if INCLUDE_ALL_GCS
 134 void InstanceClassLoaderKlass::oop_follow_contents(ParCompactionManager* cm,
 135         oop obj) {
 136   InstanceKlass::oop_follow_contents(cm, obj);
 137   ClassLoaderData * const loader_data = java_lang_ClassLoader::loader_data(obj);
 138   if (loader_data != NULL) {
 139     PSParallelCompact::follow_class_loader(cm, loader_data);
 140   }
 141 }
 142 
 143 void InstanceClassLoaderKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 144   InstanceKlass::oop_push_contents(pm, obj);
 145 
 146   // This is called by the young collector. It will already have taken care of
 147   // all class loader data. So, we don't have to follow the class loader ->
 148   // class loader data link.
 149 }
 150 
 151 int InstanceClassLoaderKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 152   InstanceKlass::oop_update_pointers(cm, obj);
 153   return size_helper();
 154 }
 155 #endif // INCLUDE_ALL_GCS
 156