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