1 /*
   2  * Copyright (c) 2011, 2017, 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_INSTANCEMIRRORKLASS_HPP
  26 #define SHARE_VM_OOPS_INSTANCEMIRRORKLASS_HPP
  27 
  28 #include "classfile/systemDictionary.hpp"
  29 #include "gc/shared/specialized_oop_closures.hpp"
  30 #include "oops/instanceKlass.hpp"
  31 #include "runtime/handles.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 class ClassFileParser;
  35 
  36 // An InstanceMirrorKlass is a specialized InstanceKlass for
  37 // java.lang.Class instances.  These instances are special because
  38 // they contain the static fields of the class in addition to the
  39 // normal fields of Class.  This means they are variable sized
  40 // instances and need special logic for computing their size and for
  41 // iteration of their oops.
  42 
  43 
  44 class InstanceMirrorKlass: public InstanceKlass {
  45   friend class VMStructs;
  46   friend class InstanceKlass;
  47 
  48  private:
  49   static int _offset_of_static_fields;
  50 
  51   InstanceMirrorKlass(const ClassFileParser& parser) : InstanceKlass(parser, InstanceKlass::_misc_kind_mirror) {}
  52 
  53  public:
  54   InstanceMirrorKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
  55 
  56   // Casting from Klass*
  57   static InstanceMirrorKlass* cast(Klass* k) {
  58     assert(InstanceKlass::cast(k)->is_mirror_instance_klass(),
  59            "cast to InstanceMirrorKlass");
  60     return static_cast<InstanceMirrorKlass*>(k);
  61   }
  62 
  63   // Returns the size of the instance including the extra static fields.
  64   virtual int oop_size(oop obj) const;
  65 
  66   // Static field offset is an offset into the Heap, should be converted by
  67   // based on UseCompressedOop for traversal
  68   static HeapWord* start_of_static_fields(oop obj) {
  69     return (HeapWord*)(cast_from_oop<intptr_t>(obj) + offset_of_static_fields());
  70   }
  71 
  72   static void init_offset_of_static_fields() {
  73     // Cache the offset of the static fields in the Class instance
  74     assert(_offset_of_static_fields == 0, "once");
  75     _offset_of_static_fields = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->size_helper() << LogHeapWordSize;
  76   }
  77 
  78   static int offset_of_static_fields() {
  79     return _offset_of_static_fields;
  80   }
  81 
  82   int compute_static_oop_field_count(oop obj);
  83 
  84   // Given a Klass return the size of the instance
  85   int instance_size(Klass* k);
  86 
  87   // allocation
  88   instanceOop allocate_instance(Klass* k, TRAPS);
  89 
  90   // GC specific object visitors
  91   //
  92 #if INCLUDE_ALL_GCS
  93   // Parallel Scavenge
  94   void oop_ps_push_contents(  oop obj, PSPromotionManager* pm);
  95   // Parallel Compact
  96   void oop_pc_follow_contents(oop obj, ParCompactionManager* cm);
  97   void oop_pc_update_pointers(oop obj, ParCompactionManager* cm);
  98 #endif
  99 
 100   // Oop fields (and metadata) iterators
 101   //  [nv = true]  Use non-virtual calls to do_oop_nv.
 102   //  [nv = false] Use virtual calls to do_oop.
 103   //
 104   // The InstanceMirrorKlass iterators also visit the hidden Klass pointer.
 105 
 106  public:
 107   // Iterate over the static fields.
 108   template <bool nv, class OopClosureType>
 109   inline void oop_oop_iterate_statics(oop obj, OopClosureType* closure);
 110 
 111  private:
 112   // Iterate over the static fields.
 113   // Specialized for [T = oop] or [T = narrowOop].
 114   template <bool nv, typename T, class OopClosureType>
 115   inline void oop_oop_iterate_statics_specialized(oop obj, OopClosureType* closure);
 116 
 117   // Forward iteration
 118   // Iterate over the oop fields and metadata.
 119   template <bool nv, class OopClosureType>
 120   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
 121 
 122 
 123   // Reverse iteration
 124 #if INCLUDE_ALL_GCS
 125   // Iterate over the oop fields and metadata.
 126   template <bool nv, class OopClosureType>
 127   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
 128 #endif
 129 
 130 
 131   // Bounded range iteration
 132   // Iterate over the oop fields and metadata.
 133   template <bool nv, class OopClosureType>
 134   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 135 
 136   // Iterate over the static fields.
 137   template <bool nv, class OopClosureType>
 138   inline void oop_oop_iterate_statics_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 139 
 140   // Iterate over the static fields.
 141   // Specialized for [T = oop] or [T = narrowOop].
 142   template <bool nv, typename T, class OopClosureType>
 143   inline void oop_oop_iterate_statics_specialized_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 144 
 145 
 146  public:
 147 
 148   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL)
 149   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL)
 150 
 151 #if INCLUDE_ALL_GCS
 152   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL_BACKWARDS)
 153   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL_BACKWARDS)
 154 #endif // INCLUDE_ALL_GCS
 155 };
 156 
 157 #endif // SHARE_VM_OOPS_INSTANCEMIRRORKLASS_HPP