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圖片描述


  • OOP體系圖片描述

    1.2 Klass

  • Klass體系圖片描述Klass物件提供

  • 語言級別的類物件(方法字典等)

  • 為物件提供虛擬機器排程行為

class Klass : public Metadata {
  friend class VMStructs;
  friend class JVMCIVMStructs;
  // 如果新增指向任何後設資料物件的新欄位,則必須將此欄位新增到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對類載入器的表示。
  ClassLoaderData* _class_loader_data;

  jint        _modifier_flags;  // 處理的訪問標誌,由Class.getModifiers使用
  AccessFlags _access_flags;    // 訪問標誌。 類/介面的區別就儲存在這裡


  // 偏向鎖定實現和統計
  // 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;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2157/viewspace-2824350/,如需轉載,請註明出處,否則將追究法律責任。