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