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