1 /*
   2  * Copyright (c) 2011, 2014, 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/iterator.inline.hpp"
  32 #include "memory/oopFactory.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/instanceMirrorKlass.inline.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/concurrentMarkSweep/cmsOopClosures.inline.hpp"
  42 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  43 #include "gc_implementation/g1/g1OopClosures.inline.hpp"
  44 #include "gc_implementation/g1/g1RemSet.inline.hpp"
  45 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
  46 #include "gc_implementation/parNew/parOopClosures.inline.hpp"
  47 #include "gc_implementation/parallelScavenge/psPromotionManager.inline.hpp"
  48 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
  49 #include "oops/oop.pcgc.inline.hpp"
  50 #endif // INCLUDE_ALL_GCS
  51 
  52 int InstanceMirrorKlass::_offset_of_static_fields = 0;
  53 
  54 InstanceMirrorKlass::InstanceMirrorKlass(int vtable_len, int itable_len, int static_field_size, int nonstatic_oop_map_size, ReferenceType rt, AccessFlags access_flags, bool is_anonymous)
  55     : InstanceKlass(vtable_len, itable_len, static_field_size, nonstatic_oop_map_size, rt, access_flags, is_anonymous, _instance_mirror) {
  56 }
  57 
  58 void InstanceMirrorKlass::oop_follow_contents(oop obj) {
  59   InstanceKlass::oop_follow_contents(obj);
  60 
  61   // Follow the klass field in the mirror.
  62   Klass* klass = java_lang_Class::as_Klass(obj);
  63   if (klass != NULL) {
  64     // An anonymous class doesn't have its own class loader, so the call
  65     // to follow_klass will mark and push its java mirror instead of the
  66     // class loader. When handling the java mirror for an anonymous class
  67     // we need to make sure its class loader data is claimed, this is done
  68     // by calling follow_class_loader explicitly. For non-anonymous classes
  69     // the call to follow_class_loader is made when the class loader itself
  70     // is handled.
  71     if (klass->oop_is_instance() && InstanceKlass::cast(klass)->is_anonymous()) {
  72       MarkSweep::follow_class_loader(klass->class_loader_data());
  73     } else {
  74       MarkSweep::follow_klass(klass);
  75     }
  76   } else {
  77     // If klass is NULL then this a mirror for a primitive type.
  78     // We don't have to follow them, since they are handled as strong
  79     // roots in Universe::oops_do.
  80     assert(java_lang_Class::is_primitive(obj), "Sanity check");
  81   }
  82 
  83   InstanceMirrorKlass_OOP_ITERATE(                                                    \
  84     start_of_static_fields(obj), java_lang_Class::static_oop_field_count(obj),        \
  85     MarkSweep::mark_and_push(p),                                                      \
  86     assert_is_in_closed_subset)
  87 }
  88 
  89 #if INCLUDE_ALL_GCS
  90 void InstanceMirrorKlass::oop_follow_contents(ParCompactionManager* cm,
  91                                               oop obj) {
  92   InstanceKlass::oop_follow_contents(cm, obj);
  93 
  94   // Follow the klass field in the mirror.
  95   Klass* klass = java_lang_Class::as_Klass(obj);
  96   if (klass != NULL) {
  97     // An anonymous class doesn't have its own class loader, so the call
  98     // to follow_klass will mark and push its java mirror instead of the
  99     // class loader. When handling the java mirror for an anonymous class
 100     // we need to make sure its class loader data is claimed, this is done
 101     // by calling follow_class_loader explicitly. For non-anonymous classes
 102     // the call to follow_class_loader is made when the class loader itself
 103     // is handled.
 104     if (klass->oop_is_instance() && InstanceKlass::cast(klass)->is_anonymous()) {
 105       PSParallelCompact::follow_class_loader(cm, klass->class_loader_data());
 106     } else {
 107       PSParallelCompact::follow_klass(cm, klass);
 108     }
 109   } else {
 110     // If klass is NULL then this a mirror for a primitive type.
 111     // We don't have to follow them, since they are handled as strong
 112     // roots in Universe::oops_do.
 113     assert(java_lang_Class::is_primitive(obj), "Sanity check");
 114   }
 115 
 116   InstanceMirrorKlass_OOP_ITERATE(                                                    \
 117     start_of_static_fields(obj), java_lang_Class::static_oop_field_count(obj),        \
 118     PSParallelCompact::mark_and_push(cm, p),                                          \
 119     assert_is_in)
 120 }
 121 #endif // INCLUDE_ALL_GCS
 122 
 123 int InstanceMirrorKlass::oop_adjust_pointers(oop obj) {
 124   int size = oop_size(obj);
 125   InstanceKlass::oop_adjust_pointers(obj);
 126 
 127   InstanceMirrorKlass_OOP_ITERATE(                                                    \
 128     start_of_static_fields(obj), java_lang_Class::static_oop_field_count(obj),        \
 129     MarkSweep::adjust_pointer(p),                                                     \
 130     assert_nothing)
 131   return size;
 132 }
 133 
 134 #if INCLUDE_ALL_GCS
 135 void InstanceMirrorKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 136   // Note that we don't have to follow the mirror -> klass pointer, since all
 137   // klasses that are dirty will be scavenged when we iterate over the
 138   // ClassLoaderData objects.
 139 
 140   InstanceKlass::oop_push_contents(pm, obj);
 141   InstanceMirrorKlass_OOP_ITERATE(                                            \
 142     start_of_static_fields(obj), java_lang_Class::static_oop_field_count(obj),\
 143     if (PSScavenge::should_scavenge(p)) {                                     \
 144       pm->claim_or_forward_depth(p);                                          \
 145     },                                                                        \
 146     assert_nothing )
 147 }
 148 
 149 int InstanceMirrorKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 150   int size = oop_size(obj);
 151   InstanceKlass::oop_update_pointers(cm, obj);
 152 
 153   InstanceMirrorKlass_OOP_ITERATE(                                            \
 154     start_of_static_fields(obj), java_lang_Class::static_oop_field_count(obj),\
 155     PSParallelCompact::adjust_pointer(p),                                     \
 156     assert_nothing)
 157   return size;
 158 }
 159 #endif // INCLUDE_ALL_GCS
 160 
 161 int InstanceMirrorKlass::instance_size(KlassHandle k) {
 162   if (k() != NULL && k->oop_is_instance()) {
 163     return align_object_size(size_helper() + InstanceKlass::cast(k())->static_field_size());
 164   }
 165   return size_helper();
 166 }
 167 
 168 instanceOop InstanceMirrorKlass::allocate_instance(KlassHandle k, TRAPS) {
 169   // Query before forming handle.
 170   int size = instance_size(k);
 171   KlassHandle h_k(THREAD, this);
 172   instanceOop i = (instanceOop)CollectedHeap::obj_allocate(h_k, size, CHECK_NULL);
 173 
 174   // Since mirrors can be variable sized because of the static fields, store
 175   // the size in the mirror itself.
 176   java_lang_Class::set_oop_size(i, size);
 177 
 178   return i;
 179 }
 180 
 181 int InstanceMirrorKlass::oop_size(oop obj) const {
 182   return java_lang_Class::oop_size(obj);
 183 }
 184 
 185 int InstanceMirrorKlass::compute_static_oop_field_count(oop obj) {
 186   Klass* k = java_lang_Class::as_Klass(obj);
 187   if (k != NULL && k->oop_is_instance()) {
 188     return InstanceKlass::cast(k)->static_oop_field_count();
 189   }
 190   return 0;
 191 }