// 6618342 Contact the team that owns the Instrument DTrace probe before renaming this symbol dispatch_source_t _dispatch_source_create2(dispatch_source_t ds, dispatch_source_attr_t attr, void *context, dispatch_source_handler_function_t handler) { if (ds == NULL || handler == NULL) { return NULL; } ds->ds_is_legacy = true; ds->ds_handler_func = (dispatch_function_t)handler; ds->ds_handler_ctxt = context; if (attr && attr != DISPATCH_SOURCE_CREATE_SUSPENDED) { ds->dq_finalizer_ctxt = attr->finalizer_ctxt; ds->dq_finalizer_func = (typeof(ds->dq_finalizer_func))attr->finalizer_func; ds->do_ctxt = attr->context; } #ifdef __BLOCKS__ if (ds->dq_finalizer_func == (void*)_dispatch_call_block_and_release2) { ds->dq_finalizer_ctxt = Block_copy(ds->dq_finalizer_ctxt); if (!ds->dq_finalizer_ctxt) { goto out_bad; } } if (handler == _dispatch_source_call_block) { struct Block_layout *bl = ds->ds_handler_ctxt = Block_copy(context); if (!ds->ds_handler_ctxt) { if (ds->dq_finalizer_func == (void*)_dispatch_call_block_and_release2) { Block_release(ds->dq_finalizer_ctxt); } goto out_bad; } ds->ds_handler_func = (void *)bl->invoke; ds->ds_handler_is_block = true; } // all legacy sources get a cancellation event on the normal event handler. dispatch_function_t func = ds->ds_handler_func; dispatch_source_handler_t block = ds->ds_handler_ctxt; void *ctxt = ds->ds_handler_ctxt; bool handler_is_block = ds->ds_handler_is_block; ds->ds_cancel_is_block = true; if (handler_is_block) { ds->ds_cancel_handler = _dispatch_Block_copy(^{ block(ds); }); } else {
int main() { __block int var = 0; int shouldbe = 0; void (^b)(void) = ^{ var++; /*printf("var is at %p with value %d\n", &var, var);*/ }; __typeof(b) _b; //printf("before copy...\n"); b(); ++shouldbe; size_t i; for (i = 0; i < 10; i++) { _b = Block_copy(b); // make a new copy each time assert(_b); ++shouldbe; _b(); // should still update the stack Block_release(_b); } //printf("after...\n"); b(); ++shouldbe; if (var != shouldbe) { fail("var is %d but should be %d", var, shouldbe); } succeed(__FILE__); }
IMP imp_implementationWithBlock(void *block) { struct Block_layout *b = block; void *start; void *end; if ((b->flags & BLOCK_USE_SRET) == BLOCK_USE_SRET) { start = &__objc_block_trampoline_sret; end = &__objc_block_trampoline_end_sret; } else { start = &__objc_block_trampoline; end = &__objc_block_trampoline_end; } size_t trampolineSize = end - start; // If we don't have a trampoline intrinsic for this architecture, return a // null IMP. if (0 >= trampolineSize) { return 0; } struct wx_buffer buf = alloc_buffer(trampolineSize + 2*sizeof(void*)); void **out = buf.w; out[0] = (void*)b->invoke; out[1] = Block_copy(b); memcpy(&out[2], start, trampolineSize); out = buf.x; return (IMP)&out[2]; }
OM_uint32 GSSAPI_LIB_FUNCTION gss_acquire_cred_ex(const gss_name_t desired_name, OM_uint32 flags, OM_uint32 time_req, gss_const_OID desired_mech, gss_cred_usage_t cred_usage, gss_auth_identity_t identity, gss_acquire_cred_complete complete) { OM_uint32 ret; complete = (gss_acquire_cred_complete)Block_copy(complete); ret = gss_acquire_cred_ex_f(NULL, desired_name, flags, time_req, desired_mech, cred_usage, identity, complete, complete_block); if (ret != GSS_S_COMPLETE) Block_release(complete); return ret; }
void RunBlockThreaded(dispatch_block_t block) { pthread_t result; pthread_attr_t attr; pthread_attr_init( &attr ); pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); pthread_create( &result, &attr, ThreadEntrypoint, Block_copy(block)); pthread_attr_destroy( &attr ); }
BoringBlock quiz_5(void) { __block int x = 1; printf("x address is %p\n", &x); BoringBlock localBlock = ^{ x++; printf("x is %d, &x is %p\n", x, &x); }; boringBlock = Block_copy(localBlock); printf("x address is %p\n", &x); BoringBlock retBlock = Block_copy(localBlock); printf("x address is %p\n", &x); return retBlock; }
void quiz_2 (void) { __block int x = 1; BoringBlock localBlock = ^{ printf("x is %d\n", x); printf("End of quiz 2\n\n"); }; boringBlock = Block_copy(localBlock); x++; }
void dyld_register_tlv_state_change_handler(enum dyld_tlv_states state, dyld_tlv_state_change_handler handler) { TLVHandler *h = malloc(sizeof(TLVHandler)); h->state = state; h->handler = Block_copy(handler); TLVHandler *old; do { old = tlv_handlers; h->next = old; } while (! OSAtomicCompareAndSwapPtrBarrier(old, h, (void * volatile *)&tlv_handlers)); }
void quiz_1 (void) { __block int x = 1; printf("x address is %p\n", &x); BoringBlock localBlock = ^{ x++; // Dummy use of x printf("End of quiz 1\n\n"); }; boringBlock = Block_copy(localBlock); printf("after copy, x address is %p\n", &x); }
voidVoid testRoutine(const char *whoami) { __block int dumbo = strlen(whoami); dummy = ^{ //printf("incring dumbo from %d\n", dumbo); ++dumbo; }; voidVoid copy = Block_copy(dummy); return copy; }
void quiz_3 (void) { __block int x = 1; __block int* ptr = &x; BoringBlock localBlock = ^{ printf("x is %d, *ptr is %d\n", x, *ptr); printf("End of quiz 3\n\n"); }; boringBlock = Block_copy(localBlock); x++; }
voidVoid testFunction() { int i = random(); __block voidVoid inner = ^{ doSomething(i); }; //printf("inner, on stack, is %p\n", (void*)inner); /*__block*/ voidVoid outer = ^{ //printf("will call inner block %p\n", (void *)inner); inner(); }; //printf("outer looks like: %s\n", _Block_dump(outer)); voidVoid result = Block_copy(outer); //Block_release(inner); return result; }
sparse_error_t sparse_begin_using_block(sparse_state_t *state, size_t initial_buffer_capacity, sparse_options_t options, sparse_block_t block) { const sparse_error_t error = sparse_begin(state, initial_buffer_capacity, options, NULL, NULL); /* Could go back and reimplement block support by providing a callback that recognizes its context pointer as a block, but then I'm not sure I can guarantee a block will fit in the space of a pointer at all times. Kind of like passing function pointers as void* - it's probably not really a good idea. */ if (error == SP_NO_ERROR) state->block = Block_copy(block); return error; }
int main() { __block int i; i = 0; void (^block)() = ^{ printf("Hello world %d\n", i); }; ++i; void (^block2)() = Block_copy(block); ++i; block2(); Block_release(block2); return 0; }
dispatch_source_attr_t dispatch_source_attr_copy(dispatch_source_attr_t proto) { dispatch_source_attr_t rval = NULL; if (proto && (rval = malloc(sizeof(struct dispatch_source_attr_s)))) { memcpy(rval, proto, sizeof(struct dispatch_source_attr_s)); #ifdef __BLOCKS__ if (rval->finalizer_func == (void*)_dispatch_call_block_and_release2) { rval->finalizer_ctxt = Block_copy(rval->finalizer_ctxt); } #endif } else if (!proto) { rval = dispatch_source_attr_create(); } return rval; }
void func(int n) { __block int sh = 0; void (^b1)(void) = ^{ sh += 1; printf("%d: b1, n=%d, sh=%d\n", ++c, n, sh); }; void (^b2)(void) = ^{ sh += 20; printf("%d: b2, n=%d, sh=%d\n", ++c, n, sh); }; b1(); b2(); g = Block_copy(b1); sh += n * 1000; n = 999; b2(); }
long dispatch_source_attr_set_finalizer(dispatch_source_attr_t attr, dispatch_source_finalizer_t finalizer) { void *ctxt; dispatch_source_finalizer_function_t func; if (finalizer) { if (!(ctxt = Block_copy(finalizer))) { return 1; } func = (void *)_dispatch_call_block_and_release2; } else { ctxt = NULL; func = NULL; } dispatch_source_attr_set_finalizer_f(attr, ctxt, func); return 0; }
IMP pl_imp_implementationWithBlock (void *block) { #if SUPPORT_APPLE_FALLBACK /* Prefer Apple's implementation */ if (&imp_implementationWithBlock != NULL) return imp_implementationWithBlock(block); #endif /* Allocate the appropriate trampoline type. */ pl_trampoline *tramp; struct Block_layout *bl = block; if (bl->flags & BLOCK_USE_STRET) { tramp = pl_trampoline_alloc(&pl_blockimp_table_stret_page_config, &blockimp_lock, &blockimp_table_stret); } else { tramp = pl_trampoline_alloc(&pl_blockimp_table_page_config, &blockimp_lock, &blockimp_table); } /* Configure the trampoline */ void **config = pl_trampoline_data_ptr(tramp->trampoline); config[0] = Block_copy(block); config[1] = tramp; /* Return the function pointer. */ return (IMP) tramp->trampoline; }
* Copyright (c) 2010 Apple Inc. All rights reserved. * * @APPLE_LLVM_LICENSE_HEADER@ */ // ..\clang -rewrite-objc -fms-extensions simpleblock.c // #include <iostream> // using namespace std; // #include "Block.h" int main(int argc, char **argv) { void(^aBlock)(int x); void(^bBlock)(int x); aBlock = ^(int x) { // cout << "Hello, " << x << endl; }; aBlock(42); bBlock = (void *)Block_copy(aBlock); bBlock(46); Block_release(bBlock); return 0; }
bool SOSKeychainAccountSetFactoryForAccount(SOSCCAccountDataSourceFactoryBlock block) { accountDataSourceOverride = Block_copy(block); return true; }
void db_get_b(database *db, const dstr doc_name, db_get_bcb callback) { db_get(db, doc_name, (void *)Block_copy(callback), _get_bcb); }
void db_apply_op_b(const database *db, client *source, ot_document *doc, uint32_t version, const ot_op *op, db_apply_bcb callback) { db_apply_op(db, source, doc, version, op, (void *)Block_copy(callback), _apply_op_b_cb); }
void db_create_b(database *db, const dstr doc_name, ot_type *type, db_create_bcb callback) { db_create(db, doc_name, type, (void *)Block_copy(callback), _create_bcb); }