How to find the structure of memory Everything is based on Apple’s open source code, so understand what we are looking at, class-class

All classes in OC come from a structure called objC_class

From this point of view, we can try to search the source layer for objc_class {

image.png

Ii. Keywords methodology: Due to the large amount of memory structure code

The cache_t structure is more than 200 lines long, with several related variables

So the landlord is going to start from each key word from the “point-line-plane” mode of thinking to study the object. (From micro to macro, and then come up with a whole idea)

It’s important to understand this, or else you’ll be confused about what the next bunch of pictures are for.

1._maskandBucketsmask_t

2.maskuint_16_t _flagsmask16_t

3.-occupied

4.struct bucket_t buckets()

image.png

5.mask_t mask()

image.png

6.mask_t occupied()

image.png

7.void incrememtOccupied()

image.png

8.capacity()

image.png

9.rellocate()

image.png

image.png

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

Create a space for newCapacity calloc

image.png

The old bucket is released, the new bucket is assigned, and the cache reclamation function is executed

Garbage usually puts data in registers like X16 and x32, not high x0 and so on, to prevent x0 to x8 from having data.

10.insert()

image.png

image.png

The insert function does the following: 1. Initialize occupied()+1

2. Determine the capacity

3. Set the cache hash header identifier index

image.png

4. Determine whether the next node in the cache is a BEGIN header

image.png

Initialize the occupied = 0

image.png

Release the cache

1. Get cache_t 2 from objc_class. On a real machine, Cache_t holds several useful variables and functions. MaskandBuckets are grouped together through logical bit operations to save memory. Cache_fill (Never cache before + initialize is done) 5 6.1 Branch 1: If the cache size is less than 3/4, do nothing. 6.2 Branch 2: If the cache size is less than 3/4, enter the memory allocation step 1.

2. SetBucketsAndMask: according to newMask < < maskShift | newBuckets 3. Occupied = 0. 4. Garbage_make_room & garbage_refs[garbage_count++]=data 6.3 Bucket 3: Cap =cap? Cap *2 :init_cache_size 6.4 Branch 4: Internal SEL and IMP assignments for the bucket of this secondary storage start after X30 and keep x0 to x8Copy the code

7. The most important idea in caching cache_t is “caching”. Apple even merges buckets and masks together to save memory, so 2-1 Maskandbucket is the punch line in the cache architecture.