1.为什么要用alloc?

We often use alloc in OC development to allocate memory for objects for example

School *s1 = [School alloc]

2. Alloc process

The alloc process can easily be found in Apple’s open source file nsobject. mm. The following

objetc_alloc->alloc->_objc_rootAlloc->callAlloc->_objc_rootAllocWithZone->_class_createInstanceFromZone

You can see that there are three conditional judgments after **_class_createInstanceFromZone**. These three conditional judgments are the essence of alloc and what we need to study

image.png

1.size= cls->instanceSize

This line of code makes it clear that when we alloc we first need to know how much memory is needed to instantiate the class of the object.

instanceSize->cache.fastInstanceSize

image.png

image.png

The setFastInstanceSize function is used to remove the MASK that has been added.

Ps. IOS development exchange technology group: welcome to join, no matter you are big or small white welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together

If we go further, we will find three questions: 1. What is flags? 2. Why do logic and operations

image.png

1. Size =_flags & MASK (MASK is just a constant defined in the initialized area =8)

Size =16 indicates that an empty object can take up 16 space, depending on how many attributes the class has

2. Align16 (size + Extra-Macro 8byte)

return (x+15) & ~15

This code is a classic 16-byte alignment algorithm, please refer to the specific process of C language – logical operation.

image.png

2.obj=(id)calloc(1,size)

calloc->malloc_zone_calloc->zone->calloc->nano_malloc_check_clear->segregated_size_to_fit

It took a long time to find the key function

segregated_size_to_fit()

k=(size+nano_size -1 ) >> shift_nano_quantum

k=(x+15)>>4

slot_bytes=k<<4

3.obc->initInstanceIsa(cls,hasCxxDtor)

The object becomes a c++ structure with a size of 8 bytes x 8=64 bits when compiled by clang

initIsa->isa_t->newisa

Features: The structure of ISA_T is the union bit field containing CLS bits;

image.png

isa-union

ISA_BITFIELD here is what differentiates arm64 architecture from X86_64 architecture

image.png

Define

Nonpointer :1 Whether to enable pointer optimization for ISA. 0: Indicates pure ISA. 1: indicates that the ISA contains class information and object reference counting

Has_assoc :1 Whether there are associated objects

Has_cxx_dtor :1 whether c++ extensions are available

Uintptr_t shiftcls :33/44 ARM /x86 store class information

Magic :6 Specifies whether to initialize space

Weakly_referenced :1 is referred to an ARC weak reference variable that can not be released faster

Deallocating: 1 destructor

Has_sidetable_rc :1 Hash table When the reference count is greater than 10, you need to borrow this variable to store carry

Extra_rc :19 application counts

The newisa function does several things

1. Assign the value to bits cxx_dtor shiftcls

newisa.bits=ISA_MAGIC_VALUE; 0x001d800000000001ULL =59

newisa.has_cxx_dtor=hasCxxDtor

Newisa.shiftcls = CLS >>3 Move noptr ASOC CXX three places to the left

image.png

isa

Shows that ISA associates Pointers with classes

isa & 0x00007ffffffffff8ULL = class

The initIsa(CLS, True,hasCxxDtor) correlation class and extension completes the three steps of class_createInstanceFromZone under alloc.

We can find knowledge involved: ISA structure class algorithm cache logic operation and so on.