NSObject nature

1: Structure

1: NSObject in memory size:

NSObject system assigned to the object instance 16 bytes, but the object only 8 bytes.

2: OC target three types: an object instance (instance), the object class (Class), meta class object (MetaClass).

instance : the store isa, members of the variable value.

Class : the ISA, superCalss, cache_t, class_data_bits_t four members.

MetaClass : consistent with the structure and the class objects, but it is less than the data stored in the object class, the class object storage method, isa, superClass. Many other places are nil.

2 members of the specific content

1 class_data_bits_t

class_data_bits_t & FAST_DATA_MASK class_rw_t acquired saved members of the list of methods, some of the information property list, the list of protocols, flags, version and so on. This list is a three-dimensional array (member of the array is an array of all classifications for each class method list, the list of attributes, the protocol list).

class_ro_t also keep a list of methods, the protocol list, the list of attributes. But the structure in the list are one-dimensional array at run time, all will be a class, classification, agreement, property re-planning, and class_ro_t is unreadable. Specifically Figure -01.

2304477-5adac84aba1d4245.png
Figure -01

It is noteworthy that class_data_bits_t also point to the beginning class_ro_t, at run time, the combined classification, it will class_data_bits_t point class_ro_t.

2 isa relationship with class and superClass

Isa class object instance of the object point, point to the class object isa metaclass object, all the element points to the class object isa (isa packets including the base class, point to themselves) base class metaclass.

SuperClass no instance object, its parent class object superClass point, the base class superClass point class object base class ([NSObject class]). SuperClass base class = nil ;.

#  define ISA_MASK        0x00007ffffffffff8ULL

isa here is the previous arm64, then if arm64, you need to take isa & ISA_MASK, out of which some concepts before us is the isa pointer. Because after arm64, Apple has optimized, Class isa became isa_t this union public body, which not only saved the class object, address metaclass object, there also existed a lot of additional information, such as representatives nonpointer, bits in a bit isa does it mean that this is an ordinary pointer


2304477-079c6ed045dbeb03.png
Figure -02

3 cache_t

cache_t: call off the list method. Deposit with the structure of three members: struct bucket_t * _buckets // hash table, mask_t _mask; // hash table length -1, ma s k_t _occupied; // number of methods have been buffered. _bucket in memory of two members: cache_key_t key; // SEL as a key IMP _imp // function memory address. Figure 02:

2304477-b09f6cae36ee9e30.png
Figure -03

Cache_t method to deposit in the process:

The method of SEL (_key), i.e. @selector (method name) & _ mask to obtain an index, and then look at the position of buckets corresponding to the index Is there a way, if not, _key = @selector (method name), _imp = function address components a bucket a position corresponding to the stored index. If a value corresponding to the start of the index has been saved, the index of -1, to keep the bucket here. Each bucket has a position index if the memory is empty buckets, mask_t multiplied by 2, to keep the bucket corresponding to the index position.

Extraction method from cache_t in the process:

The method of the SEL (_key), i.e. @selector (method name) & _ mask to obtain an index, and then removed from a _bucket _buckets The hash list index, and the _bucket Lane _key @selector (method name) comparison, if the method returns equal, if not equal, the index minus 1, look forward, has been found in 0, if still not found, skip to the last, the index position +1 has been found before. not null is returned.

Hash (hash table): using an algorithm to obtain the value as the index value acquisition, data access, space for time.

Find method process:

Cache_t go to a class to go to find, if you can not find in the list of methods to find methods to find the words to go out into cache_t in memory, if not found, locate its parent class by superCalss pointer in the parent class the cache_t where to find, can not find the method to find the list, find the words to cache_t subclass in existence (note that their cache_t). By superCalss not find the parent class's go, it has been to find a base class or throw an exception.

FIG class_data_bits_t & FAST_DATA_MASK -04 content can be obtained, which methods are two-dimensional array, exist many method_list_t (one-dimensional array), each array may be method_list_t a classification of all methods. The method or array of the class itself. method_list_t have kept many method_t, which content shown in Figure 05:


2304477-f331738d0ef15aab.png
Figure -04


Another: method structure

2304477-4742a3eefbfcc02b.png
Figure -05

Property list and a list of protocols is similar.

Note SEl: Method Name Method as serial number, which the first element is a char type, because it is the address string is converted to char, the structure address is the address of the first element. 

Content only for your own records learning content.

Reproduced in: https: //www.jianshu.com/p/245609b5190c

Guess you like

Origin blog.csdn.net/weixin_33910137/article/details/91166273