blob: 4017c2ded19404cdf997e1a06f3b239a0698f3fa [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
Blaine Garst6757aaa2010-03-16 21:21:07 +0000332010/3/16 - rev for stret return, signature field
34
Chris Lattnere54c0b52009-03-09 06:57:46 +000035This document describes the Apple ABI implementation specification of Blocks.
36
Blaine Garst6757aaa2010-03-16 21:21:07 +000037The first shipping version of this ABI is found in Mac OS X 10.6, and shall be referred to as 10.6.ABI. Proposals have been made to enhance the blocks compiler and runtime, and these changes would form a new ABI called post-10.6.ABI if these changes were ever shipped in a product, which is purely speculative.
38
39Since the Apple ABI references symbols from other elements of the system, any attempt to use this ABI on systems prior to SnowLeopard is undefined.
40
Chris Lattnere54c0b52009-03-09 06:57:46 +0000411. High Level
42
Blaine Garst6757aaa2010-03-16 21:21:07 +000043The ABI of blocks consist of their layout and the runtime functions required by the compiler.
Chris Lattnere54c0b52009-03-09 06:57:46 +000044A Block consists of a structure of the following form:
45
46struct Block_literal_1 {
47 void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
48 int flags;
49 int reserved;
50 void (*invoke)(void *, ...);
51 struct Block_descriptor_1 {
52 unsigned long int reserved; // NULL
Blaine Garst6757aaa2010-03-16 21:21:07 +000053 unsigned long int size; // sizeof(struct Block_literal_1)
Chris Lattnere54c0b52009-03-09 06:57:46 +000054 // optional helper functions
Blaine Garst6757aaa2010-03-16 21:21:07 +000055 void (*copy_helper)(void *dst, void *src); // IFF (1<<25)
56 void (*dispose_helper)(void *src); // IFF (1<<25)
57 // required post 10.6.ABI
58 const char *signature; // IFF (1<<30)
Chris Lattnere54c0b52009-03-09 06:57:46 +000059 } *descriptor;
60 // imported variables
61};
62
Blaine Garst6757aaa2010-03-16 21:21:07 +000063The following flags bits are in use thusly for a possible post.10.6.ABI:
Chris Lattnere54c0b52009-03-09 06:57:46 +000064
65enum {
66 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
67 BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
68 BLOCK_IS_GLOBAL = (1 << 28),
Blaine Garst6757aaa2010-03-16 21:21:07 +000069 BLOCK_HAS_STRET = (1 << 29),
70 BLOCK_HAS_SIGNATURE = (1 << 30),
Chris Lattnere54c0b52009-03-09 06:57:46 +000071};
72
Blaine Garst6757aaa2010-03-16 21:21:07 +000073In 10.6.ABI the (1<<29) was unconditionally set and ignored by the runtime - it was a transitional marker that did not get deleted after the transition. This bit is now paired with (1<<30), and represented as the pair (3<<30), for the following combinations of valid bit settings, and their meanings.
74
75switch (flags & (3<<29)) {
76 case (0<<29): <unused> , error
77 case (1<<29): 10.6.ABI, no signature field available
78 case (2<<29): post-10.6.ABI, regular calling convention, presence of signature field
79 case (3<<29): post-10.6.ABI, street calling convention, presence of signature field,
80}
81
82The following discussions are presented as 10.6.ABI otherwise.
83
Chris Lattnere54c0b52009-03-09 06:57:46 +000084Block 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.
85
86When a Block literal expression is evaluated the stack based structure is initialized as follows:
87
881) static descriptor structure is declared and initialized as follows:
891a) 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.
901b) the size field is set to the size of the following Block literal structure.
911c) the copy_helper and dispose_helper function pointers are set to respective helper functions if they are required by the Block literal
922) a stack (or global) Block literal data structure is created and initialized as follows:
932a) 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.
942) 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.
95
Blaine Garst6757aaa2010-03-16 21:21:07 +000096
Chris Lattnere54c0b52009-03-09 06:57:46 +000097As an example, the Block literal expression
98 ^ { printf("hello world\n"); }
99would cause to be created on a 32-bit system:
100
101struct __block_literal_1 {
102 void *isa;
103 int flags;
104 int reserved;
105 void (*invoke)(struct __block_literal_1 *);
106 struct __block_descriptor_1 *descriptor;
107};
108
109void __block_invoke_1(struct __block_literal_1 *_block) {
110 printf("hello world\n");
111}
112
113static struct __block_descriptor_1 {
114 unsigned long int reserved;
115 unsigned long int Block_size;
116} __block_descriptor_1 = { 0, sizeof(struct __block_literal_1), __block_invoke_1 };
117
118and where the block literal appeared
119
120 struct __block_literal_1 _block_literal = {
121 &_NSConcreteStackBlock,
122 (1<<29), <uninitialized>,
123 __block_invoke_1,
124 &__block_descriptor_1
125 };
126
127Blocks import other Block references, const copies of other variables, and variables marked __block. In Objective-C variables may additionally be objects.
128
129When a Block literal expression used as the initial value of a global or static local variable it is initialized as follows:
130 struct __block_literal_1 __block_literal_1 = {
131 &_NSConcreteGlobalBlock,
132 (1<<28)|(1<<29), <uninitialized>,
133 __block_invoke_1,
134 &__block_descriptor_1
135 };
136that 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.
137
138
1392. Imported Variables
140
141Variables 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.
142
1432.1 Imported const copy variables
144
145Automatic storage variables not marked with __block are imported as const copies.
146
147The simplest example is that of importing a variable of type int.
148
149 int x = 10;
150 void (^vv)(void) = ^{ printf("x is %d\n", x); }
151 x = 11;
152 vv();
153
154would be compiled
155
156struct __block_literal_2 {
157 void *isa;
158 int flags;
159 int reserved;
160 void (*invoke)(struct __block_literal_2 *);
161 struct __block_descriptor_2 *descriptor;
162 const int x;
163};
164
165void __block_invoke_2(struct __block_literal_2 *_block) {
166 printf("x is %d\n", _block->x);
167}
168
169static struct __block_descriptor_2 {
170 unsigned long int reserved;
171 unsigned long int Block_size;
172} __block_descriptor_2 = { 0, sizeof(struct __block_literal_2) };
173
174and
175
176 struct __block_literal_2 __block_literal_2 = {
177 &_NSConcreteStackBlock,
178 (1<<29), <uninitialized>,
179 __block_invoke_2,
180 &__block_descriptor_2,
181 x
182 };
183
184In summary, scalars, structures, unions, and function pointers are generally imported as const copies with no need for helper functions.
185
1862.2 Imported const copy of Block reference
187
188The 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.
189
190An example:
191
192 void (^existingBlock)(void) = ...;
193 void (^vv)(void) = ^{ existingBlock(); }
194 vv();
195
196struct __block_literal_3 {
197 ...; // existing block
198};
199
200struct __block_literal_4 {
201 void *isa;
202 int flags;
203 int reserved;
204 void (*invoke)(struct __block_literal_4 *);
205 struct __block_literal_3 *const existingBlock;
206};
207
208void __block_invoke_4(struct __block_literal_2 *_block) {
209 __block->existingBlock->invoke(__block->existingBlock);
210}
211
212void __block_copy_4(struct __block_literal_4 *dst, struct __block_literal_4 *src) {
213 //_Block_copy_assign(&dst->existingBlock, src->existingBlock, 0);
214 _Block_object_assign(&dst->existingBlock, src->existingBlock, BLOCK_FIELD_IS_BLOCK);
215}
216
217void __block_dispose_4(struct __block_literal_4 *src) {
218 // was _Block_destroy
219 _Block_object_dispose(src->existingBlock, BLOCK_FIELD_IS_BLOCK);
220}
221
222static struct __block_descriptor_4 {
223 unsigned long int reserved;
224 unsigned long int Block_size;
225 void (*copy_helper)(struct __block_literal_4 *dst, struct __block_literal_4 *src);
226 void (*dispose_helper)(struct __block_literal_4 *);
227} __block_descriptor_4 = {
228 0,
229 sizeof(struct __block_literal_4),
230 __block_copy_4,
231 __block_dispose_4,
232};
233
234and where it is used
235
236 struct __block_literal_4 _block_literal = {
237 &_NSConcreteStackBlock,
238 (1<<25)|(1<<29), <uninitialized>
239 __block_invoke_4,
240 & __block_descriptor_4
241 existingBlock,
242 };
243
2442.2.1 Importing __attribute__((NSObject)) variables.
245
246GCC 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.
247
248For example, block xyzzy in the following
249
250 struct Opaque *__attribute__((NSObject)) objectPointer = ...;
251 ...
252 void (^xyzzy)(void) = ^{ CFPrint(objectPointer); };
253
254would have helper functions
255
256void __block_copy_xyzzy(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
257 _Block_object_assign(&dst->objectPointer, src-> objectPointer, BLOCK_FIELD_IS_OBJECT);
258}
259
260void __block_dispose_xyzzy(struct __block_literal_5 *src) {
261 _Block_object_dispose(src->objectPointer, BLOCK_FIELD_IS_OBJECT);
262}
263
264generated.
265
266
2672.3 Imported __block marked variables.
268
2692.3.1 Layout of __block marked variables
270
271The compiler must embed variables that are marked __block in a specialized structure of the form:
272
273struct _block_byref_xxxx {
274 void *isa;
275 struct Block_byref *forwarding;
276 int flags; //refcount;
277 int size;
278 typeof(marked_variable) marked_variable;
279};
280
281Variables 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:
282
283struct _block_byref_xxxx {
284 void *isa;
285 struct _block_byref_xxxx *forwarding;
286 int flags; //refcount;
287 int size;
288 // helper functions called via Block_copy() and Block_release()
289 void (*byref_keep)(void *dst, void *src);
290 void (*byref_dispose)(void *);
291 typeof(marked_variable) marked_variable;
292};
293
294The structure is initialized such that
295 a) the forwarding pointer is set to the beginning of its enclosing structure,
296 b) the size field is initialized to the total size of the enclosing structure,
297 c) the flags field is set to either 0 if no helper functions are needed or (1<<25) if they are,
298 d) the helper functions are initialized (if present)
299 e) the variable itself is set to its initial value.
300 f) the isa field is set to NULL
301
3022.3.2 Access to __block variables from within its lexical scope.
303
304In 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:
305
306 int __block i = 10;
307 i = 11;
308
309would be rewritten to be:
310
311 struct _block_byref_i {
312 void *isa;
313 struct _block_byref_i *forwarding;
314 int flags; //refcount;
315 int size;
316 int captured_i;
Zhongxing Xu77f41652009-12-08 05:05:26 +0000317 } i = { NULL, &i, 0, sizeof(struct _block_byref_i), 11 };
Chris Lattnere54c0b52009-03-09 06:57:46 +0000318
319 i.forwarding->captured_i = 11;
320
321In 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:
322
323 __block void (voidBlock)(void) = blockA;
324 voidBlock = blockB;
325
326would translate into
327
328struct _block_byref_voidBlock {
329 void *isa;
330 struct _block_byref_voidBlock *forwarding;
331 int flags; //refcount;
332 int size;
333 void (*byref_keep)(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src);
334 void (*byref_dispose)(struct _block_byref_voidBlock *);
335 void (^captured_voidBlock)(void);
336};
337
338void _block_byref_keep_helper(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
339 //_Block_copy_assign(&dst->captured_voidBlock, src->captured_voidBlock, 0);
340 _Block_object_assign(&dst->captured_voidBlock, src->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
341}
342
343void _block_byref_dispose_helper(struct _block_byref_voidBlock *param) {
344 //_Block_destroy(param->captured_voidBlock, 0);
345 _Block_object_dispose(param->captured_voidBlock, BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER)}
346
347and
348 struct _block_byref_voidBlock voidBlock = {( .forwarding=&voidBlock, .flags=(1<<25), .size=sizeof(struct _block_byref_voidBlock *),
349 .byref_keep=_block_byref_keep_helper, .byref_dispose=_block_byref_dispose_helper,
350 .captured_voidBlock=blockA };
351
352 voidBlock.forwarding->captured_voidBlock = blockB;
353
354
3552.3.3 Importing __block variables into Blocks
356
357A 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.
358
359For example:
360
361 int __block i = 2;
362 functioncall(^{ i = 10; });
363
364would translate to
365
366struct _block_byref_i {
367 void *isa; // set to NULL
368 struct _block_byref_voidBlock *forwarding;
369 int flags; //refcount;
370 int size;
371 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
372 void (*byref_dispose)(struct _block_byref_i *);
373 int captured_i;
374};
375
376
377struct __block_literal_5 {
378 void *isa;
379 int flags;
380 int reserved;
381 void (*invoke)(struct __block_literal_5 *);
382 struct __block_descriptor_5 *descriptor;
383 struct _block_byref_i *i_holder;
384};
385
386void __block_invoke_5(struct __block_literal_5 *_block) {
387 _block->forwarding->captured_i = 10;
388}
389
390void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
391 //_Block_byref_assign_copy(&dst->captured_i, src->captured_i);
392 _Block_object_assign(&dst->captured_i, src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
393}
394
395void __block_dispose_5(struct __block_literal_5 *src) {
396 //_Block_byref_release(src->captured_i);
397 _Block_object_dispose(src->captured_i, BLOCK_FIELD_IS_BYREF | BLOCK_BYREF_CALLER);
398}
399
400static struct __block_descriptor_5 {
401 unsigned long int reserved;
402 unsigned long int Block_size;
403 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
404 void (*dispose_helper)(struct __block_literal_5 *);
405} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5) __block_copy_5, __block_dispose_5 };
406
407and
408
409 struct _block_byref_i i = {( .forwarding=&i, .flags=0, .size=sizeof(struct _block_byref_i) )};
410 struct __block_literal_5 _block_literal = {
411 &_NSConcreteStackBlock,
412 (1<<25)|(1<<29), <uninitialized>,
413 __block_invoke_5,
414 &__block_descriptor_5,
415 2,
416 };
417
4182.3.4 Importing __attribute__((NSObject)) __block variables
419
420A __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.
421
4222.3.5 __block escapes
423
424Because 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.
425
426
4272.3.6 Nesting
428
429Blocks 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.
430
4313. Objective C Extensions to Blocks
432
4333.1 Importing Objects
434
435Objects 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.
436
437
4383.2 Blocks as Objects
439
440The 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.
441
4423.3 __weak __block Support
443
444Objective-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:
445 objc_read_weak(&block->byref_i->forwarding->i)
446
447The __weak variable is stored in a _block_byref_xxxx structure and the Block has copy and dispose helpers for this structure that call:
448 _Block_object_assign(&dest->_block_byref_i, src-> _block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
449and
450 _Block_object_dispose(src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BYREF);
451
452
453In turn, the block_byref copy support helpers distinguish between whether the __block variable is a Block or not and should either call:
454 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_OBJECT | BLOCK_BYREF_CALLER);
455for something declared as an object or
456 _Block_object_assign(&dest->_block_byref_i, src->_block_byref_i, BLOCK_FIELD_IS_WEAK | BLOCK_FIELD_IS_BLOCK | BLOCK_BYREF_CALLER);
457for something declared as a Block.
458
459A full example follows:
460
461
462 __block __weak id obj = <initialization expression>;
463 functioncall(^{ [obj somemessage]; });
464
465would translate to
466
467struct _block_byref_obj {
468 void *isa; // uninitialized
469 struct _block_byref_obj *forwarding;
470 int flags; //refcount;
471 int size;
472 void (*byref_keep)(struct _block_byref_i *dst, struct _block_byref_i *src);
473 void (*byref_dispose)(struct _block_byref_i *);
474 int captured_obj;
475};
476
477void _block_byref_obj_keep(struct _block_byref_voidBlock *dst, struct _block_byref_voidBlock *src) {
478 //_Block_copy_assign(&dst->captured_obj, src->captured_obj, 0);
479 _Block_object_assign(&dst->captured_obj, src->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
480}
481
482void _block_byref_obj_dispose(struct _block_byref_voidBlock *param) {
483 //_Block_destroy(param->captured_obj, 0);
484 _Block_object_dispose(param->captured_obj, BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK | BLOCK_BYREF_CALLER);
485};
486
487for the block byref part and
488
489struct __block_literal_5 {
490 void *isa;
491 int flags;
492 int reserved;
493 void (*invoke)(struct __block_literal_5 *);
494 struct __block_descriptor_5 *descriptor;
495 struct _block_byref_obj *byref_obj;
496};
497
498void __block_invoke_5(struct __block_literal_5 *_block) {
499 [objc_read_weak(&_block->byref_obj->forwarding->captured_obj) somemessage];
500}
501
502void __block_copy_5(struct __block_literal_5 *dst, struct __block_literal_5 *src) {
503 //_Block_byref_assign_copy(&dst->byref_obj, src->byref_obj);
504 _Block_object_assign(&dst->byref_obj, src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
505}
506
507void __block_dispose_5(struct __block_literal_5 *src) {
508 //_Block_byref_release(src->byref_obj);
509 _Block_object_dispose(src->byref_obj, BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK);
510}
511
512static struct __block_descriptor_5 {
513 unsigned long int reserved;
514 unsigned long int Block_size;
515 void (*copy_helper)(struct __block_literal_5 *dst, struct __block_literal_5 *src);
516 void (*dispose_helper)(struct __block_literal_5 *);
517} __block_descriptor_5 = { 0, sizeof(struct __block_literal_5), __block_copy_5, __block_dispose_5 };
518
519and within the compound statement:
520
521 struct _block_byref_obj obj = {( .forwarding=&obj, .flags=(1<<25), .size=sizeof(struct _block_byref_obj),
522 .byref_keep=_block_byref_obj_keep, .byref_dispose=_block_byref_obj_dispose,
523 .captured_obj = <initialization expression> )};
524
525 struct __block_literal_5 _block_literal = {
526 &_NSConcreteStackBlock,
527 (1<<25)|(1<<29), <uninitialized>,
528 __block_invoke_5,
529 &__block_descriptor_5,
530 &obj, // a reference to the on-stack structure containing "captured_obj"
531 };
532
533
534 functioncall(_block_literal->invoke(&_block_literal));
535
536
5374.0 C++ Support
538
539Within 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.
540
541As 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:
542
543{
544 FOO foo;
545 void (^block)(void) = ^{ printf("%d\n", foo.value()); };
546}
547
548The compiler would synthesize
549
550struct __block_literal_10 {
551 void *isa;
552 int flags;
553 int reserved;
554 void (*invoke)(struct __block_literal_10 *);
555 struct __block_descriptor_10 *descriptor;
556 const FOO foo;
557};
558
559void __block_invoke_10(struct __block_literal_10 *_block) {
560 printf("%d\n", _block->foo.value());
561}
562
563void __block_literal_10(struct __block_literal_10 *dst, struct __block_literal_10 *src) {
564 comp_ctor(&dst->foo, &src->foo);
565}
566
567void __block_dispose_10(struct __block_literal_10 *src) {
568 comp_dtor(&src->foo);
569}
570
571static struct __block_descriptor_10 {
572 unsigned long int reserved;
573 unsigned long int Block_size;
574 void (*copy_helper)(struct __block_literal_10 *dst, struct __block_literal_10 *src);
575 void (*dispose_helper)(struct __block_literal_10 *);
576} __block_descriptor_10 = { 0, sizeof(struct __block_literal_10), __block_copy_10, __block_dispose_10 };
577
578and the code would be:
579{
580 FOO foo;
581 comp_ctor(&foo); // default constructor
582 struct __block_literal_10 _block_literal = {
583 &_NSConcreteStackBlock,
584 (1<<25)|(1<<26)|(1<<29), <uninitialized>,
585 __block_invoke_10,
586 &__block_descriptor_10,
587 };
588 comp_ctor(&_block_literal->foo, &foo); // const copy into stack version
589 struct __block_literal_10 &block = &_block_literal; // assign literal to block variable
590 block->invoke(block); // invoke block
591 comp_dtor(&_block_literal->foo); // destroy stack version of const block copy
592 comp_dtor(&foo); // destroy original version
593}
594
595
596C++ 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.
597
598To support member variable and function access the compiler will synthesize a const pointer to a block version of the this pointer.
599
6005.0 Runtime Helper Functions
601
602The 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.
603
604The block copy helper function should, for each of the variables of the type mentioned above, call
605 _Block_object_assign(&dst->target, src->target, BLOCK_FIELD_<appropo>);
606in the copy helper and
607 _Block_object_dispose(->target, BLOCK_FIELD_<appropo>);
608in the dispose helper where
609 <appropo> is
610
611enum {
612 BLOCK_FIELD_IS_OBJECT = 3, // id, NSObject, __attribute__((NSObject)), block, ...
613 BLOCK_FIELD_IS_BLOCK = 7, // a block variable
614 BLOCK_FIELD_IS_BYREF = 8, // the on stack structure holding the __block variable
615
616 BLOCK_FIELD_IS_WEAK = 16, // declared __weak
617
618 BLOCK_BYREF_CALLER = 128, // called from byref copy/dispose helpers
619};
620
621and of course the CTORs/DTORs for const copied C++ objects.
622
623The 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.
624
625Under 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.
626
627The prototypes, and summary, of the helper functions are
628
629/* Certain field types require runtime assistance when being copied to the heap. The following function is used
630 to copy fields of types: blocks, pointers to byref structures, and objects (including __attribute__((NSObject)) pointers.
631 BLOCK_FIELD_IS_WEAK is orthogonal to the other choices which are mutually exclusive.
632 Only in a Block copy helper will one see BLOCK_FIELD_IS_BYREF.
633 */
634void _Block_object_assign(void *destAddr, const void *object, const int flags);
635
636/* Similarly a compiler generated dispose helper needs to call back for each field of the byref data structure.
637 (Currently the implementation only packs one field into the byref structure but in principle there could be more).
638 The same flags used in the copy helper should be used for each call generated to this function:
639 */
640void _Block_object_dispose(const void *object, const int flags);
641
642The following functions have been used and will continue to be supported until new compiler support is complete.
643
644// Obsolete functions.
645// Copy helper callback for copying a block imported into a Block
646// Called by copy_helper helper functions synthesized by the compiler.
647// The address in the destination block of an imported Block is provided as the first argument
648// and the value of the existing imported Block is the second.
649// Use: _Block_object_assign(dest, src, BLOCK_FIELD_IS_BLOCK {| BLOCK_FIELD_IS_WEAK});
650void _Block_copy_assign(struct Block_basic **dest, const struct Block_basic *src, const int flags);
651
652// Destroy helper callback for releasing Blocks imported into a Block
653// Called by dispose_helper helper functions synthesized by the compiler.
654// The value of the imported Block variable is passed back.
655// Use: _Block_object_dispose(src, BLOCK_FIELD_IS_BLOCK {| BLOCK_FIELD_IS_WEAK});
656void _Block_destroy(const struct Block_basic *src, const int flags);
657
658// Byref data block copy helper callback
659// Called by block copy helpers when copying __block structures
660// Use: _Block_object_assign(dest, src, BLOCK_FIELD_IS_BYREF {| BLOCK_FIELD_IS_WEAK});
661void _Block_byref_assign_copy(struct Block_byref **destp, struct Block_byref *src);
662
663// Byref data block release helper callback
664// Called by block release helpers when releasing a Block
665// Called at escape points in scope where __block variables live (under non-GC-only conditions)
666// Use: _Block_object_dispose(src, BLOCK_FIELD_IS_BYREF {| BLOCK_FIELD_IS_WEAK});
667void ยง(struct Block_byref *shared_struct);
668
669