blob: b2ad40576b5e32297c1b8b638571ba99b4dcdfb4 [file] [log] [blame]
Chris Lattnere54c0b52009-03-09 06:57:46 +00001Block Implementation Specification
2
3Copyright 2008-2009 Apple, Inc.
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21
220. History
23
242008/7/14 - created
252008/8/21 - revised, C++
262008/9/24 - add NULL isa field to __block storage
272008/10/1 - revise block layout to use a static descriptor structure
282008/10/6 - revise block layout to use an unsigned long int flags
292008/10/28 - specify use of _Block_object_assign/dispose for all "Object" types in helper functions
302008/10/30 - revise new layout to have invoke function in same place
312008/10/30 - add __weak support
32
33This document describes the Apple ABI implementation specification of Blocks.
34
351. High Level
36
37A Block consists of a structure of the following form:
38
39struct Block_literal_1 {
40 void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
41 int flags;
42 int reserved;
43 void (*invoke)(void *, ...);
44 struct Block_descriptor_1 {
45 unsigned long int reserved; // NULL
46 unsigned long int size; // sizeof(struct Block_literal_1)
47 // optional helper functions
48 void (*copy_helper)(void *dst, void *src);
49 void (*dispose_helper)(void *src);
50 } *descriptor;
51 // imported variables
52};
53
54The following flags bits are used by the compiler:
55
56enum {
57 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
58 BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
59 BLOCK_IS_GLOBAL = (1 << 28),
60 BLOCK_HAS_DESCRIPTOR = (1 << 29), // interim until complete world build is accomplished
61};
62
63Block literals may occur within functions where the structure is created in stack local memory. They may also appear as initialization expressions for Block variables of global or static local variables.
64
65When a Block literal expression is evaluated the stack based structure is initialized as follows:
66
671) static descriptor structure is declared and initialized as follows:
681a) the invoke function pointer is set to a function that takes the Block structure as its first argument and the rest of the arguments (if any) to the Block and executes the Block compound statement.
691b) the size field is set to the size of the following Block literal structure.
701c) the copy_helper and dispose_helper function pointers are set to respective helper functions if they are required by the Block literal
712) a stack (or global) Block literal data structure is created and initialized as follows:
722a) the isa field is set to the address of the external _NSConcreteStackBlock, which is a block of uninitialized memory supplied in libSystem, or _NSConcreteGlobalBlock if this is a static or file level block literal.
732) The flags field is set to zero unless there are variables imported into the block that need helper functions for program level Block_copy() and Block_release() operations, in which case the (1<<25) flags bit is set.
74
75As an example, the Block literal expression
76 ^ { printf("hello world\n"); }
77would cause to be created on a 32-bit system:
78
79struct __block_literal_1 {
80 void *isa;
81 int flags;
82 int reserved;
83 void (*invoke)(struct __block_literal_1 *);
84 struct __block_descriptor_1 *descriptor;
85};
86
87void __block_invoke_1(struct __block_literal_1 *_block) {
88 printf("hello world\n");
89}
90
91static struct __block_descriptor_1 {
92 unsigned long int reserved;
93 unsigned long int Block_size;
94} __block_descriptor_1 = { 0, sizeof(struct __block_literal_1), __block_invoke_1 };
95
96and where the block literal appeared
97
98 struct __block_literal_1 _block_literal = {
99 &_NSConcreteStackBlock,
100 (1<<29), <uninitialized>,
101 __block_invoke_1,
102 &__block_descriptor_1
103 };
104
105Blocks import other Block references, const copies of other variables, and variables marked __block. In Objective-C variables may additionally be objects.
106
107When a Block literal expression used as the initial value of a global or static local variable it is initialized as follows:
108 struct __block_literal_1 __block_literal_1 = {
109 &_NSConcreteGlobalBlock,
110 (1<<28)|(1<<29), <uninitialized>,
111 __block_invoke_1,
112 &__block_descriptor_1
113 };
114that is, a different address is provided as the first value and a particular (1<<28) bit is set in the flags field, and otherwise it is the same as for stack based Block literals. This is an optimization that can be used for any Block literal that imports no const or __block storage variables.
115
116
1172. Imported Variables
118
119Variables of "auto" storage class are imported as const copies. Variables of "__block" storage class are imported as a pointer to an enclosing data structure. Global variables are simply referenced and not considered as imported.
120
1212.1 Imported const copy variables
122
123Automatic storage variables not marked with __block are imported as const copies.
124
125The simplest example is that of importing a variable of type int.
126
127 int x = 10;
128 void (^vv)(void) = ^{ printf("x is %d\n", x); }
129 x = 11;
130 vv();
131
132would be compiled
133
134struct __block_literal_2 {
135 void *isa;
136 int flags;
137 int reserved;
138 void (*invoke)(struct __block_literal_2 *);
139 struct __block_descriptor_2 *descriptor;
140 const int x;
141};
142
143void __block_invoke_2(struct __block_literal_2 *_block) {
144 printf("x is %d\n", _block->x);
145}
146
147static struct __block_descriptor_2 {
148 unsigned long int reserved;
149 unsigned long int Block_size;
150} __block_descriptor_2 = { 0, sizeof(struct __block_literal_2) };
151
152and
153
154 struct __block_literal_2 __block_literal_2 = {
155 &_NSConcreteStackBlock,
156 (1<<29), <uninitialized>,
157 __block_invoke_2,
158 &__block_descriptor_2,
159 x
160 };
161
162In summary, scalars, structures, unions, and function pointers are generally imported as const copies with no need for helper functions.
163
1642.2 Imported const copy of Block reference
165
166The first case where copy and dispose helper functions are required is for the case of when a block itself is imported. In this case both a copy_helper function and a dispose_helper function are needed. The copy_helper function is passed both the existing stack based pointer and the pointer to the new heap version and should call back into the runtime to actually do the copy operation on the imported fields within the block. The runtime functions are all described in Section 5.0 Runtime Helper Functions.
167
168An example:
169
170 void (^existingBlock)(void) = ...;
171 void (^vv)(void) = ^{ existingBlock(); }
172 vv();
173
174struct __block_literal_3 {
175 ...; // existing block
176};
177
178struct __block_literal_4 {
179 void *isa;
180 int flags;
181 int reserved;
182 void (*invoke)(struct __block_literal_4 *);
183 struct __block_literal_3 *const existingBlock;
184};
185
186void __block_invoke_4(struct __block_literal_2 *_block) {
187 __block->existingBlock->invoke(__block->existingBlock);
188}
189
190void __block_copy_4(struct __block_literal_4 *dst, struct __block_literal_4 *src) {
191 //_Block_copy_assign(&dst->existingBlock, src->existingBlock, 0);
192 _Block_object_assign(&dst->existingBlock, src->existingBlock, BLOCK_FIELD_IS_BLOCK);
193}
194
195void __block_dispose_4(struct __block_literal_4 *src) {
196 // was _Block_destroy
197 _Block_object_dispose(src->existingBlock, BLOCK_FIELD_IS_BLOCK);
198}
199
200static struct __block_descriptor_4 {
201 unsigned long int reserved;
202 unsigned long int Block_size;
203 void (*copy_helper)(struct __block_literal_4 *dst, struct __block_literal_4 *src);
204 void (*dispose_helper)(struct __block_literal_4 *);
205} __block_descriptor_4 = {
206 0,
207 sizeof(struct __block_literal_4),
208 __block_copy_4,
209 __block_dispose_4,
210};
211
212and where it is used
213
214 struct __block_literal_4 _block_literal = {
215 &_NSConcreteStackBlock,
216 (1<<25)|(1<<29), <uninitialized>
217 __block_invoke_4,
218 & __block_descriptor_4
219 existingBlock,
220 };
221
2222.2.1 Importing __attribute__((NSObject)) variables.
223
224GCC introduces __attribute__((NSObject)) on structure pointers to mean "this is an object". This is useful because many low level data structures are declared as opaque structure pointers, e.g. CFStringRef, CFArrayRef, etc. When used from C, however, these are still really objects and are the second case where that requires copy and dispose helper functions to be generated. The copy helper functions generated by the compiler should use the _Block_object_assign runtime helper function and in the dispose helper the _Block_object_dispose runtime helper function should be called.
225
226For example, block xyzzy in the following
227
228 struct Opaque *__attribute__((NSObject)) objectPointer = ...;
229 ...
230 void (^xyzzy)(void) = ^{ CFPrint(objectPointer); };
231
232would have helper functions
233
234void __block_copy_xyzzy(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
235 _Block_object_assign(&dst->objectPointer, src-> objectPointer, BLOCK_FIELD_IS_OBJECT);
236}
237
238void __block_dispose_xyzzy(struct __block_literal_5 *src) {
239 _Block_object_dispose(src->objectPointer, BLOCK_FIELD_IS_OBJECT);
240}
241
242generated.
243
244
2452.3 Imported __block marked variables.
246
2472.3.1 Layout of __block marked variables
248
249The compiler must embed variables that are marked __block in a specialized structure of the form:
250
251struct _block_byref_xxxx {
252 void *isa;
253 struct Block_byref *forwarding;
254 int flags; //refcount;
255 int size;
256 typeof(marked_variable) marked_variable;
257};
258
259Variables of certain types require helper functions for when Block_copy() and Block_release() are performed upon a referencing Block. At the "C" level only variables that are of type Block or ones that have __attribute__((NSObject)) marked require helper functions. In Objective-C objects require helper functions and in C++ stack based objects require helper functions. Variables that require helper functions use the form:
260
261struct _block_byref_xxxx {
262 void *isa;
263 struct _block_byref_xxxx *forwarding;
264 int flags; //refcount;
265 int size;
266 // helper functions called via Block_copy() and Block_release()
267 void (*byref_keep)(void *dst, void *src);
268 void (*byref_dispose)(void *);
269 typeof(marked_variable) marked_variable;
270};
271
272The structure is initialized such that
273 a) the forwarding pointer is set to the beginning of its enclosing structure,
274 b) the size field is initialized to the total size of the enclosing structure,
275 c) the flags field is set to either 0 if no helper functions are needed or (1<<25) if they are,
276 d) the helper functions are initialized (if present)
277 e) the variable itself is set to its initial value.
278 f) the isa field is set to NULL
279
2802.3.2 Access to __block variables from within its lexical scope.
281
282In order to "move" the variable to the heap upon a copy_helper operation the compiler must rewrite access to such a variable to be indirect through the structures forwarding pointer. For example:
283
284 int __block i = 10;
285 i = 11;
286
287would be rewritten to be:
288
289 struct _block_byref_i {
290 void *isa;
291 struct _block_byref_i *forwarding;
292 int flags; //refcount;
293 int size;
294 int captured_i;
295 } i = { NULL, &i, 0, sizeof(struct _block_byref_i), 11;
296
297 i.forwarding->captured_i = 11;
298
299In the case of a Block reference variable being marked __block the helper code generated must use the _Block_object_assign and _Block_object_dispose routines supplied by the runtime to make the copies. For example:
300
301 __block void (voidBlock)(void) = blockA;
302 voidBlock = blockB;
303
304would translate into
305
306struct _block_byref_voidBlock {
307 void *isa;
308 struct _block_byref_voidBlock *forwarding;
309 int flags; //refcount;
310 int size;
311 void (*byref_keep)(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src);
312 void (*byref_dispose)(struct _block_byref_voidBlock *);
313 void (^captured_voidBlock)(void);
314};
315
316void _block_byref_keep_helper(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
317 //_Block_copy_assign(&dst->captured_voidBlock, src->captured_voidBlock, 0);
318 _Block_object_assign(&dst->captured_voidBlock, src->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
319}
320
321void _block_byref_dispose_helper(struct _block_byref_voidBlock *param) {
322 //_Block_destroy(param->captured_voidBlock, 0);
323 _Block_object_dispose(param->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER)}
324
325and
326 struct _block_byref_voidBlock voidBlock = {( .forwarding=&voidBlock, .flags=(1<<25), .size=sizeof(struct _block_byref_voidBlock *),
327 .byref_keep=_block_byref_keep_helper, .byref_dispose=_block_byref_dispose_helper,
328 .captured_voidBlock=blockA };
329
330 voidBlock.forwarding->captured_voidBlock = blockB;
331
332
3332.3.3 Importing __block variables into Blocks
334
335A Block that uses a __block variable in its compound statement body must import the variable and emit copy_helper and dispose_helper helper functions that, in turn, call back into the runtime to actually copy or release the byref data block using the functions _Block_object_assign and _Block_object_dispose.
336
337For example:
338
339 int __block i = 2;
340 functioncall(^{ i = 10; });
341
342would translate to
343
344struct _block_byref_i {
345 void *isa; // set to NULL
346 struct _block_byref_voidBlock *forwarding;
347 int flags; //refcount;
348 int size;
349 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
350 void (*byref_dispose)(struct _block_byref_i *);
351 int captured_i;
352};
353
354
355struct __block_literal_5 {
356 void *isa;
357 int flags;
358 int reserved;
359 void (*invoke)(struct __block_literal_5 *);
360 struct __block_descriptor_5 *descriptor;
361 struct _block_byref_i *i_holder;
362};
363
364void __block_invoke_5(struct __block_literal_5 *_block) {
365 _block->forwarding->captured_i = 10;
366}
367
368void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
369 //_Block_byref_assign_copy(&dst->captured_i, src->captured_i);
370 _Block_object_assign(&dst->captured_i, src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
371}
372
373void __block_dispose_5(struct __block_literal_5 *src) {
374 //_Block_byref_release(src->captured_i);
375 _Block_object_dispose(src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
376}
377
378static struct __block_descriptor_5 {
379 unsigned long int reserved;
380 unsigned long int Block_size;
381 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
382 void (*dispose_helper)(struct __block_literal_5 *);
383} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5) __block_copy_5, __block_dispose_5 };
384
385and
386
387 struct _block_byref_i i = {( .forwarding=&i, .flags=0, .size=sizeof(struct _block_byref_i) )};
388 struct __block_literal_5 _block_literal = {
389 &_NSConcreteStackBlock,
390 (1<<25)|(1<<29), <uninitialized>,
391 __block_invoke_5,
392 &__block_descriptor_5,
393 2,
394 };
395
3962.3.4 Importing __attribute__((NSObject)) __block variables
397
398A __block variable that is also marked __attribute__((NSObject)) should have byref_keep and byref_dispose helper functions that use _Block_object_assign and _Block_object_dispose.
399
4002.3.5 __block escapes
401
402Because Blocks referencing __block variables may have Block_copy() performed upon them the underlying storage for the variables may move to the heap. In Objective-C Garbage Collection Only compilation environments the heap used is the garbage collected one and no further action is required. Otherwise the compiler must issue a call to potentially release any heap storage for __block variables at all escapes or terminations of their scope.
403
404
4052.3.6 Nesting
406
407Blocks may contain Block literal expressions. Any variables used within inner blocks are imported into all enclosing Block scopes even if the variables are not used. This includes const imports as well as __block variables.
408
4093. Objective C Extensions to Blocks
410
4113.1 Importing Objects
412
413Objects should be treated as __attribute__((NSObject)) variables; all copy_helper, dispose_helper, byref_keep, and byref_dispose helper functions should use _Block_object_assign and _Block_object_dispose. There should be no code generated that uses -retain or -release methods.
414
415
4163.2 Blocks as Objects
417
418The compiler will treat Blocks as objects when synthesizing property setters and getters, will characterize them as objects when generating garbage collection strong and weak layout information in the same manner as objects, and will issue strong and weak write-barrier assignments in the same manner as objects.
419
4203.3 __weak __block Support
421
422Objective-C (and Objective-C++) support the __weak attribute on __block variables. Under normal circumstances the compiler uses the Objective-C runtime helper support functions objc_assign_weak and objc_read_weak. Both should continue to be used for all reads and writes of __weak __block variables:
423 objc_read_weak(&block->byref_i->forwarding->i)
424
425The __weak variable is stored in a _block_byref_xxxx structure and the Block has copy and dispose helpers for this structure that call:
426 _Block_object_assign(&dest->_block_byref_i, src-> _block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
427and
428 _Block_object_dispose(src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
429
430
431In turn, the block_byref copy support helpers distinguish between whether the __block variable is a Block or not and should either call:
432 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_OBJECT | BLOCK_BYREF_CALLER);
433for something declared as an object or
434 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
435for something declared as a Block.
436
437A full example follows:
438
439
440 __block __weak id obj = <initialization expression>;
441 functioncall(^{ [obj somemessage]; });
442
443would translate to
444
445struct _block_byref_obj {
446 void *isa; // uninitialized
447 struct _block_byref_obj *forwarding;
448 int flags; //refcount;
449 int size;
450 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
451 void (*byref_dispose)(struct _block_byref_i *);
452 int captured_obj;
453};
454
455void _block_byref_obj_keep(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
456 //_Block_copy_assign(&dst->captured_obj, src->captured_obj, 0);
457 _Block_object_assign(&dst->captured_obj, src->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
458}
459
460void _block_byref_obj_dispose(struct _block_byref_voidBlock *param) {
461 //_Block_destroy(param->captured_obj, 0);
462 _Block_object_dispose(param->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
463};
464
465for the block byref part and
466
467struct __block_literal_5 {
468 void *isa;
469 int flags;
470 int reserved;
471 void (*invoke)(struct __block_literal_5 *);
472 struct __block_descriptor_5 *descriptor;
473 struct _block_byref_obj *byref_obj;
474};
475
476void __block_invoke_5(struct __block_literal_5 *_block) {
477 [objc_read_weak(&_block->byref_obj->forwarding->captured_obj) somemessage];
478}
479
480void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
481 //_Block_byref_assign_copy(&dst->byref_obj, src->byref_obj);
482 _Block_object_assign(&dst->byref_obj, src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
483}
484
485void __block_dispose_5(struct __block_literal_5 *src) {
486 //_Block_byref_release(src->byref_obj);
487 _Block_object_dispose(src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
488}
489
490static struct __block_descriptor_5 {
491 unsigned long int reserved;
492 unsigned long int Block_size;
493 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
494 void (*dispose_helper)(struct __block_literal_5 *);
495} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5), __block_copy_5, __block_dispose_5 };
496
497and within the compound statement:
498
499 struct _block_byref_obj obj = {( .forwarding=&obj, .flags=(1<<25), .size=sizeof(struct _block_byref_obj),
500 .byref_keep=_block_byref_obj_keep, .byref_dispose=_block_byref_obj_dispose,
501 .captured_obj = <initialization expression> )};
502
503 struct __block_literal_5 _block_literal = {
504 &_NSConcreteStackBlock,
505 (1<<25)|(1<<29), <uninitialized>,
506 __block_invoke_5,
507 &__block_descriptor_5,
508 &obj, // a reference to the on-stack structure containing "captured_obj"
509 };
510
511
512 functioncall(_block_literal->invoke(&_block_literal));
513
514
5154.0 C++ Support
516
517Within a block stack based C++ objects are copied as const copies using the const copy constructor. It is an error if a stack based C++ object is used within a block if it does not have a const copy constructor. In addition both copy and destroy helper routines must be synthesized for the block to support the Block_copy() operation, and the flags work marked with the (1<<26) bit in addition to the (1<<25) bit. The copy helper should call the constructor using appropriate offsets of the variable within the supplied stack based block source and heap based destination for all const constructed copies, and similarly should call the destructor in the destroy routine.
518
519As an example, suppose a C++ class FOO existed with a const copy constructor. Within a code block a stack version of a FOO object is declared and used within a Block literal expression:
520
521{
522 FOO foo;
523 void (^block)(void) = ^{ printf("%d\n", foo.value()); };
524}
525
526The compiler would synthesize
527
528struct __block_literal_10 {
529 void *isa;
530 int flags;
531 int reserved;
532 void (*invoke)(struct __block_literal_10 *);
533 struct __block_descriptor_10 *descriptor;
534 const FOO foo;
535};
536
537void __block_invoke_10(struct __block_literal_10 *_block) {
538 printf("%d\n", _block->foo.value());
539}
540
541void __block_literal_10(struct __block_literal_10 *dst, struct __block_literal_10 *src) {
542 comp_ctor(&dst->foo, &src->foo);
543}
544
545void __block_dispose_10(struct __block_literal_10 *src) {
546 comp_dtor(&src->foo);
547}
548
549static struct __block_descriptor_10 {
550 unsigned long int reserved;
551 unsigned long int Block_size;
552 void (*copy_helper)(struct __block_literal_10 *dst, struct __block_literal_10 *src);
553 void (*dispose_helper)(struct __block_literal_10 *);
554} __block_descriptor_10 = { 0, sizeof(struct __block_literal_10), __block_copy_10, __block_dispose_10 };
555
556and the code would be:
557{
558 FOO foo;
559 comp_ctor(&foo); // default constructor
560 struct __block_literal_10 _block_literal = {
561 &_NSConcreteStackBlock,
562 (1<<25)|(1<<26)|(1<<29), <uninitialized>,
563 __block_invoke_10,
564 &__block_descriptor_10,
565 };
566 comp_ctor(&_block_literal->foo, &foo); // const copy into stack version
567 struct __block_literal_10 &block = &_block_literal; // assign literal to block variable
568 block->invoke(block); // invoke block
569 comp_dtor(&_block_literal->foo); // destroy stack version of const block copy
570 comp_dtor(&foo); // destroy original version
571}
572
573
574C++ objects stored in __block storage start out on the stack in a block_byref data structure as do other variables. Such objects (if not const objects) must support a regular copy constructor. The block_byref data structure will have copy and destroy helper routines synthesized by the compiler. The copy helper will have code created to perform the copy constructor based on the initial stack block_byref data structure, and will also set the (1<<26) bit in addition to the (1<<25) bit. The destroy helper will have code to do the destructor on the object stored within the supplied block_byref heap data structure.
575
576To support member variable and function access the compiler will synthesize a const pointer to a block version of the this pointer.
577
5785.0 Runtime Helper Functions
579
580The runtime helper functions are described in /usr/local/include/Block_private.h. To summarize their use, a block requires copy/dispose helpers if it imports any block variables, __block storage variables, __attribute__((NSObject)) variables, or C++ const copied objects with constructor/destructors. The (1<<26) bit is set and functions are generated.
581
582The block copy helper function should, for each of the variables of the type mentioned above, call
583 _Block_object_assign(&dst->target, src->target, BLOCK_FIELD_<appropo>);
584in the copy helper and
585 _Block_object_dispose(->target, BLOCK_FIELD_<appropo>);
586in the dispose helper where
587 <appropo> is
588
589enum {
590 BLOCK_FIELD_IS_OBJECT = 3, // id, NSObject, __attribute__((NSObject)), block, ...
591 BLOCK_FIELD_IS_BLOCK = 7, // a block variable
592 BLOCK_FIELD_IS_BYREF = 8, // the on stack structure holding the __block variable
593
594 BLOCK_FIELD_IS_WEAK = 16, // declared __weak
595
596 BLOCK_BYREF_CALLER = 128, // called from byref copy/dispose helpers
597};
598
599and of course the CTORs/DTORs for const copied C++ objects.
600
601The block_byref data structure similarly requires copy/dispose helpers for block variables, __attribute__((NSObject)) variables, or C++ const copied objects with constructor/destructors, and again the (1<<26) bit is set and functions are generated in the same manner.
602
603Under ObjC we allow __weak as an attribute on __block variables, and this causes the addition of BLOCK_FIELD_IS_WEAK orred onto the BLOCK_FIELD_IS_BYREF flag when copying the block_byref structure in the block copy helper, and onto the BLOCK_FIELD_<appropo> field within the block_byref copy/dispose helper calls.
604
605The prototypes, and summary, of the helper functions are
606
607/* Certain field types require runtime assistance when being copied to the heap. The following function is used
608 to copy fields of types: blocks, pointers to byref structures, and objects (including __attribute__((NSObject)) pointers.
609 BLOCK_FIELD_IS_WEAK is orthogonal to the other choices which are mutually exclusive.
610 Only in a Block copy helper will one see BLOCK_FIELD_IS_BYREF.
611 */
612void _Block_object_assign(void *destAddr, const void *object, const int flags);
613
614/* Similarly a compiler generated dispose helper needs to call back for each field of the byref data structure.
615 (Currently the implementation only packs one field into the byref structure but in principle there could be more).
616 The same flags used in the copy helper should be used for each call generated to this function:
617 */
618void _Block_object_dispose(const void *object, const int flags);
619
620The following functions have been used and will continue to be supported until new compiler support is complete.
621
622// Obsolete functions.
623// Copy helper callback for copying a block imported into a Block
624// Called by copy_helper helper functions synthesized by the compiler.
625// The address in the destination block of an imported Block is provided as the first argument
626// and the value of the existing imported Block is the second.
627// Use: _Block_object_assign(dest, src, BLOCK_FIELD_IS_BLOCK {| BLOCK_FIELD_IS_WEAK});
628void _Block_copy_assign(struct Block_basic **dest, const struct Block_basic *src, const int flags);
629
630// Destroy helper callback for releasing Blocks imported into a Block
631// Called by dispose_helper helper functions synthesized by the compiler.
632// The value of the imported Block variable is passed back.
633// Use: _Block_object_dispose(src, BLOCK_FIELD_IS_BLOCK {| BLOCK_FIELD_IS_WEAK});
634void _Block_destroy(const struct Block_basic *src, const int flags);
635
636// Byref data block copy helper callback
637// Called by block copy helpers when copying __block structures
638// Use: _Block_object_assign(dest, src, BLOCK_FIELD_IS_BYREF {| BLOCK_FIELD_IS_WEAK});
639void _Block_byref_assign_copy(struct Block_byref **destp, struct Block_byref *src);
640
641// Byref data block release helper callback
642// Called by block release helpers when releasing a Block
643// Called at escape points in scope where __block variables live (under non-GC-only conditions)
644// Use: _Block_object_dispose(src, BLOCK_FIELD_IS_BYREF {| BLOCK_FIELD_IS_WEAK});
645void ยง(struct Block_byref *shared_struct);
646
647