custom_objc_class.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // custom_objc_class.h
  3. // Objc4_Study
  4. //
  5. // Created by huangyirong on 2020/6/27.
  6. // Copyright © 2020 Sogou. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. # if __arm64__
  10. # define ISA_MASK 0x0000000ffffffff8ULL
  11. # elif __x86_64__
  12. # define ISA_MASK 0x00007ffffffffff8ULL
  13. # endif
  14. #ifndef custom_objc_class_h
  15. #define custom_objc_class_h
  16. #if __LP64__
  17. typedef uint32_t mask_t;
  18. #else
  19. typedef uint16_t mask_t;
  20. #endif
  21. typedef uintptr_t cache_key_t;
  22. struct bucket_t {
  23. cache_key_t _key;
  24. IMP _imp;
  25. };
  26. struct cache_t {
  27. bucket_t *_buckets;
  28. mask_t _mask;
  29. mask_t _occupied;
  30. };
  31. struct entsize_list_tt {
  32. uint32_t entsizeAndFlags;
  33. uint32_t count;
  34. };
  35. struct method_t {
  36. SEL name;
  37. const char *types;
  38. IMP imp;
  39. };
  40. struct method_list_t : entsize_list_tt {
  41. method_t first;
  42. };
  43. struct ivar_t {
  44. int32_t *offset;
  45. const char *name;
  46. const char *type;
  47. uint32_t alignment_raw;
  48. uint32_t size;
  49. };
  50. struct ivar_list_t : entsize_list_tt {
  51. ivar_t first;
  52. };
  53. struct property_t {
  54. const char *name;
  55. const char *attributes;
  56. };
  57. struct property_list_t : entsize_list_tt {
  58. property_t first;
  59. };
  60. struct chained_property_list {
  61. chained_property_list *next;
  62. uint32_t count;
  63. property_t list[0];
  64. };
  65. typedef uintptr_t protocol_ref_t;
  66. struct protocol_list_t {
  67. uintptr_t count;
  68. protocol_ref_t list[0];
  69. };
  70. struct class_ro_t {
  71. uint32_t flags;
  72. uint32_t instanceStart;
  73. uint32_t instanceSize; // instance对象占用的内存空间
  74. #ifdef __LP64__
  75. uint32_t reserved;
  76. #endif
  77. const uint8_t * ivarLayout;
  78. const char * name; // 类名
  79. method_list_t * baseMethodList;
  80. protocol_list_t * baseProtocols;
  81. const ivar_list_t * ivars; // 成员变量列表
  82. const uint8_t * weakIvarLayout;
  83. property_list_t *baseProperties;
  84. };
  85. struct class_rw_t {
  86. uint32_t flags;
  87. uint32_t version;
  88. const class_ro_t *ro;
  89. method_list_t * methods; // 方法列表
  90. property_list_t *properties; // 属性列表
  91. const protocol_list_t * protocols; // 协议列表
  92. Class firstSubclass;
  93. Class nextSiblingClass;
  94. char *demangledName;
  95. };
  96. #define FAST_DATA_MASK 0x00007ffffffffff8UL
  97. struct class_data_bits_t {
  98. uintptr_t bits;
  99. public:
  100. class_rw_t* data() {
  101. return (class_rw_t *)(bits & FAST_DATA_MASK);
  102. }
  103. };
  104. /* OC对象 */
  105. struct custom_objc_object {
  106. // void *isa;
  107. Class _Nonnull isa OBJC_ISA_AVAILABILITY;
  108. };
  109. /* 类对象 */
  110. struct custom_objc_class : custom_objc_object {
  111. Class superclass;
  112. cache_t cache;
  113. class_data_bits_t bits;
  114. public:
  115. class_rw_t* data() {
  116. return bits.data();
  117. }
  118. custom_objc_class* metaClass() {
  119. return (custom_objc_class *)((long long)isa & ISA_MASK);
  120. }
  121. };
  122. #endif /* custom_objc_class_h */