< prev index next >

src/hotspot/share/oops/klass.hpp

Print this page

  1 /*
  2  * Copyright (c) 1997, 2025, 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  *

 35 #include "jfr/support/jfrTraceIdExtension.hpp"
 36 #endif
 37 
 38 //
 39 // A Klass provides:
 40 //  1: language level class object (method dictionary etc.)
 41 //  2: provide vm dispatch behavior for the object
 42 // Both functions are combined into one C++ class.
 43 
 44 // One reason for the oop/klass dichotomy in the implementation is
 45 // that we don't want a C++ vtbl pointer in every object.  Thus,
 46 // normal oops don't have any virtual functions.  Instead, they
 47 // forward all "virtual" functions to their klass, which does have
 48 // a vtbl and does the C++ dispatch depending on the object's
 49 // actual type.  (See oop.inline.hpp for some of the forwarding code.)
 50 // ALL FUNCTIONS IMPLEMENTING THIS DISPATCH ARE PREFIXED WITH "oop_"!
 51 
 52 // Forward declarations.
 53 template <class T> class Array;
 54 template <class T> class GrowableArray;

 55 class ClassLoaderData;
 56 class fieldDescriptor;
 57 class klassVtable;
 58 class ModuleEntry;
 59 class PackageEntry;
 60 class vtableEntry;
 61 
 62 class Klass : public Metadata {
 63 
 64   friend class VMStructs;
 65   friend class JVMCIVMStructs;
 66  public:
 67   // Klass Kinds for all subclasses of Klass
 68   enum KlassKind : u2 {
 69     InstanceKlassKind,
 70     InstanceRefKlassKind,
 71     InstanceMirrorKlassKind,
 72     InstanceClassLoaderKlassKind,
 73     InstanceStackChunkKlassKind,
 74     TypeArrayKlassKind,
 75     ObjArrayKlassKind,
 76     UnknownKlassKind
 77   };
 78 
 79   static const uint KLASS_KIND_COUNT = ObjArrayKlassKind + 1;




 80  protected:
 81 
 82   // If you add a new field that points to any metaspace object, you
 83   // must add this field to Klass::metaspace_pointers_do().
 84 
 85   // note: put frequently-used fields together at start of klass structure
 86   // for better cache behavior (may not make much of a difference but sure won't hurt)
 87   enum { _primary_super_limit = 8 };
 88 
 89   // The "layout helper" is a combined descriptor of object layout.
 90   // For klasses which are neither instance nor array, the value is zero.
 91   //
 92   // For instances, layout helper is a positive number, the instance size.
 93   // This size is already passed through align_object_size and scaled to bytes.
 94   // The low order bit is set if instances of this class cannot be
 95   // allocated using the fastpath.
 96   //
 97   // For arrays, layout helper is a negative number, containing four
 98   // distinct bytes, as follows:
 99   //    MSB:[tag, hsz, ebt, log2(esz)]:LSB
100   // where:
101   //    tag is 0x80 if the elements are oops, 0xC0 if non-oops
102   //    hsz is array header size in bytes (i.e., offset of first element)
103   //    ebt is the BasicType of the elements
104   //    esz is the element size in bytes
105   // This packed word is arranged so as to be quickly unpacked by the
106   // various fast paths that use the various subfields.
107   //
108   // The esz bits can be used directly by a SLL instruction, without masking.
109   //
110   // Note that the array-kind tag looks like 0x00 for instance klasses,
111   // since their length in bytes is always less than 24Mb.
112   //
113   // Final note:  This comes first, immediately after C++ vtable,
114   // because it is frequently queried.
115   jint _layout_helper;
116 
117   // Klass kind used to resolve the runtime type of the instance.
118   //  - Used to implement devirtualized oop closure dispatching.
119   //  - Various type checking in the JVM
120   const KlassKind _kind;
121 

185                                                      // no further need to call <clinit>
186     _has_aot_safe_initializer              = 1 << 7, // has @AOTSafeClassInitializer annotation
187     _is_runtime_setup_required             = 1 << 8, // has a runtimeSetup method to be called when
188                                                      // this class is loaded from AOT cache
189   };
190 #endif
191 
192   int _vtable_len;              // vtable length. This field may be read very often when we
193                                 // have lots of itable dispatches (e.g., lambdas and streams).
194                                 // Keep it away from the beginning of a Klass to avoid cacheline
195                                 // contention that may happen when a nearby object is modified.
196 
197   CDS_JAVA_HEAP_ONLY(int _archived_mirror_index;)
198 
199 public:
200 
201   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
202 
203 protected:
204 
205   Klass(KlassKind kind);
206   Klass();
207 
208   void* operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw();
209 
210  public:
211   int kind() { return _kind; }
212 
213   enum class DefaultsLookupMode { find, skip };
214   enum class OverpassLookupMode { find, skip };
215   enum class StaticLookupMode   { find, skip };
216   enum class PrivateLookupMode  { find, skip };
217 
218   bool is_klass() const override { return true; }
219 
220   // super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
221   // to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
222   // If this is not what your code expects, you're probably looking for:
223   // - Klass::java_super() - if you have a Klass*
224   // - InstanceKlass::super() - if you have an InstanceKlass* ik, ik->super() returns InstanceKlass*.
225   Klass* super() const               { return _super; }

452   static ByteSize class_loader_data_offset()     { return byte_offset_of(Klass, _class_loader_data); }
453   static ByteSize layout_helper_offset()         { return byte_offset_of(Klass, _layout_helper); }
454 #if INCLUDE_JVMCI
455   static ByteSize subklass_offset()              { return byte_offset_of(Klass, _subklass); }
456   static ByteSize next_sibling_offset()          { return byte_offset_of(Klass, _next_sibling); }
457 #endif
458   static ByteSize secondary_supers_bitmap_offset()
459                                                  { return byte_offset_of(Klass, _secondary_supers_bitmap); }
460   static ByteSize hash_slot_offset()             { return byte_offset_of(Klass, _hash_slot); }
461   static ByteSize misc_flags_offset()            { return byte_offset_of(Klass, _misc_flags._flags); }
462 
463   // Unpacking layout_helper:
464   static const int _lh_neutral_value           = 0;  // neutral non-array non-instance value
465   static const int _lh_instance_slow_path_bit  = 0x01;
466   static const int _lh_log2_element_size_shift = BitsPerByte*0;
467   static const int _lh_log2_element_size_mask  = BitsPerLong-1;
468   static const int _lh_element_type_shift      = BitsPerByte*1;
469   static const int _lh_element_type_mask       = right_n_bits(BitsPerByte);  // shifted mask
470   static const int _lh_header_size_shift       = BitsPerByte*2;
471   static const int _lh_header_size_mask        = right_n_bits(BitsPerByte);  // shifted mask
472   static const int _lh_array_tag_bits          = 2;
473   static const int _lh_array_tag_shift         = BitsPerInt - _lh_array_tag_bits;
474   static const int _lh_array_tag_obj_value     = ~0x01;   // 0x80000000 >> 30
475 
476   static const unsigned int _lh_array_tag_type_value = 0Xffffffff; // ~0x00,  // 0xC0000000 >> 30








477 
478   static int layout_helper_size_in_bytes(jint lh) {
479     assert(lh > (jint)_lh_neutral_value, "must be instance");
480     return (int) lh & ~_lh_instance_slow_path_bit;
481   }
482   static bool layout_helper_needs_slow_path(jint lh) {
483     assert(lh > (jint)_lh_neutral_value, "must be instance");
484     return (lh & _lh_instance_slow_path_bit) != 0;
485   }
486   static bool layout_helper_is_instance(jint lh) {
487     return (jint)lh > (jint)_lh_neutral_value;
488   }
489   static bool layout_helper_is_array(jint lh) {
490     return (jint)lh < (jint)_lh_neutral_value;
491   }
492   static bool layout_helper_is_typeArray(jint lh) {
493     // _lh_array_tag_type_value == (lh >> _lh_array_tag_shift);
494     return (juint)lh >= (juint)(_lh_array_tag_type_value << _lh_array_tag_shift);


495   }
496   static bool layout_helper_is_objArray(jint lh) {
497     // _lh_array_tag_obj_value == (lh >> _lh_array_tag_shift);
498     return (jint)lh < (jint)(_lh_array_tag_type_value << _lh_array_tag_shift);








499   }
500   static int layout_helper_header_size(jint lh) {
501     assert(lh < (jint)_lh_neutral_value, "must be array");
502     int hsize = (lh >> _lh_header_size_shift) & _lh_header_size_mask;
503     assert(hsize > 0 && hsize < (int)sizeof(oopDesc)*3, "sanity");
504     return hsize;
505   }
506   static BasicType layout_helper_element_type(jint lh) {
507     assert(lh < (jint)_lh_neutral_value, "must be array");
508     int btvalue = (lh >> _lh_element_type_shift) & _lh_element_type_mask;
509     assert(btvalue >= T_BOOLEAN && btvalue <= T_OBJECT, "sanity");
510     return (BasicType) btvalue;
511   }
512 
513   // Return a value containing a single set bit that is in the bitset difference between the
514   // layout helpers for array-of-boolean and array-of-byte.
515   static int layout_helper_boolean_diffbit() {
516     uint zlh = static_cast<uint>(array_layout_helper(T_BOOLEAN));
517     uint blh = static_cast<uint>(array_layout_helper(T_BYTE));
518     // get all the bits that are set in zlh and clear in blh
519     uint candidates = (zlh & ~blh);
520     assert(candidates != 0, "must be"); // must be some if there is a solution.
521     // Use well known bit hack to isolate the low bit of candidates.
522     uint result = candidates & (-candidates);
523     assert(is_power_of_2(result), "must be power of 2");
524     assert((result & zlh) != 0, "must be set in alh of T_BOOLEAN");
525     assert((result & blh) == 0, "must be clear in alh of T_BYTE");
526     return static_cast<int>(result);
527   }
528 
529   static int layout_helper_log2_element_size(jint lh) {
530     assert(lh < (jint)_lh_neutral_value, "must be array");
531     int l2esz = (lh >> _lh_log2_element_size_shift) & _lh_log2_element_size_mask;
532     assert(l2esz <= LogBytesPerLong,
533            "sanity. l2esz: 0x%x for lh: 0x%x", (uint)l2esz, (uint)lh);
534     return l2esz;
535   }
536   static jint array_layout_helper(jint tag, int hsize, BasicType etype, int log2_esize) {
537     return (tag        << _lh_array_tag_shift)

538       |    (hsize      << _lh_header_size_shift)
539       |    ((int)etype << _lh_element_type_shift)
540       |    (log2_esize << _lh_log2_element_size_shift);
541   }
542   static jint instance_layout_helper(jint size, bool slow_path_flag) {
543     return (size << LogBytesPerWord)
544       |    (slow_path_flag ? _lh_instance_slow_path_bit : 0);
545   }
546   static int layout_helper_to_size_helper(jint lh) {
547     assert(lh > (jint)_lh_neutral_value, "must be instance");
548     // Note that the following expression discards _lh_instance_slow_path_bit.
549     return lh >> LogBytesPerWord;
550   }
551   // Out-of-line version computes everything based on the etype:
552   static jint array_layout_helper(BasicType etype);
553 
554   // What is the maximum number of primary superclasses any klass can have?
555   static juint primary_super_limit()         { return _primary_super_limit; }
556 
557   // vtables

658   const char* external_name() const;
659   // Returns the name for a class (Resource allocated) as the class
660   // would appear in a signature.
661   // For arrays, this returns the name of the element with a leading '['.
662   // For classes, this returns the name with a leading 'L' and a trailing ';'
663   //     and the package separators as '/'.
664   virtual const char* signature_name() const;
665 
666   const char* joint_in_module_of_loader(const Klass* class2, bool include_parent_loader = false) const;
667   const char* class_in_module_of_loader(bool use_are = false, bool include_parent_loader = false) const;
668 
669   // Returns "interface", "abstract class" or "class".
670   const char* external_kind() const;
671 
672   // type testing operations
673 #ifdef ASSERT
674  protected:
675   virtual bool is_instance_klass_slow()     const { return false; }
676   virtual bool is_array_klass_slow()        const { return false; }
677   virtual bool is_objArray_klass_slow()     const { return false; }

678   virtual bool is_typeArray_klass_slow()    const { return false; }

679 #endif // ASSERT


680  public:
681 
682   // Fast non-virtual versions
683   #ifndef ASSERT
684   #define assert_same_query(xval, xcheck) xval
685   #else
686  private:
687   static bool assert_same_query(bool xval, bool xslow) {
688     assert(xval == xslow, "slow and fast queries agree");
689     return xval;
690   }
691  public:
692   #endif
693 
694   bool is_instance_klass()              const { return assert_same_query(_kind <= InstanceStackChunkKlassKind, is_instance_klass_slow()); }
695   // Other is anything that is not one of the more specialized kinds of InstanceKlass.
696   bool is_other_instance_klass()        const { return _kind == InstanceKlassKind; }
697   bool is_reference_instance_klass()    const { return _kind == InstanceRefKlassKind; }
698   bool is_mirror_instance_klass()       const { return _kind == InstanceMirrorKlassKind; }
699   bool is_class_loader_instance_klass() const { return _kind == InstanceClassLoaderKlassKind; }
700   bool is_array_klass()                 const { return assert_same_query( _kind >= TypeArrayKlassKind, is_array_klass_slow()); }
701   bool is_stack_chunk_instance_klass()  const { return _kind == InstanceStackChunkKlassKind; }
702   bool is_objArray_klass()              const { return assert_same_query( _kind == ObjArrayKlassKind,  is_objArray_klass_slow()); }


703   bool is_typeArray_klass()             const { return assert_same_query( _kind == TypeArrayKlassKind, is_typeArray_klass_slow()); }

704   #undef assert_same_query
705 


706 
707   virtual bool is_interface() const     { return false; }
708   virtual bool is_abstract() const      { return false; }

709 
710   bool has_finalizer() const            { return _misc_flags.has_finalizer(); }
711   void set_has_finalizer()              { _misc_flags.set_has_finalizer(true); }
712   bool is_hidden() const                { return _misc_flags.is_hidden_class(); }
713   void set_is_hidden()                  { _misc_flags.set_is_hidden_class(true); }
714   bool is_value_based() const           { return _misc_flags.is_value_based_class(); }
715   void set_is_value_based()             { _misc_flags.set_is_value_based_class(true); }
716 
717   klass_flags_t misc_flags() const      { return _misc_flags.value(); }
718 
719   inline bool is_non_strong_hidden() const;
720 
721   bool is_cloneable() const;
722   void set_is_cloneable_fast() { _misc_flags.set_is_cloneable_fast(true); }
723 

724   inline markWord prototype_header() const;
725   inline void set_prototype_header(markWord header);
726   static ByteSize prototype_header_offset() { return in_ByteSize(offset_of(Klass, _prototype_header)); }

727 
728   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
729 
730   void metaspace_pointers_do(MetaspaceClosure* iter) override;
731   MetaspaceObj::Type type() const override { return ClassType; }
732 
733   inline bool is_loader_alive() const;
734   inline bool is_loader_present_and_alive() const;
735 
736   Klass* clean_subklass(bool log = false);
737 
738   // Clean out unnecessary weak klass links from the whole klass hierarchy.
739   static void clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses = true);
740   // Clean out unnecessary weak klass links from the given InstanceKlass.
741   static void clean_weak_instanceklass_links(InstanceKlass* ik);
742 
743   // Return self, except for abstract classes with exactly 1
744   // implementor.  Then return the 1 concrete implementation.
745   Klass *up_cast_abstract();
746 

763   virtual jint jvmti_class_status() const;
764 
765   // Printing
766   void print_on(outputStream* st) const override;
767 
768   virtual void oop_print_value_on(oop obj, outputStream* st);
769   virtual void oop_print_on      (oop obj, outputStream* st);
770 
771   void print_secondary_supers_on(outputStream* st) const;
772 
773   // Verification
774   virtual void verify_on(outputStream* st);
775   void verify() { verify_on(tty); }
776 
777 #ifndef PRODUCT
778   bool verify_vtable_index(int index);
779 #endif
780 
781   virtual void oop_verify_on(oop obj, outputStream* st);
782 


783   // for error reporting
784   static bool is_valid(Klass* k);
785 
786   static void on_secondary_supers_verification_failure(Klass* super, Klass* sub, bool linear_result, bool table_result, const char* msg);
787 };
788 
789 #endif // SHARE_OOPS_KLASS_HPP

  1 /*
  2  * Copyright (c) 1997, 2026, 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  *

 35 #include "jfr/support/jfrTraceIdExtension.hpp"
 36 #endif
 37 
 38 //
 39 // A Klass provides:
 40 //  1: language level class object (method dictionary etc.)
 41 //  2: provide vm dispatch behavior for the object
 42 // Both functions are combined into one C++ class.
 43 
 44 // One reason for the oop/klass dichotomy in the implementation is
 45 // that we don't want a C++ vtbl pointer in every object.  Thus,
 46 // normal oops don't have any virtual functions.  Instead, they
 47 // forward all "virtual" functions to their klass, which does have
 48 // a vtbl and does the C++ dispatch depending on the object's
 49 // actual type.  (See oop.inline.hpp for some of the forwarding code.)
 50 // ALL FUNCTIONS IMPLEMENTING THIS DISPATCH ARE PREFIXED WITH "oop_"!
 51 
 52 // Forward declarations.
 53 template <class T> class Array;
 54 template <class T> class GrowableArray;
 55 class ArrayDescription;
 56 class ClassLoaderData;
 57 class fieldDescriptor;
 58 class klassVtable;
 59 class ModuleEntry;
 60 class PackageEntry;
 61 class vtableEntry;
 62 
 63 class Klass : public Metadata {
 64 
 65   friend class VMStructs;
 66   friend class JVMCIVMStructs;
 67  public:
 68   // Klass Kinds for all subclasses of Klass
 69    enum KlassKind : u2
 70    {
 71      InstanceKlassKind,
 72      InlineKlassKind,
 73      InstanceRefKlassKind,
 74      InstanceMirrorKlassKind,
 75      InstanceClassLoaderKlassKind,
 76      InstanceStackChunkKlassKind,
 77      TypeArrayKlassKind,
 78      ObjArrayKlassKind,
 79      RefArrayKlassKind,
 80      FlatArrayKlassKind,
 81      UnknownKlassKind
 82    };
 83 
 84    static const uint KLASS_KIND_COUNT = FlatArrayKlassKind + 1;
 85  protected:
 86 
 87   // If you add a new field that points to any metaspace object, you
 88   // must add this field to Klass::metaspace_pointers_do().
 89 
 90   // note: put frequently-used fields together at start of klass structure
 91   // for better cache behavior (may not make much of a difference but sure won't hurt)
 92   enum { _primary_super_limit = 8 };
 93 
 94   // The "layout helper" is a combined descriptor of object layout.
 95   // For klasses which are neither instance nor array, the value is zero.
 96   //
 97   // For instances, layout helper is a positive number, the instance size.
 98   // This size is already passed through align_object_size and scaled to bytes.
 99   // The low order bit is set if instances of this class cannot be
100   // allocated using the fastpath.
101   //
102   // For arrays, layout helper is a negative number, containing four
103   // distinct bytes, as follows:
104   //    MSB:[tag, hsz, ebt, log2(esz)]:LSB
105   // where:
106   //    tag is 0x80 if the elements are oops, 0xC0 if non-oops, 0xA0 if value types
107   //    hsz is array header size in bytes (i.e., offset of first element)
108   //    ebt is the BasicType of the elements
109   //    esz is the element size in bytes
110   // This packed word is arranged so as to be quickly unpacked by the
111   // various fast paths that use the various subfields.
112   //
113   // The esz bits can be used directly by a SLL instruction, without masking.
114   //
115   // Note that the array-kind tag looks like 0x00 for instance klasses,
116   // since their length in bytes is always less than 24Mb.
117   //
118   // Final note:  This comes first, immediately after C++ vtable,
119   // because it is frequently queried.
120   jint _layout_helper;
121 
122   // Klass kind used to resolve the runtime type of the instance.
123   //  - Used to implement devirtualized oop closure dispatching.
124   //  - Various type checking in the JVM
125   const KlassKind _kind;
126 

190                                                      // no further need to call <clinit>
191     _has_aot_safe_initializer              = 1 << 7, // has @AOTSafeClassInitializer annotation
192     _is_runtime_setup_required             = 1 << 8, // has a runtimeSetup method to be called when
193                                                      // this class is loaded from AOT cache
194   };
195 #endif
196 
197   int _vtable_len;              // vtable length. This field may be read very often when we
198                                 // have lots of itable dispatches (e.g., lambdas and streams).
199                                 // Keep it away from the beginning of a Klass to avoid cacheline
200                                 // contention that may happen when a nearby object is modified.
201 
202   CDS_JAVA_HEAP_ONLY(int _archived_mirror_index;)
203 
204 public:
205 
206   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
207 
208 protected:
209 
210   Klass(KlassKind kind, markWord prototype_header = markWord::prototype());
211   Klass();
212 
213   void* operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw();
214 
215  public:
216   int kind() { return _kind; }
217 
218   enum class DefaultsLookupMode { find, skip };
219   enum class OverpassLookupMode { find, skip };
220   enum class StaticLookupMode   { find, skip };
221   enum class PrivateLookupMode  { find, skip };
222 
223   bool is_klass() const override { return true; }
224 
225   // super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
226   // to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
227   // If this is not what your code expects, you're probably looking for:
228   // - Klass::java_super() - if you have a Klass*
229   // - InstanceKlass::super() - if you have an InstanceKlass* ik, ik->super() returns InstanceKlass*.
230   Klass* super() const               { return _super; }

457   static ByteSize class_loader_data_offset()     { return byte_offset_of(Klass, _class_loader_data); }
458   static ByteSize layout_helper_offset()         { return byte_offset_of(Klass, _layout_helper); }
459 #if INCLUDE_JVMCI
460   static ByteSize subklass_offset()              { return byte_offset_of(Klass, _subklass); }
461   static ByteSize next_sibling_offset()          { return byte_offset_of(Klass, _next_sibling); }
462 #endif
463   static ByteSize secondary_supers_bitmap_offset()
464                                                  { return byte_offset_of(Klass, _secondary_supers_bitmap); }
465   static ByteSize hash_slot_offset()             { return byte_offset_of(Klass, _hash_slot); }
466   static ByteSize misc_flags_offset()            { return byte_offset_of(Klass, _misc_flags._flags); }
467 
468   // Unpacking layout_helper:
469   static const int _lh_neutral_value           = 0;  // neutral non-array non-instance value
470   static const int _lh_instance_slow_path_bit  = 0x01;
471   static const int _lh_log2_element_size_shift = BitsPerByte*0;
472   static const int _lh_log2_element_size_mask  = BitsPerLong-1;
473   static const int _lh_element_type_shift      = BitsPerByte*1;
474   static const int _lh_element_type_mask       = right_n_bits(BitsPerByte);  // shifted mask
475   static const int _lh_header_size_shift       = BitsPerByte*2;
476   static const int _lh_header_size_mask        = right_n_bits(BitsPerByte);  // shifted mask
477   static const int _lh_array_tag_bits          = 4;
478   static const int _lh_array_tag_shift         = BitsPerInt - _lh_array_tag_bits;

479 
480   static const unsigned int _lh_array_tag_type_value = 0Xfffffffc;
481   static const unsigned int _lh_array_tag_flat_value = 0Xfffffffa;
482   static const unsigned int _lh_array_tag_ref_value  = 0Xfffffff8;
483 
484   // null-free array flag bit under the array tag bits, shift one more to get array tag value
485   static const int _lh_null_free_shift = _lh_array_tag_shift - 1;
486   static const int _lh_null_free_mask  = 1;
487 
488   static const jint _lh_array_tag_flat_value_bit_inplace = (jint) (1 << (_lh_array_tag_shift + 1));
489 
490   static int layout_helper_size_in_bytes(jint lh) {
491     assert(lh > (jint)_lh_neutral_value, "must be instance");
492     return (int) lh & ~_lh_instance_slow_path_bit;
493   }
494   static bool layout_helper_needs_slow_path(jint lh) {
495     assert(lh > (jint)_lh_neutral_value, "must be instance");
496     return (lh & _lh_instance_slow_path_bit) != 0;
497   }
498   static bool layout_helper_is_instance(jint lh) {
499     return (jint)lh > (jint)_lh_neutral_value;
500   }
501   static bool layout_helper_is_array(jint lh) {
502     return (jint)lh < (jint)_lh_neutral_value;
503   }
504   static bool layout_helper_is_typeArray(jint lh) {
505     return (juint) _lh_array_tag_type_value == (juint)(lh >> _lh_array_tag_shift);
506   }
507   static bool layout_helper_is_refArray(jint lh) {
508     return (juint)_lh_array_tag_ref_value == (juint)(lh >> _lh_array_tag_shift);
509   }
510   static bool layout_helper_is_flatArray(jint lh) {
511     return (juint)_lh_array_tag_flat_value == (juint)(lh >> _lh_array_tag_shift);
512   }
513   static bool layout_helper_is_null_free(jint lh) {
514     assert(layout_helper_is_flatArray(lh) || layout_helper_is_refArray(lh), "must be array of inline types");
515     return ((lh >> _lh_null_free_shift) & _lh_null_free_mask);
516   }
517   static jint layout_helper_set_null_free(jint lh) {
518     lh |= (_lh_null_free_mask << _lh_null_free_shift);
519     assert(layout_helper_is_null_free(lh), "Bad encoding");
520     return lh;
521   }
522   static int layout_helper_header_size(jint lh) {
523     assert(lh < (jint)_lh_neutral_value, "must be array");
524     int hsize = (lh >> _lh_header_size_shift) & _lh_header_size_mask;
525     assert(hsize > 0 && hsize < (int)sizeof(oopDesc)*3, "sanity");
526     return hsize;
527   }
528   static BasicType layout_helper_element_type(jint lh) {
529     assert(lh < (jint)_lh_neutral_value, "must be array");
530     int btvalue = (lh >> _lh_element_type_shift) & _lh_element_type_mask;
531     assert((btvalue >= T_BOOLEAN && btvalue <= T_OBJECT) || btvalue == T_FLAT_ELEMENT, "sanity");
532     return (BasicType) btvalue;
533   }
534 
535   // Return a value containing a single set bit that is in the bitset difference between the
536   // layout helpers for array-of-boolean and array-of-byte.
537   static int layout_helper_boolean_diffbit() {
538     uint zlh = static_cast<uint>(array_layout_helper(T_BOOLEAN));
539     uint blh = static_cast<uint>(array_layout_helper(T_BYTE));
540     // get all the bits that are set in zlh and clear in blh
541     uint candidates = (zlh & ~blh);
542     assert(candidates != 0, "must be"); // must be some if there is a solution.
543     // Use well known bit hack to isolate the low bit of candidates.
544     uint result = candidates & (-candidates);
545     assert(is_power_of_2(result), "must be power of 2");
546     assert((result & zlh) != 0, "must be set in alh of T_BOOLEAN");
547     assert((result & blh) == 0, "must be clear in alh of T_BYTE");
548     return static_cast<int>(result);
549   }
550 
551   static int layout_helper_log2_element_size(jint lh) {
552     assert(lh < (jint)_lh_neutral_value, "must be array");
553     int l2esz = (lh >> _lh_log2_element_size_shift) & _lh_log2_element_size_mask;
554     assert(layout_helper_element_type(lh) == T_FLAT_ELEMENT || l2esz <= LogBytesPerLong,
555            "sanity. l2esz: 0x%x for lh: 0x%x", (uint)l2esz, (uint)lh);
556     return l2esz;
557   }
558   static jint array_layout_helper(jint tag, bool null_free, int hsize, BasicType etype, int log2_esize) {
559     return (tag        << _lh_array_tag_shift)
560       |    ((null_free ? 1 : 0) <<  _lh_null_free_shift)
561       |    (hsize      << _lh_header_size_shift)
562       |    ((int)etype << _lh_element_type_shift)
563       |    (log2_esize << _lh_log2_element_size_shift);
564   }
565   static jint instance_layout_helper(jint size, bool slow_path_flag) {
566     return (size << LogBytesPerWord)
567       |    (slow_path_flag ? _lh_instance_slow_path_bit : 0);
568   }
569   static int layout_helper_to_size_helper(jint lh) {
570     assert(lh > (jint)_lh_neutral_value, "must be instance");
571     // Note that the following expression discards _lh_instance_slow_path_bit.
572     return lh >> LogBytesPerWord;
573   }
574   // Out-of-line version computes everything based on the etype:
575   static jint array_layout_helper(BasicType etype);
576 
577   // What is the maximum number of primary superclasses any klass can have?
578   static juint primary_super_limit()         { return _primary_super_limit; }
579 
580   // vtables

681   const char* external_name() const;
682   // Returns the name for a class (Resource allocated) as the class
683   // would appear in a signature.
684   // For arrays, this returns the name of the element with a leading '['.
685   // For classes, this returns the name with a leading 'L' and a trailing ';'
686   //     and the package separators as '/'.
687   virtual const char* signature_name() const;
688 
689   const char* joint_in_module_of_loader(const Klass* class2, bool include_parent_loader = false) const;
690   const char* class_in_module_of_loader(bool use_are = false, bool include_parent_loader = false) const;
691 
692   // Returns "interface", "abstract class" or "class".
693   const char* external_kind() const;
694 
695   // type testing operations
696 #ifdef ASSERT
697  protected:
698   virtual bool is_instance_klass_slow()     const { return false; }
699   virtual bool is_array_klass_slow()        const { return false; }
700   virtual bool is_objArray_klass_slow()     const { return false; }
701   virtual bool is_refArray_klass_slow()     const { return false; }
702   virtual bool is_typeArray_klass_slow()    const { return false; }
703   virtual bool is_flatArray_klass_slow()    const { return false; }
704 #endif // ASSERT
705   // current implementation uses this method even in non debug builds
706   virtual bool is_inline_klass_slow()       const { return false; }
707  public:
708 
709   // Fast non-virtual versions
710   #ifndef ASSERT
711   #define assert_same_query(xval, xcheck) xval
712   #else
713  private:
714   static bool assert_same_query(bool xval, bool xslow) {
715     assert(xval == xslow, "slow and fast queries agree");
716     return xval;
717   }
718  public:
719   #endif
720 
721   bool is_instance_klass()              const { return assert_same_query(_kind <= InstanceStackChunkKlassKind, is_instance_klass_slow()); }
722   bool is_inline_klass()                const { return assert_same_query(_kind == InlineKlassKind, is_inline_klass_slow()); }

723   bool is_reference_instance_klass()    const { return _kind == InstanceRefKlassKind; }
724   bool is_mirror_instance_klass()       const { return _kind == InstanceMirrorKlassKind; }
725   bool is_class_loader_instance_klass() const { return _kind == InstanceClassLoaderKlassKind; }
726   bool is_array_klass()                 const { return assert_same_query( _kind >= TypeArrayKlassKind, is_array_klass_slow()); }
727   bool is_stack_chunk_instance_klass()  const { return _kind == InstanceStackChunkKlassKind; }
728   bool is_flatArray_klass()             const { return assert_same_query( _kind == FlatArrayKlassKind, is_flatArray_klass_slow()); }
729   bool is_objArray_klass()              const { return assert_same_query( _kind == ObjArrayKlassKind || _kind == RefArrayKlassKind || _kind == FlatArrayKlassKind,  is_objArray_klass_slow()); }
730   bool is_refArray_klass()              const { return assert_same_query( _kind == RefArrayKlassKind, is_refArray_klass_slow()); }
731   bool is_typeArray_klass()             const { return assert_same_query( _kind == TypeArrayKlassKind, is_typeArray_klass_slow()); }
732   bool is_refined_objArray_klass()      const { return is_refArray_klass() || is_flatArray_klass(); }
733   #undef assert_same_query
734 
735   inline bool is_null_free_array_klass() const { return !is_typeArray_klass() && layout_helper_is_null_free(layout_helper()); }
736 
737 
738   virtual bool is_interface() const     { return false; }
739   virtual bool is_abstract() const      { return false; }
740   virtual bool is_identity_class() const { return false; }
741 
742   bool has_finalizer() const            { return _misc_flags.has_finalizer(); }
743   void set_has_finalizer()              { _misc_flags.set_has_finalizer(true); }
744   bool is_hidden() const                { return _misc_flags.is_hidden_class(); }
745   void set_is_hidden()                  { _misc_flags.set_is_hidden_class(true); }
746   bool is_value_based() const           { return _misc_flags.is_value_based_class(); }
747   void set_is_value_based()             { _misc_flags.set_is_value_based_class(true); }
748 
749   klass_flags_t misc_flags() const      { return _misc_flags.value(); }
750 
751   inline bool is_non_strong_hidden() const;
752 
753   bool is_cloneable() const;
754   void set_is_cloneable_fast() { _misc_flags.set_is_cloneable_fast(true); }
755 
756   static inline markWord make_prototype_header(const Klass* kls, markWord prototype = markWord::prototype());
757   inline markWord prototype_header() const;
758   inline void set_prototype_header(markWord header);
759   static ByteSize prototype_header_offset() { return in_ByteSize(offset_of(Klass, _prototype_header)); }
760   inline void set_prototype_header_klass(narrowKlass klass);
761 
762   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
763 
764   void metaspace_pointers_do(MetaspaceClosure* iter) override;
765   MetaspaceObj::Type type() const override { return ClassType; }
766 
767   inline bool is_loader_alive() const;
768   inline bool is_loader_present_and_alive() const;
769 
770   Klass* clean_subklass(bool log = false);
771 
772   // Clean out unnecessary weak klass links from the whole klass hierarchy.
773   static void clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses = true);
774   // Clean out unnecessary weak klass links from the given InstanceKlass.
775   static void clean_weak_instanceklass_links(InstanceKlass* ik);
776 
777   // Return self, except for abstract classes with exactly 1
778   // implementor.  Then return the 1 concrete implementation.
779   Klass *up_cast_abstract();
780 

797   virtual jint jvmti_class_status() const;
798 
799   // Printing
800   void print_on(outputStream* st) const override;
801 
802   virtual void oop_print_value_on(oop obj, outputStream* st);
803   virtual void oop_print_on      (oop obj, outputStream* st);
804 
805   void print_secondary_supers_on(outputStream* st) const;
806 
807   // Verification
808   virtual void verify_on(outputStream* st);
809   void verify() { verify_on(tty); }
810 
811 #ifndef PRODUCT
812   bool verify_vtable_index(int index);
813 #endif
814 
815   virtual void oop_verify_on(oop obj, outputStream* st);
816 
817   void validate_array_description(const ArrayDescription& ad) NOT_DEBUG_RETURN;
818 
819   // for error reporting
820   static bool is_valid(Klass* k);
821 
822   static void on_secondary_supers_verification_failure(Klass* super, Klass* sub, bool linear_result, bool table_result, const char* msg);
823 };
824 
825 #endif // SHARE_OOPS_KLASS_HPP
< prev index next >