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