1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * runtime.c
3*c54f35caSApple OSS Distributions * libclosure
4*c54f35caSApple OSS Distributions *
5*c54f35caSApple OSS Distributions * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
6*c54f35caSApple OSS Distributions *
7*c54f35caSApple OSS Distributions * @APPLE_LLVM_LICENSE_HEADER@
8*c54f35caSApple OSS Distributions */
9*c54f35caSApple OSS Distributions
10*c54f35caSApple OSS Distributions
11*c54f35caSApple OSS Distributions #ifndef KERNEL
12*c54f35caSApple OSS Distributions
13*c54f35caSApple OSS Distributions #include "Block_private.h"
14*c54f35caSApple OSS Distributions #include <stdio.h>
15*c54f35caSApple OSS Distributions #include <stdlib.h>
16*c54f35caSApple OSS Distributions #include <dlfcn.h>
17*c54f35caSApple OSS Distributions #include <os/assumes.h>
18*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
19*c54f35caSApple OSS Distributions
20*c54f35caSApple OSS Distributions #else /* !KERNEL */
21*c54f35caSApple OSS Distributions #define TARGET_OS_WIN32 0
22*c54f35caSApple OSS Distributions
23*c54f35caSApple OSS Distributions #include <libkern/Block_private.h>
24*c54f35caSApple OSS Distributions __BEGIN_DECLS
25*c54f35caSApple OSS Distributions #include <kern/kalloc.h>
26*c54f35caSApple OSS Distributions __END_DECLS
27*c54f35caSApple OSS Distributions
28*c54f35caSApple OSS Distributions /* void * is a bit of a lie, but that will have to do */
29*c54f35caSApple OSS Distributions KALLOC_TYPE_VAR_DEFINE(KT_BLOCK_LAYOUT, struct Block_layout, void *, KT_DEFAULT);
30*c54f35caSApple OSS Distributions KALLOC_TYPE_VAR_DEFINE(KT_BLOCK_BYREF, struct Block_byref, void *, KT_DEFAULT);
31*c54f35caSApple OSS Distributions
32*c54f35caSApple OSS Distributions static inline struct Block_layout *
block_layout_alloc(size_t size)33*c54f35caSApple OSS Distributions block_layout_alloc(size_t size)
34*c54f35caSApple OSS Distributions {
35*c54f35caSApple OSS Distributions return (struct Block_layout *)kalloc_type_var_impl(KT_BLOCK_LAYOUT,
36*c54f35caSApple OSS Distributions size, Z_WAITOK_ZERO_NOFAIL, NULL);
37*c54f35caSApple OSS Distributions }
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions static inline void
block_layout_free(Block_layout * ptr,size_t size)40*c54f35caSApple OSS Distributions block_layout_free(Block_layout *ptr, size_t size)
41*c54f35caSApple OSS Distributions {
42*c54f35caSApple OSS Distributions kfree_type_var_impl(KT_BLOCK_LAYOUT, ptr, size);
43*c54f35caSApple OSS Distributions }
44*c54f35caSApple OSS Distributions
45*c54f35caSApple OSS Distributions static inline struct Block_byref *
block_byref_alloc(size_t size)46*c54f35caSApple OSS Distributions block_byref_alloc(size_t size)
47*c54f35caSApple OSS Distributions {
48*c54f35caSApple OSS Distributions return (struct Block_byref *)kalloc_type_var_impl(KT_BLOCK_BYREF,
49*c54f35caSApple OSS Distributions size, Z_WAITOK_ZERO_NOFAIL, NULL);
50*c54f35caSApple OSS Distributions }
51*c54f35caSApple OSS Distributions
52*c54f35caSApple OSS Distributions static inline void
block_byref_free(Block_byref * ptr,size_t size)53*c54f35caSApple OSS Distributions block_byref_free(Block_byref *ptr, size_t size)
54*c54f35caSApple OSS Distributions {
55*c54f35caSApple OSS Distributions kfree_type_var_impl(KT_BLOCK_BYREF, ptr, size);
56*c54f35caSApple OSS Distributions }
57*c54f35caSApple OSS Distributions
58*c54f35caSApple OSS Distributions #endif /* KERNEL */
59*c54f35caSApple OSS Distributions
60*c54f35caSApple OSS Distributions #include <machine/atomic.h>
61*c54f35caSApple OSS Distributions #include <string.h>
62*c54f35caSApple OSS Distributions #include <stdint.h>
63*c54f35caSApple OSS Distributions #ifndef os_assumes
64*c54f35caSApple OSS Distributions #define os_assumes(_x) (_x)
65*c54f35caSApple OSS Distributions #endif
66*c54f35caSApple OSS Distributions #ifndef os_assert
67*c54f35caSApple OSS Distributions #define os_assert(_x) assert(_x)
68*c54f35caSApple OSS Distributions #endif
69*c54f35caSApple OSS Distributions
70*c54f35caSApple OSS Distributions #if TARGET_OS_WIN32
71*c54f35caSApple OSS Distributions #define _CRT_SECURE_NO_WARNINGS 1
72*c54f35caSApple OSS Distributions #include <windows.h>
73*c54f35caSApple OSS Distributions static __inline bool
OSAtomicCompareAndSwapLong(long oldl,long newl,long volatile * dst)74*c54f35caSApple OSS Distributions OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
75*c54f35caSApple OSS Distributions {
76*c54f35caSApple OSS Distributions // fixme barrier is overkill -- see objc-os.h
77*c54f35caSApple OSS Distributions long original = InterlockedCompareExchange(dst, newl, oldl);
78*c54f35caSApple OSS Distributions return original == oldl;
79*c54f35caSApple OSS Distributions }
80*c54f35caSApple OSS Distributions
81*c54f35caSApple OSS Distributions static __inline bool
OSAtomicCompareAndSwapInt(int oldi,int newi,int volatile * dst)82*c54f35caSApple OSS Distributions OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst)
83*c54f35caSApple OSS Distributions {
84*c54f35caSApple OSS Distributions // fixme barrier is overkill -- see objc-os.h
85*c54f35caSApple OSS Distributions int original = InterlockedCompareExchange(dst, newi, oldi);
86*c54f35caSApple OSS Distributions return original == oldi;
87*c54f35caSApple OSS Distributions }
88*c54f35caSApple OSS Distributions #else
89*c54f35caSApple OSS Distributions #define OSAtomicCompareAndSwapLong(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
90*c54f35caSApple OSS Distributions #define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
91*c54f35caSApple OSS Distributions #endif
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions /*******************************************************************************
95*c54f35caSApple OSS Distributions * Internal Utilities
96*c54f35caSApple OSS Distributions ********************************************************************************/
97*c54f35caSApple OSS Distributions
98*c54f35caSApple OSS Distributions static int32_t
latching_incr_int(volatile int32_t * where)99*c54f35caSApple OSS Distributions latching_incr_int(volatile int32_t *where)
100*c54f35caSApple OSS Distributions {
101*c54f35caSApple OSS Distributions while (1) {
102*c54f35caSApple OSS Distributions int32_t old_value = *where;
103*c54f35caSApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
104*c54f35caSApple OSS Distributions return BLOCK_REFCOUNT_MASK;
105*c54f35caSApple OSS Distributions }
106*c54f35caSApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
107*c54f35caSApple OSS Distributions return old_value + 2;
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions }
110*c54f35caSApple OSS Distributions }
111*c54f35caSApple OSS Distributions
112*c54f35caSApple OSS Distributions static bool
latching_incr_int_not_deallocating(volatile int32_t * where)113*c54f35caSApple OSS Distributions latching_incr_int_not_deallocating(volatile int32_t *where)
114*c54f35caSApple OSS Distributions {
115*c54f35caSApple OSS Distributions while (1) {
116*c54f35caSApple OSS Distributions int32_t old_value = *where;
117*c54f35caSApple OSS Distributions if (old_value & BLOCK_DEALLOCATING) {
118*c54f35caSApple OSS Distributions // if deallocating we can't do this
119*c54f35caSApple OSS Distributions return false;
120*c54f35caSApple OSS Distributions }
121*c54f35caSApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
122*c54f35caSApple OSS Distributions // if latched, we're leaking this block, and we succeed
123*c54f35caSApple OSS Distributions return true;
124*c54f35caSApple OSS Distributions }
125*c54f35caSApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
126*c54f35caSApple OSS Distributions // otherwise, we must store a new retained value without the deallocating bit set
127*c54f35caSApple OSS Distributions return true;
128*c54f35caSApple OSS Distributions }
129*c54f35caSApple OSS Distributions }
130*c54f35caSApple OSS Distributions }
131*c54f35caSApple OSS Distributions
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions // return should_deallocate?
134*c54f35caSApple OSS Distributions static bool
latching_decr_int_should_deallocate(volatile int32_t * where)135*c54f35caSApple OSS Distributions latching_decr_int_should_deallocate(volatile int32_t *where)
136*c54f35caSApple OSS Distributions {
137*c54f35caSApple OSS Distributions while (1) {
138*c54f35caSApple OSS Distributions int32_t old_value = *where;
139*c54f35caSApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
140*c54f35caSApple OSS Distributions return false; // latched high
141*c54f35caSApple OSS Distributions }
142*c54f35caSApple OSS Distributions if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
143*c54f35caSApple OSS Distributions return false; // underflow, latch low
144*c54f35caSApple OSS Distributions }
145*c54f35caSApple OSS Distributions int32_t new_value = old_value - 2;
146*c54f35caSApple OSS Distributions bool result = false;
147*c54f35caSApple OSS Distributions if ((old_value & (BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING)) == 2) {
148*c54f35caSApple OSS Distributions new_value = old_value - 1;
149*c54f35caSApple OSS Distributions result = true;
150*c54f35caSApple OSS Distributions }
151*c54f35caSApple OSS Distributions if (OSAtomicCompareAndSwapInt(old_value, new_value, where)) {
152*c54f35caSApple OSS Distributions return result;
153*c54f35caSApple OSS Distributions }
154*c54f35caSApple OSS Distributions }
155*c54f35caSApple OSS Distributions }
156*c54f35caSApple OSS Distributions
157*c54f35caSApple OSS Distributions
158*c54f35caSApple OSS Distributions /**************************************************************************
159*c54f35caSApple OSS Distributions * Framework callback functions and their default implementations.
160*c54f35caSApple OSS Distributions ***************************************************************************/
161*c54f35caSApple OSS Distributions #if !TARGET_OS_WIN32
162*c54f35caSApple OSS Distributions #pragma mark Framework Callback Routines
163*c54f35caSApple OSS Distributions #endif
164*c54f35caSApple OSS Distributions #if KERNEL
165*c54f35caSApple OSS Distributions static inline void
_Block_retain_object(const void * ptr __unused)166*c54f35caSApple OSS Distributions _Block_retain_object(const void *ptr __unused)
167*c54f35caSApple OSS Distributions {
168*c54f35caSApple OSS Distributions }
169*c54f35caSApple OSS Distributions
170*c54f35caSApple OSS Distributions static inline void
_Block_release_object(const void * ptr __unused)171*c54f35caSApple OSS Distributions _Block_release_object(const void *ptr __unused)
172*c54f35caSApple OSS Distributions {
173*c54f35caSApple OSS Distributions }
174*c54f35caSApple OSS Distributions
175*c54f35caSApple OSS Distributions static inline void
_Block_destructInstance(const void * aBlock __unused)176*c54f35caSApple OSS Distributions _Block_destructInstance(const void *aBlock __unused)
177*c54f35caSApple OSS Distributions {
178*c54f35caSApple OSS Distributions }
179*c54f35caSApple OSS Distributions
180*c54f35caSApple OSS Distributions #else
181*c54f35caSApple OSS Distributions
182*c54f35caSApple OSS Distributions static void
_Block_retain_object_default(const void * ptr __unused)183*c54f35caSApple OSS Distributions _Block_retain_object_default(const void *ptr __unused)
184*c54f35caSApple OSS Distributions {
185*c54f35caSApple OSS Distributions }
186*c54f35caSApple OSS Distributions
187*c54f35caSApple OSS Distributions static void
_Block_release_object_default(const void * ptr __unused)188*c54f35caSApple OSS Distributions _Block_release_object_default(const void *ptr __unused)
189*c54f35caSApple OSS Distributions {
190*c54f35caSApple OSS Distributions }
191*c54f35caSApple OSS Distributions
192*c54f35caSApple OSS Distributions static void
_Block_destructInstance_default(const void * aBlock __unused)193*c54f35caSApple OSS Distributions _Block_destructInstance_default(const void *aBlock __unused)
194*c54f35caSApple OSS Distributions {
195*c54f35caSApple OSS Distributions }
196*c54f35caSApple OSS Distributions
197*c54f35caSApple OSS Distributions static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default;
198*c54f35caSApple OSS Distributions static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default;
199*c54f35caSApple OSS Distributions static void (*_Block_destructInstance) (const void *aBlock) = _Block_destructInstance_default;
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions
202*c54f35caSApple OSS Distributions /**************************************************************************
203*c54f35caSApple OSS Distributions * Callback registration from ObjC runtime and CoreFoundation
204*c54f35caSApple OSS Distributions ***************************************************************************/
205*c54f35caSApple OSS Distributions
206*c54f35caSApple OSS Distributions void
_Block_use_RR2(const Block_callbacks_RR * callbacks)207*c54f35caSApple OSS Distributions _Block_use_RR2(const Block_callbacks_RR *callbacks)
208*c54f35caSApple OSS Distributions {
209*c54f35caSApple OSS Distributions _Block_retain_object = callbacks->retain;
210*c54f35caSApple OSS Distributions _Block_release_object = callbacks->release;
211*c54f35caSApple OSS Distributions _Block_destructInstance = callbacks->destructInstance;
212*c54f35caSApple OSS Distributions }
213*c54f35caSApple OSS Distributions #endif // !KERNEL
214*c54f35caSApple OSS Distributions
215*c54f35caSApple OSS Distributions /****************************************************************************
216*c54f35caSApple OSS Distributions * Accessors for block descriptor fields
217*c54f35caSApple OSS Distributions *****************************************************************************/
218*c54f35caSApple OSS Distributions
219*c54f35caSApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
220*c54f35caSApple OSS Distributions template <class T>
221*c54f35caSApple OSS Distributions static T *
unwrap_relative_pointer(int32_t & offset)222*c54f35caSApple OSS Distributions unwrap_relative_pointer(int32_t &offset)
223*c54f35caSApple OSS Distributions {
224*c54f35caSApple OSS Distributions if (offset == 0) {
225*c54f35caSApple OSS Distributions return nullptr;
226*c54f35caSApple OSS Distributions }
227*c54f35caSApple OSS Distributions
228*c54f35caSApple OSS Distributions uintptr_t base = (uintptr_t)&offset;
229*c54f35caSApple OSS Distributions uintptr_t extendedOffset = (uintptr_t)(intptr_t)offset;
230*c54f35caSApple OSS Distributions uintptr_t pointer = base + extendedOffset;
231*c54f35caSApple OSS Distributions return (T *)pointer;
232*c54f35caSApple OSS Distributions }
233*c54f35caSApple OSS Distributions #endif
234*c54f35caSApple OSS Distributions
235*c54f35caSApple OSS Distributions #if 0
236*c54f35caSApple OSS Distributions static struct Block_descriptor_2 *
237*c54f35caSApple OSS Distributions _Block_descriptor_2(struct Block_layout *aBlock)
238*c54f35caSApple OSS Distributions {
239*c54f35caSApple OSS Distributions uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
240*c54f35caSApple OSS Distributions desc += sizeof(struct Block_descriptor_1);
241*c54f35caSApple OSS Distributions return __IGNORE_WCASTALIGN((struct Block_descriptor_2 *)desc);
242*c54f35caSApple OSS Distributions }
243*c54f35caSApple OSS Distributions #endif
244*c54f35caSApple OSS Distributions
245*c54f35caSApple OSS Distributions static struct Block_descriptor_3 *
_Block_descriptor_3(struct Block_layout * aBlock)246*c54f35caSApple OSS Distributions _Block_descriptor_3(struct Block_layout *aBlock)
247*c54f35caSApple OSS Distributions {
248*c54f35caSApple OSS Distributions uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
249*c54f35caSApple OSS Distributions desc += sizeof(struct Block_descriptor_1);
250*c54f35caSApple OSS Distributions if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE) {
251*c54f35caSApple OSS Distributions desc += sizeof(struct Block_descriptor_2);
252*c54f35caSApple OSS Distributions }
253*c54f35caSApple OSS Distributions return __IGNORE_WCASTALIGN((struct Block_descriptor_3 *)desc);
254*c54f35caSApple OSS Distributions }
255*c54f35caSApple OSS Distributions
256*c54f35caSApple OSS Distributions static void
_Block_call_copy_helper(void * result,struct Block_layout * aBlock)257*c54f35caSApple OSS Distributions _Block_call_copy_helper(void *result, struct Block_layout *aBlock)
258*c54f35caSApple OSS Distributions {
259*c54f35caSApple OSS Distributions if (auto *pFn = _Block_get_copy_function(aBlock)) {
260*c54f35caSApple OSS Distributions pFn(result, aBlock);
261*c54f35caSApple OSS Distributions }
262*c54f35caSApple OSS Distributions }
263*c54f35caSApple OSS Distributions
264*c54f35caSApple OSS Distributions static void
_Block_call_dispose_helper(struct Block_layout * aBlock)265*c54f35caSApple OSS Distributions _Block_call_dispose_helper(struct Block_layout *aBlock)
266*c54f35caSApple OSS Distributions {
267*c54f35caSApple OSS Distributions if (auto *pFn = _Block_get_dispose_function(aBlock)) {
268*c54f35caSApple OSS Distributions pFn(aBlock);
269*c54f35caSApple OSS Distributions }
270*c54f35caSApple OSS Distributions }
271*c54f35caSApple OSS Distributions
272*c54f35caSApple OSS Distributions /*******************************************************************************
273*c54f35caSApple OSS Distributions * Internal Support routines for copying
274*c54f35caSApple OSS Distributions ********************************************************************************/
275*c54f35caSApple OSS Distributions
276*c54f35caSApple OSS Distributions #if !TARGET_OS_WIN32
277*c54f35caSApple OSS Distributions #pragma mark Copy/Release support
278*c54f35caSApple OSS Distributions #endif
279*c54f35caSApple OSS Distributions
280*c54f35caSApple OSS Distributions // Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
281*c54f35caSApple OSS Distributions void *
_Block_copy(const void * arg)282*c54f35caSApple OSS Distributions _Block_copy(const void *arg)
283*c54f35caSApple OSS Distributions {
284*c54f35caSApple OSS Distributions struct Block_layout *aBlock;
285*c54f35caSApple OSS Distributions
286*c54f35caSApple OSS Distributions if (!arg) {
287*c54f35caSApple OSS Distributions return NULL;
288*c54f35caSApple OSS Distributions }
289*c54f35caSApple OSS Distributions
290*c54f35caSApple OSS Distributions // The following would be better done as a switch statement
291*c54f35caSApple OSS Distributions aBlock = (struct Block_layout *)arg;
292*c54f35caSApple OSS Distributions if (aBlock->flags & BLOCK_NEEDS_FREE) {
293*c54f35caSApple OSS Distributions // latches on high
294*c54f35caSApple OSS Distributions latching_incr_int(&aBlock->flags);
295*c54f35caSApple OSS Distributions return aBlock;
296*c54f35caSApple OSS Distributions } else if (aBlock->flags & BLOCK_IS_GLOBAL) {
297*c54f35caSApple OSS Distributions return aBlock;
298*c54f35caSApple OSS Distributions } else {
299*c54f35caSApple OSS Distributions // Its a stack block. Make a copy.
300*c54f35caSApple OSS Distributions size_t size = Block_size(aBlock);
301*c54f35caSApple OSS Distributions struct Block_layout *result = block_layout_alloc(size);
302*c54f35caSApple OSS Distributions memmove(result, aBlock, size); // bitcopy first
303*c54f35caSApple OSS Distributions #if __has_feature(ptrauth_calls)
304*c54f35caSApple OSS Distributions // Resign the invoke pointer as it uses address authentication.
305*c54f35caSApple OSS Distributions result->invoke = aBlock->invoke;
306*c54f35caSApple OSS Distributions
307*c54f35caSApple OSS Distributions #if __has_feature(ptrauth_signed_block_descriptors)
308*c54f35caSApple OSS Distributions uintptr_t oldDesc =
309*c54f35caSApple OSS Distributions ptrauth_blend_discriminator(
310*c54f35caSApple OSS Distributions &aBlock->descriptor, _Block_descriptor_ptrauth_discriminator);
311*c54f35caSApple OSS Distributions uintptr_t newDesc =
312*c54f35caSApple OSS Distributions ptrauth_blend_discriminator(
313*c54f35caSApple OSS Distributions &result->descriptor, _Block_descriptor_ptrauth_discriminator);
314*c54f35caSApple OSS Distributions
315*c54f35caSApple OSS Distributions result->descriptor =
316*c54f35caSApple OSS Distributions ptrauth_auth_and_resign(aBlock->descriptor, ptrauth_key_asda, oldDesc,
317*c54f35caSApple OSS Distributions ptrauth_key_asda, newDesc);
318*c54f35caSApple OSS Distributions #endif
319*c54f35caSApple OSS Distributions #endif
320*c54f35caSApple OSS Distributions
321*c54f35caSApple OSS Distributions // reset refcount
322*c54f35caSApple OSS Distributions result->flags &= ~(BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING); // XXX not needed
323*c54f35caSApple OSS Distributions result->flags |= BLOCK_NEEDS_FREE | 2; // logical refcount 1
324*c54f35caSApple OSS Distributions _Block_call_copy_helper(result, aBlock);
325*c54f35caSApple OSS Distributions // Set isa last so memory analysis tools see a fully-initialized object.
326*c54f35caSApple OSS Distributions result->isa = _NSConcreteMallocBlock;
327*c54f35caSApple OSS Distributions return result;
328*c54f35caSApple OSS Distributions }
329*c54f35caSApple OSS Distributions }
330*c54f35caSApple OSS Distributions
331*c54f35caSApple OSS Distributions
332*c54f35caSApple OSS Distributions // Runtime entry points for maintaining the sharing knowledge of byref data blocks.
333*c54f35caSApple OSS Distributions
334*c54f35caSApple OSS Distributions // A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
335*c54f35caSApple OSS Distributions // Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
336*c54f35caSApple OSS Distributions // We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment and return it.
337*c54f35caSApple OSS Distributions // Otherwise we need to copy it and update the stack forwarding pointer
338*c54f35caSApple OSS Distributions static struct Block_byref *
_Block_byref_copy(const void * arg)339*c54f35caSApple OSS Distributions _Block_byref_copy(const void *arg)
340*c54f35caSApple OSS Distributions {
341*c54f35caSApple OSS Distributions struct Block_byref *src = (struct Block_byref *)arg;
342*c54f35caSApple OSS Distributions
343*c54f35caSApple OSS Distributions if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
344*c54f35caSApple OSS Distributions // src points to stack
345*c54f35caSApple OSS Distributions struct Block_byref *copy = block_byref_alloc(src->size);
346*c54f35caSApple OSS Distributions copy->isa = NULL;
347*c54f35caSApple OSS Distributions // byref value 4 is logical refcount of 2: one for caller, one for stack
348*c54f35caSApple OSS Distributions copy->flags = src->flags | BLOCK_BYREF_NEEDS_FREE | 4;
349*c54f35caSApple OSS Distributions copy->forwarding = copy; // patch heap copy to point to itself
350*c54f35caSApple OSS Distributions src->forwarding = copy; // patch stack to point to heap copy
351*c54f35caSApple OSS Distributions copy->size = src->size;
352*c54f35caSApple OSS Distributions
353*c54f35caSApple OSS Distributions if (src->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
354*c54f35caSApple OSS Distributions // Trust copy helper to copy everything of interest
355*c54f35caSApple OSS Distributions // If more than one field shows up in a byref block this is wrong XXX
356*c54f35caSApple OSS Distributions struct Block_byref_2 *src2 = (struct Block_byref_2 *)(src + 1);
357*c54f35caSApple OSS Distributions struct Block_byref_2 *copy2 = (struct Block_byref_2 *)(copy + 1);
358*c54f35caSApple OSS Distributions copy2->byref_keep = src2->byref_keep;
359*c54f35caSApple OSS Distributions copy2->byref_destroy = src2->byref_destroy;
360*c54f35caSApple OSS Distributions
361*c54f35caSApple OSS Distributions if (src->flags & BLOCK_BYREF_LAYOUT_EXTENDED) {
362*c54f35caSApple OSS Distributions struct Block_byref_3 *src3 = (struct Block_byref_3 *)(src2 + 1);
363*c54f35caSApple OSS Distributions struct Block_byref_3 *copy3 = (struct Block_byref_3*)(copy2 + 1);
364*c54f35caSApple OSS Distributions copy3->layout = src3->layout;
365*c54f35caSApple OSS Distributions }
366*c54f35caSApple OSS Distributions
367*c54f35caSApple OSS Distributions (*src2->byref_keep)(copy, src);
368*c54f35caSApple OSS Distributions } else {
369*c54f35caSApple OSS Distributions // Bitwise copy.
370*c54f35caSApple OSS Distributions // This copy includes Block_byref_3, if any.
371*c54f35caSApple OSS Distributions memmove(copy + 1, src + 1, src->size - sizeof(*src));
372*c54f35caSApple OSS Distributions }
373*c54f35caSApple OSS Distributions }
374*c54f35caSApple OSS Distributions // already copied to heap
375*c54f35caSApple OSS Distributions else if ((src->forwarding->flags & BLOCK_BYREF_NEEDS_FREE) == BLOCK_BYREF_NEEDS_FREE) {
376*c54f35caSApple OSS Distributions latching_incr_int(&src->forwarding->flags);
377*c54f35caSApple OSS Distributions }
378*c54f35caSApple OSS Distributions
379*c54f35caSApple OSS Distributions return src->forwarding;
380*c54f35caSApple OSS Distributions }
381*c54f35caSApple OSS Distributions
382*c54f35caSApple OSS Distributions static void
_Block_byref_release(const void * arg)383*c54f35caSApple OSS Distributions _Block_byref_release(const void *arg)
384*c54f35caSApple OSS Distributions {
385*c54f35caSApple OSS Distributions struct Block_byref *byref = (struct Block_byref *)arg;
386*c54f35caSApple OSS Distributions
387*c54f35caSApple OSS Distributions // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
388*c54f35caSApple OSS Distributions byref = byref->forwarding;
389*c54f35caSApple OSS Distributions
390*c54f35caSApple OSS Distributions if (byref->flags & BLOCK_BYREF_NEEDS_FREE) {
391*c54f35caSApple OSS Distributions __assert_only int32_t refcount = byref->flags & BLOCK_REFCOUNT_MASK;
392*c54f35caSApple OSS Distributions os_assert(refcount);
393*c54f35caSApple OSS Distributions if (latching_decr_int_should_deallocate(&byref->flags)) {
394*c54f35caSApple OSS Distributions if (byref->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
395*c54f35caSApple OSS Distributions struct Block_byref_2 *byref2 = (struct Block_byref_2 *)(byref + 1);
396*c54f35caSApple OSS Distributions (*byref2->byref_destroy)(byref);
397*c54f35caSApple OSS Distributions }
398*c54f35caSApple OSS Distributions block_byref_free(byref, byref->size);
399*c54f35caSApple OSS Distributions }
400*c54f35caSApple OSS Distributions }
401*c54f35caSApple OSS Distributions }
402*c54f35caSApple OSS Distributions
403*c54f35caSApple OSS Distributions
404*c54f35caSApple OSS Distributions /************************************************************
405*c54f35caSApple OSS Distributions *
406*c54f35caSApple OSS Distributions * API supporting SPI
407*c54f35caSApple OSS Distributions * _Block_copy, _Block_release, and (old) _Block_destroy
408*c54f35caSApple OSS Distributions *
409*c54f35caSApple OSS Distributions ***********************************************************/
410*c54f35caSApple OSS Distributions
411*c54f35caSApple OSS Distributions #if !TARGET_OS_WIN32
412*c54f35caSApple OSS Distributions #pragma mark SPI/API
413*c54f35caSApple OSS Distributions #endif
414*c54f35caSApple OSS Distributions
415*c54f35caSApple OSS Distributions
416*c54f35caSApple OSS Distributions // API entry point to release a copied Block
417*c54f35caSApple OSS Distributions void
_Block_release(const void * arg)418*c54f35caSApple OSS Distributions _Block_release(const void *arg)
419*c54f35caSApple OSS Distributions {
420*c54f35caSApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
421*c54f35caSApple OSS Distributions if (!aBlock) {
422*c54f35caSApple OSS Distributions return;
423*c54f35caSApple OSS Distributions }
424*c54f35caSApple OSS Distributions if (aBlock->flags & BLOCK_IS_GLOBAL) {
425*c54f35caSApple OSS Distributions return;
426*c54f35caSApple OSS Distributions }
427*c54f35caSApple OSS Distributions if (!(aBlock->flags & BLOCK_NEEDS_FREE)) {
428*c54f35caSApple OSS Distributions return;
429*c54f35caSApple OSS Distributions }
430*c54f35caSApple OSS Distributions
431*c54f35caSApple OSS Distributions if (latching_decr_int_should_deallocate(&aBlock->flags)) {
432*c54f35caSApple OSS Distributions _Block_call_dispose_helper(aBlock);
433*c54f35caSApple OSS Distributions _Block_destructInstance(aBlock);
434*c54f35caSApple OSS Distributions block_layout_free(aBlock, Block_size(aBlock));
435*c54f35caSApple OSS Distributions }
436*c54f35caSApple OSS Distributions }
437*c54f35caSApple OSS Distributions
438*c54f35caSApple OSS Distributions bool
_Block_tryRetain(const void * arg)439*c54f35caSApple OSS Distributions _Block_tryRetain(const void *arg)
440*c54f35caSApple OSS Distributions {
441*c54f35caSApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
442*c54f35caSApple OSS Distributions return latching_incr_int_not_deallocating(&aBlock->flags);
443*c54f35caSApple OSS Distributions }
444*c54f35caSApple OSS Distributions
445*c54f35caSApple OSS Distributions bool
_Block_isDeallocating(const void * arg)446*c54f35caSApple OSS Distributions _Block_isDeallocating(const void *arg)
447*c54f35caSApple OSS Distributions {
448*c54f35caSApple OSS Distributions struct Block_layout *aBlock = (struct Block_layout *)arg;
449*c54f35caSApple OSS Distributions return (aBlock->flags & BLOCK_DEALLOCATING) != 0;
450*c54f35caSApple OSS Distributions }
451*c54f35caSApple OSS Distributions
452*c54f35caSApple OSS Distributions
453*c54f35caSApple OSS Distributions /************************************************************
454*c54f35caSApple OSS Distributions *
455*c54f35caSApple OSS Distributions * SPI used by other layers
456*c54f35caSApple OSS Distributions *
457*c54f35caSApple OSS Distributions ***********************************************************/
458*c54f35caSApple OSS Distributions
459*c54f35caSApple OSS Distributions size_t
Block_size(void * aBlock)460*c54f35caSApple OSS Distributions Block_size(void *aBlock)
461*c54f35caSApple OSS Distributions {
462*c54f35caSApple OSS Distributions auto *layout = (Block_layout *)aBlock;
463*c54f35caSApple OSS Distributions void *desc = _Block_get_descriptor(layout);
464*c54f35caSApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
465*c54f35caSApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
466*c54f35caSApple OSS Distributions return ((Block_descriptor_small *)desc)->size;
467*c54f35caSApple OSS Distributions }
468*c54f35caSApple OSS Distributions #endif
469*c54f35caSApple OSS Distributions return ((Block_descriptor_1 *)desc)->size;
470*c54f35caSApple OSS Distributions }
471*c54f35caSApple OSS Distributions
472*c54f35caSApple OSS Distributions bool
_Block_use_stret(void * aBlock)473*c54f35caSApple OSS Distributions _Block_use_stret(void *aBlock)
474*c54f35caSApple OSS Distributions {
475*c54f35caSApple OSS Distributions struct Block_layout *layout = (struct Block_layout *)aBlock;
476*c54f35caSApple OSS Distributions
477*c54f35caSApple OSS Distributions int requiredFlags = BLOCK_HAS_SIGNATURE | BLOCK_USE_STRET;
478*c54f35caSApple OSS Distributions return (layout->flags & requiredFlags) == requiredFlags;
479*c54f35caSApple OSS Distributions }
480*c54f35caSApple OSS Distributions
481*c54f35caSApple OSS Distributions // Checks for a valid signature, not merely the BLOCK_HAS_SIGNATURE bit.
482*c54f35caSApple OSS Distributions bool
_Block_has_signature(void * aBlock)483*c54f35caSApple OSS Distributions _Block_has_signature(void *aBlock)
484*c54f35caSApple OSS Distributions {
485*c54f35caSApple OSS Distributions return _Block_signature(aBlock) ? true : false;
486*c54f35caSApple OSS Distributions }
487*c54f35caSApple OSS Distributions
488*c54f35caSApple OSS Distributions const char *
_Block_signature(void * aBlock)489*c54f35caSApple OSS Distributions _Block_signature(void *aBlock)
490*c54f35caSApple OSS Distributions {
491*c54f35caSApple OSS Distributions struct Block_layout *layout = (struct Block_layout *)aBlock;
492*c54f35caSApple OSS Distributions if (!(layout->flags & BLOCK_HAS_SIGNATURE)) {
493*c54f35caSApple OSS Distributions return nullptr;
494*c54f35caSApple OSS Distributions }
495*c54f35caSApple OSS Distributions
496*c54f35caSApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
497*c54f35caSApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
498*c54f35caSApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
499*c54f35caSApple OSS Distributions return unwrap_relative_pointer<const char>(bds->signature);
500*c54f35caSApple OSS Distributions }
501*c54f35caSApple OSS Distributions #endif
502*c54f35caSApple OSS Distributions
503*c54f35caSApple OSS Distributions struct Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
504*c54f35caSApple OSS Distributions return desc3->signature;
505*c54f35caSApple OSS Distributions }
506*c54f35caSApple OSS Distributions
507*c54f35caSApple OSS Distributions const char *
_Block_layout(void * aBlock)508*c54f35caSApple OSS Distributions _Block_layout(void *aBlock)
509*c54f35caSApple OSS Distributions {
510*c54f35caSApple OSS Distributions // Don't return extended layout to callers expecting old GC layout
511*c54f35caSApple OSS Distributions Block_layout *layout = (Block_layout *)aBlock;
512*c54f35caSApple OSS Distributions if ((layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
513*c54f35caSApple OSS Distributions !(layout->flags & BLOCK_HAS_SIGNATURE)) {
514*c54f35caSApple OSS Distributions return nullptr;
515*c54f35caSApple OSS Distributions }
516*c54f35caSApple OSS Distributions
517*c54f35caSApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
518*c54f35caSApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
519*c54f35caSApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
520*c54f35caSApple OSS Distributions return unwrap_relative_pointer<const char>(bds->layout);
521*c54f35caSApple OSS Distributions }
522*c54f35caSApple OSS Distributions #endif
523*c54f35caSApple OSS Distributions
524*c54f35caSApple OSS Distributions Block_descriptor_3 *desc = _Block_descriptor_3(layout);
525*c54f35caSApple OSS Distributions return desc->layout;
526*c54f35caSApple OSS Distributions }
527*c54f35caSApple OSS Distributions
528*c54f35caSApple OSS Distributions const char *
_Block_extended_layout(void * aBlock)529*c54f35caSApple OSS Distributions _Block_extended_layout(void *aBlock)
530*c54f35caSApple OSS Distributions {
531*c54f35caSApple OSS Distributions // Don't return old GC layout to callers expecting extended layout
532*c54f35caSApple OSS Distributions Block_layout *layout = (Block_layout *)aBlock;
533*c54f35caSApple OSS Distributions if (!(layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
534*c54f35caSApple OSS Distributions !(layout->flags & BLOCK_HAS_SIGNATURE)) {
535*c54f35caSApple OSS Distributions return nullptr;
536*c54f35caSApple OSS Distributions }
537*c54f35caSApple OSS Distributions
538*c54f35caSApple OSS Distributions const char *extLayout;
539*c54f35caSApple OSS Distributions #if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
540*c54f35caSApple OSS Distributions if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
541*c54f35caSApple OSS Distributions auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
542*c54f35caSApple OSS Distributions if (layout->flags & BLOCK_INLINE_LAYOUT_STRING) {
543*c54f35caSApple OSS Distributions extLayout = (const char *)(uintptr_t)bds->layout;
544*c54f35caSApple OSS Distributions } else {
545*c54f35caSApple OSS Distributions extLayout = unwrap_relative_pointer<const char>(bds->layout);
546*c54f35caSApple OSS Distributions }
547*c54f35caSApple OSS Distributions } else
548*c54f35caSApple OSS Distributions #endif
549*c54f35caSApple OSS Distributions {
550*c54f35caSApple OSS Distributions Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
551*c54f35caSApple OSS Distributions extLayout = desc3->layout;
552*c54f35caSApple OSS Distributions }
553*c54f35caSApple OSS Distributions
554*c54f35caSApple OSS Distributions // Return empty string (all non-object bytes) instead of NULL
555*c54f35caSApple OSS Distributions // so callers can distinguish "empty layout" from "no layout".
556*c54f35caSApple OSS Distributions if (!extLayout) {
557*c54f35caSApple OSS Distributions extLayout = "";
558*c54f35caSApple OSS Distributions }
559*c54f35caSApple OSS Distributions return extLayout;
560*c54f35caSApple OSS Distributions }
561*c54f35caSApple OSS Distributions
562*c54f35caSApple OSS Distributions #if !TARGET_OS_WIN32
563*c54f35caSApple OSS Distributions #pragma mark Compiler SPI entry points
564*c54f35caSApple OSS Distributions #endif
565*c54f35caSApple OSS Distributions
566*c54f35caSApple OSS Distributions
567*c54f35caSApple OSS Distributions /*******************************************************
568*c54f35caSApple OSS Distributions *
569*c54f35caSApple OSS Distributions * Entry points used by the compiler - the real API!
570*c54f35caSApple OSS Distributions *
571*c54f35caSApple OSS Distributions *
572*c54f35caSApple OSS Distributions * A Block can reference four different kinds of things that require help when the Block is copied to the heap.
573*c54f35caSApple OSS Distributions * 1) C++ stack based objects
574*c54f35caSApple OSS Distributions * 2) References to Objective-C objects
575*c54f35caSApple OSS Distributions * 3) Other Blocks
576*c54f35caSApple OSS Distributions * 4) __block variables
577*c54f35caSApple OSS Distributions *
578*c54f35caSApple OSS Distributions * 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.
579*c54f35caSApple OSS Distributions *
580*c54f35caSApple OSS Distributions * The flags parameter of _Block_object_assign and _Block_object_dispose is set to
581*c54f35caSApple OSS Distributions * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
582*c54f35caSApple OSS Distributions * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
583*c54f35caSApple OSS Distributions * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
584*c54f35caSApple OSS Distributions * If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16)
585*c54f35caSApple OSS Distributions *
586*c54f35caSApple OSS Distributions * So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
587*c54f35caSApple OSS Distributions *
588*c54f35caSApple OSS Distributions * 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.
589*c54f35caSApple OSS Distributions *
590*c54f35caSApple OSS Distributions * 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:
591*c54f35caSApple OSS Distributions * __block id 128+3 (0x83)
592*c54f35caSApple OSS Distributions * __block (^Block) 128+7 (0x87)
593*c54f35caSApple OSS Distributions * __weak __block id 128+3+16 (0x93)
594*c54f35caSApple OSS Distributions * __weak __block (^Block) 128+7+16 (0x97)
595*c54f35caSApple OSS Distributions *
596*c54f35caSApple OSS Distributions *
597*c54f35caSApple OSS Distributions ********************************************************/
598*c54f35caSApple OSS Distributions
599*c54f35caSApple OSS Distributions //
600*c54f35caSApple OSS Distributions // When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
601*c54f35caSApple OSS Distributions // to do the assignment.
602*c54f35caSApple OSS Distributions //
603*c54f35caSApple OSS Distributions void
_Block_object_assign(void * destArg,const void * object,const int flags)604*c54f35caSApple OSS Distributions _Block_object_assign(void *destArg, const void *object, const int flags)
605*c54f35caSApple OSS Distributions {
606*c54f35caSApple OSS Distributions const void **dest = (const void **)destArg;
607*c54f35caSApple OSS Distributions switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
608*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_OBJECT:
609*c54f35caSApple OSS Distributions /*******
610*c54f35caSApple OSS Distributions * id object = ...;
611*c54f35caSApple OSS Distributions * [^{ object; } copy];
612*c54f35caSApple OSS Distributions ********/
613*c54f35caSApple OSS Distributions
614*c54f35caSApple OSS Distributions _Block_retain_object(object);
615*c54f35caSApple OSS Distributions *dest = object;
616*c54f35caSApple OSS Distributions break;
617*c54f35caSApple OSS Distributions
618*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BLOCK:
619*c54f35caSApple OSS Distributions /*******
620*c54f35caSApple OSS Distributions * void (^object)(void) = ...;
621*c54f35caSApple OSS Distributions * [^{ object; } copy];
622*c54f35caSApple OSS Distributions ********/
623*c54f35caSApple OSS Distributions
624*c54f35caSApple OSS Distributions *dest = _Block_copy(object);
625*c54f35caSApple OSS Distributions break;
626*c54f35caSApple OSS Distributions
627*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
628*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BYREF:
629*c54f35caSApple OSS Distributions /*******
630*c54f35caSApple OSS Distributions * // copy the onstack __block container to the heap
631*c54f35caSApple OSS Distributions * // Note this __weak is old GC-weak/MRC-unretained.
632*c54f35caSApple OSS Distributions * // ARC-style __weak is handled by the copy helper directly.
633*c54f35caSApple OSS Distributions * __block ... x;
634*c54f35caSApple OSS Distributions * __weak __block ... x;
635*c54f35caSApple OSS Distributions * [^{ x; } copy];
636*c54f35caSApple OSS Distributions ********/
637*c54f35caSApple OSS Distributions
638*c54f35caSApple OSS Distributions *dest = _Block_byref_copy(object);
639*c54f35caSApple OSS Distributions break;
640*c54f35caSApple OSS Distributions
641*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
642*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
643*c54f35caSApple OSS Distributions /*******
644*c54f35caSApple OSS Distributions * // copy the actual field held in the __block container
645*c54f35caSApple OSS Distributions * // Note this is MRC unretained __block only.
646*c54f35caSApple OSS Distributions * // ARC retained __block is handled by the copy helper directly.
647*c54f35caSApple OSS Distributions * __block id object;
648*c54f35caSApple OSS Distributions * __block void (^object)(void);
649*c54f35caSApple OSS Distributions * [^{ object; } copy];
650*c54f35caSApple OSS Distributions ********/
651*c54f35caSApple OSS Distributions
652*c54f35caSApple OSS Distributions *dest = object;
653*c54f35caSApple OSS Distributions break;
654*c54f35caSApple OSS Distributions
655*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
656*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
657*c54f35caSApple OSS Distributions /*******
658*c54f35caSApple OSS Distributions * // copy the actual field held in the __block container
659*c54f35caSApple OSS Distributions * // Note this __weak is old GC-weak/MRC-unretained.
660*c54f35caSApple OSS Distributions * // ARC-style __weak is handled by the copy helper directly.
661*c54f35caSApple OSS Distributions * __weak __block id object;
662*c54f35caSApple OSS Distributions * __weak __block void (^object)(void);
663*c54f35caSApple OSS Distributions * [^{ object; } copy];
664*c54f35caSApple OSS Distributions ********/
665*c54f35caSApple OSS Distributions
666*c54f35caSApple OSS Distributions *dest = object;
667*c54f35caSApple OSS Distributions break;
668*c54f35caSApple OSS Distributions
669*c54f35caSApple OSS Distributions default:
670*c54f35caSApple OSS Distributions break;
671*c54f35caSApple OSS Distributions }
672*c54f35caSApple OSS Distributions }
673*c54f35caSApple OSS Distributions
674*c54f35caSApple OSS Distributions // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
675*c54f35caSApple OSS Distributions // to help dispose of the contents
676*c54f35caSApple OSS Distributions void
_Block_object_dispose(const void * object,const int flags)677*c54f35caSApple OSS Distributions _Block_object_dispose(const void *object, const int flags)
678*c54f35caSApple OSS Distributions {
679*c54f35caSApple OSS Distributions switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
680*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
681*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BYREF:
682*c54f35caSApple OSS Distributions // get rid of the __block data structure held in a Block
683*c54f35caSApple OSS Distributions _Block_byref_release(object);
684*c54f35caSApple OSS Distributions break;
685*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_BLOCK:
686*c54f35caSApple OSS Distributions _Block_release(object);
687*c54f35caSApple OSS Distributions break;
688*c54f35caSApple OSS Distributions case BLOCK_FIELD_IS_OBJECT:
689*c54f35caSApple OSS Distributions _Block_release_object(object);
690*c54f35caSApple OSS Distributions break;
691*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
692*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
693*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
694*c54f35caSApple OSS Distributions case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
695*c54f35caSApple OSS Distributions break;
696*c54f35caSApple OSS Distributions default:
697*c54f35caSApple OSS Distributions break;
698*c54f35caSApple OSS Distributions }
699*c54f35caSApple OSS Distributions }
700*c54f35caSApple OSS Distributions
701*c54f35caSApple OSS Distributions
702*c54f35caSApple OSS Distributions // Workaround for <rdar://26015603> dylib with no __DATA segment fails to rebase
703*c54f35caSApple OSS Distributions __attribute__((used))
704*c54f35caSApple OSS Distributions static int let_there_be_data = 42;
705