探寻block

2020-07-09  本文已影响0人  SK_Wang

block的本质

block本质上也是一个OC对象,它内部也有个isa指针。
block是封装了函数调用以及函数调用环境的OC对象。

//
//  Created by SK on 2020/7/7.
//  Copyright © 2020 SK. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

NSString *name = @"jack";

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        int age = 20;
        static int sex = 0;
        Person *person1 = [[Person alloc] init];
        Person *person2 = [[Person alloc] init];
        Person *person3 = [[Person alloc] init];
        
        __strong Person *strongPerson = person1;
        __weak Person *weakPerson = person2;
        __block Person *blockPerson = person3;
        
        void (^block)(void) = ^{
            NSLog(@"%d-%d-%@", age, sex, name);
            [strongPerson run];
            [weakPerson run];
            [blockPerson run];
        };
        block();
        
    }
    return 0;
}

我们通过执行
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-9.0 main.m
编译一下上述代码,来看一下block的底层结构

struct __block_impl {
  void *isa;
  int Flags;
  int Reserved;
  void *FuncPtr;
};

struct __Block_byref_blockPerson_0 {
  void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 Person *__strong blockPerson;
};

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int age;
  int *sex;
  Person *__strong strongPerson;
  Person *__weak weakPerson;
  __Block_byref_blockPerson_0 *blockPerson; // by ref
}


static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __Block_byref_blockPerson_0 *blockPerson = __cself->blockPerson; // bound by ref
  int age = __cself->age; // bound by copy
  int *sex = __cself->sex; // bound by copy
  Person *__strong strongPerson = __cself->strongPerson; // bound by copy
  Person *__weak weakPerson = __cself->weakPerson; // bound by copy
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_h6_tk2fn5rd5r92tqhsv84hkj540000gn_T_main_e976c4_mi_1, age, (*sex), name);
            ((void (*)(id, SEL))(void *)objc_msgSend)((id)strongPerson, sel_registerName("run"));
            ((void (*)(id, SEL))(void *)objc_msgSend)((id)weakPerson, sel_registerName("run"));
            ((void (*)(id, SEL))(void *)objc_msgSend)((id)(blockPerson->__forwarding->blockPerson), sel_registerName("run"));
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->strongPerson, (void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->weakPerson, (void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->blockPerson, (void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}

static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
  void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 

        int age = 20;
        static int sex = 0;
        Person *person1 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
        Person *person2 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
        Person *person3 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));

        __attribute__((objc_ownership(strong))) Person *strongPerson = person1;
        __attribute__((objc_ownership(weak))) Person *weakPerson = person2;
        __attribute__((__blocks__(byref))) __Block_byref_blockPerson_0 blockPerson = {(void*)0,(__Block_byref_blockPerson_0 *)&blockPerson, 33554432, sizeof(__Block_byref_blockPerson_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, person3};

        void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, age, &sex, strongPerson, weakPerson, (__Block_byref_blockPerson_0 *)&blockPerson, 570425344));
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

    }
    return 0;
}

从上述代码可以看出,block的底层结构如下:


Block底层结构.png

block的类型

block有3种类型,可以通过调用class方法或者isa指针查看具体类型,最终都是继承自NSBlock类型。

block的变量捕获

为了保证block内部能够正常访问外部的变量,block有个变量捕获机制


image.png

在ARC环境下,编译器会根据情况自动将栈上的block复制到堆上,例如:

Auto类型变量

当block内部访问了对象类型的auto变量时

__block修饰符

struct __Block_byref_blockPerson_0 {
  void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 Person *__strong blockPerson;
};

__block的内存管理

循环引用

__weak typeof(self) weakSelf = self;
上一篇下一篇

猜你喜欢

热点阅读