Path: blob/main/contrib/llvm-project/compiler-rt/lib/BlocksRuntime/runtime.c
35260 views
/*1* runtime.c2*3* Copyright 2008-2010 Apple, Inc. Permission is hereby granted, free of charge,4* to any person obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without restriction,6* including without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to permit8* persons to whom the Software is furnished to do so, subject to the following9* conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22*/2324#include "Block_private.h"25#include <stdio.h>26#include <stdlib.h>27#include <string.h>28#include <stdint.h>2930#include "config.h"3132#ifdef HAVE_AVAILABILITY_MACROS_H33#include <AvailabilityMacros.h>34#endif /* HAVE_AVAILABILITY_MACROS_H */3536#ifdef HAVE_TARGET_CONDITIONALS_H37#include <TargetConditionals.h>38#endif /* HAVE_TARGET_CONDITIONALS_H */3940#if defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_INT) && defined(HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG)4142#ifdef HAVE_LIBKERN_OSATOMIC_H43#include <libkern/OSAtomic.h>44#endif /* HAVE_LIBKERN_OSATOMIC_H */4546#elif defined(__WIN32__) || defined(_WIN32)47#define _CRT_SECURE_NO_WARNINGS 148#include <windows.h>4950static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) {51/* fixme barrier is overkill -- see objc-os.h */52long original = InterlockedCompareExchange(dst, newl, oldl);53return (original == oldl);54}5556static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) {57/* fixme barrier is overkill -- see objc-os.h */58int original = InterlockedCompareExchange(dst, newi, oldi);59return (original == oldi);60}6162/*63* Check to see if the GCC atomic built-ins are available. If we're on64* a 64-bit system, make sure we have an 8-byte atomic function65* available.66*67*/6869#elif defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_INT) && defined(HAVE_SYNC_BOOL_COMPARE_AND_SWAP_LONG)7071static __inline bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst) {72return __sync_bool_compare_and_swap(dst, oldl, newl);73}7475static __inline bool OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst) {76return __sync_bool_compare_and_swap(dst, oldi, newi);77}7879#else80#error unknown atomic compare-and-swap primitive81#endif /* HAVE_OSATOMIC_COMPARE_AND_SWAP_INT && HAVE_OSATOMIC_COMPARE_AND_SWAP_LONG */828384/*85* Globals:86*/8788static void *_Block_copy_class = _NSConcreteMallocBlock;89static void *_Block_copy_finalizing_class = _NSConcreteMallocBlock;90static int _Block_copy_flag = BLOCK_NEEDS_FREE;91static int _Byref_flag_initial_value = BLOCK_NEEDS_FREE | 2;9293static const int WANTS_ONE = (1 << 16);9495static bool isGC = false;9697/*98* Internal Utilities:99*/100101#if 0102static unsigned long int latching_incr_long(unsigned long int *where) {103while (1) {104unsigned long int old_value = *(volatile unsigned long int *)where;105if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {106return BLOCK_REFCOUNT_MASK;107}108if (OSAtomicCompareAndSwapLong(old_value, old_value+1, (volatile long int *)where)) {109return old_value+1;110}111}112}113#endif /* if 0 */114115static int latching_incr_int(int *where) {116while (1) {117int old_value = *(volatile int *)where;118if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {119return BLOCK_REFCOUNT_MASK;120}121if (OSAtomicCompareAndSwapInt(old_value, old_value+1, (volatile int *)where)) {122return old_value+1;123}124}125}126127#if 0128static int latching_decr_long(unsigned long int *where) {129while (1) {130unsigned long int old_value = *(volatile int *)where;131if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {132return BLOCK_REFCOUNT_MASK;133}134if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {135return 0;136}137if (OSAtomicCompareAndSwapLong(old_value, old_value-1, (volatile long int *)where)) {138return old_value-1;139}140}141}142#endif /* if 0 */143144static int latching_decr_int(int *where) {145while (1) {146int old_value = *(volatile int *)where;147if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {148return BLOCK_REFCOUNT_MASK;149}150if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {151return 0;152}153if (OSAtomicCompareAndSwapInt(old_value, old_value-1, (volatile int *)where)) {154return old_value-1;155}156}157}158159160/*161* GC support stub routines:162*/163#if 0164#pragma mark GC Support Routines165#endif /* if 0 */166167168static void *_Block_alloc_default(const unsigned long size, const bool initialCountIsOne, const bool isObject) {169return malloc(size);170}171172static void _Block_assign_default(void *value, void **destptr) {173*destptr = value;174}175176static void _Block_setHasRefcount_default(const void *ptr, const bool hasRefcount) {177}178179static void _Block_do_nothing(const void *aBlock) { }180181static void _Block_retain_object_default(const void *ptr) {182if (!ptr) return;183}184185static void _Block_release_object_default(const void *ptr) {186if (!ptr) return;187}188189static void _Block_assign_weak_default(const void *ptr, void *dest) {190*(void **)dest = (void *)ptr;191}192193static void _Block_memmove_default(void *dst, void *src, unsigned long size) {194memmove(dst, src, (size_t)size);195}196197static void _Block_memmove_gc_broken(void *dest, void *src, unsigned long size) {198void **destp = (void **)dest;199void **srcp = (void **)src;200while (size) {201_Block_assign_default(*srcp, destp);202destp++;203srcp++;204size -= sizeof(void *);205}206}207208/*209* GC support callout functions - initially set to stub routines:210*/211212static void *(*_Block_allocator)(const unsigned long, const bool isOne, const bool isObject) = _Block_alloc_default;213static void (*_Block_deallocator)(const void *) = (void (*)(const void *))free;214static void (*_Block_assign)(void *value, void **destptr) = _Block_assign_default;215static void (*_Block_setHasRefcount)(const void *ptr, const bool hasRefcount) = _Block_setHasRefcount_default;216static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default;217static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default;218static void (*_Block_assign_weak)(const void *dest, void *ptr) = _Block_assign_weak_default;219static void (*_Block_memmove)(void *dest, void *src, unsigned long size) = _Block_memmove_default;220221222/*223* GC support SPI functions - called from ObjC runtime and CoreFoundation:224*/225226/* Public SPI227* Called from objc-auto to turn on GC.228* version 3, 4 arg, but changed 1st arg229*/230void _Block_use_GC( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),231void (*setHasRefcount)(const void *, const bool),232void (*gc_assign)(void *, void **),233void (*gc_assign_weak)(const void *, void *),234void (*gc_memmove)(void *, void *, unsigned long)) {235236isGC = true;237_Block_allocator = alloc;238_Block_deallocator = _Block_do_nothing;239_Block_assign = gc_assign;240_Block_copy_flag = BLOCK_IS_GC;241_Block_copy_class = _NSConcreteAutoBlock;242/* blocks with ctors & dtors need to have the dtor run from a class with a finalizer */243_Block_copy_finalizing_class = _NSConcreteFinalizingBlock;244_Block_setHasRefcount = setHasRefcount;245_Byref_flag_initial_value = BLOCK_IS_GC; // no refcount246_Block_retain_object = _Block_do_nothing;247_Block_release_object = _Block_do_nothing;248_Block_assign_weak = gc_assign_weak;249_Block_memmove = gc_memmove;250}251252/* transitional */253void _Block_use_GC5( void *(*alloc)(const unsigned long, const bool isOne, const bool isObject),254void (*setHasRefcount)(const void *, const bool),255void (*gc_assign)(void *, void **),256void (*gc_assign_weak)(const void *, void *)) {257/* until objc calls _Block_use_GC it will call us; supply a broken internal memmove implementation until then */258_Block_use_GC(alloc, setHasRefcount, gc_assign, gc_assign_weak, _Block_memmove_gc_broken);259}260261262/*263* Called from objc-auto to alternatively turn on retain/release.264* Prior to this the only "object" support we can provide is for those265* super special objects that live in libSystem, namely dispatch queues.266* Blocks and Block_byrefs have their own special entry points.267*268*/269void _Block_use_RR( void (*retain)(const void *),270void (*release)(const void *)) {271_Block_retain_object = retain;272_Block_release_object = release;273}274275/*276* Internal Support routines for copying:277*/278279#if 0280#pragma mark Copy/Release support281#endif /* if 0 */282283/* Copy, or bump refcount, of a block. If really copying, call the copy helper if present. */284static void *_Block_copy_internal(const void *arg, const int flags) {285struct Block_layout *aBlock;286const bool wantsOne = (WANTS_ONE & flags) == WANTS_ONE;287288//printf("_Block_copy_internal(%p, %x)\n", arg, flags);289if (!arg) return NULL;290291292// The following would be better done as a switch statement293aBlock = (struct Block_layout *)arg;294if (aBlock->flags & BLOCK_NEEDS_FREE) {295// latches on high296latching_incr_int(&aBlock->flags);297return aBlock;298}299else if (aBlock->flags & BLOCK_IS_GC) {300// GC refcounting is expensive so do most refcounting here.301if (wantsOne && ((latching_incr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK) == 1)) {302// Tell collector to hang on this - it will bump the GC refcount version303_Block_setHasRefcount(aBlock, true);304}305return aBlock;306}307else if (aBlock->flags & BLOCK_IS_GLOBAL) {308return aBlock;309}310311// Its a stack block. Make a copy.312if (!isGC) {313struct Block_layout *result = malloc(aBlock->descriptor->size);314if (!result) return (void *)0;315memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first316// reset refcount317result->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not needed318result->flags |= BLOCK_NEEDS_FREE | 1;319result->isa = _NSConcreteMallocBlock;320if (result->flags & BLOCK_HAS_COPY_DISPOSE) {321//printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock);322(*aBlock->descriptor->copy)(result, aBlock); // do fixup323}324return result;325}326else {327// Under GC want allocation with refcount 1 so we ask for "true" if wantsOne328// This allows the copy helper routines to make non-refcounted block copies under GC329unsigned long int flags = aBlock->flags;330bool hasCTOR = (flags & BLOCK_HAS_CTOR) != 0;331struct Block_layout *result = _Block_allocator(aBlock->descriptor->size, wantsOne, hasCTOR);332if (!result) return (void *)0;333memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first334// reset refcount335// if we copy a malloc block to a GC block then we need to clear NEEDS_FREE.336flags &= ~(BLOCK_NEEDS_FREE|BLOCK_REFCOUNT_MASK); // XXX not needed337if (wantsOne)338flags |= BLOCK_IS_GC | 1;339else340flags |= BLOCK_IS_GC;341result->flags = flags;342if (flags & BLOCK_HAS_COPY_DISPOSE) {343//printf("calling block copy helper...\n");344(*aBlock->descriptor->copy)(result, aBlock); // do fixup345}346if (hasCTOR) {347result->isa = _NSConcreteFinalizingBlock;348}349else {350result->isa = _NSConcreteAutoBlock;351}352return result;353}354}355356357/*358* Runtime entry points for maintaining the sharing knowledge of byref data blocks.359*360* A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data361* Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.362* We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment it.363* Otherwise we need to copy it and update the stack forwarding pointer364* XXX We need to account for weak/nonretained read-write barriers.365*/366367static void _Block_byref_assign_copy(void *dest, const void *arg, const int flags) {368struct Block_byref **destp = (struct Block_byref **)dest;369struct Block_byref *src = (struct Block_byref *)arg;370371//printf("_Block_byref_assign_copy called, byref destp %p, src %p, flags %x\n", destp, src, flags);372//printf("src dump: %s\n", _Block_byref_dump(src));373if (src->forwarding->flags & BLOCK_IS_GC) {374; // don't need to do any more work375}376else if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {377//printf("making copy\n");378// src points to stack379bool isWeak = ((flags & (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK)) == (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK));380// if its weak ask for an object (only matters under GC)381struct Block_byref *copy = (struct Block_byref *)_Block_allocator(src->size, false, isWeak);382copy->flags = src->flags | _Byref_flag_initial_value; // non-GC one for caller, one for stack383copy->forwarding = copy; // patch heap copy to point to itself (skip write-barrier)384src->forwarding = copy; // patch stack to point to heap copy385copy->size = src->size;386if (isWeak) {387copy->isa = &_NSConcreteWeakBlockVariable; // mark isa field so it gets weak scanning388}389if (src->flags & BLOCK_HAS_COPY_DISPOSE) {390// Trust copy helper to copy everything of interest391// If more than one field shows up in a byref block this is wrong XXX392copy->byref_keep = src->byref_keep;393copy->byref_destroy = src->byref_destroy;394(*src->byref_keep)(copy, src);395}396else {397// just bits. Blast 'em using _Block_memmove in case they're __strong398_Block_memmove(399(void *)©->byref_keep,400(void *)&src->byref_keep,401src->size - sizeof(struct Block_byref_header));402}403}404// already copied to heap405else if ((src->forwarding->flags & BLOCK_NEEDS_FREE) == BLOCK_NEEDS_FREE) {406latching_incr_int(&src->forwarding->flags);407}408// assign byref data block pointer into new Block409_Block_assign(src->forwarding, (void **)destp);410}411412// Old compiler SPI413static void _Block_byref_release(const void *arg) {414struct Block_byref *shared_struct = (struct Block_byref *)arg;415int refcount;416417// dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)418shared_struct = shared_struct->forwarding;419420//printf("_Block_byref_release %p called, flags are %x\n", shared_struct, shared_struct->flags);421// To support C++ destructors under GC we arrange for there to be a finalizer for this422// by using an isa that directs the code to a finalizer that calls the byref_destroy method.423if ((shared_struct->flags & BLOCK_NEEDS_FREE) == 0) {424return; // stack or GC or global425}426refcount = shared_struct->flags & BLOCK_REFCOUNT_MASK;427if (refcount <= 0) {428printf("_Block_byref_release: Block byref data structure at %p underflowed\n", arg);429}430else if ((latching_decr_int(&shared_struct->flags) & BLOCK_REFCOUNT_MASK) == 0) {431//printf("disposing of heap based byref block\n");432if (shared_struct->flags & BLOCK_HAS_COPY_DISPOSE) {433//printf("calling out to helper\n");434(*shared_struct->byref_destroy)(shared_struct);435}436_Block_deallocator((struct Block_layout *)shared_struct);437}438}439440441/*442*443* API supporting SPI444* _Block_copy, _Block_release, and (old) _Block_destroy445*446*/447448#if 0449#pragma mark SPI/API450#endif /* if 0 */451452void *_Block_copy(const void *arg) {453return _Block_copy_internal(arg, WANTS_ONE);454}455456457// API entry point to release a copied Block458void _Block_release(void *arg) {459struct Block_layout *aBlock = (struct Block_layout *)arg;460int32_t newCount;461if (!aBlock) return;462newCount = latching_decr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK;463if (newCount > 0) return;464// Hit zero465if (aBlock->flags & BLOCK_IS_GC) {466// Tell GC we no longer have our own refcounts. GC will decr its refcount467// and unless someone has done a CFRetain or marked it uncollectable it will468// now be subject to GC reclamation.469_Block_setHasRefcount(aBlock, false);470}471else if (aBlock->flags & BLOCK_NEEDS_FREE) {472if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE)(*aBlock->descriptor->dispose)(aBlock);473_Block_deallocator(aBlock);474}475else if (aBlock->flags & BLOCK_IS_GLOBAL) {476;477}478else {479printf("Block_release called upon a stack Block: %p, ignored\n", (void *)aBlock);480}481}482483484485// Old Compiler SPI point to release a copied Block used by the compiler in dispose helpers486static void _Block_destroy(const void *arg) {487struct Block_layout *aBlock;488if (!arg) return;489aBlock = (struct Block_layout *)arg;490if (aBlock->flags & BLOCK_IS_GC) {491// assert(aBlock->Block_flags & BLOCK_HAS_CTOR);492return; // ignore, we are being called because of a DTOR493}494_Block_release(aBlock);495}496497498499/*500*501* SPI used by other layers502*503*/504505// SPI, also internal. Called from NSAutoBlock only under GC506void *_Block_copy_collectable(const void *aBlock) {507return _Block_copy_internal(aBlock, 0);508}509510511// SPI512unsigned long int Block_size(void *arg) {513return ((struct Block_layout *)arg)->descriptor->size;514}515516517#if 0518#pragma mark Compiler SPI entry points519#endif /* if 0 */520521522/*******************************************************523524Entry points used by the compiler - the real API!525526527A Block can reference four different kinds of things that require help when the Block is copied to the heap.5281) C++ stack based objects5292) References to Objective-C objects5303) Other Blocks5314) __block variables532533In 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.534535The flags parameter of _Block_object_assign and _Block_object_dispose is set to536* BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,537* BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and538* BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.539If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16).540541So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.542543When 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.544545So 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:546__block id 128+3547__weak block id 128+3+16548__block (^Block) 128+7549__weak __block (^Block) 128+7+16550551The implementation of the two routines would be improved by switch statements enumerating the eight cases.552553********************************************************/554555/*556* When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point557* to do the assignment.558*/559void _Block_object_assign(void *destAddr, const void *object, const int flags) {560//printf("_Block_object_assign(*%p, %p, %x)\n", destAddr, object, flags);561if ((flags & BLOCK_BYREF_CALLER) == BLOCK_BYREF_CALLER) {562if ((flags & BLOCK_FIELD_IS_WEAK) == BLOCK_FIELD_IS_WEAK) {563_Block_assign_weak(object, destAddr);564}565else {566// do *not* retain or *copy* __block variables whatever they are567_Block_assign((void *)object, destAddr);568}569}570else if ((flags & BLOCK_FIELD_IS_BYREF) == BLOCK_FIELD_IS_BYREF) {571// copying a __block reference from the stack Block to the heap572// flags will indicate if it holds a __weak reference and needs a special isa573_Block_byref_assign_copy(destAddr, object, flags);574}575// (this test must be before next one)576else if ((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_BLOCK) {577// copying a Block declared variable from the stack Block to the heap578_Block_assign(_Block_copy_internal(object, flags), destAddr);579}580// (this test must be after previous one)581else if ((flags & BLOCK_FIELD_IS_OBJECT) == BLOCK_FIELD_IS_OBJECT) {582//printf("retaining object at %p\n", object);583_Block_retain_object(object);584//printf("done retaining object at %p\n", object);585_Block_assign((void *)object, destAddr);586}587}588589// When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point590// to help dispose of the contents591// Used initially only for __attribute__((NSObject)) marked pointers.592void _Block_object_dispose(const void *object, const int flags) {593//printf("_Block_object_dispose(%p, %x)\n", object, flags);594if (flags & BLOCK_FIELD_IS_BYREF) {595// get rid of the __block data structure held in a Block596_Block_byref_release(object);597}598else if ((flags & (BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_BLOCK) {599// get rid of a referenced Block held by this Block600// (ignore __block Block variables, compiler doesn't need to call us)601_Block_destroy(object);602}603else if ((flags & (BLOCK_FIELD_IS_WEAK|BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_OBJECT) {604// get rid of a referenced object held by this Block605// (ignore __block object variables, compiler doesn't need to call us)606_Block_release_object(object);607}608}609610611/*612* Debugging support:613*/614#if 0615#pragma mark Debugging616#endif /* if 0 */617618619const char *_Block_dump(const void *block) {620struct Block_layout *closure = (struct Block_layout *)block;621static char buffer[512];622char *cp = buffer;623if (closure == NULL) {624sprintf(cp, "NULL passed to _Block_dump\n");625return buffer;626}627if (! (closure->flags & BLOCK_HAS_DESCRIPTOR)) {628printf("Block compiled by obsolete compiler, please recompile source for this Block\n");629exit(1);630}631cp += sprintf(cp, "^%p (new layout) =\n", (void *)closure);632if (closure->isa == NULL) {633cp += sprintf(cp, "isa: NULL\n");634}635else if (closure->isa == _NSConcreteStackBlock) {636cp += sprintf(cp, "isa: stack Block\n");637}638else if (closure->isa == _NSConcreteMallocBlock) {639cp += sprintf(cp, "isa: malloc heap Block\n");640}641else if (closure->isa == _NSConcreteAutoBlock) {642cp += sprintf(cp, "isa: GC heap Block\n");643}644else if (closure->isa == _NSConcreteGlobalBlock) {645cp += sprintf(cp, "isa: global Block\n");646}647else if (closure->isa == _NSConcreteFinalizingBlock) {648cp += sprintf(cp, "isa: finalizing Block\n");649}650else {651cp += sprintf(cp, "isa?: %p\n", (void *)closure->isa);652}653cp += sprintf(cp, "flags:");654if (closure->flags & BLOCK_HAS_DESCRIPTOR) {655cp += sprintf(cp, " HASDESCRIPTOR");656}657if (closure->flags & BLOCK_NEEDS_FREE) {658cp += sprintf(cp, " FREEME");659}660if (closure->flags & BLOCK_IS_GC) {661cp += sprintf(cp, " ISGC");662}663if (closure->flags & BLOCK_HAS_COPY_DISPOSE) {664cp += sprintf(cp, " HASHELP");665}666if (closure->flags & BLOCK_HAS_CTOR) {667cp += sprintf(cp, " HASCTOR");668}669cp += sprintf(cp, "\nrefcount: %u\n", closure->flags & BLOCK_REFCOUNT_MASK);670cp += sprintf(cp, "invoke: %p\n", (void *)(uintptr_t)closure->invoke);671{672struct Block_descriptor *dp = closure->descriptor;673cp += sprintf(cp, "descriptor: %p\n", (void *)dp);674cp += sprintf(cp, "descriptor->reserved: %lu\n", dp->reserved);675cp += sprintf(cp, "descriptor->size: %lu\n", dp->size);676677if (closure->flags & BLOCK_HAS_COPY_DISPOSE) {678cp += sprintf(cp, "descriptor->copy helper: %p\n", (void *)(uintptr_t)dp->copy);679cp += sprintf(cp, "descriptor->dispose helper: %p\n", (void *)(uintptr_t)dp->dispose);680}681}682return buffer;683}684685686const char *_Block_byref_dump(struct Block_byref *src) {687static char buffer[256];688char *cp = buffer;689cp += sprintf(cp, "byref data block %p contents:\n", (void *)src);690cp += sprintf(cp, " forwarding: %p\n", (void *)src->forwarding);691cp += sprintf(cp, " flags: 0x%x\n", src->flags);692cp += sprintf(cp, " size: %d\n", src->size);693if (src->flags & BLOCK_HAS_COPY_DISPOSE) {694cp += sprintf(cp, " copy helper: %p\n", (void *)(uintptr_t)src->byref_keep);695cp += sprintf(cp, " dispose helper: %p\n", (void *)(uintptr_t)src->byref_destroy);696}697return buffer;698}699700701702