5 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 * @APPLE_LLVM_LICENSE_HEADER@
13 #include "Block_private.h"
17 #include <os/assumes.h>
21 #include <libkern/Block_private.h>
22 #include <libkern/OSRuntime.h>
24 #define malloc(s) kern_os_malloc((s))
25 #define free(a) kern_os_free((a))
29 #include <machine/atomic.h>
33 #define os_assumes(_x) (_x)
36 #define os_assert(_x) assert(_x)
40 #define _CRT_SECURE_NO_WARNINGS 1
43 OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
)
45 // fixme barrier is overkill -- see objc-os.h
46 long original
= InterlockedCompareExchange(dst
, newl
, oldl
);
47 return original
== oldl
;
51 OSAtomicCompareAndSwapInt(int oldi
, int newi
, int volatile *dst
)
53 // fixme barrier is overkill -- see objc-os.h
54 int original
= InterlockedCompareExchange(dst
, newi
, oldi
);
55 return original
== oldi
;
58 #define OSAtomicCompareAndSwapLong(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
59 #define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
63 /*******************************************************************************
65 ********************************************************************************/
68 latching_incr_int(volatile int32_t *where
)
71 int32_t old_value
= *where
;
72 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
73 return BLOCK_REFCOUNT_MASK
;
75 if (OSAtomicCompareAndSwapInt(old_value
, old_value
+ 2, where
)) {
82 latching_incr_int_not_deallocating(volatile int32_t *where
)
85 int32_t old_value
= *where
;
86 if (old_value
& BLOCK_DEALLOCATING
) {
87 // if deallocating we can't do this
90 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
91 // if latched, we're leaking this block, and we succeed
94 if (OSAtomicCompareAndSwapInt(old_value
, old_value
+ 2, where
)) {
95 // otherwise, we must store a new retained value without the deallocating bit set
102 // return should_deallocate?
104 latching_decr_int_should_deallocate(volatile int32_t *where
)
107 int32_t old_value
= *where
;
108 if ((old_value
& BLOCK_REFCOUNT_MASK
) == BLOCK_REFCOUNT_MASK
) {
109 return false; // latched high
111 if ((old_value
& BLOCK_REFCOUNT_MASK
) == 0) {
112 return false; // underflow, latch low
114 int32_t new_value
= old_value
- 2;
116 if ((old_value
& (BLOCK_REFCOUNT_MASK
| BLOCK_DEALLOCATING
)) == 2) {
117 new_value
= old_value
- 1;
120 if (OSAtomicCompareAndSwapInt(old_value
, new_value
, where
)) {
127 /**************************************************************************
128 * Framework callback functions and their default implementations.
129 ***************************************************************************/
131 #pragma mark Framework Callback Routines
135 _Block_retain_object_default(const void *ptr __unused
)
140 _Block_release_object_default(const void *ptr __unused
)
145 _Block_destructInstance_default(const void *aBlock __unused
)
149 static void (*_Block_retain_object
)(const void *ptr
) = _Block_retain_object_default
;
150 static void (*_Block_release_object
)(const void *ptr
) = _Block_release_object_default
;
151 static void (*_Block_destructInstance
) (const void *aBlock
) = _Block_destructInstance_default
;
154 /**************************************************************************
155 * Callback registration from ObjC runtime and CoreFoundation
156 ***************************************************************************/
159 _Block_use_RR2(const Block_callbacks_RR
*callbacks
)
161 _Block_retain_object
= callbacks
->retain
;
162 _Block_release_object
= callbacks
->release
;
163 _Block_destructInstance
= callbacks
->destructInstance
;
166 /****************************************************************************
167 * Accessors for block descriptor fields
168 *****************************************************************************/
170 static struct Block_descriptor_1
*
171 _Block_descriptor_1(struct Block_layout
*aBlock
)
173 return aBlock
->descriptor
;
177 static struct Block_descriptor_2
*
178 _Block_descriptor_2(struct Block_layout
*aBlock
)
180 if (!(aBlock
->flags
& BLOCK_HAS_COPY_DISPOSE
)) {
183 uint8_t *desc
= (uint8_t *)aBlock
->descriptor
;
184 desc
+= sizeof(struct Block_descriptor_1
);
185 return __IGNORE_WCASTALIGN((struct Block_descriptor_2
*)desc
);
188 static struct Block_descriptor_3
*
189 _Block_descriptor_3(struct Block_layout
*aBlock
)
191 if (!(aBlock
->flags
& BLOCK_HAS_SIGNATURE
)) {
194 uint8_t *desc
= (uint8_t *)aBlock
->descriptor
;
195 desc
+= sizeof(struct Block_descriptor_1
);
196 if (aBlock
->flags
& BLOCK_HAS_COPY_DISPOSE
) {
197 desc
+= sizeof(struct Block_descriptor_2
);
199 return __IGNORE_WCASTALIGN((struct Block_descriptor_3
*)desc
);
203 _Block_call_copy_helper(void *result
, struct Block_layout
*aBlock
)
205 struct Block_descriptor_2
*desc
= _Block_descriptor_2(aBlock
);
210 (*desc
->copy
)(result
, aBlock
); // do fixup
214 _Block_call_dispose_helper(struct Block_layout
*aBlock
)
216 struct Block_descriptor_2
*desc
= _Block_descriptor_2(aBlock
);
221 (*desc
->dispose
)(aBlock
);
224 /*******************************************************************************
225 * Internal Support routines for copying
226 ********************************************************************************/
229 #pragma mark Copy/Release support
232 // Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
234 _Block_copy(const void *arg
)
236 struct Block_layout
*aBlock
;
242 // The following would be better done as a switch statement
243 aBlock
= (struct Block_layout
*)arg
;
244 if (aBlock
->flags
& BLOCK_NEEDS_FREE
) {
246 latching_incr_int(&aBlock
->flags
);
248 } else if (aBlock
->flags
& BLOCK_IS_GLOBAL
) {
251 // Its a stack block. Make a copy.
252 struct Block_layout
*result
= (typeof(result
))malloc(aBlock
->descriptor
->size
);
256 memmove(result
, aBlock
, aBlock
->descriptor
->size
); // bitcopy first
257 #if __has_feature(ptrauth_calls)
258 // Resign the invoke pointer as it uses address authentication.
259 result
->invoke
= aBlock
->invoke
;
262 result
->flags
&= ~(BLOCK_REFCOUNT_MASK
| BLOCK_DEALLOCATING
); // XXX not needed
263 result
->flags
|= BLOCK_NEEDS_FREE
| 2; // logical refcount 1
264 _Block_call_copy_helper(result
, aBlock
);
265 // Set isa last so memory analysis tools see a fully-initialized object.
266 result
->isa
= _NSConcreteMallocBlock
;
272 // Runtime entry points for maintaining the sharing knowledge of byref data blocks.
274 // A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
275 // Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
276 // We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment and return it.
277 // Otherwise we need to copy it and update the stack forwarding pointer
278 static struct Block_byref
*
279 _Block_byref_copy(const void *arg
)
281 struct Block_byref
*src
= (struct Block_byref
*)arg
;
283 if ((src
->forwarding
->flags
& BLOCK_REFCOUNT_MASK
) == 0) {
284 // src points to stack
285 struct Block_byref
*copy
= (struct Block_byref
*)malloc(src
->size
);
287 // byref value 4 is logical refcount of 2: one for caller, one for stack
288 copy
->flags
= src
->flags
| BLOCK_BYREF_NEEDS_FREE
| 4;
289 copy
->forwarding
= copy
; // patch heap copy to point to itself
290 src
->forwarding
= copy
; // patch stack to point to heap copy
291 copy
->size
= src
->size
;
293 if (src
->flags
& BLOCK_BYREF_HAS_COPY_DISPOSE
) {
294 // Trust copy helper to copy everything of interest
295 // If more than one field shows up in a byref block this is wrong XXX
296 struct Block_byref_2
*src2
= (struct Block_byref_2
*)(src
+ 1);
297 struct Block_byref_2
*copy2
= (struct Block_byref_2
*)(copy
+ 1);
298 copy2
->byref_keep
= src2
->byref_keep
;
299 copy2
->byref_destroy
= src2
->byref_destroy
;
301 if (src
->flags
& BLOCK_BYREF_LAYOUT_EXTENDED
) {
302 struct Block_byref_3
*src3
= (struct Block_byref_3
*)(src2
+ 1);
303 struct Block_byref_3
*copy3
= (struct Block_byref_3
*)(copy2
+ 1);
304 copy3
->layout
= src3
->layout
;
307 (*src2
->byref_keep
)(copy
, src
);
310 // This copy includes Block_byref_3, if any.
311 memmove(copy
+ 1, src
+ 1, src
->size
- sizeof(*src
));
314 // already copied to heap
315 else if ((src
->forwarding
->flags
& BLOCK_BYREF_NEEDS_FREE
) == BLOCK_BYREF_NEEDS_FREE
) {
316 latching_incr_int(&src
->forwarding
->flags
);
319 return src
->forwarding
;
323 _Block_byref_release(const void *arg
)
325 struct Block_byref
*byref
= (struct Block_byref
*)arg
;
327 // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
328 byref
= byref
->forwarding
;
330 if (byref
->flags
& BLOCK_BYREF_NEEDS_FREE
) {
331 __assert_only
int32_t refcount
= byref
->flags
& BLOCK_REFCOUNT_MASK
;
333 if (latching_decr_int_should_deallocate(&byref
->flags
)) {
334 if (byref
->flags
& BLOCK_BYREF_HAS_COPY_DISPOSE
) {
335 struct Block_byref_2
*byref2
= (struct Block_byref_2
*)(byref
+ 1);
336 (*byref2
->byref_destroy
)(byref
);
344 /************************************************************
347 * _Block_copy, _Block_release, and (old) _Block_destroy
349 ***********************************************************/
356 // API entry point to release a copied Block
358 _Block_release(const void *arg
)
360 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
364 if (aBlock
->flags
& BLOCK_IS_GLOBAL
) {
367 if (!(aBlock
->flags
& BLOCK_NEEDS_FREE
)) {
371 if (latching_decr_int_should_deallocate(&aBlock
->flags
)) {
372 _Block_call_dispose_helper(aBlock
);
373 _Block_destructInstance(aBlock
);
379 _Block_tryRetain(const void *arg
)
381 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
382 return latching_incr_int_not_deallocating(&aBlock
->flags
);
386 _Block_isDeallocating(const void *arg
)
388 struct Block_layout
*aBlock
= (struct Block_layout
*)arg
;
389 return (aBlock
->flags
& BLOCK_DEALLOCATING
) != 0;
393 /************************************************************
395 * SPI used by other layers
397 ***********************************************************/
400 Block_size(void *aBlock
)
402 return ((struct Block_layout
*)aBlock
)->descriptor
->size
;
406 _Block_use_stret(void *aBlock
)
408 struct Block_layout
*layout
= (struct Block_layout
*)aBlock
;
410 int requiredFlags
= BLOCK_HAS_SIGNATURE
| BLOCK_USE_STRET
;
411 return (layout
->flags
& requiredFlags
) == requiredFlags
;
414 // Checks for a valid signature, not merely the BLOCK_HAS_SIGNATURE bit.
416 _Block_has_signature(void *aBlock
)
418 return _Block_signature(aBlock
) ? true : false;
422 _Block_signature(void *aBlock
)
424 struct Block_descriptor_3
*desc3
= _Block_descriptor_3((struct Block_layout
*)aBlock
);
429 return desc3
->signature
;
433 _Block_layout(void *aBlock
)
435 // Don't return extended layout to callers expecting old GC layout
436 struct Block_layout
*layout
= (struct Block_layout
*)aBlock
;
437 if (layout
->flags
& BLOCK_HAS_EXTENDED_LAYOUT
) {
441 struct Block_descriptor_3
*desc3
= _Block_descriptor_3((struct Block_layout
*)aBlock
);
446 return desc3
->layout
;
450 _Block_extended_layout(void *aBlock
)
452 // Don't return old GC layout to callers expecting extended layout
453 struct Block_layout
*layout
= (struct Block_layout
*)aBlock
;
454 if (!(layout
->flags
& BLOCK_HAS_EXTENDED_LAYOUT
)) {
458 struct Block_descriptor_3
*desc3
= _Block_descriptor_3((struct Block_layout
*)aBlock
);
463 // Return empty string (all non-object bytes) instead of NULL
464 // so callers can distinguish "empty layout" from "no layout".
465 if (!desc3
->layout
) {
468 return desc3
->layout
;
473 #pragma mark Compiler SPI entry points
477 /*******************************************************
479 * Entry points used by the compiler - the real API!
482 * A Block can reference four different kinds of things that require help when the Block is copied to the heap.
483 * 1) C++ stack based objects
484 * 2) References to Objective-C objects
486 * 4) __block variables
488 * In these cases helper functions are synthesized by the compiler for use in Block_copy and Block_release, called the copy and dispose helpers. The copy helper emits a call to the C++ const copy constructor for C++ stack based objects and for the rest calls into the runtime support function _Block_object_assign. The dispose helper has a call to the C++ destructor for case 1 and a call into _Block_object_dispose for the rest.
490 * The flags parameter of _Block_object_assign and _Block_object_dispose is set to
491 * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
492 * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
493 * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
494 * If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16)
496 * So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
498 * When a __block variable is either a C++ object, an Objective-C object, or another Block then the compiler also generates copy/dispose helper functions. Similarly to the Block copy helper, the "__block" copy helper (formerly and still a.k.a. "byref" copy helper) will do a C++ copy constructor (not a const one though!) and the dispose helper will do the destructor. And similarly the helpers will call into the same two support functions with the same values for objects and Blocks with the additional BLOCK_BYREF_CALLER (128) bit of information supplied.
500 * So the __block copy/dispose helpers will generate flag values of 3 or 7 for objects and Blocks respectively, with BLOCK_FIELD_IS_WEAK (16) or'ed as appropriate and always 128 or'd in, for the following set of possibilities:
501 * __block id 128+3 (0x83)
502 * __block (^Block) 128+7 (0x87)
503 * __weak __block id 128+3+16 (0x93)
504 * __weak __block (^Block) 128+7+16 (0x97)
507 ********************************************************/
510 // When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
511 // to do the assignment.
514 _Block_object_assign(void *destArg
, const void *object
, const int flags
)
516 const void **dest
= (const void **)destArg
;
517 switch (os_assumes(flags
& BLOCK_ALL_COPY_DISPOSE_FLAGS
)) {
518 case BLOCK_FIELD_IS_OBJECT
:
521 * [^{ object; } copy];
524 _Block_retain_object(object
);
528 case BLOCK_FIELD_IS_BLOCK
:
530 * void (^object)(void) = ...;
531 * [^{ object; } copy];
534 *dest
= _Block_copy(object
);
537 case BLOCK_FIELD_IS_BYREF
| BLOCK_FIELD_IS_WEAK
:
538 case BLOCK_FIELD_IS_BYREF
:
540 * // copy the onstack __block container to the heap
541 * // Note this __weak is old GC-weak/MRC-unretained.
542 * // ARC-style __weak is handled by the copy helper directly.
544 * __weak __block ... x;
548 *dest
= _Block_byref_copy(object
);
551 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
:
552 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
:
554 * // copy the actual field held in the __block container
555 * // Note this is MRC unretained __block only.
556 * // ARC retained __block is handled by the copy helper directly.
558 * __block void (^object)(void);
559 * [^{ object; } copy];
565 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
| BLOCK_FIELD_IS_WEAK
:
566 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
| BLOCK_FIELD_IS_WEAK
:
568 * // copy the actual field held in the __block container
569 * // Note this __weak is old GC-weak/MRC-unretained.
570 * // ARC-style __weak is handled by the copy helper directly.
571 * __weak __block id object;
572 * __weak __block void (^object)(void);
573 * [^{ object; } copy];
584 // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
585 // to help dispose of the contents
587 _Block_object_dispose(const void *object
, const int flags
)
589 switch (os_assumes(flags
& BLOCK_ALL_COPY_DISPOSE_FLAGS
)) {
590 case BLOCK_FIELD_IS_BYREF
| BLOCK_FIELD_IS_WEAK
:
591 case BLOCK_FIELD_IS_BYREF
:
592 // get rid of the __block data structure held in a Block
593 _Block_byref_release(object
);
595 case BLOCK_FIELD_IS_BLOCK
:
596 _Block_release(object
);
598 case BLOCK_FIELD_IS_OBJECT
:
599 _Block_release_object(object
);
601 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
:
602 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
:
603 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_OBJECT
| BLOCK_FIELD_IS_WEAK
:
604 case BLOCK_BYREF_CALLER
| BLOCK_FIELD_IS_BLOCK
| BLOCK_FIELD_IS_WEAK
:
612 // Workaround for <rdar://26015603> dylib with no __DATA segment fails to rebase
613 __attribute__((used
))
614 static int let_there_be_data
= 42;