• 【jvm源码】-2.对象核心源码


    对象核心源码

    各位道友配上上面synchroized的源码,继续卷一段相关知识,这个是最常见的对象内容。

    作为java开发天天面向对象开发,但是对象到底是啥?好像真没见过具体长什么样子,之前看《深入理解jvm虚拟机》中有讲到java对象组成部分,对象头,实例数据,对齐填充,当时只是记住了,并不知道具体做了什么,下面我们来卷一卷:

    oop

    官方描述
    // oopDesc is the top baseclass for objects classes.  The {name}Desc classes describe
    // the format of Java objects so the fields can be accessed from C++.
    // oopDesc is abstract.
    // (see oopHierarchy for complete oop class hierarchy)
    
    • 1
    • 2
    • 3
    • 4

    oopDesc 是对象类的最基础根类,通过{name}Desc 类描述java对象的格式,能够让c++获取字段对应值。

    oopDesc是抽象的

    oop类完整的层次关系请看oopHierarchy。

    通过官方描述可知道,oop是根类,且是抽象的 ;其中还提到层次关系,类似java多态的结构,可通过oopHierarchy知道。

    源码

    oop.hpp

    class oopDesc {
      friend class VMStructs;
     private://对象头
      volatile markOop  _mark; 
      
      union _metadata {
      	//实例对应的 Klass (实例对应的类)的指针
      	Klass*      _klass;
     	//压缩指针 	  
        narrowKlass _compressed_klass;
      } _metadata;
    
      // Fast access to barrier set.  Must be initialized.
      static BarrierSet* _bs;
    
     private:
      // field addresses in oop
      //字段数据
      void*     field_base(int offset)        const;
    
      jbyte*    byte_field_addr(int offset)   const;
      jchar*    char_field_addr(int offset)   const;
      jboolean* bool_field_addr(int offset)   const;
      jint*     int_field_addr(int offset)    const;
      jshort*   short_field_addr(int offset)  const;
      jlong*    long_field_addr(int offset)   const;
      jfloat*   float_field_addr(int offset)  const;
      jdouble*  double_field_addr(int offset) const;
      Metadata** metadata_field_addr(int offset) const;
      
     public:
      // type test operations (inlined in oop.inline.h)
      bool is_instance()            const;
      bool is_instanceMirror()      const;
      bool is_instanceClassLoader() const;
      bool is_instanceRef()         const;
      bool is_array()               const;
      bool is_objArray()            const;
      bool is_typeArray()           const;
    
    
      //方法很多,不进行
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    从源码可以看出整个oop.hpp类中有三个核心数据markOop,Klass,Metadata。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yd0sOEdJ-1663318720495)(..\内存模型\对象组成.png)]

    markOop

    官方描述

    he markOop describes the header of an object.(markOop 描述了一个对象的头部。)

    源码
    markOop.hpp
    class markOopDesc: public oopDesc {
     private:
      // Conversion
      uintptr_t value() const { return (uintptr_t) this; }
    
     public:
      // Constants常量
      enum { age_bits                 = 4,//分代年龄
             lock_bits                = 2,//锁标识
             biased_lock_bits         = 1,//是否是偏向锁
             max_hash_bits            = BitsPerWord - age_bits - lock_bits - biased_lock_bits,//最大哈希标志位
             hash_bits                = max_hash_bits > 31 ? 31 : max_hash_bits,//对象哈希标注位
             cms_bits                 = LP64_ONLY(1) NOT_LP64(0),
             epoch_bits               = 2//偏向锁时间戳
      };
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    对于对象头中的markOop主要是存储一些信息,主要包括:分代年龄,锁标识,偏向锁,哈希值,偏向锁时间戳。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FQeCyRgK-1663318720498)(..\内存模型\对象头mark word.png)]

    klass

    官方描述
    // A Klass provides:
    //  1: language level class object (method dictionary etc.)
    //  2: provide vm dispatch behavior for the object
    // Both functions are combined into one C++ class.
    
    • 1
    • 2
    • 3
    • 4

    一个klass提供了:

    ​ 1:语言层面的对象

    ​ 2:实现对象的虚分派(virtual dispatch)。所谓的虚分派,是JVM用来实现多态的一种机制。

    这两个功能完成了一个C++类的功能。

    源码
    klass.hpp
    class Klass : public Metadata {
      
      //......
    
      // 类名,其中普通类名和数组类名略有不同
      // 普通类名如:java/lang/String,数组类名如:[Ljava/lang/String;
      Symbol*     _name;
    
      //最后观察到的次要超类型的缓存
      Klass*      _secondary_super_cache;
      
      // 所有次要超类型的数组
      Array* _secondary_supers;
      
      // 所有主要超类型的有序列表
      Klass*      _primary_supers[_primary_super_limit];
      
      //java实例类映射的oop
      oop       _java_mirror;
      
      // 超类
      Klass*      _super;
      
      // 第一个 子类
      Klass*      _subklass;
      // 同级的链接
      Klass*      _next_sibling;
    
      // 类加载器 加载的所有类 都通过这些链接链接
      Klass*      _next_link;
    
      // The VM's representation of the ClassLoader used to load this class.
      // Provide access the corresponding instance java.lang.ClassLoader.
      ClassLoaderData* _class_loader_data;
    
      jint        _modifier_flags;  // Processed access flags, for use by Class.getModifiers.
      AccessFlags _access_flags;    // Access flags. The class/interface distinction is stored here.
    
      // Biased locking implementation and statistics
      // (the 64-bit chunk goes first, to avoid some fragmentation)
      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;
    
      JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
    
      // Remembered sets support for the oops in the klasses.
      jbyte _modified_oops;             // Card Table Equivalent (YC/CMS support)
      jbyte _accumulated_modified_oops; // Mod Union Equivalent (CMS support)
    
    private:
      // This is an index into FileMapHeader::_classpath_entry_table[], to
      // associate this class with the JAR file where it's loaded from during
      // dump time. If a class is not loaded from the shared archive, this field is
      // -1.
      jshort _shared_class_path_index;
    
      friend class SharedClassUtil;
    protected:
    
      // Constructor
      Klass();
    
      void* operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw();
    
     public:
      enum DefaultsLookupMode { find_defaults, skip_defaults };
      enum OverpassLookupMode { find_overpass, skip_overpass };
      enum StaticLookupMode   { find_static,   skip_static };
      enum PrivateLookupMode  { find_private,  skip_private };
    
      bool is_klass() const volatile { return true; }
     
    		//内容有点多 ,没有去细细研究
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    oopHierarchy

    官方描述
    // OBJECT hierarchy
    // This hierarchy is a representation hierarchy, i.e. if A is a superclass
    // of B, A's representation is a prefix of B's representation.
    
    • 1
    • 2
    • 3

    对象层次结构

    这个层次结构是一个表示层次结构,即如果 A 是 B 的超类,A 的表示是 B 的表示的前缀。

    源码
    typedef class oopDesc*                            oop;//oop对象根类
    typedef class   instanceOopDesc*            instanceOop;//java的实例对象
    typedef class   arrayOopDesc*                    arrayOop;//Java的数组对象
    typedef class     objArrayOopDesc*            objArrayOop;//数组对象
    typedef class     typeArrayOopDesc*            typeArrayOop;//类型对象
    
    
    // The klass hierarchy is separate from the oop hierarchy.
    //klass层次结构是与oop的层次结构区分开的
    
    class Klass;//Klass继承体系的最高父类
    class   InstanceKlass;// 表示一个Java普通类,包含了一个类运行时的所有信息
    class     InstanceMirrorKlass; // 表示java.lang.Class
    class     InstanceClassLoaderKlass;// 主要用于遍历ClassLoader继承体系
    class     InstanceRefKlass;// 表示java.lang.ref.Reference及其子类
    class   ArrayKlass;// 表示一个Java数组类
    class     ObjArrayKlass;// 普通对象的数组类
    class     TypeArrayKlass;// 基础类型的数组类
    
    
    //以下是对oop对象操作的方式
    class oop {
      oopDesc* _o;
    
      void register_oop();
      void unregister_oop();
    
      // friend class markOop;
    public:
      void set_obj(const void* p)         {
        raw_set_obj(p);
        if (CheckUnhandledOops) register_oop();
      }
      void raw_set_obj(const void* p)     { _o = (oopDesc*)p; }
    
      oop()                               { set_obj(NULL); }
      oop(const oop& o)                   { set_obj(o.obj()); }
      oop(const volatile oop& o)          { set_obj(o.obj()); }
      oop(const void* p)                  { set_obj(p); }
      ~oop()                              {
        if (CheckUnhandledOops) unregister_oop();
      }
    
      oopDesc* obj()  const volatile      { return _o; }
    
      // General access
      oopDesc*  operator->() const        { return obj(); }
      bool operator==(const oop o) const  { return obj() == o.obj(); }
      bool operator==(void *p) const      { return obj() == p; }
      bool operator!=(const volatile oop o) const  { return obj() != o.obj(); }
      bool operator!=(void *p) const      { return obj() != p; }
    
      bool operator<(oop o) const         { return obj() < o.obj(); }
      bool operator>(oop o) const         { return obj() > o.obj(); }
      bool operator<=(oop o) const        { return obj() <= o.obj(); }
      bool operator>=(oop o) const        { return obj() >= o.obj(); }
      bool operator!() const              { return !obj(); }
    
      // Assignment
      oop& operator=(const oop& o)                            { _o = o.obj(); return *this; }
      volatile oop& operator=(const oop& o) volatile          { _o = o.obj(); return *this; }
      volatile oop& operator=(const volatile oop& o) volatile { _o = o.obj(); return *this; }
    
      // Explict user conversions
      operator void* () const             { return (void *)obj(); }
    #ifndef SOLARIS
      operator void* () const volatile    { return (void *)obj(); }
    #endif
      operator HeapWord* () const         { return (HeapWord*)obj(); }
      operator oopDesc* () const volatile { return obj(); }
      operator intptr_t* () const         { return (intptr_t*)obj(); }
      operator PromotedObject* () const   { return (PromotedObject*)obj(); }
      operator markOop () const volatile  { return markOop(obj()); }
      operator address   () const         { return (address)obj(); }
    
      // from javaCalls.cpp
      operator jobject () const           { return (jobject)obj(); }
      // from javaClasses.cpp
      operator JavaThread* () const       { return (JavaThread*)obj(); }
    
    #ifndef _LP64
      // from jvm.cpp
      operator jlong* () const            { return (jlong*)obj(); }
    #endif
    
      // from parNewGeneration and other things that want to get to the end of
      // an oop for stuff (like ObjArrayKlass.cpp)
      operator oop* () const              { return (oop *)obj(); }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    各位道友可以消化下了,两次jvm源码我看了两周多,担心走火入魔,最近几天就先缓缓消化下了。
    c++不是老本行,jvm源码就到这了,后面先不整了。
    继续卷spring吧!

  • 相关阅读:
    (南京观海微电子)——屏幕材质及优缺点对比
    SpringBoot的JSON工具类(java),用于前后端分离
    day35-IO流02
    古人的雅趣
    vue--5.nextTick、局部和全局、单文件组件、单文件组件的属性、css相关技术
    【opencv】传统图像识别:hog+svm行人识别实战
    Nginx反向代理
    免费1年服务器,部署个ChatGPT专属网页版!
    设计模式-单例模式 (Singleton)
    Lumiprobe非荧光叠丨氮化物研究丨3-叠丨氮丙醇
  • 原文地址:https://blog.csdn.net/smd2575624555/article/details/126894038