My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Copyright 2008 - 2009 Apple, Inc.
* Copyright 2009 - 2010 Plausible Labs Cooperative, Inc.
*
* All rights reserved.
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE 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.
*/

#import "Block.h"
#import "Block_private.h"
#import "NSBlock.h"

#import <stdlib.h>
#import <string.h>
#import <libkern/OSAtomic.h>

#import <objc/runtime.h>
#import <objc/objc-auto.h>

#import <Foundation/Foundation.h>
#import <AvailabilityMacros.h>

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif

static void *_PLBlock_copy_internal(const void *arg, const bool wantsOne);

// We have to override these declarations to ensure weak import
extern void *_Block_copy(const void *aBlock) __attribute__((weak_import));
extern void _Block_release(const void *aBlock) __attribute__((weak_import));

extern void _Block_object_dispose(const void *object, const int flags) __attribute__((weak_import));
extern void _Block_object_assign(void *destAddr, const void *object, const int flags) __attribute__((weak_import));

#ifdef __OBJC_GC__
static void _PLBlock_enable_GC ();
#endif


/* If true, we should use Apple's block runtime. The check for this is done in +[PLBlockRuntime load]. */
bool PLBlock_AppleRuntimeEnabled = false;

/* Manages runtime initialization */
@interface PLBlockRuntime : NSObject @end

static int latching_incr_int(int *where);
static int latching_decr_int(int *where);

/* If true, Garbage Collection is enabled */
static bool isGC = false;

/* Flag to set for by-ref variables. (Defaults to non-GC flag) */
static int _Byref_flag_initial_value = BLOCK_NEEDS_FREE | 2;

/* GC/Non-GC Implementation Function Prototypes */
typedef void *(_PLBlock_allocator_t)(const unsigned long, const bool isOne, const bool isObject);
typedef void (_PLBlock_deallocator_t)(const void *);
typedef void (_PLBlock_assign_t)(void *value, void **destptr);
typedef void (_PLBlock_setHasRefcount_t)(const void *ptr, const bool hasRefcount);
typedef void (_PLBlock_retain_object_t)(const void *ptr);
typedef void (_PLBlock_release_object_t)(const void *ptr);
typedef void (_PLBlock_assign_weak_t)(const void *value, void **destptr);
typedef void (_PLBlock_memmove_t)(void *dest, void *src, unsigned long size);

/* Default retain/release implementations */
static _PLBlock_allocator_t _PLBlock_alloc_default;
static _PLBlock_assign_t _PLBlock_assign_default;
static _PLBlock_setHasRefcount_t _PLBlock_setHasRefcount_default;
static _PLBlock_retain_object_t _PLBlock_retain_object_default;
static _PLBlock_release_object_t _PLBlock_release_object_default;
static _PLBlock_assign_weak_t _PLBlock_assign_weak_default;
static _PLBlock_memmove_t _PLBlock_memmove_default;

/* GC implementations */
#ifdef __OBJC_GC__
static _PLBlock_allocator_t _PLBlock_alloc_gc;
static _PLBlock_assign_t _PLBlock_assign_gc;
static _PLBlock_setHasRefcount_t _PLBlock_setHasRefcount_gc;
static _PLBlock_assign_weak_t _PLBlock_assign_weak_gc;
static _PLBlock_memmove_t _PLBlock_memmove_gc;
#endif /* __OBJC_GC__ */

/* GC/Non-GC support callout functions pointers. Default to non-GC implementation. */
static _PLBlock_allocator_t *_PLBlock_allocator = _PLBlock_alloc_default;
static _PLBlock_deallocator_t *_PLBlock_deallocator = (void (*)(const void *))free;
static _PLBlock_assign_t *_PLBlock_assign = _PLBlock_assign_default;
static _PLBlock_setHasRefcount_t *_PLBlock_setHasRefcount = _PLBlock_setHasRefcount_default;
static _PLBlock_retain_object_t *_PLBlock_retain_object = _PLBlock_retain_object_default;
static _PLBlock_release_object_t *_PLBlock_release_object = _PLBlock_release_object_default;
static _PLBlock_assign_weak_t *_PLBlock_assign_weak = _PLBlock_assign_weak_default;
static _PLBlock_memmove_t *_PLBlock_memmove = _PLBlock_memmove_default;

/**
* @ingroup functions
* @{
*/


/**
* Copy a a stack-allocated block, or increase the retain count for blocks that do not require copying
* (such as global or malloc-allocated blocks).
*
* @warning Calls to PLBlock_copy must be balanced with calls to PLBlock_release.
*/
void *_PLBlock_copy (const void *arg) {
if (PLBlock_AppleRuntimeEnabled)
return _Block_copy(arg);

return _PLBlock_copy_internal(arg, true);
}

/**
* @internal
* @ingroup private_api
*
* Create a GC-collectable copy of the provided block.
* Must not be called if the Apple runtime is enabled.
*/
void *_PLBlock_copy_gc (const void *arg) {
assert(!PLBlock_AppleRuntimeEnabled);

return _PLBlock_copy_internal(arg, false);
}

/**
* Release a copied block.
* API entry point to release a copied Block
*/
void _PLBlock_release(const void *arg) {
if (PLBlock_AppleRuntimeEnabled)
return _Block_release(arg);

struct Block_layout *aBlock = (struct Block_layout *)arg;
int32_t newCount;
if (!aBlock) return;
newCount = latching_decr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK;
if (newCount > 0) return;
// Hit zero
if (aBlock->flags & BLOCK_IS_GC) {
// Tell GC we no longer have our own refcounts. GC will decr its refcount
// and unless someone has done a CFRetain or marked it uncollectable it will
// now be subject to GC reclamation.
_PLBlock_setHasRefcount(aBlock, false);
}
else if (aBlock->flags & BLOCK_NEEDS_FREE) {
if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE)(*aBlock->descriptor->dispose)(aBlock);
_PLBlock_deallocator(aBlock);
}
else if (aBlock->flags & BLOCK_IS_GLOBAL) {
;
}
else {
printf("Block_release called upon a stack Block: %p, ignored\n", aBlock);
}
}


/**
* @}
*/


/**
* @internal
* @defgroup functions_internal_implementation Internal Implementation Functions
* @ingroup functions
* @{
*/

/** Non-GC block allocator */
static void *_PLBlock_alloc_default(const unsigned long size, const bool initialCountIsOne, const bool isObject) {
return malloc(size);
}

/** Non-GC block assignment */
static void _PLBlock_assign_default(void *value, void **destptr) {
*destptr = value;
}

/** Increment/decrement GC refcount. No-op if GC is disabled. */
static void _PLBlock_setHasRefcount_default(const void *ptr, const bool hasRefcount) {
}

/** Retain an object instance */
static void _PLBlock_retain_object_default(const void *ptr) {
if (!ptr) return;

/* Bump the object's reference count */
CFRetain(ptr);
}

/** Release an object instance */
static void _PLBlock_release_object_default(const void *ptr) {
if (!ptr) return;

/* Decrement the object's reference count */
CFRelease(ptr);
}

/** Non-GC weak assignment */
static void _PLBlock_assign_weak_default(const void *value, void **destptr) {
*destptr = (void *) value;
}

/** Non-GC memmove */
static void _PLBlock_memmove_default(void *dst, void *src, unsigned long size) {
memmove(dst, src, (size_t)size);
}



/** GC block allocator */
#ifdef __OBJC_GC__
static void *_PLBlock_alloc_gc (const unsigned long size, const bool initialCountIsOne, const bool isObject) {
struct Block_layout *result;

/* Allocate the GC-scanned space */
if (isObject)
result = (void *) NSAllocateObject([_PLFinalizingBlock class], size, nil); // allocates larger than requested size.
else
result = NSAllocateCollectable(size, NSScannedOption);

/* Bump the GC reference count */
if (initialCountIsOne) {
latching_incr_int(&result->flags);
_PLBlock_setHasRefcount_gc(result, true);
}

return result;
}


/** No-op block call. Used to replace retain/release calls if GC is enabled. */
static void _PLBlock_do_nothing_gc(const void *aBlock) {
}

/** GC block assignment */
static void _PLBlock_assign_gc (void *value, void **destptr) {
objc_assign_strongCast(value, (id *) destptr);
}

/** Increment/decrement GC refcount. */
static void _PLBlock_setHasRefcount_gc (const void *ptr, const bool hasRefcount) {
if (hasRefcount)
[[NSGarbageCollector defaultCollector] disableCollectorForPointer: (void *) ptr];
else
[[NSGarbageCollector defaultCollector] enableCollectorForPointer: (void *) ptr];
}

/** GC weak assignment */
static void _PLBlock_assign_weak_gc (const void *value, void **destptr) {
objc_assign_weak((void *) value, (id *) destptr);
}

/** GC memmove */
static void _PLBlock_memmove_gc (void *dst, void *src, unsigned long size) {
objc_memmove_collectable(dst, src, (size_t)size);
}

/**
* @internal
*
* Enable garbage collection.
*/
static void _PLBlock_enable_GC () {
isGC = true;

_PLBlock_allocator = _PLBlock_alloc_gc;
_PLBlock_deallocator = _PLBlock_do_nothing_gc;
_PLBlock_assign = _PLBlock_assign_gc;

_PLBlock_setHasRefcount = _PLBlock_setHasRefcount_gc;
_Byref_flag_initial_value = BLOCK_IS_GC; // no refcount

_PLBlock_retain_object = _PLBlock_do_nothing_gc;
_PLBlock_release_object = _PLBlock_do_nothing_gc;

_PLBlock_assign_weak = _PLBlock_assign_weak_gc;
_PLBlock_memmove = _PLBlock_memmove_gc;
}

#endif /* __OBJC_GC__ */

// Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
static void *_PLBlock_copy_internal(const void *arg, const bool wantsOne) {
struct Block_layout *aBlock;

//printf("_Block_copy_internal(%p, %x)\n", arg, flags);
if (!arg) return NULL;


// The following would be better done as a switch statement
aBlock = (struct Block_layout *)arg;
if (aBlock->flags & BLOCK_NEEDS_FREE) {
// latches on high
latching_incr_int(&aBlock->flags);
return aBlock;
}
else if (aBlock->flags & BLOCK_IS_GC) {
// GC refcounting is expensive so do most refcounting here.
if (wantsOne && ((latching_incr_int(&aBlock->flags) & BLOCK_REFCOUNT_MASK) == 1)) {
// Tell collector to hang on this - it will bump the GC refcount version
_PLBlock_setHasRefcount(aBlock, true);
}
return aBlock;
}
else if (aBlock->flags & BLOCK_IS_GLOBAL) {
return aBlock;
}

// Its a stack block. Make a copy.
if (!isGC) {
struct Block_layout *result = malloc(aBlock->descriptor->size);
if (!result) return (void *)0;
memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first
// reset refcount
result->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not needed
result->flags |= BLOCK_NEEDS_FREE | 1;
result->isa = [_PLMallocBlock class];
if (result->flags & BLOCK_HAS_COPY_DISPOSE) {
//printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock);
(*aBlock->descriptor->copy)(result, aBlock); // do fixup
}
return result;
}
else {
// Under GC want allocation with refcount 1 so we ask for "true" if wantsOne
// This allows the copy helper routines to make non-refcounted block copies under GC
unsigned long int flags = aBlock->flags;
bool hasCTOR = (flags & BLOCK_HAS_CTOR) != 0;
struct Block_layout *result = _PLBlock_allocator(aBlock->descriptor->size, wantsOne, hasCTOR);
if (!result) return (void *)0;
_PLBlock_memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first
// reset refcount
// if we copy a malloc block to a GC block then we need to clear NEEDS_FREE.
flags &= ~(BLOCK_NEEDS_FREE|BLOCK_REFCOUNT_MASK); // XXX not needed
if (wantsOne)
flags |= BLOCK_IS_GC | 1;
else
flags |= BLOCK_IS_GC;
result->flags = flags;
if (flags & BLOCK_HAS_COPY_DISPOSE) {
//printf("calling block copy helper...\n");
(*aBlock->descriptor->copy)(result, aBlock); // do fixup
}
if (hasCTOR) {
result->isa = [_PLFinalizingBlock class];
}
else {
result->isa = [_PLAutoBlock class];
}
return result;
}
}


static void _PLBlock_destroy(const void *arg) {
struct Block_layout *aBlock;
if (!arg) return;
aBlock = (struct Block_layout *)arg;
if (aBlock->flags & BLOCK_IS_GC) {
// assert(aBlock->Block_flags & BLOCK_HAS_CTOR);
return; // ignore, we are being called because of a DTOR
}
_PLBlock_release(aBlock);
}


// A closure has been copied and its fixup routine is asking us to fix up the reference to the shared byref data
// Closures that aren't copied must still work, so everyone always accesses variables after dereferencing the forwarding ptr.
// We ask if the byref pointer that we know about has already been copied to the heap, and if so, increment it.
// Otherwise we need to copy it and update the stack forwarding pointer
// XXX We need to account for weak/nonretained read-write barriers.
static void _PLBlock_byref_assign_copy(void *dest, const void *arg, const int flags) {
struct Block_byref **destp = (struct Block_byref **)dest;
struct Block_byref *src = (struct Block_byref *)arg;

//printf("_Block_byref_assign_copy called, byref destp %p, src %p, flags %x\n", destp, src, flags);
//printf("src dump: %s\n", _Block_byref_dump(src));
if (src->forwarding->flags & BLOCK_IS_GC) {
; // don't need to do any more work
}
else if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
//printf("making copy\n");
// src points to stack
bool isWeak = ((flags & (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK)) == (BLOCK_FIELD_IS_BYREF|BLOCK_FIELD_IS_WEAK));
// if its weak ask for an object (only matters under GC)
struct Block_byref *copy = (struct Block_byref *)_PLBlock_allocator(src->size, false, isWeak);
copy->flags = src->flags | _Byref_flag_initial_value; // non-GC one for caller, one for stack
copy->forwarding = copy; // patch heap copy to point to itself (skip write-barrier)
src->forwarding = copy; // patch stack to point to heap copy
copy->size = src->size;
if (isWeak) {
copy->isa = [_PLWeakBlockVariable class]; // mark isa field so it gets weak scanning
}
if (src->flags & BLOCK_HAS_COPY_DISPOSE) {
// Trust copy helper to copy everything of interest
// If more than one field shows up in a byref block this is wrong XXX
copy->byref_keep = src->byref_keep;
copy->byref_destroy = src->byref_destroy;
(*src->byref_keep)(copy, src);
}
else {
// just bits. Blast 'em using _Block_memmove in case they're __strong
_PLBlock_memmove(
(void *)&copy->byref_keep,
(void *)&src->byref_keep,
src->size - sizeof(struct Block_byref_header));
}
}
// already copied to heap
else if ((src->forwarding->flags & BLOCK_NEEDS_FREE) == BLOCK_NEEDS_FREE) {
latching_incr_int(&src->forwarding->flags);
}
// assign byref data block pointer into new Block
_PLBlock_assign(src->forwarding, (void **)destp);
}


static void _PLBlock_byref_release(const void *arg) {
struct Block_byref *shared_struct = (struct Block_byref *)arg;
int refcount;

// dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
shared_struct = shared_struct->forwarding;

//printf("_Block_byref_release %p called, flags are %x\n", shared_struct, shared_struct->flags);
// To support C++ destructors under GC we arrange for there to be a finalizer for this
// by using an isa that directs the code to a finalizer that calls the byref_destroy method.
if ((shared_struct->flags & BLOCK_NEEDS_FREE) == 0) {
return; // stack or GC or global
}
refcount = shared_struct->flags & BLOCK_REFCOUNT_MASK;
if (refcount <= 0) {
printf("_Block_byref_release: Block byref data structure at %p underflowed\n", arg);
}
else if ((latching_decr_int(&shared_struct->flags) & BLOCK_REFCOUNT_MASK) == 0) {
//printf("disposing of heap based byref block\n");
if (shared_struct->flags & BLOCK_HAS_COPY_DISPOSE) {
//printf("calling out to helper\n");
(*shared_struct->byref_destroy)(shared_struct);
}
_PLBlock_deallocator((struct Block_layout *)shared_struct);
}
}


/**
* @}
*/



/**
* @internal
* @defgroup functions_internal_utilities Internal Utilities
* @ingroup functions
* @{
*/

/**
* Atomic reference count increment.
*/
static int latching_incr_int(int *where) {
while (1) {
int old_value = *(volatile int *)where;
if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
return BLOCK_REFCOUNT_MASK;
}
if (OSAtomicCompareAndSwapInt(old_value, old_value+1, (volatile int *)where)) {
return old_value+1;
}
}
}


/**
* Atomic reference count decrement.
*/
static int latching_decr_int(int *where) {
while (1) {
int old_value = *(volatile int *)where;
if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
return BLOCK_REFCOUNT_MASK;
}
if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
return 0;
}
if (OSAtomicCompareAndSwapInt(old_value, old_value-1, (volatile int *)where)) {
return old_value-1;
}
}
}

/**
* @}
*/


/**
* @defgroup private_api_abi_functions Block ABI Functions
*
* Functions required by the Blocks Implementation ABI.
*
* A block-compatible compiler will issue calls to these ABI-required functions, and
* they are not exposed to API clients.
*
* http://clang.llvm.org/docs/BlockImplementation.txt
*
* @ingroup private_api
* @{
*/

/**
* When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
* to do the assignment.
*/
void _PLBlock_object_assign(void *destAddr, const void *object, const int flags) {
if (PLBlock_AppleRuntimeEnabled)
return _Block_object_assign(destAddr, object, flags);

//printf("_Block_object_assign(*%p, %p, %x)\n", destAddr, object, flags);
if ((flags & BLOCK_BYREF_CALLER) == BLOCK_BYREF_CALLER) {
if ((flags & BLOCK_FIELD_IS_WEAK) == BLOCK_FIELD_IS_WEAK) {
_PLBlock_assign_weak(object, destAddr);
}
else {
// do *not* retain or *copy* __block variables whatever they are
_PLBlock_assign((void *)object, destAddr);
}
}
else if ((flags & BLOCK_FIELD_IS_BYREF) == BLOCK_FIELD_IS_BYREF) {
// copying a __block reference from the stack Block to the heap
// flags will indicate if it holds a __weak reference and needs a special isa
_PLBlock_byref_assign_copy(destAddr, object, flags);
}
// (this test must be before next one)
else if ((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_BLOCK) {
// copying a Block declared variable from the stack Block to the heap
_PLBlock_assign(_PLBlock_copy_internal(object, flags), destAddr);
}
// (this test must be after previous one)
else if ((flags & BLOCK_FIELD_IS_OBJECT) == BLOCK_FIELD_IS_OBJECT) {
//printf("retaining object at %p\n", object);
_PLBlock_retain_object(object);
//printf("done retaining object at %p\n", object);
_PLBlock_assign((void *)object, destAddr);
}
}

/**
* When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
* to help dispose of the contents
* Used initially only for __attribute__((NSObject)) marked pointers.
*/
void _PLBlock_object_dispose(const void *object, const int flags) {
if (PLBlock_AppleRuntimeEnabled)
return _Block_object_dispose(object, flags);

//printf("_Block_object_dispose(%p, %x)\n", object, flags);
if (flags & BLOCK_FIELD_IS_BYREF) {
// get rid of the __block data structure held in a Block
_PLBlock_byref_release(object);
}
else if ((flags & (BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_BLOCK) {
// get rid of a referenced Block held by this Block
// (ignore __block Block variables, compiler doesn't need to call us)
_PLBlock_destroy(object);
}
else if ((flags & (BLOCK_FIELD_IS_WEAK|BLOCK_FIELD_IS_BLOCK|BLOCK_BYREF_CALLER)) == BLOCK_FIELD_IS_OBJECT) {
// get rid of a referenced object held by this Block
// (ignore __block object variables, compiler doesn't need to call us)
_PLBlock_release_object(object);
}
}

/**
* @internal
*
* Manages runtime initialization.
*/
@implementation PLBlockRuntime

/** Perform runtime initialization. We do so in +load to be sure our frameworks are active, but before
* anyone has attempted to run any block code */
+ (void) load {
/* Determine GC state */
#ifdef __OBJC_GC__
if (objc_collectingEnabled()) {
_PLBlock_enable_GC();
}
#endif

/* On Mac OS X, if the block symbols are available, they are known to work. */
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
if (_Block_copy != NULL)
PLBlock_AppleRuntimeEnabled = true;
#endif

/* On iPhone OS, block symbols were available as early as iPhone OS 3.2, but are broken
* and considered private. We have to check the OS version to verify that we're running on
* 4.0 or later before enabling fallback to Apple's runtime. */
#if TARGET_OS_IPHONE
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *requiredIOS = @"4.0";
NSString *currentIOS = [[UIDevice currentDevice] systemVersion];
if (CFStringCompare((CFStringRef) currentIOS, (CFStringRef) requiredIOS, kCFCompareNumerically) != kCFCompareLessThan)
PLBlock_AppleRuntimeEnabled = true;

[pool release];
#endif
}

@end

/**
* @}
*/

Change log

r165 by landon.j.fuller on Jun 28, 2010   Diff
Move the runtimes to a top-level
subproject.
Go to: 
Project members, sign in to write a code review

Older revisions

r160 by landon.j.fuller on Jun 10, 2010   Diff
Add support for delegating to Apple's
block runtime, if it is available and
supported.
r138 by landon.j.fuller on Mar 10, 2010   Diff
Use +load to trigger early
initialization of the stack and global
block classes.
r129 by landon.j.fuller on Feb 14, 2010   Diff
Restore code that ensures that the
block classes are realized by the ObjC
runtime prior to direct referencing
from any block isa pointers.
All revisions of this file

File info

Size: 22911 bytes, 638 lines
Powered by Google Project Hosting