iOS底層探索之類結構

一、前置知識

CPU 訪問內存時需要的是地址,而不是變量名和函數名!變量名和函數名只是地址的一種助記符,當源文件被編譯和鏈接成可執行程序後,它們都會被替換成地址。編譯和鏈接過程的一項重要任務就是找到這些名稱所對應的地址。

1.1 C語言指針變量的運算(加法、減法和比較運算)

指針 變量保存的是 地址,而地址本質上是一個整數,所以指針變量可以進行部分運算,例如加法、減法、比較等,請看下面的代碼:

        int    a = 10,   *pa = &a, *paa = &a;
        double b = 99.9, *pb = &b;
        char   c = '@',  *pc = &c;
        //最初的值
        NSLog(@"最初的值:");
        NSLog(@"&a=%p, &b=%p, &c=%p \n", &a, &b, &c);
        NSLog(@"pa=%p, pb=%p, pc=%p \n", pa, pb, pc);
        //加法運算
        pa++; pb++; pc++;
        NSLog(@"各自+1後:");
        NSLog(@"pa=%p, pb=%p, pc=%p \n", pa, pb, pc);
        //減法運算
        NSLog(@"然後-2後:");
        pa -= 2; pb -= 2; pc -= 2;
        NSLog(@"pa=%p, pb=%p, pc=%p \n", pa, pb, pc);
        //比較運算
        if(pa == paa){
            NSLog(@"pa和paa相等: %d\n", *paa);
        }else{
            NSLog(@"pa和paa不相等: %d\n", *pa);
        }
最初的值:
&a=0x7ffeed1bbd80, &b=0x7ffeed1bbd78, &c=0x7ffeed1bbd87
pa=0x7ffeed1bbd80, pb=0x7ffeed1bbd78, pc=0x7ffeed1bbd87
各自+1後:
pa=0x7ffeed1bbd84, pb=0x7ffeed1bbd80, pc=0x7ffeed1bbd88
然後-2後:
pa=0x7ffeed1bbd7c, pb=0x7ffeed1bbd70, pc=0x7ffeed1bbd86
pa和paa不相等: 1079572889

從運算結果可以看出:papbpc 每次加 1,它們的地址分別增加 4、8、1,正好是 intdoublechar 類型的長度;減 2 時,地址分別減少 8、16、2,正好是 intdoublechar 類型長度的 2 倍。

這很奇怪,指針變量加減運算的結果跟數據類型的長度有關,而不是簡單地加 1 或減 1,這是爲什麼呢?

apa 爲例,a 的類型爲int,佔用 4 個字節,pa 是指向 a 的指針,如下圖所示:

剛開始的時候,pa 指向 a 的開頭,通過 *pa 讀取數據時,從 pa 指向的位置向後移動 4 個字節,把這 4 個字節的內容作爲要獲取的數據,這 4 個字節也正好是變量 a 佔用的內存。

如果 pa++;使得地址加 1 的話,就會變成如下圖所示的指向關係:

這個時候 pa 指向整數 a 的中間,*pa 使用的是紅色虛線畫出的 4 個字節,其中前 3 個是變量 a 的,後面 1 個是其它數據的,把它們“攪和”在一起顯然沒有實際的意義,取得的數據也會非常怪異。

如果pa++ ;使得地址加 4 的話,正好能夠完全跳過整數 a,指向它後面的內存,如下圖所示:

總結:指針做 + 、- 運算的時候,是按指向內容類型倍數運算。

二、Class (進入正題)

上篇文章分析isa的時候,遇到了 ClassClass 是什麼,內部又是怎麼定義的?

源碼用的還是objc4-787.1版本。

2.1 源碼查看
typedef struct objc_class *Class;

繼續查看 objc_class,這個時候發現有3個 objc_class 的定義,都是 struct,但是注意一個是 runtime.h 中的,會發現 已經標記 OBJC2_UNAVAILABLE(OC2不可用了)。一個是 objc-runtime-old.h (舊的不管)中的,還有個 objc-runtime-new.h 中的,跟進 objc-runtime-new.h 中的代碼:

struct objc_class : objc_object {
    // Class ISA;
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

    class_rw_t *data() const {
        return bits.data();
    }
    void setData(class_rw_t *newData) {
        bits.setData(newData);
    }
  // 中間省略很多方法 ...
    bool isMetaClass() {
        ASSERT(this);
        ASSERT(isRealized());
#if FAST_CACHE_META
        return cache.getBit(FAST_CACHE_META);
#else
        return data()->flags & RW_META;
#endif
    }

    // Like isMetaClass, but also valid on un-realized classes
    bool isMetaClassMaybeUnrealized() {
        static_assert(offsetof(class_rw_t, flags) == offsetof(class_ro_t, flags), "flags alias");
        static_assert(RO_META == RW_META, "flags alias");
        return data()->flags & RW_META;
    }

    // NOT identical to this->ISA when this is a metaclass
    Class getMeta() {
        if (isMetaClass()) return (Class)this;
        else return this->ISA();
    }

    bool isRootClass() {
        return superclass == nil;
    }
    bool isRootMetaclass() {
        return ISA() == (Class)this;
    }
    // 省略 ...
    
    // 以前分析alloc的時候,熟悉的instanceSize
    uint32_t alignedInstanceStart() const {
        return word_align(unalignedInstanceStart());
    }

    // May be unaligned depending on class's ivars.
    uint32_t unalignedInstanceSize() const {
        ASSERT(isRealized());
        return data()->ro()->instanceSize;
    }

    // Class's ivar size rounded up to a pointer-size boundary.
    uint32_t alignedInstanceSize() const {
        return word_align(unalignedInstanceSize());
    }

    size_t instanceSize(size_t extraBytes) const {
        if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
            return cache.fastInstanceSize(extraBytes);
        }

        size_t size = alignedInstanceSize() + extraBytes;
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;
        return size;
    }

    void setInstanceSize(uint32_t newSize) {
        ASSERT(isRealized());
        ASSERT(data()->flags & RW_REALIZING);
        auto ro = data()->ro();
        if (newSize != ro->instanceSize) {
            ASSERT(data()->flags & RW_COPIED_RO);
            *const_cast<uint32_t *>(&ro->instanceSize) = newSize;
        }
        cache.setFastInstanceSize(newSize);
    }

}

發現 objc_class 繼承與 objc_object,繼續跟進 objc_object

struct objc_object {
private:
    isa_t isa;

public:

    // ISA() assumes this is NOT a tagged pointer object
    Class ISA();

    // rawISA() assumes this is NOT a tagged pointer object or a non pointer ISA
    Class rawISA();

    // getIsa() allows this to be a tagged pointer object
    Class getIsa();
    
    uintptr_t isaBits() const;

    // 省略很多方法 方法 方法 ...
}

發現 objc_object 只有一個私有屬性 isa_t isa;

2.2 驗證經典isa流程圖

這個isa流程圖,見過好多次了,明白其流程,但是不知其怎麼來的,

在分析 alloc 的時候,會初始化 instanceisa 賦值該對象的 Class

根據源碼可知,

  • 只要繼承與 NSObject 的對象都會有一個 isa
  • isa 中最重要的就是 shiftcls,其實就是指向 Class
        GLPerson *supP = [[GLPerson alloc] init];
        // GLStudent 繼承於 GLPerson
        GLStudent *subS = [[GLStudent alloc] init];

驗證 subS 也是一樣的流程。

2.3 類(對象)只存在一份
        Class c1 = object_getClass(subS1);
        Class c2 = object_getClass(subS2);
        Class c3 = object_getClass(subS3);
        Class c4 = object_getClass(subS4);
        
        NSLog(@"c1:%p; c2:%p; c3:%p; c4:%p", c1, c2, c3, c4);

console: c1:0x1026ca820; c2:0x1026ca820; c3:0x1026ca820; c4:0x1026ca820

三、objc_class

對象初始化的時候,好像沒有要初始化屬性、方法吧,那屬性、方法、協議這些東西是放在那裏了。

找了下 objc-runtime-old.h舊定義文件) 裏面的定義,發現 objc_class 裏面直接是有 ivars methodLists protocols 這些信息的。

而在 objc-runtime-new.h新定義文件)裏面這些屬性沒了,而多了一個 class_data_bits_t bits 屬性。

class_data_bits_t 源碼:

struct class_data_bits_t {
    friend objc_class;
    // Values are the FAST_ flags above.
    uintptr_t bits;

public:
    class_rw_t* data() const {
        return (class_rw_t *)(bits & FAST_DATA_MASK);
    }
// 省略一些方法 ...
}

class_data_bits_t 裏面有個方法 class_rw_t* data(),會返回 class_rw_t 的數據。

繼續查看 class_rw_t

struct class_rw_t {
    // 省略 ...
    const method_array_t methods() const {
        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>()->methods;
        } else {
            return method_array_t{v.get<const class_ro_t *>()->baseMethods()};
        }
    }

    const property_array_t properties() const {
        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>()->properties;
        } else {
            return property_array_t{v.get<const class_ro_t *>()->baseProperties};
        }
    }

    const protocol_array_t protocols() const {
        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>()->protocols;
        } else {
            return protocol_array_t{v.get<const class_ro_t *>()->baseProtocols};
        }
    }
}

可在裏面找到 method_array_tproperty_array_tprotocol_array_t 這些方法。從字面意思應該能看出這就是我們要找的東西了。

3.1 獲取 class_data_bits_t

運行環境是在配置好可運行的 objc 源碼的工程。

struct objc_class : objc_object {
    // Class ISA;
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
    // 省略...
}

通過 objc_class 的源碼可知,bits 存在第4個成員。只要計算出前面3個成員的大小,就可以通過內存偏移得到 bitsisa 根據以前文章的分析可知是8字節。Class superclass 是一個指針類型,所以也是8字節。看下 cache_t 源碼:

struct cache_t {
// typedef uint32_t mask_t;
// typedef unsigned int uint32_t; 所以mask_t類型是4字節
// typedef unsigned long           uintptr_t; 所以 uintptr_t 類型是8字節
#if CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_OUTLINED
    explicit_atomic<struct bucket_t *> _buckets; // 指針8字節
    explicit_atomic<mask_t> _mask;  // 4字節
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_HIGH_16
    explicit_atomic<uintptr_t> _maskAndBuckets; // long 8字節
    mask_t _mask_unused; // 4字節
#elif CACHE_MASK_STORAGE == CACHE_MASK_STORAGE_LOW_4
    explicit_atomic<uintptr_t> _maskAndBuckets; // long 8字節
    mask_t _mask_unused; // 4字節
#else
#error Unknown cache mask storage type.
#endif

// typedef unsigned short uint16_t; 這是uint16_t的定義
#if __LP64__
    uint16_t _flags;  // 2字節
#endif
    uint16_t _occupied; // 2字節

// 省略很多方法和靜態變量...
}

cache_t 源碼總結:不管第一個if else走哪個,加上下面2個變量,總共佔用16字節。

計算偏移量

  • 繼承的isa : 8字節;
  • Class superclass : 8字節;
  • cache_t cache:16字節;

所以,要想獲取 bits,總共需要在類的首地址上偏移32字節。

(lldb) x/4gx GLPerson.class
0x100002620: 0x00000001000025f8 0x0000000100334140
0x100002630: 0x00000001006a8540 0x0001802400000003
// 在GLPerson類首地址0x100002620的基礎上偏移32字節。記得是16進制哦
(lldb) p (class_data_bits_t *)0x100002640
(class_data_bits_t *) $4 = 0x0000000100002640
3.2 打印 class_rw_t 中存儲
@protocol GLPersonWalkProtocol <NSObject>
- (void)walk;
@end

@interface GLPerson : NSObject <GLPersonWalkProtocol>

@property (nonatomic, strong) NSString *name; /**<  8個字節  */
@property (nonatomic, strong) NSString *nickName; /**<  8個字節  */

- (void)sayHello;

@end

@interface GLStudent : GLPerson
@end
---
        GLPerson *supP = [[GLPerson alloc] init];
        GLStudent *subS = [[GLStudent alloc] init];

通過 xppo 命令打印

lldb) x/4gx supP
0x1006a4730: 0x001d800100002625 0x0000000000000000
0x1006a4740: 0x0000000000000000 0x0000000000000000
(lldb) po 0x001d800100002625 & 0x00007ffffffffff8ULL
GLPerson

(lldb) p 0x001d800100002625 & 0x00007ffffffffff8ULL
(unsigned long long) $2 = 4294977056 // 這是GLPerson類
(lldb) x/4gx $2  // 打印GLPerson類的地址和內存
0x100002620: 0x00000001000025f8 0x0000000100334140
0x100002630: 0x00000001006a8540 0x0001802400000003
// 另一種方式打印GLPerson類的地址和內存,再次驗證是一樣的
(lldb) x/4gx GLPerson.class  
0x100002620: 0x00000001000025f8 0x0000000100334140
0x100002630: 0x00000001006a8540 0x0001802400000003
// 通過類的首地址偏移32字節得到$4,$4即是class_data_bits_t的首地址
(lldb) p (class_data_bits_t *)0x100002640
(class_data_bits_t *) $4 = 0x0000000100002640
// 調用class_data_bits_t的方法的到class_rw_t數據
(lldb) p *$4->data()
(class_rw_t) $5 = {
  flags = 2148007936
  witness = 1
  ro_or_rw_ext = {
    std::__1::atomic<unsigned long> = 4294976416
  }
  firstSubclass = GLStudent
  nextSiblingClass = NSUUID
}

總結流程:

  1. 打印 GLPerson.class 的首地址和內存;
  2. 通過地址偏移獲取 class_data_bits_t *指針賦值給$4變量;
  3. 通過 *$4 取地址裏面的值可得到 class_data_bits_t
  4. 然後調用 data() 方法,返回 class_rw_t 數據;
  5. 通過 p 打印 class_rw_t;
3.3 獲取 method_array_t
(lldb) x/4gx GLPerson.class
0x100002638: 0x0000000100002610 0x0000000100334140
0x100002648: 0x00000001006c2ac0 0x0001802400000003
(lldb) p (class_data_bits_t *)0x100002658
(class_data_bits_t *) $3 = 0x0000000100002658
(lldb) p $3->data()
(class_rw_t *) $4 = 0x00000001006c2a40
(lldb) p *$4
(class_rw_t) $5 = {
  flags = 2148007936
  witness = 1
  ro_or_rw_ext = {
    std::__1::atomic<unsigned long> = 4294976416
  }
  firstSubclass = GLStudent
  nextSiblingClass = NSUUID
}
(lldb) p $5.methods()
(const method_array_t) $6 = {
  list_array_tt<method_t, method_list_t> = {
     = {
      list = 0x00000001000023e8
      arrayAndFlag = 4294976488
    }
  }
}
(lldb) p $6.list
(method_list_t *const) $7 = 0x00000001000023e8
(lldb) p *$7
(method_list_t) $8 = {
  entsize_list_tt<method_t, method_list_t, 3> = {
    entsizeAndFlags = 26
    count = 8
    first = {
      name = "walk"
      types = 0x0000000100000e22 "v16@0:8"
      imp = 0x0000000100000b30 (KCObjc`-[GLPerson walk])
    }
  }
}

總結流程

  1. 通過 $3->data() 獲取到 (class_rw_t *) $4指針;
  2. 通過 *$4 打印 class_rw_t 得到 $5;
  3. 調用 (class_rw_t) $5methods 方法獲取 (const method_array_t) $6;
  4. 調用 $6.list方法 獲取指向 method_list_t 的指針 (method_list_t *const) $7;
  5. *$7$7 指向的存儲內容,通過 p 打印;

同樣方式也可打印出 const property_array_t properties()

(lldb) x/4gx GLPerson.class
0x100002638: 0x0000000100002610 0x0000000100334140
0x100002648: 0x00000001006c2ac0 0x0001802400000003
(lldb) p (class_data_bits_t *)0x100002658
(class_data_bits_t *) $10 = 0x0000000100002658
(lldb) p *$10.data()
(class_rw_t) $11 = {
  flags = 2148007936
  witness = 1
  ro_or_rw_ext = {
    std::__1::atomic<unsigned long> = 4294976416
  }
  firstSubclass = GLStudent
  nextSiblingClass = NSUUID
}
  Fix-it applied, fixed expression was: 
    *$10->data()
(lldb) p $11.properties()
(const property_array_t) $13 = {
  list_array_tt<property_t, property_list_t> = {
     = {
      list = 0x00000001000024f8
      arrayAndFlag = 4294976760
    }
  }
}
(lldb) p $13.list
(property_list_t *const) $14 = 0x00000001000024f8
(lldb) p *$14
(property_list_t) $15 = {
  entsize_list_tt<property_t, property_list_t, 0> = {
    entsizeAndFlags = 16
    count = 6
    first = (name = "name", attributes = "T@\"NSString\",&,N,V_name")
  }
}

未完待續...


參考

C語言指針:http://c.biancheng.net/view/1992.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章