-
Notifications
You must be signed in to change notification settings - Fork 193
/
runtime.cpp
704 lines (606 loc) · 20.2 KB
/
runtime.cpp
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
* runtime.c
* libclosure
*
* Copyright (c) 2008-2010 Apple Inc. All rights reserved.
*
* @APPLE_LLVM_LICENSE_HEADER@
*/
#ifndef KERNEL
#include "Block_private.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <os/assumes.h>
#include <TargetConditionals.h>
#else /* !KERNEL */
#define TARGET_OS_WIN32 0
#include <libkern/Block_private.h>
__BEGIN_DECLS
#include <kern/kalloc.h>
__END_DECLS
/* void * is a bit of a lie, but that will have to do */
KALLOC_TYPE_VAR_DEFINE(KT_BLOCK_LAYOUT, struct Block_layout, void *, KT_DEFAULT);
KALLOC_TYPE_VAR_DEFINE(KT_BLOCK_BYREF, struct Block_byref, void *, KT_DEFAULT);
static inline struct Block_layout *
block_layout_alloc(size_t size)
{
return (struct Block_layout *)kalloc_type_var_impl(KT_BLOCK_LAYOUT,
size, Z_WAITOK_ZERO_NOFAIL, NULL);
}
static inline void
block_layout_free(Block_layout *ptr, size_t size)
{
kfree_type_var_impl(KT_BLOCK_LAYOUT, ptr, size);
}
static inline struct Block_byref *
block_byref_alloc(size_t size)
{
return (struct Block_byref *)kalloc_type_var_impl(KT_BLOCK_BYREF,
size, Z_WAITOK_ZERO_NOFAIL, NULL);
}
static inline void
block_byref_free(Block_byref *ptr, size_t size)
{
kfree_type_var_impl(KT_BLOCK_BYREF, ptr, size);
}
#endif /* KERNEL */
#include <machine/atomic.h>
#include <string.h>
#include <stdint.h>
#ifndef os_assumes
#define os_assumes(_x) (_x)
#endif
#ifndef os_assert
#define os_assert(_x) assert(_x)
#endif
#if TARGET_OS_WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
static __inline bool
OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
{
// fixme barrier is overkill -- see objc-os.h
long original = InterlockedCompareExchange(dst, newl, oldl);
return original == oldl;
}
static __inline bool
OSAtomicCompareAndSwapInt(int oldi, int newi, int volatile *dst)
{
// fixme barrier is overkill -- see objc-os.h
int original = InterlockedCompareExchange(dst, newi, oldi);
return original == oldi;
}
#else
#define OSAtomicCompareAndSwapLong(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
#define OSAtomicCompareAndSwapInt(_Old, _New, _Ptr) os_atomic_cmpxchg(_Ptr, _Old, _New, relaxed)
#endif
/*******************************************************************************
* Internal Utilities
********************************************************************************/
static int32_t
latching_incr_int(volatile int32_t *where)
{
while (1) {
int32_t old_value = *where;
if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
return BLOCK_REFCOUNT_MASK;
}
if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
return old_value + 2;
}
}
}
static bool
latching_incr_int_not_deallocating(volatile int32_t *where)
{
while (1) {
int32_t old_value = *where;
if (old_value & BLOCK_DEALLOCATING) {
// if deallocating we can't do this
return false;
}
if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
// if latched, we're leaking this block, and we succeed
return true;
}
if (OSAtomicCompareAndSwapInt(old_value, old_value + 2, where)) {
// otherwise, we must store a new retained value without the deallocating bit set
return true;
}
}
}
// return should_deallocate?
static bool
latching_decr_int_should_deallocate(volatile int32_t *where)
{
while (1) {
int32_t old_value = *where;
if ((old_value & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK) {
return false; // latched high
}
if ((old_value & BLOCK_REFCOUNT_MASK) == 0) {
return false; // underflow, latch low
}
int32_t new_value = old_value - 2;
bool result = false;
if ((old_value & (BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING)) == 2) {
new_value = old_value - 1;
result = true;
}
if (OSAtomicCompareAndSwapInt(old_value, new_value, where)) {
return result;
}
}
}
/**************************************************************************
* Framework callback functions and their default implementations.
***************************************************************************/
#if !TARGET_OS_WIN32
#pragma mark Framework Callback Routines
#endif
#if KERNEL
static inline void
_Block_retain_object(const void *ptr __unused)
{
}
static inline void
_Block_release_object(const void *ptr __unused)
{
}
static inline void
_Block_destructInstance(const void *aBlock __unused)
{
}
#else
static void
_Block_retain_object_default(const void *ptr __unused)
{
}
static void
_Block_release_object_default(const void *ptr __unused)
{
}
static void
_Block_destructInstance_default(const void *aBlock __unused)
{
}
static void (*_Block_retain_object)(const void *ptr) = _Block_retain_object_default;
static void (*_Block_release_object)(const void *ptr) = _Block_release_object_default;
static void (*_Block_destructInstance) (const void *aBlock) = _Block_destructInstance_default;
/**************************************************************************
* Callback registration from ObjC runtime and CoreFoundation
***************************************************************************/
void
_Block_use_RR2(const Block_callbacks_RR *callbacks)
{
_Block_retain_object = callbacks->retain;
_Block_release_object = callbacks->release;
_Block_destructInstance = callbacks->destructInstance;
}
#endif // !KERNEL
/****************************************************************************
* Accessors for block descriptor fields
*****************************************************************************/
#if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
template <class T>
static T *
unwrap_relative_pointer(int32_t &offset)
{
if (offset == 0) {
return nullptr;
}
uintptr_t base = (uintptr_t)&offset;
uintptr_t extendedOffset = (uintptr_t)(intptr_t)offset;
uintptr_t pointer = base + extendedOffset;
return (T *)pointer;
}
#endif
#if 0
static struct Block_descriptor_2 *
_Block_descriptor_2(struct Block_layout *aBlock)
{
uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
desc += sizeof(struct Block_descriptor_1);
return __IGNORE_WCASTALIGN((struct Block_descriptor_2 *)desc);
}
#endif
static struct Block_descriptor_3 *
_Block_descriptor_3(struct Block_layout *aBlock)
{
uint8_t *desc = (uint8_t *)_Block_get_descriptor(aBlock);
desc += sizeof(struct Block_descriptor_1);
if (aBlock->flags & BLOCK_HAS_COPY_DISPOSE) {
desc += sizeof(struct Block_descriptor_2);
}
return __IGNORE_WCASTALIGN((struct Block_descriptor_3 *)desc);
}
static void
_Block_call_copy_helper(void *result, struct Block_layout *aBlock)
{
if (auto *pFn = _Block_get_copy_function(aBlock)) {
pFn(result, aBlock);
}
}
static void
_Block_call_dispose_helper(struct Block_layout *aBlock)
{
if (auto *pFn = _Block_get_dispose_function(aBlock)) {
pFn(aBlock);
}
}
/*******************************************************************************
* Internal Support routines for copying
********************************************************************************/
#if !TARGET_OS_WIN32
#pragma mark Copy/Release support
#endif
// Copy, or bump refcount, of a block. If really copying, call the copy helper if present.
void *
_Block_copy(const void *arg)
{
struct Block_layout *aBlock;
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_GLOBAL) {
return aBlock;
} else {
// Its a stack block. Make a copy.
size_t size = Block_size(aBlock);
struct Block_layout *result = block_layout_alloc(size);
memmove(result, aBlock, size); // bitcopy first
#if __has_feature(ptrauth_calls)
// Resign the invoke pointer as it uses address authentication.
result->invoke = aBlock->invoke;
#if __has_feature(ptrauth_signed_block_descriptors)
uintptr_t oldDesc =
ptrauth_blend_discriminator(
&aBlock->descriptor, _Block_descriptor_ptrauth_discriminator);
uintptr_t newDesc =
ptrauth_blend_discriminator(
&result->descriptor, _Block_descriptor_ptrauth_discriminator);
result->descriptor =
ptrauth_auth_and_resign(aBlock->descriptor, ptrauth_key_asda, oldDesc,
ptrauth_key_asda, newDesc);
#endif
#endif
// reset refcount
result->flags &= ~(BLOCK_REFCOUNT_MASK | BLOCK_DEALLOCATING); // XXX not needed
result->flags |= BLOCK_NEEDS_FREE | 2; // logical refcount 1
_Block_call_copy_helper(result, aBlock);
// Set isa last so memory analysis tools see a fully-initialized object.
result->isa = _NSConcreteMallocBlock;
return result;
}
}
// Runtime entry points for maintaining the sharing knowledge of byref data blocks.
// 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 and return it.
// Otherwise we need to copy it and update the stack forwarding pointer
static struct Block_byref *
_Block_byref_copy(const void *arg)
{
struct Block_byref *src = (struct Block_byref *)arg;
if ((src->forwarding->flags & BLOCK_REFCOUNT_MASK) == 0) {
// src points to stack
struct Block_byref *copy = block_byref_alloc(src->size);
copy->isa = NULL;
// byref value 4 is logical refcount of 2: one for caller, one for stack
copy->flags = src->flags | BLOCK_BYREF_NEEDS_FREE | 4;
copy->forwarding = copy; // patch heap copy to point to itself
src->forwarding = copy; // patch stack to point to heap copy
copy->size = src->size;
if (src->flags & BLOCK_BYREF_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
struct Block_byref_2 *src2 = (struct Block_byref_2 *)(src + 1);
struct Block_byref_2 *copy2 = (struct Block_byref_2 *)(copy + 1);
copy2->byref_keep = src2->byref_keep;
copy2->byref_destroy = src2->byref_destroy;
if (src->flags & BLOCK_BYREF_LAYOUT_EXTENDED) {
struct Block_byref_3 *src3 = (struct Block_byref_3 *)(src2 + 1);
struct Block_byref_3 *copy3 = (struct Block_byref_3*)(copy2 + 1);
copy3->layout = src3->layout;
}
(*src2->byref_keep)(copy, src);
} else {
// Bitwise copy.
// This copy includes Block_byref_3, if any.
memmove(copy + 1, src + 1, src->size - sizeof(*src));
}
}
// already copied to heap
else if ((src->forwarding->flags & BLOCK_BYREF_NEEDS_FREE) == BLOCK_BYREF_NEEDS_FREE) {
latching_incr_int(&src->forwarding->flags);
}
return src->forwarding;
}
static void
_Block_byref_release(const void *arg)
{
struct Block_byref *byref = (struct Block_byref *)arg;
// dereference the forwarding pointer since the compiler isn't doing this anymore (ever?)
byref = byref->forwarding;
if (byref->flags & BLOCK_BYREF_NEEDS_FREE) {
__assert_only int32_t refcount = byref->flags & BLOCK_REFCOUNT_MASK;
os_assert(refcount);
if (latching_decr_int_should_deallocate(&byref->flags)) {
if (byref->flags & BLOCK_BYREF_HAS_COPY_DISPOSE) {
struct Block_byref_2 *byref2 = (struct Block_byref_2 *)(byref + 1);
(*byref2->byref_destroy)(byref);
}
block_byref_free(byref, byref->size);
}
}
}
/************************************************************
*
* API supporting SPI
* _Block_copy, _Block_release, and (old) _Block_destroy
*
***********************************************************/
#if !TARGET_OS_WIN32
#pragma mark SPI/API
#endif
// API entry point to release a copied Block
void
_Block_release(const void *arg)
{
struct Block_layout *aBlock = (struct Block_layout *)arg;
if (!aBlock) {
return;
}
if (aBlock->flags & BLOCK_IS_GLOBAL) {
return;
}
if (!(aBlock->flags & BLOCK_NEEDS_FREE)) {
return;
}
if (latching_decr_int_should_deallocate(&aBlock->flags)) {
_Block_call_dispose_helper(aBlock);
_Block_destructInstance(aBlock);
block_layout_free(aBlock, Block_size(aBlock));
}
}
bool
_Block_tryRetain(const void *arg)
{
struct Block_layout *aBlock = (struct Block_layout *)arg;
return latching_incr_int_not_deallocating(&aBlock->flags);
}
bool
_Block_isDeallocating(const void *arg)
{
struct Block_layout *aBlock = (struct Block_layout *)arg;
return (aBlock->flags & BLOCK_DEALLOCATING) != 0;
}
/************************************************************
*
* SPI used by other layers
*
***********************************************************/
size_t
Block_size(void *aBlock)
{
auto *layout = (Block_layout *)aBlock;
void *desc = _Block_get_descriptor(layout);
#if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
return ((Block_descriptor_small *)desc)->size;
}
#endif
return ((Block_descriptor_1 *)desc)->size;
}
bool
_Block_use_stret(void *aBlock)
{
struct Block_layout *layout = (struct Block_layout *)aBlock;
int requiredFlags = BLOCK_HAS_SIGNATURE | BLOCK_USE_STRET;
return (layout->flags & requiredFlags) == requiredFlags;
}
// Checks for a valid signature, not merely the BLOCK_HAS_SIGNATURE bit.
bool
_Block_has_signature(void *aBlock)
{
return _Block_signature(aBlock) ? true : false;
}
const char *
_Block_signature(void *aBlock)
{
struct Block_layout *layout = (struct Block_layout *)aBlock;
if (!(layout->flags & BLOCK_HAS_SIGNATURE)) {
return nullptr;
}
#if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
return unwrap_relative_pointer<const char>(bds->signature);
}
#endif
struct Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
return desc3->signature;
}
const char *
_Block_layout(void *aBlock)
{
// Don't return extended layout to callers expecting old GC layout
Block_layout *layout = (Block_layout *)aBlock;
if ((layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
!(layout->flags & BLOCK_HAS_SIGNATURE)) {
return nullptr;
}
#if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
return unwrap_relative_pointer<const char>(bds->layout);
}
#endif
Block_descriptor_3 *desc = _Block_descriptor_3(layout);
return desc->layout;
}
const char *
_Block_extended_layout(void *aBlock)
{
// Don't return old GC layout to callers expecting extended layout
Block_layout *layout = (Block_layout *)aBlock;
if (!(layout->flags & BLOCK_HAS_EXTENDED_LAYOUT) ||
!(layout->flags & BLOCK_HAS_SIGNATURE)) {
return nullptr;
}
const char *extLayout;
#if BLOCK_SMALL_DESCRIPTOR_SUPPORTED
if (layout->flags & BLOCK_SMALL_DESCRIPTOR) {
auto *bds = (Block_descriptor_small *)_Block_get_descriptor(layout);
if (layout->flags & BLOCK_INLINE_LAYOUT_STRING) {
extLayout = (const char *)(uintptr_t)bds->layout;
} else {
extLayout = unwrap_relative_pointer<const char>(bds->layout);
}
} else
#endif
{
Block_descriptor_3 *desc3 = _Block_descriptor_3(layout);
extLayout = desc3->layout;
}
// Return empty string (all non-object bytes) instead of NULL
// so callers can distinguish "empty layout" from "no layout".
if (!extLayout) {
extLayout = "";
}
return extLayout;
}
#if !TARGET_OS_WIN32
#pragma mark Compiler SPI entry points
#endif
/*******************************************************
*
* Entry points used by the compiler - the real API!
*
*
* A Block can reference four different kinds of things that require help when the Block is copied to the heap.
* 1) C++ stack based objects
* 2) References to Objective-C objects
* 3) Other Blocks
* 4) __block variables
*
* 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.
*
* The flags parameter of _Block_object_assign and _Block_object_dispose is set to
* BLOCK_FIELD_IS_OBJECT (3), for the case of an Objective-C Object,
* BLOCK_FIELD_IS_BLOCK (7), for the case of another Block, and
* BLOCK_FIELD_IS_BYREF (8), for the case of a __block variable.
* If the __block variable is marked weak the compiler also or's in BLOCK_FIELD_IS_WEAK (16)
*
* So the Block copy/dispose helpers should only ever generate the four flag values of 3, 7, 8, and 24.
*
* 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.
*
* 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:
* __block id 128+3 (0x83)
* __block (^Block) 128+7 (0x87)
* __weak __block id 128+3+16 (0x93)
* __weak __block (^Block) 128+7+16 (0x97)
*
*
********************************************************/
//
// When Blocks or Block_byrefs hold objects then their copy routine helpers use this entry point
// to do the assignment.
//
void
_Block_object_assign(void *destArg, const void *object, const int flags)
{
const void **dest = (const void **)destArg;
switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
case BLOCK_FIELD_IS_OBJECT:
/*******
* id object = ...;
* [^{ object; } copy];
********/
_Block_retain_object(object);
*dest = object;
break;
case BLOCK_FIELD_IS_BLOCK:
/*******
* void (^object)(void) = ...;
* [^{ object; } copy];
********/
*dest = _Block_copy(object);
break;
case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
case BLOCK_FIELD_IS_BYREF:
/*******
* // copy the onstack __block container to the heap
* // Note this __weak is old GC-weak/MRC-unretained.
* // ARC-style __weak is handled by the copy helper directly.
* __block ... x;
* __weak __block ... x;
* [^{ x; } copy];
********/
*dest = _Block_byref_copy(object);
break;
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
/*******
* // copy the actual field held in the __block container
* // Note this is MRC unretained __block only.
* // ARC retained __block is handled by the copy helper directly.
* __block id object;
* __block void (^object)(void);
* [^{ object; } copy];
********/
*dest = object;
break;
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
/*******
* // copy the actual field held in the __block container
* // Note this __weak is old GC-weak/MRC-unretained.
* // ARC-style __weak is handled by the copy helper directly.
* __weak __block id object;
* __weak __block void (^object)(void);
* [^{ object; } copy];
********/
*dest = object;
break;
default:
break;
}
}
// When Blocks or Block_byrefs hold objects their destroy helper routines call this entry point
// to help dispose of the contents
void
_Block_object_dispose(const void *object, const int flags)
{
switch (os_assumes(flags & BLOCK_ALL_COPY_DISPOSE_FLAGS)) {
case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
case BLOCK_FIELD_IS_BYREF:
// get rid of the __block data structure held in a Block
_Block_byref_release(object);
break;
case BLOCK_FIELD_IS_BLOCK:
_Block_release(object);
break;
case BLOCK_FIELD_IS_OBJECT:
_Block_release_object(object);
break;
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT:
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK:
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT | BLOCK_FIELD_IS_WEAK:
case BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK | BLOCK_FIELD_IS_WEAK:
break;
default:
break;
}
}
// Workaround for <rdar://26015603> dylib with no __DATA segment fails to rebase
__attribute__((used))
static int let_there_be_data = 42;