手记

JVM源码实战 - OOP-Klass模型

1 OOP-Klass(Ordinary Object Pointer)模型

OOP-Klass模型用来描述class的属性和行为 设计为OOP和Klass两部分是因为不希望每个对象都有一个C ++ vtbl指针, 因此,普通的oops没有任何虚拟功能。 相反,他们将所有“虚拟”函数转发到它们的klass,它具有vtbl并根据对象的实际类型执行C ++调度。

1.1 OOP

oopDesc是对象类的最高父类。 {name}Desc类描述了Java对象的格式,可从C++访问这些字段

  • 路径: /hotspot/share/oops/oop.hpp

完整的类层次结构,请阅读src/hotspot/share/oops/oopsHierarchy.hpp

  • OOP体系

    1.2 Klass

  • Klass体系Klass对象提供

  • 语言级别的类对象(方法字典等)

  • 为对象提供虚拟机调度行为

class Klass : public Metadata {
  friend class VMStructs;
  friend class JVMCIVMStructs;
 protected:
  // 如果添加指向任何元数据对象的新字段,则必须将此字段添加到Klass :: metaspace_pointers_do()
  // 注意:在klass结构的起始处将常用字段放在一起,以获得更好的缓存行为(虽然可能不会有太大的区别,但可以肯定不会造成伤害)
  enum { _primary_super_limit = 8 };

  // The "layout helper" is a combined descriptor of object layout.
  // For klasses which are neither instance nor array, the value is zero.
  //
  // For instances, layout helper is a positive number, the instance size.
  // This size is already passed through align_object_size and scaled to bytes.
  // The low order bit is set if instances of this class cannot be
  // allocated using the fastpath.
  //
  // For arrays, layout helper is a negative number, containing four
  // distinct bytes, as follows:
  //    MSB:[tag, hsz, ebt, log2(esz)]:LSB
  // where:
  //    tag is 0x80 if the elements are oops, 0xC0 if non-oops
  //    hsz is array header size in bytes (i.e., offset of first element)
  //    ebt is the BasicType of the elements
  //    esz is the element size in bytes
  // This packed word is arranged so as to be quickly unpacked by the
  // various fast paths that use the various subfields.
  //
  // The esz bits can be used directly by a SLL instruction, without masking.
  //
  // Note that the array-kind tag looks like 0x00 for instance klasses,
  // since their length in bytes is always less than 24Mb.
  //
  // Final note:  This comes first, immediately after C++ vtable,
  // because it is frequently queried.
  jint        _layout_helper;

  // Klass identifier used to implement devirtualized oop closure dispatching.
  const KlassID _id;

  // The fields _super_check_offset, _secondary_super_cache, _secondary_supers
  // and _primary_supers all help make fast subtype checks.  See big discussion
  // in doc/server_compiler/checktype.txt
  //
  // Where to look to observe a supertype (it is &_secondary_super_cache for
  // secondary supers, else is &_primary_supers[depth()].
  juint       _super_check_offset;

  // 类名.  Instance classes: java/lang/String, etc.  Array classes: [I,
  // [Ljava/lang/String;, etc.  Set to zero for all other kinds of classes.
  Symbol*     _name;

  // Cache of last observed secondary supertype
  Klass*      _secondary_super_cache;
  // Array of all secondary supertypes
  Array<Klass*>* _secondary_supers;
  // Ordered list of all primary supertypes
  Klass*      _primary_supers[_primary_super_limit];
  // java/lang/Class instance mirroring this class
  OopHandle _java_mirror;
  // Superclass
  Klass*      _super;
  // First subclass (NULL if none); _subklass->next_sibling() is next one
  Klass* volatile _subklass;
  // Sibling link (or NULL); links all subklasses of a klass
  Klass* volatile _next_sibling;

  // All klasses loaded by a class loader are chained through these links
  Klass*      _next_link;

  // 用于加载此类的VM对类加载器的表示。
  //提供访问相应的java.lang.ClassLoader实例
  ClassLoaderData* _class_loader_data;

  jint        _modifier_flags;  // 处理的访问标志,由Class.getModifiers使用
  AccessFlags _access_flags;    // 访问标志。 类/接口的区别就存储在这里

  JFR_ONLY(DEFINE_TRACE_ID_FIELD;)

  // 偏向锁定实现和统计
  // 64位块优先,以避免碎片
  jlong    _last_biased_lock_bulk_revocation_time;
  markOop  _prototype_header;   // Used when biased locking is both enabled and disabled for this type
  jint     _biased_lock_revocation_count;

  // 虚表长度
  int _vtable_len;
  ...
0人推荐
随时随地看视频
慕课网APP