1 /*
  2  * Copyright (c) 2011, 2023, 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_OOPS_INSTANCEMIRRORKLASS_HPP
 26 #define SHARE_OOPS_INSTANCEMIRRORKLASS_HPP
 27 
 28 #include "classfile/vmClasses.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 InstanceKlass;
 45 
 46  public:
 47   static const KlassKind Kind = InstanceMirrorKlassKind;
 48 
 49  private:
 50   static int _offset_of_static_fields;
 51 
 52   InstanceMirrorKlass(const ClassFileParser& parser) : InstanceKlass(parser, Kind) {}
 53 
 54   template <class OopClosureType>
 55   inline void do_metadata(oop obj, OopClosureType* closure);
 56 
 57  public:
 58   InstanceMirrorKlass();
 59 
 60   static InstanceMirrorKlass* cast(Klass* k) {
 61     return const_cast<InstanceMirrorKlass*>(cast(const_cast<const Klass*>(k)));
 62   }
 63 
 64   static const InstanceMirrorKlass* cast(const Klass* k) {
 65     assert(k->is_mirror_instance_klass(), "cast to InstanceMirrorKlass");
 66     return static_cast<const InstanceMirrorKlass*>(k);
 67   }
 68 
 69   // Returns the size of the instance including the extra static fields.
 70   size_t oop_size(oop obj, markWord mark) const;
 71   int hash_offset_in_bytes(oop obj, markWord m) const;
 72 
 73   // Static field offset is an offset into the Heap, should be converted by
 74   // based on UseCompressedOop for traversal
 75   static HeapWord* start_of_static_fields(oop obj) {
 76     return (HeapWord*)(cast_from_oop<intptr_t>(obj) + offset_of_static_fields());
 77   }
 78 
 79   static void init_offset_of_static_fields() {
 80     // Cache the offset of the static fields in the Class instance
 81     assert(_offset_of_static_fields == 0, "once");
 82     _offset_of_static_fields = InstanceMirrorKlass::cast(vmClasses::Class_klass())->size_helper() << LogHeapWordSize;
 83   }
 84 
 85   static int offset_of_static_fields() {
 86     return _offset_of_static_fields;
 87   }
 88 
 89   int compute_static_oop_field_count(oop obj);
 90 
 91   // Given a Klass return the size of the instance
 92   size_t instance_size(Klass* k);
 93 
 94   // allocation
 95   instanceOop allocate_instance(Klass* k, bool extend, TRAPS);
 96 
 97   static void serialize_offsets(class SerializeClosure* f) NOT_CDS_RETURN;
 98 
 99   // Oop fields (and metadata) iterators
100   //
101   // The InstanceMirrorKlass iterators also visit the hidden Klass pointer.
102 
103   // Iterate over the static fields.
104   template <typename T, class OopClosureType>
105   inline void oop_oop_iterate_statics(oop obj, OopClosureType* closure);
106 
107   // Forward iteration
108   // Iterate over the oop fields and metadata.
109   template <typename T, class OopClosureType>
110   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
111 
112   // Reverse iteration
113   // Iterate over the oop fields and metadata.
114   template <typename T, class OopClosureType>
115   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
116 
117   // Bounded range iteration
118   // Iterate over the oop fields and metadata.
119   template <typename T, class OopClosureType>
120   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
121 
122  private:
123 
124   // Iterate over the static fields.
125   template <typename T, class OopClosureType>
126   inline void oop_oop_iterate_statics_bounded(oop obj, OopClosureType* closure, MemRegion mr);
127 };
128 
129 #endif // SHARE_OOPS_INSTANCEMIRRORKLASS_HPP