iOS学习笔记iOSblock

iOS Block __block说明符

2019-01-03  本文已影响3人  1江春水

系列文章:
iOS Block概念、语法及基本使用
iOS Block实现原理
iOS Block存储域及循环引用

上一节我们已经明白Block的实质了,语法奇特,最终也是转换为C语言来处理的。那么这一节来看看 __block修饰符到底干了啥,为什么使用了 __block修饰符,在Block内部就可以改变变量值了呢,带着疑问来看下编译后的源码。

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block int val = 10;
        void (^blk)(void) = ^{
            val = 2;
            printf("%d\n",val);
        };
        blk();
    }
    return 0;
}

编译后,
一步一步来看:

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

上述__block_impl结构体没有变化。

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
};

Desc结构体多了两个函数,copy dispose,貌似从字面上可以猜测到是用来干嘛的,持有和销毁。

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->val, (void*)src->val, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->val, 8/*BLOCK_FIELD_IS_BYREF*/);}

这两个函数貌似不就是对应__main_block_desc_0结构体copy和dispose函数嘛,现在只是猜想,接着往下看。

struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __Block_byref_val_0 *val; // by ref 
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_val_0 *_val, int flags=0) : val(_val->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

来看看Block变量编译后的Block结构体。多了 __Block_byref_val_0 *val结构体指针变量,这个东西是什么呢,带有val,val不是我们的变量名称吗(int val = 10),构造函数参数也有相应增加。

struct __Block_byref_val_0 {
  void *__isa;
__Block_byref_val_0 *__forwarding;
 int __flags;
 int __size;
 int val;
};

卧槽看到了吧,原来把我们的变量转成了结构体。__block int val = 10;这是源代码,如果不加
__block说明符,val会变成__main_block_impl_0的一个变量(上节我们已经得知),加上了__block说明符后,这个变量就变成了结构体了,神不神奇。

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __Block_byref_val_0 *val = __cself->val; // bound by ref 上下文拿到__cself,也就是__main_block_impl_0结构体实例

            (val->__forwarding->val) = 2;
            printf("%d\n",(val->__forwarding->val));
        }

该函数赋值给结构体__block_impl内的FuncPtr函数指针。

int main(int argc, const char * argv[]) {
    /* @autoreleasepool*/ { __AtAutoreleasePool __autoreleasepool; 
    
        __attribute__((__blocks__(byref))) __Block_byref_val_0 val = {//初始化
            (void*)0,
            (__Block_byref_val_0 *)&val,
            0,
            sizeof(__Block_byref_val_0),
            10
        };
        //block结构体实例初始化及赋值
        void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_val_0 *)&val, 570425344));
        //block调用
        ((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
    }
    return 0;
}

来看下main函数,把__block修饰的变量初始化 为_Block_byref_val_0结构体变量,并把默认值10带进去。Block的__main_block_impl_0结构体实例持有指向 __block 变量的__Block_byref_val_0结构体实例的指针。

__Block_byref_val_0结构体实例的成员变量_forwarding指针指向自己。通过__forwarding访问成员变量val(成员变量val是该实例自己持有的变量,它相当于原自动变量,也就是 int val= 10)。

_forwarding指针.png

下面看看 single APP 使用__block说明符编译后的源代码:

源代码
- (void)viewDidLoad {
    [super viewDidLoad];
    __block NSArray *arr = nil;
    void (^blk)(void) = ^{
        arr = [NSArray array];
    };
    blk();
}

编译后:

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

static struct __ViewController__viewDidLoad_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __ViewController__viewDidLoad_block_impl_0*, struct __ViewController__viewDidLoad_block_impl_0*);
  void (*dispose)(struct __ViewController__viewDidLoad_block_impl_0*);
} __ViewController__viewDidLoad_block_desc_0_DATA = {
    0,
    sizeof(struct __ViewController__viewDidLoad_block_impl_0),
    __ViewController__viewDidLoad_block_copy_0,
    __ViewController__viewDidLoad_block_dispose_0
};

//变量结构体
struct __Block_byref_arr_0 {
  void *__isa;
__Block_byref_arr_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 NSArray *arr;
};

//赋值给__block_impl结构体实例内的FuncPtr函数指针 
static void __ViewController__viewDidLoad_block_func_0(struct __ViewController__viewDidLoad_block_impl_0 *__cself) {
  __Block_byref_arr_0 *arr = __cself->arr; // bound by ref

        (arr->__forwarding->arr) = ((NSArray * _Nonnull (*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSArray"), sel_registerName("array"));
    }

//copy dispose
static void __ViewController__viewDidLoad_block_copy_0(struct __ViewController__viewDidLoad_block_impl_0*dst, struct __ViewController__viewDidLoad_block_impl_0*src) {_Block_object_assign((void*)&dst->arr, (void*)src->arr, 8/*BLOCK_FIELD_IS_BYREF*/);}

static void __ViewController__viewDidLoad_block_dispose_0(struct __ViewController__viewDidLoad_block_impl_0*src) {_Block_object_dispose((void*)src->arr, 8/*BLOCK_FIELD_IS_BYREF*/);}

//block结构体实例
struct __ViewController__viewDidLoad_block_impl_0 {
  struct __block_impl impl;
  struct __ViewController__viewDidLoad_block_desc_0* Desc;
  __Block_byref_arr_0 *arr; // by ref
  __ViewController__viewDidLoad_block_impl_0(void *fp, struct __ViewController__viewDidLoad_block_desc_0 *desc, __Block_byref_arr_0 *_arr, int flags=0) : arr(_arr->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

//viewDidLoad函数(后边为OC语言的默认参数:id self, SEL _cmd)
static void _I_ViewController_viewDidLoad(ViewController * self, SEL _cmd) {
    ((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("viewDidLoad"));
    
    __attribute__((__blocks__(byref))) __Block_byref_arr_0 arr = {
        (void*)0,
        (__Block_byref_arr_0 *)&arr,
        33554432,
        sizeof(__Block_byref_arr_0),
        __Block_byref_id_object_copy_131,
        __Block_byref_id_object_dispose_131,
        __null
    };
    
    //block结构体实例初始化并赋值
    void (*blk)(void) = ((void (*)())&__ViewController__viewDidLoad_block_impl_0((void *)__ViewController__viewDidLoad_block_func_0, &__ViewController__viewDidLoad_block_desc_0_DATA, (__Block_byref_arr_0 *)&arr, 570425344));
    
    //block调用
    ((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
}

以上就是__block说明符(具体说是存储域类说明符)所做的事情。

当然肯定会有疑问的,copy dispose 函数指针只是提了一下,它们到底做了哪些事情没有具体讲,下节会讲到。

上一篇下一篇

猜你喜欢

热点阅读