1 /*
  2  * Copyright (c) 2011, 2018, 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 "oops/instanceKlass.hpp"
 30 #include "runtime/handles.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 class ClassFileParser;
 34 
 35 // An InstanceMirrorKlass is a specialized InstanceKlass for
 36 // java.lang.Class instances.  These instances are special because
 37 // they contain the static fields of the class in addition to the
 38 // normal fields of Class.  This means they are variable sized
 39 // instances and need special logic for computing their size and for
 40 // iteration of their oops.
 41 
 42 
 43 class InstanceMirrorKlass: public InstanceKlass {
 44   friend class VMStructs;
 45   friend class InstanceKlass;
 46 
 47  public:
 48   static const KlassID ID = InstanceMirrorKlassID;
 49 
 50  private:
 51   static int _offset_of_static_fields;
 52 
 53   InstanceMirrorKlass(const ClassFileParser& parser) : InstanceKlass(parser, InstanceKlass::_misc_kind_mirror, ID) {}
 54 
 55  public:
 56   InstanceMirrorKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
 57 
 58   // Casting from Klass*
 59   static InstanceMirrorKlass* cast(Klass* k) {
 60     assert(InstanceKlass::cast(k)->is_mirror_instance_klass(),
 61            "cast to InstanceMirrorKlass");
 62     return static_cast<InstanceMirrorKlass*>(k);
 63   }
 64 
 65   // Returns the size of the instance including the extra static fields.
 66   virtual int oop_size(oop obj) const;
 67 
 68   // Static field offset is an offset into the Heap, should be converted by
 69   // based on UseCompressedOop for traversal
 70   static HeapWord* start_of_static_fields(oop obj) {
 71     return (HeapWord*)(cast_from_oop<intptr_t>(obj) + offset_of_static_fields());
 72   }
 73 
 74   static void init_offset_of_static_fields() {
 75     // Cache the offset of the static fields in the Class instance
 76     assert(_offset_of_static_fields == 0, "once");
 77     _offset_of_static_fields = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->size_helper() << LogHeapWordSize;
 78   }
 79 
 80   static int offset_of_static_fields() {
 81     return _offset_of_static_fields;
 82   }
 83 
 84   int compute_static_oop_field_count(oop obj);
 85 
 86   // Given a Klass return the size of the instance
 87   int instance_size(Klass* k);
 88 
 89   // allocation
 90   instanceOop allocate_instance(Klass* k, TRAPS);
 91 
 92   // GC specific object visitors
 93   //
 94 #if INCLUDE_PARALLELGC
 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   static void serialize_offsets(class SerializeClosure* f) NOT_CDS_RETURN;
101 
102   // Oop fields (and metadata) iterators
103   //
104   // The InstanceMirrorKlass iterators also visit the hidden Klass pointer.
105 
106   // Iterate over the static fields.
107   template <typename T, class OopClosureType>
108   inline void oop_oop_iterate_statics(oop obj, OopClosureType* closure);
109 
110   // Forward iteration
111   // Iterate over the oop fields and metadata.
112   template <typename T, class OopClosureType>
113   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
114 
115   // Reverse iteration
116   // Iterate over the oop fields and metadata.
117   template <typename T, class OopClosureType>
118   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
119 
120   // Bounded range iteration
121   // Iterate over the oop fields and metadata.
122   template <typename T, class OopClosureType>
123   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
124 
125  private:
126 
127   // Iterate over the static fields.
128   template <typename T, class OopClosureType>
129   inline void oop_oop_iterate_statics_bounded(oop obj, OopClosureType* closure, MemRegion mr);
130 };
131 
132 #endif // SHARE_VM_OOPS_INSTANCEMIRRORKLASS_HPP