[iOS]Block系列探究二 - 捕獲變量

上一篇文章中咱們得知了OC的block其實就是C的函數指針,這一篇文章咱們來研究下block與局部變量的各類化學反應。函數

1、截獲變量

咱們主要截獲三種變量,分別是全局變量、靜態全局變量、佈局變量、靜態局部變量,代碼以下:佈局

/** 全局變量 */
int global_count = 10;
/** 靜態全局變量 */
static int static_global_count = 10;

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 聲明block
        void (^block)(void);
        // 局部變量
        int count = 10;
        // 靜態局部變量
        static int static_count = 10;
        // block的實現
        block = ^{
            global_count = 11;
            static_global_count = 11;
            NSLog(@"count = %@", @(count));
            static_count = 11;
        };
        // block的調用
        block();
        // 從新賦值
        global_count = 12;
        static_global_count = 12;
        count = 12;
        static_count = 12;
    }
    return 0;
}
複製代碼

上面代碼,咱們向block傳了一個參數count,如今咱們clang一下,獲得下面的C/C++代碼:ui

// __block_imp結構體
struct __block_impl {
  void *isa;
  int Flags;
  int Reserved;
  void *FuncPtr;
};

int global_count = 10;

static int static_global_count = 10;

// __main_block_impl_0結構體
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int count;
  int *static_count;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _count, int *_static_count, int flags=0) : count(_count), static_count(_static_count) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

// __main_block_func_0函數
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  int count = __cself->count; // bound by copy
  int *static_count = __cself->static_count; // bound by copy

            global_count = 11;
            static_global_count = 11;
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_0r_hkkmpct143n4wd3xxk0l1j8c0000gn_T_main_23de66_mi_0, ((NSNumber *(*)(Class, SEL, int))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithInt:"), (int)(count)));
            (*static_count) = 11;
        }

// __main_block_desc_0結構體
static struct __main_block_desc_0 {
  size_t reserved;
  size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};

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

        void (*block)(void);

        int count = 10;

        static int static_count = 10;

        block = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, count, &static_count));

        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);

        global_count = 12;
        static_global_count = 12;
        count = 12;
        static_count = 12;
    }
    return 0;
}
複製代碼

咱們重點看一下__main_block_impl_0這個結構體:spa

// __main_block_impl_0結構體
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  int count;
  int *static_count;
  
  /** 這裏須要說明的是:冒號語法後面的內容至關於爲參數成員count初始化 int count = _count; */
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _count, int *_static_count, int flags=0) : count(_count), static_count(_static_count) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
複製代碼

咱們能夠看到__main_block_impl_0結構體多了兩個成員變量countstatic_count。 在聲明block時,count的值就已經被保存到了__main_block_impl_0結構體內部,注意這裏是值傳遞,也就是說在調用前對count從新賦值,並不會影響到__main_block_impl_0結構體內部的count成員變量的值。 一樣的,在聲明block是,靜態變量static_count的指針被保存到了__main_block_impl_0結構體內部,由於是引用傳遞,因此block內部對static_count的修改會直接影響到static_count的值。 全局變量global_count和靜態全局變量static_global_count由於做用域是全局的,因此block內部的修改會直接影響到它們的值。3d

2、__block說明符

咱們都知道block是沒法修改外部的局部變量的值的,可是__block卻可讓咱們修改外部的局部變量的值,接下來咱們探索一下緣由。 首先,上OC代碼:指針

// 聲明block
void (^block)(void);
__block int count = 10;
// block的實現
block = ^{
    count = 11;
};
// block的調用
block();
// 給count從新賦值
count = 12;
複製代碼

咱們clang一下,獲得下面代碼:code

// __block_impl結構體
struct __block_impl {
  void *isa;
  int Flags;
  int Reserved;
  void *FuncPtr;
};

// __Block_byref_count_0結構體
struct __Block_byref_count_0 {
  void *__isa;
__Block_byref_count_0 *__forwarding;
 int __flags;
 int __size;
 int count;
};

// __main_block_impl_0結構體
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  __Block_byref_count_0 *count; // by ref
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_count_0 *_count, int flags=0) : count(_count->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

// __main_block_func_0函數
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
    __Block_byref_count_0 *count = __cself->count; // bound by ref
    (count->__forwarding->count) = 11;
}

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

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

// __main_block_desc_0結構體
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; 

        void (*block)(void);
        __attribute__((__blocks__(byref))) __Block_byref_count_0 count = {(void*)0,(__Block_byref_count_0 *)&count, 0, sizeof(__Block_byref_count_0), 10};

        block = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_count_0 *)&count, 570425344));

        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
        
        (count.__forwarding->count) = 12;
    }
    return 0;
}
複製代碼

咱們發現,__main_block_desc_0結構體多了成員變量copydispose,另外還多了幾個函數和結構體,分別是__Block_byref_count_0結構體、__main_block_copy_0函數和__main_block_dispose_0函數。對象

2.一、__Block_byref_count_0結構體

咱們首先看一下__Block_byref_count_0這個結構體,代碼以下:作用域

// __Block_byref_count_0結構體
struct __Block_byref_count_0 {
  void *__isa;
__Block_byref_count_0 *__forwarding;
 int __flags;
 int __size;
 int count 
};
複製代碼

這個結構體是__block變量的包裝,至關於OC的對象,咱們看一下main()函數中該結構體的初始化函數:get

__attribute__((__blocks__(byref))) __Block_byref_count_0 count = {(void*)0,(__Block_byref_count_0 *)&count, 0, sizeof(__Block_byref_count_0), 10};
複製代碼

咱們能夠看到結構體中最後的成員變量至關於原局部變量,從成員變量名和初始化函數也能夠看出。 咱們來具體看一下__block變量的賦值:

// block的實現
block = ^{
    count = 11;
};
複製代碼

這段代碼的轉換以下:

// __main_block_func_0函數
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
    __Block_byref_count_0 *count = __cself->count; // bound by ref
    (count->__forwarding->count) = 11;
}
複製代碼

__main_block_impl_0結構體實例持有指向__block變量的__Block_byref_count_0結構體實例的指針。 __Block_byref_count_0結構體實例的成員變量__forwarding持有指向該實例自身的指針。經過成員變量__forwarding訪問成員變量count。成員變量count是該實例自身持有的變量,它至關於局部變量。


`__forwarding`變量、`__main_block_copy_0`函數和`__main_block_dispose_0`函數存在的意義咱們會在下一篇文章`Block存儲域`中探究。

3、總結

咱們總結一下,block捕獲變量分如下幾種狀況:

  • 變量爲全局變量和靜態全局變量時,block沒有額外處理,直接拿來用;
  • 變量爲局部變量時,只能捕獲在聲明block前的變量的值,在block內部不能修改該變量的值。
  • 變量爲靜態局部變量時,捕獲該變量的指針,在block內部能夠修改該變量的值。
  • 變量爲被__block修飾過的局部變量是,該變量被包裝成結構體(OC對象),該結構體的最後一個成員變量爲被__block修飾過的局部變量,block捕獲該結構體的指針,在block內部能夠修改該變量的值。
相關文章
相關標籤/搜索