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   // Mark Sweep
  93   int  oop_ms_adjust_pointers(oop obj);
  94 #if INCLUDE_ALL_GCS
  95   // Parallel Scavenge
  96   void oop_ps_push_contents(  oop obj, PSPromotionManager* pm);
  97   // Parallel Compact
  98   void oop_pc_follow_contents(oop obj, ParCompactionManager* cm);
  99   void oop_pc_update_pointers(oop obj, ParCompactionManager* cm);
 100 #endif
 101 
 102   // Oop fields (and metadata) iterators
 103   //  [nv = true]  Use non-virtual calls to do_oop_nv.
 104   //  [nv = false] Use virtual calls to do_oop.
 105   //
 106   // The InstanceMirrorKlass iterators also visit the hidden Klass pointer.
 107 
 108  public:
 109   // Iterate over the static fields.
 110   template <bool nv, class OopClosureType>
 111   inline void oop_oop_iterate_statics(oop obj, OopClosureType* closure);
 112 
 113  private:
 114   // Iterate over the static fields.
 115   // Specialized for [T = oop] or [T = narrowOop].
 116   template <bool nv, typename T, class OopClosureType>
 117   inline void oop_oop_iterate_statics_specialized(oop obj, OopClosureType* closure);
 118 
 119   // Forward iteration
 120   // Iterate over the oop fields and metadata.
 121   template <bool nv, class OopClosureType>
 122   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
 123 
 124 
 125   // Reverse iteration
 126 #if INCLUDE_ALL_GCS
 127   // Iterate over the oop fields and metadata.
 128   template <bool nv, class OopClosureType>
 129   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
 130 #endif
 131 
 132 
 133   // Bounded range iteration
 134   // Iterate over the oop fields and metadata.
 135   template <bool nv, class OopClosureType>
 136   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 137 
 138   // Iterate over the static fields.
 139   template <bool nv, class OopClosureType>
 140   inline void oop_oop_iterate_statics_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 141 
 142   // Iterate over the static fields.
 143   // Specialized for [T = oop] or [T = narrowOop].
 144   template <bool nv, typename T, class OopClosureType>
 145   inline void oop_oop_iterate_statics_specialized_bounded(oop obj, OopClosureType* closure, MemRegion mr);
 146 
 147 
 148  public:
 149 
 150   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL)
 151   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL)
 152 
 153 #if INCLUDE_ALL_GCS
 154   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL_BACKWARDS)
 155   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL_BACKWARDS)
 156 #endif // INCLUDE_ALL_GCS
 157 };
 158 
 159 #endif // SHARE_VM_OOPS_INSTANCEMIRRORKLASS_HPP