1*3cab2bb3Spatrick /*
2*3cab2bb3Spatrick * runtime.c
3*3cab2bb3Spatrick *
4*3cab2bb3Spatrick * Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge,
5*3cab2bb3Spatrick * to any person obtaining a copy of this software and associated documentation
6*3cab2bb3Spatrick * files (the "Software"), to deal in the Software without restriction,
7*3cab2bb3Spatrick * including without limitation the rights to use, copy, modify, merge, publish,
8*3cab2bb3Spatrick * distribute, sublicense, and/or sell copies of the Software, and to permit
9*3cab2bb3Spatrick * persons to whom the Software is furnished to do so, subject to the following
10*3cab2bb3Spatrick * conditions:
11*3cab2bb3Spatrick *
12*3cab2bb3Spatrick * The above copyright notice and this permission notice shall be included in
13*3cab2bb3Spatrick * all copies or substantial portions of the Software.
14*3cab2bb3Spatrick *
15*3cab2bb3Spatrick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*3cab2bb3Spatrick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*3cab2bb3Spatrick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18*3cab2bb3Spatrick * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*3cab2bb3Spatrick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*3cab2bb3Spatrick * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*3cab2bb3Spatrick * SOFTWARE.
22*3cab2bb3Spatrick *
23*3cab2bb3Spatrick */
24*3cab2bb3Spatrick
25*3cab2bb3Spatrick #include "Block_private.h"
26*3cab2bb3Spatrick #include <stdio.h>
27*3cab2bb3Spatrick #include <stdlib.h>
28*3cab2bb3Spatrick #include <string.h>
29*3cab2bb3Spatrick #include <stdint.h>
30*3cab2bb3Spatrick
31*3cab2bb3Spatrick #include "config.h"
32*3cab2bb3Spatrick
33*3cab2bb3Spatrick #ifdef HAVE_AVAILABILITY_MACROS_H
34*3cab2bb3Spatrick #include <AvailabilityMacros.h>
35*3cab2bb3Spatrick #endif /* HAVE_AVAILABILITY_MACROS_H */
36*3cab2bb3Spatrick
37*3cab2bb3Spatrick #ifdef HAVE_TARGET_CONDITIONALS_H
38*3cab2bb3Spatrick #include <TargetConditionals.h>
39*3cab2bb3Spatrick #endif /* HAVE_TARGET_CONDITIONALS_H */
40*3cab2bb3Spatrick
41*3cab2bb3Spatrick #if defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_INT) && defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG)
42*3cab2bb3Spatrick
43*3cab2bb3Spatrick #ifdef HAVE_LIBKERN_OSATOMIC_H
44*3cab2bb3Spatrick #include <libkern/OSAtomic.h>
45*3cab2bb3Spatrick #endif /* HAVE_LIBKERN_OSATOMIC_H */
46*3cab2bb3Spatrick
47*3cab2bb3Spatrick #elif defined(__WIN32__) || defined(_WIN32)
48*3cab2bb3Spatrick #define _CRT_SECURE_NO_WARNINGS 1
49*3cab2bb3Spatrick #include <windows.h>
50*3cab2bb3Spatrick
OSAtomicCompareAndSwapLong(long oldl,long newl,long volatile * dst)51*3cab2bb3Spatrick static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) {
52*3cab2bb3Spatrick /* fixme barrier is overkill -- see objc-os.h */
53*3cab2bb3Spatrick long original = InterlockedCompareExchange(dst, newl, oldl);
54*3cab2bb3Spatrick return (original == oldl);
55*3cab2bb3Spatrick }
56*3cab2bb3Spatrick
OSAtomicCompareAndSwapInt(int oldi,int newi,int volatile * dst)57*3cab2bb3Spatrick static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) {
58*3cab2bb3Spatrick /* fixme barrier is overkill -- see objc-os.h */
59*3cab2bb3Spatrick int original = InterlockedCompareExchange(dst, newi, oldi);
60*3cab2bb3Spatrick return (original == oldi);
61*3cab2bb3Spatrick }
62*3cab2bb3Spatrick
63*3cab2bb3Spatrick /*
64*3cab2bb3Spatrick * Check to see if the GCC atomic built-ins are available. If we're on
65*3cab2bb3Spatrick * a 64-bit system, make sure we have an 8-byte atomic function
66*3cab2bb3Spatrick * available.
67*3cab2bb3Spatrick *
68*3cab2bb3Spatrick */
69*3cab2bb3Spatrick
70*3cab2bb3Spatrick #elif defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT) && defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG)
71*3cab2bb3Spatrick
OSAtomicCompareAndSwapLong(long oldl,long newl,long volatile * dst)72*3cab2bb3Spatrick static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) {
73*3cab2bb3Spatrick return __sync_bool_compare_and_swap(dst, oldl, newl);
74*3cab2bb3Spatrick }
75*3cab2bb3Spatrick
OSAtomicCompareAndSwapInt(int oldi,int newi,int volatile * dst)76*3cab2bb3Spatrick static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) {
77*3cab2bb3Spatrick return __sync_bool_compare_and_swap(dst, oldi, newi);
78*3cab2bb3Spatrick }
79*3cab2bb3Spatrick
80*3cab2bb3Spatrick #else
81*3cab2bb3Spatrick #error unknown atomic compare-and-swap primitive
82*3cab2bb3Spatrick #endif /* HAVE_OSATOMIC_COMPARE_AND_SWAP_INT && HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG */
83*3cab2bb3Spatrick
84*3cab2bb3Spatrick
85*3cab2bb3Spatrick /*
86*3cab2bb3Spatrick * Globals:
87*3cab2bb3Spatrick */
88*3cab2bb3Spatrick
89*3cab2bb3Spatrick static void *_Block_copy_class = _NSConcreteMallocBlock;
90*3cab2bb3Spatrick static void *_Block_copy_finalizing_class = _NSConcreteMallocBlock;
91*3cab2bb3Spatrick static int _Block_copy_flag = BLOCK_NEEDS_FREE;
92*3cab2bb3Spatrick static int _Byref_flag_initial_value = BLOCK_NEEDS_FREE | 2;
93*3cab2bb3Spatrick
94*3cab2bb3Spatrick static const int WANTS_ONE = (1 << 16);
95*3cab2bb3Spatrick
96*3cab2bb3Spatrick static bool isGC = false;
97*3cab2bb3Spatrick
98*3cab2bb3Spatrick /*
99*3cab2bb3Spatrick * Internal Utilities:
100*3cab2bb3Spatrick */
101*3cab2bb3Spatrick
102*3cab2bb3Spatrick #if 0
103*3cab2bb3Spatrick static unsigned long int latching_incr_long(unsigned long int *where) {
104*3cab2bb3Spatrick while (1) {
105*3cab2bb3Spatrick unsigned long int old_value = *(volatile unsigned long int *)where;
106*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
107*3cab2bb3Spatrick return BLOCK_REFCOUNT_MASK;
108*3cab2bb3Spatrick }
109*3cab2bb3Spatrick if (OSAtomicCompareAndSwapLong(old_value, old_value+1, (volatile long int *)where)) {
110*3cab2bb3Spatrick return old_value+1;
111*3cab2bb3Spatrick }
112*3cab2bb3Spatrick }
113*3cab2bb3Spatrick }
114*3cab2bb3Spatrick #endif /* if 0 */
115*3cab2bb3Spatrick
latching_incr_int(int * where)116*3cab2bb3Spatrick static int latching_incr_int(int *where) {
117*3cab2bb3Spatrick while (1) {
118*3cab2bb3Spatrick int old_value = *(volatile int *)where;
119*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
120*3cab2bb3Spatrick return BLOCK_REFCOUNT_MASK;
121*3cab2bb3Spatrick }
122*3cab2bb3Spatrick if (OSAtomicCompareAndSwapInt(old_value, old_value+1, (volatile int *)where)) {
123*3cab2bb3Spatrick return old_value+1;
124*3cab2bb3Spatrick }
125*3cab2bb3Spatrick }
126*3cab2bb3Spatrick }
127*3cab2bb3Spatrick
128*3cab2bb3Spatrick #if 0
129*3cab2bb3Spatrick static int latching_decr_long(unsigned long int *where) {
130*3cab2bb3Spatrick while (1) {
131*3cab2bb3Spatrick unsigned long int old_value = *(volatile int *)where;
132*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
133*3cab2bb3Spatrick return BLOCK_REFCOUNT_MASK;
134*3cab2bb3Spatrick }
135*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
136*3cab2bb3Spatrick return 0;
137*3cab2bb3Spatrick }
138*3cab2bb3Spatrick if (OSAtomicCompareAndSwapLong(old_value, old_value-1, (volatile long int *)where)) {
139*3cab2bb3Spatrick return old_value-1;
140*3cab2bb3Spatrick }
141*3cab2bb3Spatrick }
142*3cab2bb3Spatrick }
143*3cab2bb3Spatrick #endif /* if 0 */
144*3cab2bb3Spatrick
latching_decr_int(int * where)145*3cab2bb3Spatrick static int latching_decr_int(int *where) {
146*3cab2bb3Spatrick while (1) {
147*3cab2bb3Spatrick int old_value = *(volatile int *)where;
148*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
149*3cab2bb3Spatrick return BLOCK_REFCOUNT_MASK;
150*3cab2bb3Spatrick }
151*3cab2bb3Spatrick if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
152*3cab2bb3Spatrick return 0;
153*3cab2bb3Spatrick }
154*3cab2bb3Spatrick if (OSAtomicCompareAndSwapInt(old_value, old_value-1, (volatile int *)where)) {
155*3cab2bb3Spatrick return old_value-1;
156*3cab2bb3Spatrick }
157*3cab2bb3Spatrick }
158*3cab2bb3Spatrick }
159*3cab2bb3Spatrick
160*3cab2bb3Spatrick
161*3cab2bb3Spatrick /*
162*3cab2bb3Spatrick * GC support stub routines:
163*3cab2bb3Spatrick */
164*3cab2bb3Spatrick #if 0
165*3cab2bb3Spatrick #pragma mark GC Support Routines
166*3cab2bb3Spatrick #endif /* if 0 */
167*3cab2bb3Spatrick
168*3cab2bb3Spatrick
_Block_alloc_default(const unsigned long size,const bool initialCountIsOne,const bool isObject)169*3cab2bb3Spatrick static void *_Block_alloc_default(const unsigned long size, const bool initialCountIsOne, const bool isObject) {
170*3cab2bb3Spatrick return malloc(size);
171*3cab2bb3Spatrick }
172*3cab2bb3Spatrick
_Block_assign_default(void * value,void ** destptr)173*3cab2bb3Spatrick static void _Block_assign_default(void *value, void **destptr) {
174*3cab2bb3Spatrick *destptr = value;
175*3cab2bb3Spatrick }
176*3cab2bb3Spatrick
_Block_setHasRefcount_default(const void * ptr,const bool hasRefcount)177*3cab2bb3Spatrick static void _Block_setHasRefcount_default(const void *ptr, const bool hasRefcount) {
178*3cab2bb3Spatrick }
179*3cab2bb3Spatrick
_Block_do_nothing(const void * aBlock)180*3cab2bb3Spatrick static void _Block_do_nothing(const void *aBlock) { }
181*3cab2bb3Spatrick
_Block_retain_object_default(const void * ptr)182*3cab2bb3Spatrick static void _Block_retain_object_default(const void *ptr) {
183*3cab2bb3Spatrick if (!ptr) return;
184*3cab2bb3Spatrick }
185*3cab2bb3Spatrick
_Block_release_object_default(const void * ptr)186*3cab2bb3Spatrick static void _Block_release_object_default(const void *ptr) {
187*3cab2bb3Spatrick if (!ptr) return;
188*3cab2bb3Spatrick }
189*3cab2bb3Spatrick
_Block_assign_weak_default(const void * ptr,void * dest)190*3cab2bb3Spatrick static void _Block_assign_weak_default(const void *ptr, void *dest) {
191*3cab2bb3Spatrick *(void **)dest = (void *)ptr;
192*3cab2bb3Spatrick }
193*3cab2bb3Spatrick
_Block_memmove_default(void * dst,void * src,unsigned long size)194*3cab2bb3Spatrick static void _Block_memmove_default(void *dst, void *src, unsigned long size) {
195*3cab2bb3Spatrick memmove(dst, src, (size_t)size);
196*3cab2bb3Spatrick }
197*3cab2bb3Spatrick
_Block_memmove_gc_broken(void * dest,void * src,unsigned long size)198*3cab2bb3Spatrick static void _Block_memmove_gc_broken(void *dest, void *src, unsigned long size) {
199*3cab2bb3Spatrick void **destp = (void **)dest;
200*3cab2bb3Spatrick void **srcp = (void **)src;
201*3cab2bb3Spatrick while (size) {
202*3cab2bb3Spatrick _Block_assign_default(*srcp, destp);
203*3cab2bb3Spatrick destp++;
204*3cab2bb3Spatrick srcp++;
205*3cab2bb3Spatrick size -= sizeof(void *);
206*3cab2bb3Spatrick }
207*3cab2bb3Spatrick }
208*3cab2bb3Spatrick
209*3cab2bb3Spatrick /*
210*3cab2bb3Spatrick * GC support callout functions - initially set to stub routines:
211*3cab2bb3Spatrick */
212*3cab2bb3Spatrick
213*3cab2bb3Spatrick static void *(*_Block_allocator)(const unsigned long, const bool isOne, const bool isObject) = _Block_alloc_default;
214*3cab2bb3Spatrick static void (*_Block_deallocator)(const void *) = (void (*)(const void *))free;
215*3cab2bb3Spatrick static void (*_Block_assign)(void *value, void **destptr) = _Block_assign_default;
216*3cab2bb3Spatrick static void (*_Block_setHasRefcount)(const void *ptr, const bool hasRefcount) = _Block_setHasRefcount_default;
217*3cab2bb3Spatrick static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default;
218*3cab2bb3Spatrick static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default;
219*3cab2bb3Spatrick static void (*_Block_assign_weak)(const void *dest, void *ptr) = _Block_assign_weak_default;
220*3cab2bb3Spatrick static void (*_Block_memmove)(void *dest, void *src, unsigned long size) = _Block_memmove_default;
221*3cab2bb3Spatrick
222*3cab2bb3Spatrick
223*3cab2bb3Spatrick /*
224*3cab2bb3Spatrick * GC support SPI functions - called from ObjC runtime and CoreFoundation:
225*3cab2bb3Spatrick */
226*3cab2bb3Spatrick
227*3cab2bb3Spatrick /* Public SPI
228*3cab2bb3Spatrick * Called from objc-auto to turn on GC.
229*3cab2bb3Spatrick * version 3, 4 arg, but changed 1st arg
230*3cab2bb3Spatrick */
_Block_use_GC(void * (* alloc)(const unsigned long,const bool isOne,const bool isObject),void (* setHasRefcount)(const void *,const bool),void (* gc_assign)(void *,void **),void (* gc_assign_weak)(const void *,void *),void (* gc_memmove)(void *,void *,unsigned long))231*3cab2bb3Spatrick void _Block_use_GC( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
232*3cab2bb3Spatrick void (*setHasRefcount)(const void *, const bool),
233*3cab2bb3Spatrick void (*gc_assign)(void *, void **),
234*3cab2bb3Spatrick void (*gc_assign_weak)(const void *, void *),
235*3cab2bb3Spatrick void (*gc_memmove)(void *, void *, unsigned long)) {
236*3cab2bb3Spatrick
237*3cab2bb3Spatrick isGC = true;
238*3cab2bb3Spatrick _Block_allocator = alloc;
239*3cab2bb3Spatrick _Block_deallocator = _Block_do_nothing;
240*3cab2bb3Spatrick _Block_assign = gc_assign;
241*3cab2bb3Spatrick _Block_copy_flag = BLOCK_IS_GC;
242*3cab2bb3Spatrick _Block_copy_class = _NSConcreteAutoBlock;
243*3cab2bb3Spatrick /* blocks with ctors & dtors need to have the dtor run from a class with a finalizer */
244*3cab2bb3Spatrick _Block_copy_finalizing_class = _NSConcreteFinalizingBlock;
245*3cab2bb3Spatrick _Block_setHasRefcount = setHasRefcount;
246*3cab2bb3Spatrick _Byref_flag_initial_value = BLOCK_IS_GC; // no refcount
247*3cab2bb3Spatrick _Block_retain_object = _Block_do_nothing;
248*3cab2bb3Spatrick _Block_release_object = _Block_do_nothing;
249*3cab2bb3Spatrick _Block_assign_weak = gc_assign_weak;
250*3cab2bb3Spatrick _Block_memmove = gc_memmove;
251*3cab2bb3Spatrick }
252*3cab2bb3Spatrick
253*3cab2bb3Spatrick /* transitional */
_Block_use_GC5(void * (* alloc)(const unsigned long,const bool isOne,const bool isObject),void (* setHasRefcount)(const void *,const bool),void (* gc_assign)(void *,void **),void (* gc_assign_weak)(const void *,void *))254*3cab2bb3Spatrick void _Block_use_GC5( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),
255*3cab2bb3Spatrick void (*setHasRefcount)(const void *, const bool),
256*3cab2bb3Spatrick void (*gc_assign)(void *, void **),
257*3cab2bb3Spatrick void (*gc_assign_weak)(const void *, void *)) {
258*3cab2bb3Spatrick /* until objc calls _Block_use_GC it will call us; supply a broken internal memmove implementation until then */
259*3cab2bb3Spatrick _Block_use_GC(alloc, setHasRefcount, gc_assign, gc_assign_weak, _Block_memmove_gc_broken);
260*3cab2bb3Spatrick }
261*3cab2bb3Spatrick
262*3cab2bb3Spatrick
263*3cab2bb3Spatrick /*
264*3cab2bb3Spatrick * Called from objc-auto to alternatively turn on retain/release.
265*3cab2bb3Spatrick * Prior to this the only "object" support we can provide is for those
266*3cab2bb3Spatrick * super special objects that live in libSystem, namely dispatch queues.
267*3cab2bb3Spatrick * Blocks and Block_byrefs have their own special entry points.
268*3cab2bb3Spatrick *
269*3cab2bb3Spatrick */
_Block_use_RR(void (* retain)(const void *),void (* release)(const void *))270*3cab2bb3Spatrick void _Block_use_RR( void (*retain)(const void *),
271*3cab2bb3Spatrick void (*release)(const void *)) {
272*3cab2bb3Spatrick _Block_retain_object = retain;
273*3cab2bb3Spatrick _Block_release_object = release;
274*3cab2bb3Spatrick }
275*3cab2bb3Spatrick
276*3cab2bb3Spatrick /*
277*3cab2bb3Spatrick * Internal Support routines for copying:
278*3cab2bb3Spatrick */
279*3cab2bb3Spatrick
280*3cab2bb3Spatrick #if 0
281*3cab2bb3Spatrick #pragma mark Copy/Release support
282*3cab2bb3Spatrick #endif /* if 0 */
283*3cab2bb3Spatrick
284*3cab2bb3Spatrick /* Copy, or bump refcount, of a block. If really copying, call the copy helper if present. */
_Block_copy_internal(const void * arg,const int flags)285*3cab2bb3Spatrick static void *_Block_copy_internal(const void *arg, const int flags) {
286*3cab2bb3Spatrick struct Block_layout *aBlock;
287*3cab2bb3Spatrick const bool wantsOne = (WANTS_ONE & flags) == WANTS_ONE;
288*3cab2bb3Spatrick
289*3cab2bb3Spatrick //printf("_Block_copy_internal(%p, %x)\n", arg, flags);
290*3cab2bb3Spatrick if (!arg) return NULL;
291*3cab2bb3Spatrick
292*3cab2bb3Spatrick
293*3cab2bb3Spatrick // The following would be better done as a switch statement
294*3cab2bb3Spatrick aBlock = (struct Block_layout *)arg;
295*3cab2bb3Spatrick if (aBlock->flags & BLOCK_NEEDS_FREE) {
296*3cab2bb3Spatrick // latches on high
297*3cab2bb3Spatrick latching_incr_int(&aBlock->flags);
298*3cab2bb3Spatrick return aBlock;
299*3cab2bb3Spatrick }
300*3cab2bb3Spatrick else if (aBlock->flags & BLOCK_IS_GC) {
301*3cab2bb3Spatrick // GC refcounting is expensive so do most refcounting here.
302*3cab2bb3Spatrick if (wantsOne && ((latching_incr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK) == 1)) {
303*3cab2bb3Spatrick // Tell collector to hang on this - it will bump the GC refcount version
304*3cab2bb3Spatrick _Block_setHasRefcount(aBlock, true);
305*3cab2bb3Spatrick }
306*3cab2bb3Spatrick return aBlock;
307*3cab2bb3Spatrick }
308*3cab2bb3Spatrick else if (aBlock->flags & BLOCK_IS_GLOBAL) {
309*3cab2bb3Spatrick return aBlock;
310*3cab2bb3Spatrick }
311*3cab2bb3Spatrick
312*3cab2bb3Spatrick // Its a stack block. Make a copy.
313*3cab2bb3Spatrick if (!isGC) {
314*3cab2bb3Spatrick struct Block_layout *result = malloc(aBlock->descriptor->size);
315*3cab2bb3Spatrick if (!result) return (void *)0;
316*3cab2bb3Spatrick memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first
317*3cab2bb3Spatrick // reset refcount
318*3cab2bb3Spatrick result->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not needed
319*3cab2bb3Spatrick result->flags |= BLOCK_NEEDS_FREE | 1;
320*3cab2bb3Spatrick result->isa = _NSConcreteMallocBlock;
321*3cab2bb3Spatrick if (result->flags & BLOCK_HAS_COPY_DISPOSE) {
322*3cab2bb3Spatrick //printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock);
323*3cab2bb3Spatrick (*aBlock->descriptor->copy)(result, aBlock); // do fixup
324*3cab2bb3Spatrick }
325*3cab2bb3Spatrick return result;
326*3cab2bb3Spatrick }
327*3cab2bb3Spatrick else {
328*3cab2bb3Spatrick // Under GC want allocation with refcount 1 so we ask for "true" if wantsOne
329*3cab2bb3Spatrick // This allows the copy helper routines to make non-refcounted block copies under GC
330*3cab2bb3Spatrick unsigned long int flags = aBlock->flags;
331*3cab2bb3Spatrick bool hasCTOR = (flags & BLOCK_HAS_CTOR) != 0;
332*3cab2bb3Spatrick struct Block_layout *result = _Block_allocator(aBlock->descriptor->size, wantsOne, hasCTOR);
333*3cab2bb3Spatrick if (!result) return (void *)0;
334*3cab2bb3Spatrick memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first
335*3cab2bb3Spatrick // reset refcount
336*3cab2bb3Spatrick // if we copy a malloc block to a GC block then we need to clear NEEDS_FREE.
337*3cab2bb3Spatrick flags &= ~(BLOCK_NEEDS_FREE|BLOCK_REFCOUNT_MASK); // XXX not needed
338*3cab2bb3Spatrick if (wantsOne)
339*3cab2bb3Spatrick flags |= BLOCK_IS_GC | 1;
340*3cab2bb3Spatrick else
341*3cab2bb3Spatrick flags |= BLOCK_IS_GC;
342*3cab2bb3Spatrick result->flags = flags;
343*3cab2bb3Spatrick if (flags & BLOCK_HAS_COPY_DISPOSE) {
344*3cab2bb3Spatrick //printf("calling block copy helper...\n");
345*3cab2bb3Spatrick (*aBlock->descriptor->copy)(result, aBlock); // do fixup
346*3cab2bb3Spatrick }
347*3cab2bb3Spatrick if (hasCTOR) {
348*3cab2bb3Spatrick result->isa = _NSConcreteFinalizingBlock;
349*3cab2bb3Spatrick }
350*3cab2bb3Spatrick else {
351*3cab2bb3Spatrick result->isa = _NSConcreteAutoBlock;
352*3cab2bb3Spatrick }
353*3cab2bb3Spatrick return result;
354*3cab2bb3Spatrick }
355*3cab2bb3Spatrick }
356*3cab2bb3Spatrick
357*3cab2bb3Spatrick
358*3cab2bb3Spatrick /*
359*3cab2bb3Spatrick * Runtime entry points for maintaining the sharing knowledge of byref data blocks.
360*3cab2bb3Spatrick *
361*3cab2bb3Spatrick * A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
362*3cab2bb3Spatrick * Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
363*3cab2bb3Spatrick * We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment it.
364*3cab2bb3Spatrick * Otherwise we need to copy it and update the stack forwarding pointer
365*3cab2bb3Spatrick * XXX We need to account for weak/nonretained read-write barriers.
366*3cab2bb3Spatrick */
367*3cab2bb3Spatrick
_Block_byref_assign_copy(void * dest,const void * arg,const int flags)368*3cab2bb3Spatrick static void _Block_byref_assign_copy(void *dest, const void *arg, const int flags) {
369*3cab2bb3Spatrick struct Block_byref **destp = (struct Block_byref **)dest;
370*3cab2bb3Spatrick struct Block_byref *src = (struct Block_byref *)arg;
371*3cab2bb3Spatrick
372*3cab2bb3Spatrick //printf("_Block_byref_assign_copy called, byref destp %p, src %p, flags %x\n", destp, src, flags);
373*3cab2bb3Spatrick //printf("src dump: %s\n", _Block_byref_dump(src));
374*3cab2bb3Spatrick if (src->forwarding->flags & BLOCK_IS_GC) {
375*3cab2bb3Spatrick ; // don't need to do any more work
376*3cab2bb3Spatrick }
377*3cab2bb3Spatrick else if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
378*3cab2bb3Spatrick //printf("making copy\n");
379*3cab2bb3Spatrick // src points to stack
380*3cab2bb3Spatrick bool isWeak = ((flags & (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK)) == (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK));
381*3cab2bb3Spatrick // if its weak ask for an object (only matters under GC)
382*3cab2bb3Spatrick struct Block_byref *copy = (struct Block_byref *)_Block_allocator(src->size, false, isWeak);
383*3cab2bb3Spatrick copy->flags = src->flags | _Byref_flag_initial_value; // non-GC one for caller, one for stack
384*3cab2bb3Spatrick copy->forwarding = copy; // patch heap copy to point to itself (skip write-barrier)
385*3cab2bb3Spatrick src->forwarding = copy; // patch stack to point to heap copy
386*3cab2bb3Spatrick copy->size = src->size;
387*3cab2bb3Spatrick if (isWeak) {
388*3cab2bb3Spatrick copy->isa = &_NSConcreteWeakBlockVariable; // mark isa field so it gets weak scanning
389*3cab2bb3Spatrick }
390*3cab2bb3Spatrick if (src->flags & BLOCK_HAS_COPY_DISPOSE) {
391*3cab2bb3Spatrick // Trust copy helper to copy everything of interest
392*3cab2bb3Spatrick // If more than one field shows up in a byref block this is wrong XXX
393*3cab2bb3Spatrick copy->byref_keep = src->byref_keep;
394*3cab2bb3Spatrick copy->byref_destroy = src->byref_destroy;
395*3cab2bb3Spatrick (*src->byref_keep)(copy, src);
396*3cab2bb3Spatrick }
397*3cab2bb3Spatrick else {
398*3cab2bb3Spatrick // just bits. Blast 'em using _Block_memmove in case they're __strong
399*3cab2bb3Spatrick _Block_memmove(
400*3cab2bb3Spatrick (void *)©->byref_keep,
401*3cab2bb3Spatrick (void *)&src->byref_keep,
402*3cab2bb3Spatrick src->size - sizeof(struct Block_byref_header));
403*3cab2bb3Spatrick }
404*3cab2bb3Spatrick }
405*3cab2bb3Spatrick // already copied to heap
406*3cab2bb3Spatrick else if ((src->forwarding->flags & BLOCK_NEEDS_FREE) == BLOCK_NEEDS_FREE) {
407*3cab2bb3Spatrick latching_incr_int(&src->forwarding->flags);
408*3cab2bb3Spatrick }
409*3cab2bb3Spatrick // assign byref data block pointer into new Block
410*3cab2bb3Spatrick _Block_assign(src->forwarding, (void **)destp);
411*3cab2bb3Spatrick }
412*3cab2bb3Spatrick
413*3cab2bb3Spatrick // Old compiler SPI
_Block_byref_release(const void * arg)414*3cab2bb3Spatrick static void _Block_byref_release(const void *arg) {
415*3cab2bb3Spatrick struct Block_byref *shared_struct = (struct Block_byref *)arg;
416*3cab2bb3Spatrick int refcount;
417*3cab2bb3Spatrick
418*3cab2bb3Spatrick // dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
419*3cab2bb3Spatrick shared_struct = shared_struct->forwarding;
420*3cab2bb3Spatrick
421*3cab2bb3Spatrick //printf("_Block_byref_release %p called, flags are %x\n", shared_struct, shared_struct->flags);
422*3cab2bb3Spatrick // To support C++ destructors under GC we arrange for there to be a finalizer for this
423*3cab2bb3Spatrick // by using an isa that directs the code to a finalizer that calls the byref_destroy method.
424*3cab2bb3Spatrick if ((shared_struct->flags & BLOCK_NEEDS_FREE) == 0) {
425*3cab2bb3Spatrick return; // stack or GC or global
426*3cab2bb3Spatrick }
427*3cab2bb3Spatrick refcount = shared_struct->flags & BLOCK_REFCOUNT_MASK;
428*3cab2bb3Spatrick if (refcount <= 0) {
429*3cab2bb3Spatrick printf("_Block_byref_release: Block byref data structure at %p underflowed\n", arg);
430*3cab2bb3Spatrick }
431*3cab2bb3Spatrick else if ((latching_decr_int(&shared_struct->flags) & BLOCK_REFCOUNT_MASK) == 0) {
432*3cab2bb3Spatrick //printf("disposing of heap based byref block\n");
433*3cab2bb3Spatrick if (shared_struct->flags & BLOCK_HAS_COPY_DISPOSE) {
434*3cab2bb3Spatrick //printf("calling out to helper\n");
435*3cab2bb3Spatrick (*shared_struct->byref_destroy)(shared_struct);
436*3cab2bb3Spatrick }
437*3cab2bb3Spatrick _Block_deallocator((struct Block_layout *)shared_struct);
438*3cab2bb3Spatrick }
439*3cab2bb3Spatrick }
440*3cab2bb3Spatrick
441*3cab2bb3Spatrick
442*3cab2bb3Spatrick /*
443*3cab2bb3Spatrick *
444*3cab2bb3Spatrick * API supporting SPI
445*3cab2bb3Spatrick * _Block_copy, _Block_release, and (old) _Block_destroy
446*3cab2bb3Spatrick *
447*3cab2bb3Spatrick */
448*3cab2bb3Spatrick
449*3cab2bb3Spatrick #if 0
450*3cab2bb3Spatrick #pragma mark SPI/API
451*3cab2bb3Spatrick #endif /* if 0 */
452*3cab2bb3Spatrick
_Block_copy(const void * arg)453*3cab2bb3Spatrick void *_Block_copy(const void *arg) {
454*3cab2bb3Spatrick return _Block_copy_internal(arg, WANTS_ONE);
455*3cab2bb3Spatrick }
456*3cab2bb3Spatrick
457*3cab2bb3Spatrick
458*3cab2bb3Spatrick // API entry point to release a copied Block
_Block_release(void * arg)459*3cab2bb3Spatrick void _Block_release(void *arg) {
460*3cab2bb3Spatrick struct Block_layout *aBlock = (struct Block_layout *)arg;
461*3cab2bb3Spatrick int32_t newCount;
462*3cab2bb3Spatrick if (!aBlock) return;
463*3cab2bb3Spatrick newCount = latching_decr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK;
464*3cab2bb3Spatrick if (newCount > 0) return;
465*3cab2bb3Spatrick // Hit zero
466*3cab2bb3Spatrick if (aBlock->flags & BLOCK_IS_GC) {
467*3cab2bb3Spatrick // Tell GC we no longer have our own refcounts. GC will decr its refcount
468*3cab2bb3Spatrick // and unless someone has done a CFRetain or marked it uncollectable it will
469*3cab2bb3Spatrick // now be subject to GC reclamation.
470*3cab2bb3Spatrick _Block_setHasRefcount(aBlock, false);
471*3cab2bb3Spatrick }
472*3cab2bb3Spatrick else if (aBlock->flags & BLOCK_NEEDS_FREE) {
473*3cab2bb3Spatrick if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE)(*aBlock->descriptor->dispose)(aBlock);
474*3cab2bb3Spatrick _Block_deallocator(aBlock);
475*3cab2bb3Spatrick }
476*3cab2bb3Spatrick else if (aBlock->flags & BLOCK_IS_GLOBAL) {
477*3cab2bb3Spatrick ;
478*3cab2bb3Spatrick }
479*3cab2bb3Spatrick else {
480*3cab2bb3Spatrick printf("Block_release called upon a stack Block: %p, ignored\n", (void *)aBlock);
481*3cab2bb3Spatrick }
482*3cab2bb3Spatrick }
483*3cab2bb3Spatrick
484*3cab2bb3Spatrick
485*3cab2bb3Spatrick
486*3cab2bb3Spatrick // Old Compiler SPI point to release a copied Block used by the compiler in dispose helpers
_Block_destroy(const void * arg)487*3cab2bb3Spatrick static void _Block_destroy(const void *arg) {
488*3cab2bb3Spatrick struct Block_layout *aBlock;
489*3cab2bb3Spatrick if (!arg) return;
490*3cab2bb3Spatrick aBlock = (struct Block_layout *)arg;
491*3cab2bb3Spatrick if (aBlock->flags & BLOCK_IS_GC) {
492*3cab2bb3Spatrick // assert(aBlock->Block_flags & BLOCK_HAS_CTOR);
493*3cab2bb3Spatrick return; // ignore, we are being called because of a DTOR
494*3cab2bb3Spatrick }
495*3cab2bb3Spatrick _Block_release(aBlock);
496*3cab2bb3Spatrick }
497*3cab2bb3Spatrick
498*3cab2bb3Spatrick
499*3cab2bb3Spatrick
500*3cab2bb3Spatrick /*
501*3cab2bb3Spatrick *
502*3cab2bb3Spatrick * SPI used by other layers
503*3cab2bb3Spatrick *
504*3cab2bb3Spatrick */
505*3cab2bb3Spatrick
506*3cab2bb3Spatrick // SPI, also internal. Called from NSAutoBlock only under GC
_Block_copy_collectable(const void * aBlock)507*3cab2bb3Spatrick void *_Block_copy_collectable(const void *aBlock) {
508*3cab2bb3Spatrick return _Block_copy_internal(aBlock, 0);
509*3cab2bb3Spatrick }
510*3cab2bb3Spatrick
511*3cab2bb3Spatrick
512*3cab2bb3Spatrick // SPI
Block_size(void * arg)513*3cab2bb3Spatrick unsigned long int Block_size(void *arg) {
514*3cab2bb3Spatrick return ((struct Block_layout *)arg)->descriptor->size;
515*3cab2bb3Spatrick }
516*3cab2bb3Spatrick
517*3cab2bb3Spatrick
518*3cab2bb3Spatrick #if 0
519*3cab2bb3Spatrick #pragma mark Compiler SPI entry points
520*3cab2bb3Spatrick #endif /* if 0 */
521*3cab2bb3Spatrick
522*3cab2bb3Spatrick
523*3cab2bb3Spatrick /*******************************************************
524*3cab2bb3Spatrick
525*3cab2bb3Spatrick Entry points used by the compiler - the real API!
526*3cab2bb3Spatrick
527*3cab2bb3Spatrick
528*3cab2bb3Spatrick A Block can reference four different kinds of things that require help when the Block is copied to the heap.
529*3cab2bb3Spatrick 1) C++ stack based objects
530*3cab2bb3Spatrick 2) References to Objective-C objects
531*3cab2bb3Spatrick 3) Other Blocks
532*3cab2bb3Spatrick 4) __block variables
533*3cab2bb3Spatrick
534*3cab2bb3Spatrick 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.
535*3cab2bb3Spatrick
536*3cab2bb3Spatrick The flags parameter of _Block_object_assign and _Block_object_dispose is set to
537*3cab2bb3Spatrick * BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
538*3cab2bb3Spatrick * BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
539*3cab2bb3Spatrick * BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
540*3cab2bb3Spatrick If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16).
541*3cab2bb3Spatrick
542*3cab2bb3Spatrick So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
543*3cab2bb3Spatrick
544*3cab2bb3Spatrick 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.
545*3cab2bb3Spatrick
546*3cab2bb3Spatrick 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:
547*3cab2bb3Spatrick __block id 128+3
548*3cab2bb3Spatrick __weak block id 128+3+16
549*3cab2bb3Spatrick __block (^Block) 128+7
550*3cab2bb3Spatrick __weak __block (^Block) 128+7+16
551*3cab2bb3Spatrick
552*3cab2bb3Spatrick The implementation of the two routines would be improved by switch statements enumerating the eight cases.
553*3cab2bb3Spatrick
554*3cab2bb3Spatrick ********************************************************/
555*3cab2bb3Spatrick
556*3cab2bb3Spatrick /*
557*3cab2bb3Spatrick * When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
558*3cab2bb3Spatrick * to do the assignment.
559*3cab2bb3Spatrick */
_Block_object_assign(void * destAddr,const void * object,const int flags)560*3cab2bb3Spatrick void _Block_object_assign(void *destAddr, const void *object, const int flags) {
561*3cab2bb3Spatrick //printf("_Block_object_assign(*%p, %p, %x)\n", destAddr, object, flags);
562*3cab2bb3Spatrick if ((flags & BLOCK_BYREF_CALLER) == BLOCK_BYREF_CALLER) {
563*3cab2bb3Spatrick if ((flags & BLOCK_FIELD_IS_WEAK) == BLOCK_FIELD_IS_WEAK) {
564*3cab2bb3Spatrick _Block_assign_weak(object, destAddr);
565*3cab2bb3Spatrick }
566*3cab2bb3Spatrick else {
567*3cab2bb3Spatrick // do *not* retain or *copy* __block variables whatever they are
568*3cab2bb3Spatrick _Block_assign((void *)object, destAddr);
569*3cab2bb3Spatrick }
570*3cab2bb3Spatrick }
571*3cab2bb3Spatrick else if ((flags & BLOCK_FIELD_IS_BYREF) == BLOCK_FIELD_IS_BYREF) {
572*3cab2bb3Spatrick // copying a __block reference from the stack Block to the heap
573*3cab2bb3Spatrick // flags will indicate if it holds a __weak reference and needs a special isa
574*3cab2bb3Spatrick _Block_byref_assign_copy(destAddr, object, flags);
575*3cab2bb3Spatrick }
576*3cab2bb3Spatrick // (this test must be before next one)
577*3cab2bb3Spatrick else if ((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_BLOCK) {
578*3cab2bb3Spatrick // copying a Block declared variable from the stack Block to the heap
579*3cab2bb3Spatrick _Block_assign(_Block_copy_internal(object, flags), destAddr);
580*3cab2bb3Spatrick }
581*3cab2bb3Spatrick // (this test must be after previous one)
582*3cab2bb3Spatrick else if ((flags & BLOCK_FIELD_IS_OBJECT) == BLOCK_FIELD_IS_OBJECT) {
583*3cab2bb3Spatrick //printf("retaining object at %p\n", object);
584*3cab2bb3Spatrick _Block_retain_object(object);
585*3cab2bb3Spatrick //printf("done retaining object at %p\n", object);
586*3cab2bb3Spatrick _Block_assign((void *)object, destAddr);
587*3cab2bb3Spatrick }
588*3cab2bb3Spatrick }
589*3cab2bb3Spatrick
590*3cab2bb3Spatrick // When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
591*3cab2bb3Spatrick // to help dispose of the contents
592*3cab2bb3Spatrick // Used initially only for __attribute__((NSObject)) marked pointers.
_Block_object_dispose(const void * object,const int flags)593*3cab2bb3Spatrick void _Block_object_dispose(const void *object, const int flags) {
594*3cab2bb3Spatrick //printf("_Block_object_dispose(%p, %x)\n", object, flags);
595*3cab2bb3Spatrick if (flags & BLOCK_FIELD_IS_BYREF) {
596*3cab2bb3Spatrick // get rid of the __block data structure held in a Block
597*3cab2bb3Spatrick _Block_byref_release(object);
598*3cab2bb3Spatrick }
599*3cab2bb3Spatrick else if ((flags & (BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_BLOCK) {
600*3cab2bb3Spatrick // get rid of a referenced Block held by this Block
601*3cab2bb3Spatrick // (ignore __block Block variables, compiler doesn't need to call us)
602*3cab2bb3Spatrick _Block_destroy(object);
603*3cab2bb3Spatrick }
604*3cab2bb3Spatrick else if ((flags & (BLOCK_FIELD_IS_WEAK|BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_OBJECT) {
605*3cab2bb3Spatrick // get rid of a referenced object held by this Block
606*3cab2bb3Spatrick // (ignore __block object variables, compiler doesn't need to call us)
607*3cab2bb3Spatrick _Block_release_object(object);
608*3cab2bb3Spatrick }
609*3cab2bb3Spatrick }
610*3cab2bb3Spatrick
611*3cab2bb3Spatrick
612*3cab2bb3Spatrick /*
613*3cab2bb3Spatrick * Debugging support:
614*3cab2bb3Spatrick */
615*3cab2bb3Spatrick #if 0
616*3cab2bb3Spatrick #pragma mark Debugging
617*3cab2bb3Spatrick #endif /* if 0 */
618*3cab2bb3Spatrick
619*3cab2bb3Spatrick
_Block_dump(const void * block)620*3cab2bb3Spatrick const char *_Block_dump(const void *block) {
621*3cab2bb3Spatrick struct Block_layout *closure = (struct Block_layout *)block;
622*3cab2bb3Spatrick static char buffer[512];
623*3cab2bb3Spatrick char *cp = buffer;
624*3cab2bb3Spatrick if (closure == NULL) {
625*3cab2bb3Spatrick sprintf(cp, "NULL passed to _Block_dump\n");
626*3cab2bb3Spatrick return buffer;
627*3cab2bb3Spatrick }
628*3cab2bb3Spatrick if (! (closure->flags & BLOCK_HAS_DESCRIPTOR)) {
629*3cab2bb3Spatrick printf("Block compiled by obsolete compiler, please recompile source for this Block\n");
630*3cab2bb3Spatrick exit(1);
631*3cab2bb3Spatrick }
632*3cab2bb3Spatrick cp += sprintf(cp, "^%p (new layout) =\n", (void *)closure);
633*3cab2bb3Spatrick if (closure->isa == NULL) {
634*3cab2bb3Spatrick cp += sprintf(cp, "isa: NULL\n");
635*3cab2bb3Spatrick }
636*3cab2bb3Spatrick else if (closure->isa == _NSConcreteStackBlock) {
637*3cab2bb3Spatrick cp += sprintf(cp, "isa: stack Block\n");
638*3cab2bb3Spatrick }
639*3cab2bb3Spatrick else if (closure->isa == _NSConcreteMallocBlock) {
640*3cab2bb3Spatrick cp += sprintf(cp, "isa: malloc heap Block\n");
641*3cab2bb3Spatrick }
642*3cab2bb3Spatrick else if (closure->isa == _NSConcreteAutoBlock) {
643*3cab2bb3Spatrick cp += sprintf(cp, "isa: GC heap Block\n");
644*3cab2bb3Spatrick }
645*3cab2bb3Spatrick else if (closure->isa == _NSConcreteGlobalBlock) {
646*3cab2bb3Spatrick cp += sprintf(cp, "isa: global Block\n");
647*3cab2bb3Spatrick }
648*3cab2bb3Spatrick else if (closure->isa == _NSConcreteFinalizingBlock) {
649*3cab2bb3Spatrick cp += sprintf(cp, "isa: finalizing Block\n");
650*3cab2bb3Spatrick }
651*3cab2bb3Spatrick else {
652*3cab2bb3Spatrick cp += sprintf(cp, "isa?: %p\n", (void *)closure->isa);
653*3cab2bb3Spatrick }
654*3cab2bb3Spatrick cp += sprintf(cp, "flags:");
655*3cab2bb3Spatrick if (closure->flags & BLOCK_HAS_DESCRIPTOR) {
656*3cab2bb3Spatrick cp += sprintf(cp, " HASDESCRIPTOR");
657*3cab2bb3Spatrick }
658*3cab2bb3Spatrick if (closure->flags & BLOCK_NEEDS_FREE) {
659*3cab2bb3Spatrick cp += sprintf(cp, " FREEME");
660*3cab2bb3Spatrick }
661*3cab2bb3Spatrick if (closure->flags & BLOCK_IS_GC) {
662*3cab2bb3Spatrick cp += sprintf(cp, " ISGC");
663*3cab2bb3Spatrick }
664*3cab2bb3Spatrick if (closure->flags & BLOCK_HAS_COPY_DISPOSE) {
665*3cab2bb3Spatrick cp += sprintf(cp, " HASHELP");
666*3cab2bb3Spatrick }
667*3cab2bb3Spatrick if (closure->flags & BLOCK_HAS_CTOR) {
668*3cab2bb3Spatrick cp += sprintf(cp, " HASCTOR");
669*3cab2bb3Spatrick }
670*3cab2bb3Spatrick cp += sprintf(cp, "\nrefcount: %u\n", closure->flags & BLOCK_REFCOUNT_MASK);
671*3cab2bb3Spatrick cp += sprintf(cp, "invoke: %p\n", (void *)(uintptr_t)closure->invoke);
672*3cab2bb3Spatrick {
673*3cab2bb3Spatrick struct Block_descriptor *dp = closure->descriptor;
674*3cab2bb3Spatrick cp += sprintf(cp, "descriptor: %p\n", (void *)dp);
675*3cab2bb3Spatrick cp += sprintf(cp, "descriptor->reserved: %lu\n", dp->reserved);
676*3cab2bb3Spatrick cp += sprintf(cp, "descriptor->size: %lu\n", dp->size);
677*3cab2bb3Spatrick
678*3cab2bb3Spatrick if (closure->flags & BLOCK_HAS_COPY_DISPOSE) {
679*3cab2bb3Spatrick cp += sprintf(cp, "descriptor->copy helper: %p\n", (void *)(uintptr_t)dp->copy);
680*3cab2bb3Spatrick cp += sprintf(cp, "descriptor->dispose helper: %p\n", (void *)(uintptr_t)dp->dispose);
681*3cab2bb3Spatrick }
682*3cab2bb3Spatrick }
683*3cab2bb3Spatrick return buffer;
684*3cab2bb3Spatrick }
685*3cab2bb3Spatrick
686*3cab2bb3Spatrick
_Block_byref_dump(struct Block_byref * src)687*3cab2bb3Spatrick const char *_Block_byref_dump(struct Block_byref *src) {
688*3cab2bb3Spatrick static char buffer[256];
689*3cab2bb3Spatrick char *cp = buffer;
690*3cab2bb3Spatrick cp += sprintf(cp, "byref data block %p contents:\n", (void *)src);
691*3cab2bb3Spatrick cp += sprintf(cp, " forwarding: %p\n", (void *)src->forwarding);
692*3cab2bb3Spatrick cp += sprintf(cp, " flags: 0x%x\n", src->flags);
693*3cab2bb3Spatrick cp += sprintf(cp, " size: %d\n", src->size);
694*3cab2bb3Spatrick if (src->flags & BLOCK_HAS_COPY_DISPOSE) {
695*3cab2bb3Spatrick cp += sprintf(cp, " copy helper: %p\n", (void *)(uintptr_t)src->byref_keep);
696*3cab2bb3Spatrick cp += sprintf(cp, " dispose helper: %p\n", (void *)(uintptr_t)src->byref_destroy);
697*3cab2bb3Spatrick }
698*3cab2bb3Spatrick return buffer;
699*3cab2bb3Spatrick }
700*3cab2bb3Spatrick
701