经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Objective-C » 查看文章
Objective-C之Class底层结构探索
来源:cnblogs  作者:一眼万年的星空  时间:2024/3/25 8:54:43  对本文有异议

isa 走位图

在讲 OC->Class 底层类结构之前,先看下下面这张图:

isa走位

通过isa走位图 得出的结论是:
1,类,父类,元类都包含了 isa, superclass

2,对象isa指向类对象,类对象的isa指向了元类,元类的 isa 指向了根元类,根元类 isa 指向自己

3,类的 superclass 指向父类,父类的 superclass 指向的根类,根类的superclass 指向的nil

4,元类的 superclass 指向父元类,父元类 superclass 指向的根元类,根元类 superclass 指向根类,根类 superclass 指向nil

这下又复习了 isasuperclass 走位;那么问题这些类,类对象,元类对象当中的在底层展现的数据结构是怎样呢,这是我需要探索的,于是把源码贴出来展开分析下:

struct objc_class

  1. struct objc_class : objc_object {
  2. // Class ISA;
  3. Class superclass;
  4. cache_t cache; // formerly cache pointer and vtable
  5. class_data_bits_t bits;
  6. class_rw_t *data() const {
  7. return bits.data();
  8. }
  9. const class_ro_t *safe_ro() const {
  10. return bits.safe_ro();
  11. }
  12. }

从源码没见 isa 属性,其实它继承了objc_object ,而 objc_object 中有个isa ,在运行时类图生成中会产生一个isa 指向objc_object 这个类图,而 superclass 指向它的父类;根据上面 isa , superclass 走位图就知道它的指向关系。

cache_t & class_data_bits_t

cache 方法缓存,这个作用将常调用的方法缓存下来;便于下次直接查找调用,提高查找效率。
它的结构:

  1. struct cache_t {
  2. struct bucket_t *buckets() const;//存储方法的散列表
  3. mask_t mask() const;//散列表缓存长度
  4. mask_t occupied() const;//已缓存方法个数
  5. }
  1. struct class_data_bits_t {
  2. class_rw_t* data() const;//类信息
  3. }

bits 存储具体类信息,它需要&FAST_DATA_MASK来计算得到类心所有信息,源码如下:

FAST_DATA_MASK 掩码值

imageng

  1. bool has_rw_pointer() const {
  2. #if FAST_IS_RW_POINTER
  3. return (bool)(bits & FAST_IS_RW_POINTER);
  4. #else
  5. class_rw_t *maybe_rw = (class_rw_t *)(bits & FAST_DATA_MASK);
  6. return maybe_rw && (bool)(maybe_rw->flags & RW_REALIZED);
  7. #endif
  8. }

通过源码确实需要这种方式计算能得到类的存储信息;那为什么要用这种方式去处理呢。
比如说我要得到存储在 class_rw_t 类信息信息我只要通过 FAST_DATA_MASK 掩码值就能得到它的地址信息,通过地址信息就能从内存中拿到所有类的存储信息。

那这样我的FAST_DATA_MASK掩码值不一样,我通过&计算,得到的数据信息也就不一样,不得不说苹果工程师想的周到,而且这种方式不仅isa也是这样,很多地方都用这种方式取值,大大提高访问速度,数据提取效率。

class_rw_t ,class_ro_t,class_rw_ext_t

  1. struct class_rw_t {
  2. const class_ro_t *ro() const ;
  3. const method_array_t methods() const ;//如果是类对象:放对象方法,元类:元类对象方法
  4. const property_array_t properties() const;
  5. const protocol_array_t protocols() const;
  6. class_rw_ext_t *ext() const;
  7. }
  8. struct class_rw_ext_t {
  9. method_array_t methods;
  10. property_array_t properties;
  11. protocol_array_t protocols;
  12. uint32_t version;
  13. }

可以看出类的信息具体就存储在class_rw_tclass_ro_tclass_rw_ext_t 中,

剖析下class_rw_t
先看看method_array_tproperty_array_tprotocol_array_t源码结构

  1. class property_array_t :
  2. public list_array_tt<property_t, property_list_t, RawPtr>
  3. {
  4. typedef list_array_tt<property_t, property_list_t, RawPtr> Super;
  5. public:
  6. property_array_t() : Super() { }
  7. property_array_t(property_list_t *l) : Super(l) { }
  8. };
  9. class protocol_array_t :
  10. public list_array_tt<protocol_ref_t, protocol_list_t, RawPtr>
  11. {
  12. typedef list_array_tt<protocol_ref_t, protocol_list_t, RawPtr> Super;
  13. public:
  14. protocol_array_t() : Super() { }
  15. protocol_array_t(protocol_list_t *l) : Super(l) { }
  16. };

看完之后,他们都继承list_array_tt,那么 list_array_tt 是什么鬼,它数据结构是怎样的,这下在取找下它。源码如下:

  1. template <typename Element, typename List, template<typename> class Ptr>
  2. class list_array_tt {
  3. protected:
  4. template <bool authenticated>
  5. class iteratorImpl {
  6. const Ptr<List> *lists;
  7. const Ptr<List> *listsEnd;
  8. }
  9. using iterator = iteratorImpl<false>;
  10. using signedIterator = iteratorImpl<true>;
  11. public:
  12. list_array_tt() : list(nullptr) { }
  13. list_array_tt(List *l) : list(l) { }
  14. list_array_tt(const list_array_tt &other) {
  15. *this = other;
  16. }
  17. void attachLists(List* const * addedLists, uint32_t addedCount) {
  18. if (addedCount == 0) return;
  19. if (hasArray()) {
  20. // many lists -> many lists
  21. uint32_t oldCount = array()->count;
  22. uint32_t newCount = oldCount + addedCount;
  23. array_t *newArray =(array_t*)malloc(array_t::byteSize(newCount));
  24. newArray->count = newCount;
  25. array()->count = newCount;
  26. for (int i = oldCount - 1; i >= 0; i--)
  27. newArray->lists[i + addedCount] = array()->lists[i];
  28. for (unsigned i = 0; i < addedCount; i++)
  29. newArray->lists[i] = addedLists[i];
  30. free(array());
  31. setArray(newArray);
  32. validate();
  33. }
  34. else if (!list && addedCount == 1) {
  35. // 0 lists -> 1 list
  36. list = addedLists[0];
  37. validate();
  38. }
  39. else {
  40. // 1 list -> many lists
  41. Ptr<List> oldList = list;
  42. uint32_t oldCount = oldList ? 1 : 0;
  43. uint32_t newCount = oldCount + addedCount;
  44. setArray((array_t *)malloc(array_t::byteSize(newCount)));
  45. array()->count = newCount;
  46. if (oldList) array()->lists[addedCount] = oldList;
  47. for (unsigned i = 0; i < addedCount; i++)
  48. array()->lists[i] = addedLists[i];
  49. validate();
  50. }
  51. }
  52. }

我把主要地方拿去出来,可以看到 attachLists 它的目的是将一个或多个列表(List 类型)附加到某个 list_array_tt对象中。这个对象可以包含零个、一个或多个列表,这些列表可以是单个指针,也可以是指针数组。函数的输入参数是一个指向 List 指针数组的指针 addedLists 和一个无符号整数 addedCount,表示要添加的列表数量。

由此我推断它是一个数组,而且是一个二维数组存储的,所有由此得出 class_rw_tmethodspropertiesprotocols这几个属性利用二维数组取存储类的方法,协议等信息,而且是可读可写的属性。

那它设计这种二维数组有什么好处呢?当然有好处,它可以动态的给数组里面增加删除方法,很方便我们分类方法的编写完进行存储。

那搞清楚了 class_rw_t 几个重要数据存储信息,那 class_rw_t 它的作用是干什么的呢;

class_rw_t 结构体定义来看;它是在应用运行时,将OC类,分类的信息直接写入到class_rw_t结构的数据结构中,在类的方法,协议进行调用时,从里面去读取,然后常调用的方法,又存储在cache_t这个结构体中,可想而知,苹果对OC类的处理,煞费苦心。

struct class_ro_t

class_rw_t结构体中有个 class_ro_t 结构体,在探索下这个东西做什么的,它的源码如下:

  1. struct class_ro_t {
  2. WrappedPtr<method_list_t, method_list_t::Ptrauth> baseMethods;
  3. protocol_list_t * baseProtocols;
  4. const ivar_list_t * ivars;
  5. property_list_t *baseProperties;
  6. }

先说说 ivars 这个属性修饰的结构体源码如下:

  1. struct ivar_list_t : entsize_list_tt<ivar_t, ivar_list_t, 0> {
  2. bool containsIvar(Ivar ivar) const {
  3. return (ivar >= (Ivar)&*begin() && ivar < (Ivar)&*end());
  4. }
  5. };

这个貌似只有一个继承 entsize_list_tt,那在探索下源码:

  1. struct entsize_list_tt {
  2. uint32_t entsizeAndFlags;
  3. uint32_t count;
  4. struct iteratorImpl {
  5. uint32_t entsize;
  6. uint32_t index; // keeping track of this saves a divide in operator-
  7. using ElementPtr = std::conditional_t<authenticated, Element * __ptrauth(ptrauth_key_process_dependent_data, 1, 0xdead), Element *>;
  8. ElementPtr element;
  9. typedef std::random_access_iterator_tag iterator_category;
  10. typedef Element value_type;
  11. typedef ptrdiff_t difference_type;
  12. typedef Element* pointer;
  13. typedef Element& reference;
  14. iteratorImpl() { }
  15. iteratorImpl(const List& list, uint32_t start = 0)
  16. : entsize(list.entsize())
  17. , index(start)
  18. , element(&list.getOrEnd(start))
  19. { }
  20. }
  21. }

可以看出这段代码定义了一个结构体 entsize_list_tt,它内部包含一个嵌套的结构体 iteratorImpl,用于实现一个迭代器。遍历容器(如列表、数组等)的对象。

到此可以得出ivars 是一个 ivar_list_t 数组,它存储了类的属性变量信息,那protocol_list_t结构体内部也是数组形式构建的。

baseProtocolsbaseProperties 这两个属性对类的存储信息只能读取,不能写入。

所以总结的是:从 class_ro_t 结构体定义来看,它存储类的变量,方法,协议信息,而且这个结构体属于类的只读信息,它包含了类的初始信息。

class_rw_ext_t

这个结构体不在过多叙述,简单来说它是基于 class_rw_t 之后为了更好管理oc类的高级特性,比如关联属性等,衍生出来的一个结构体,包括:method_array_t ,property_arrat_t ,protocol_array_t 等定义属性类型

到这里类结构及存储所关联的信息都在这里了;来一张他们关联的结构思维图:

imageng

总结:一开始编译时,程序将类的初始信息放在 class_ro_t中,当程序运行时,将类的信息合并在一起的时候,它会将 class_ro_t 类的信息合并到 class_rw_t 结构体中去。

struct method_t

为什么要说method_t,因为它不仅在 class_ro_t 有使用,在OC底层其他地方也有使用;比如如下源码:

  1. void method_exchangeImplementations(Method m1Signed, Method m2Signed)
  2. {
  3. if (!m1Signed || !m2Signed) return;
  4. method_t *m1 = _method_auth(m1Signed);
  5. method_t *m2 = _method_auth(m2Signed);
  6. mutex_locker_t lock(runtimeLock);
  7. IMP imp1 = m1->imp(false);
  8. IMP imp2 = m2->imp(false);
  9. SEL sel1 = m1->name();
  10. SEL sel2 = m2->name();
  11. m1->setImp(imp2);
  12. m2->setImp(imp1);
  13. // RR/AWZ updates are slow because class is unknown
  14. // Cache updates are slow because class is unknown
  15. // fixme build list of classes whose Methods are known externally?
  16. flushCaches(nil, __func__, [sel1, sel2, imp1, imp2](Class c){
  17. return c->cache.shouldFlush(sel1, imp1) || c->cache.shouldFlush(sel2, imp2);
  18. });
  19. adjustCustomFlagsForMethodChange(nil, m1);
  20. adjustCustomFlagsForMethodChange(nil, m2);
  21. }
  22. static IMP
  23. _method_setImplementation(Class cls, method_t *m, IMP imp)
  24. {
  25. lockdebug::assert_locked(&runtimeLock);
  26. if (!m) return nil;
  27. if (!imp) return nil;
  28. IMP old = m->imp(false);
  29. SEL sel = m->name();
  30. m->setImp(imp);
  31. // Cache updates are slow if cls is nil (i.e. unknown)
  32. // RR/AWZ updates are slow if cls is nil (i.e. unknown)
  33. // fixme build list of classes whose Methods are known externally?
  34. flushCaches(cls, __func__, [sel, old](Class c){
  35. return c->cache.shouldFlush(sel, old);
  36. });
  37. adjustCustomFlagsForMethodChange(cls, m);
  38. return old;
  39. }

方法交换,实现中底层都有用到,我们探索下,先看看 method_t 源码:

  1. struct method_t {
  2. // The representation of a "big" method. This is the traditional
  3. // representation of three pointers storing the selector, types
  4. // and implementation.
  5. struct big {
  6. SEL name;
  7. const char *types;
  8. MethodListIMP imp;
  9. };
  10. // A "big" method, but name is signed. Used for method lists created at runtime.
  11. struct bigSigned {
  12. SEL __ptrauth_objc_sel name;
  13. const char * ptrauth_method_list_types types;
  14. MethodListIMP imp;
  15. };
  16. // ***HACK: This is a TEMPORARY HACK FOR EXCLAVEKIT. It MUST go away.
  17. // rdar://96885136 (Disallow insecure un-signed big method lists for ExclaveKit)
  18. #if TARGET_OS_EXCLAVEKIT
  19. struct bigStripped {
  20. SEL name;
  21. const char *types;
  22. MethodListIMP imp;
  23. };
  24. #endif
  25. }

可以看到这结构体中掐套了多个结构体;在把它简化下:

  1. struct method_t {
  2. SEL name;//方法名
  3. const char *types;//包含函数具有参数编码的字符串类型的返回值
  4. MethodListIMP imp;//函数指针(指向函数地址的指针)
  5. }

SEL :函数名,没特别的意义;

特点:
1,使用@selector()sel_registerName()获得
2,使用sel_getName()NSStringFromSelector()转成字符串
3,不同类中相同名字方法,对应的方法选择器是相同或相等的

底层代码结构:

  1. /// An opaque type that represents a method selector.
  2. typedef struct objc_selector *SEL;

types:包含了函数返回值、参数编码的字符串

imageng
imageng

可以看到types在值:v16@0:8 ,可以看出name,types,IMP其实都在class_ro_t结构体中,这样确实证明了之前说的;class_ro_t结构体在运行时存储着类的初始状态数据。

v16@0:8说明下:

v:方法返回类型,这里说void,

16:第一个参数,

@:id类型第二个参数,

0:第三个参数

: :selector类型

8:第四个参数

那这种types参数又是什么鬼东西,查下了资料这叫:Type Encoding(类型编码)
怎么证明了,使用如下代码:
imagepng

苹果官网types encoding表格:
imageng

IMP 其实就是指向函数的指针,感觉这个就没有必要讲了。

struct cache_t

cache_t 用于 class的方法缓存,对class常调用的方法缓存下来,提高查询效率,这个上之前都已经说过;接下来看看 bucket_t

struct bucket_t

  1. struct bucket_t {
  2. cache_key_t _key;//函数名
  3. IMP _imp;//函数内存地址
  4. }

这种散列表的模型,其实在底层用一个数组展现:

imagng

其实它的内部就是一个一维数组,那可能问了,数组难道它是循环查找吗,其实不然;在它元素超找时,它是拿到你的 函数名 & mask,而这个 mask 就是 cache_t 结构体中的 mask值;计算得到函数在 散列表 存储的索引值,在通过索引拿到函数地址,进行执行。

接下来看个事例:

  1. int main(int argc, const char * argv[]) {
  2.     @autoreleasepool {
  3.         Student *stu=[Student new];
  4.         [stu test];
  5.         [stu test];
  6.         [stu test];
  7.         [stu test];
  8.     }
  9.     return 0;
  10. }

如上方法:当首次调用它会去类对象中查找,在方法执行时,他会放入cache_t 缓存中,当第二次,第三次,第四次时,它就去缓存中查找。

imagpng

当方法执行后;我们看到 _mask 是:3,这个3代表了我类中定义了三个函数;而——_occupied 是一个随意的值;它其实代表了换存方法的个数。

那如何知道方法有缓存了,再继续往下执行:

imageng

这时候执行完 test02, _mask的值从 3 变成了 7 ,说明散列表 bucket_t 做了扩容操作。在这里bucket_t 元素需要 _mask 个元素,所以最终 bucket_t 从原有的3个元素进行了 2倍 扩容。

在看下方法是否进行缓存:

imageng

可以看见当执行完 [stu test02] 时,数据做了扩容,并且扩容的数据使用(null) 进行填充。

在看个事例:

imageng

在执行 [stu test] 之前;其实bucket_t 就3个元素,并且存入了 init 方法;

imageng

当执行完 [stu test] 之后;就存入 test 方法。

但是注意的地方:它在扩容时对之前的缓存进行清除。

image.png

通过查看源码,我们知道了它如何进行清除操作,

imageng

当执行完 [stu test02];[stu test03]; 之后,它先将缓存清空;这时候 init , test 方法被清空,bucket_t扩容完在存储:test02test03 方法。

那问题又来了,它是如何快速定位到方法的,然后执行的?接下来看看代码:

imagepng

可以清楚看见,当我使用 @selector(test03)&stu_cache._mask 就可以得到下标,然后再从 bucket_t 拿到方法。

到这里 class结构,类的方法缓存到此结束了,从上面也可以思考下:如果自己去实现散列表数组,是不是思路就跟清晰了。

谢谢大家!青山不改,绿水长流。后会有期!

原文链接:https://www.cnblogs.com/mysweetAngleBaby/p/18092347

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号