objective c - Is copy the same as Block_copy()? -



objective c - Is copy the same as Block_copy()? -

are same, copy , block_copy()? how? or if not, differences?

i curious while decided researches.

see found answer.

finally, found answer. copy , block_copy() same, not exactly.

here found :

let's take @ classes of blocks, there 3 of them :

__nsglobalblock__ - block of class if implement block without references outside of block.

void(^block)() = ^{ nslog(@"grazzzz"); };

__nsstackblock__ - block of class if implement block 1 or more references outside of block, (you when running on non-arc environment only. in arc env, arc copies automatically).

// non-arc code int theoutsider = 1234; void(^block)() = ^{ nslog(@"%d", theoutsider); };

__nsmallocblock__ - block of class if re-create __nsstackblock__ block.

int theoutsider = 1234; void(^block1)() = [^{ nslog(@"%d", theoutsider); } copy]; // non-arc version void(^block2)() = ^{ nslog(@"%d", theoutsider); }; // arc version

for __nsglobalblock__, when phone call copy it, nothing, return. so, ignore it. there no need move heap. below assembly of -[__nsglobalblock copy] (its super class) :

corefoundation`-[__nsglobalblock copy]: 0x7fff8bd016f0: pushq %rbp 0x7fff8bd016f1: movq %rsp, %rbp 0x7fff8bd016f4: movq %rdi, %rax 0x7fff8bd016f7: popq %rbp 0x7fff8bd016f8: ret 0x7fff8bd016f9: nopl (%rax)

for __nsstackblock__ , __nsmallocblock__, when phone call copy them, forwards copy method of super class, -[nsblock copy]. here assembly code on os x 10.9 sdk :

corefoundation`-[nsblock copy]: 0x7fff8bcc86a0: pushq %rbp 0x7fff8bcc86a1: movq %rsp, %rbp 0x7fff8bcc86a4: pushq %rbx 0x7fff8bcc86a5: pushq %rax 0x7fff8bcc86a6: movq %rdi, %rbx 0x7fff8bcc86a9: callq 0x7fff8be360ac ; symbol stub for: objc_collectingenabled 0x7fff8bcc86ae: movq %rbx, %rdi 0x7fff8bcc86b1: addq $0x8, %rsp 0x7fff8bcc86b5: testb %al, %al 0x7fff8bcc86b7: je 0x7fff8bcc86c0 ; -[nsblock copy] + 32 0x7fff8bcc86b9: popq %rbx 0x7fff8bcc86ba: popq %rbp 0x7fff8bcc86bb: jmpq 0x7fff8be361f6 ; symbol stub for: _block_copy_collectable 0x7fff8bcc86c0: popq %rbx 0x7fff8bcc86c1: popq %rbp 0x7fff8bcc86c2: jmpq 0x7fff8be361f0 ; symbol stub for: _block_copy 0x7fff8bcc86c7: nopw (%rax,%rax)

the assembly code on ios 7.1 sdk :

corefoundation`-[nsblock copy]: 0x17949d0: pushl %ebp 0x17949d1: movl %esp, %ebp 0x17949d3: popl %ebp 0x17949d4: jmp 0x18d3d8a ; symbol stub for: _block_copy 0x17949d9: nopl (%eax)

as can see in code, -[nsblock copy] phone call _block_copy!.

enough copy, let's take @ block_copy(). defined in block.h :

#define block_copy(...) ((__typeof(__va_args__))_block_copy((const void *)(__va_args__)))

block_copy() phone call _block_copy too!!

to point, can summarise copy , block_copy() same!.

hope can help same curiosity me :d.

objective-c objective-c-blocks

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -