-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlocksRuntime.c
More file actions
296 lines (244 loc) · 7.25 KB
/
BlocksRuntime.c
File metadata and controls
296 lines (244 loc) · 7.25 KB
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
/*
* Copyright (c) 2025-2026 Paul Olteanu
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdatomic.h>
#include "Block.h"
#include "Block_private.h"
/*
* Objective-C Class variables.
*/
void *_NSConcreteStackBlock[32] = { 0 };
void *_NSConcreteMallocBlock[32] = { 0 };
void *_NSConcreteGlobalBlock[32] = { 0 };
void *_NSConcreteAutoBlock[32] = { 0 };
void *_NSConcreteFinalizingBlock[32] = { 0 };
/*
* Function pointers for Objective-C object management routines.
*/
[[clang::always_inline]] static void empty(void const *x) { (void)x; }
static void (*_Block_objc_retain)(void const *) = empty;
static void (*_Block_objc_release)(void const *) = empty;
static void (*_Block_objc_delete_weak_refs)(void const *) = empty;
/*
* Reference counting routines for Blocks and byrefs.
*/
[[clang::always_inline]]
inline static void
retainFlags(int _Atomic *flags)
{
int f = atomic_load(flags);
while (true) {
if ((f & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK ||
atomic_compare_exchange_strong(flags, &f, f + 2))
return;
}
}
[[clang::always_inline]]
inline static bool
releaseFlags(int _Atomic *flags)
{
int f = atomic_load(flags);
while (true) {
if ((f & BLOCK_REFCOUNT_MASK) == BLOCK_REFCOUNT_MASK ||
(f & BLOCK_REFCOUNT_MASK) == 0)
return false;
/* On the final reference, set deallocating bit. */
if ((unsigned short)f == 2) {
if (atomic_compare_exchange_strong(flags, &f, f - 1))
return true;
} else {
if (atomic_compare_exchange_strong(flags, &f, f - 2))
return false;
}
}
}
/*
* Public routines for managing Blocks.
*/
void *
_Block_copy(void *b)
{
BlockLiteral *aBlock, *blockCopy;
int f;
aBlock = (BlockLiteral *)b;
f = atomic_load(&aBlock->flags);
/* Global Blocks are immutable, just return it. */
if (f & BLOCK_IS_GLOBAL && aBlock->isa == &_NSConcreteGlobalBlock)
return aBlock;
/* Increment reference count for heap-allocated Blocks. */
if (f & BLOCK_NEEDS_FREE && aBlock->isa == &_NSConcreteMallocBlock) {
retainFlags(&aBlock->flags);
return aBlock;
}
/* If execution reached here, we must be a stack Block. */
if (aBlock->isa != &_NSConcreteStackBlock)
abort();
/* Make a copy of the stack Block and call its copy helper. */
blockCopy = malloc(aBlock->descriptor->blockSize);
memcpy(blockCopy, aBlock, aBlock->descriptor->blockSize);
blockCopy->isa = _NSConcreteMallocBlock;
atomic_init(&blockCopy->flags, f | BLOCK_NEEDS_FREE | 2);
if (f & BLOCK_HAS_COPY_DISPOSE)
aBlock->copyDisposeDescriptor->BlockCopyHelper(blockCopy,
aBlock);
return blockCopy;
}
void
_Block_release(void *b)
{
BlockLiteral *aBlock;
int f;
aBlock = (BlockLiteral *)b;
f = atomic_load(&aBlock->flags);
/*
* Global Blocks and stack Blocks do not participate in reference
* counting. A valid heap Block must have BLOCK_NEEDS_FREE set; its
* final reference is signaled when releaseFlags() returns true.
*/
if (f & BLOCK_IS_GLOBAL || (f & BLOCK_NEEDS_FREE) == 0 ||
aBlock->isa == &_NSConcreteStackBlock ||
releaseFlags(&aBlock->flags) == false)
return;
/*
* If execution reached here, the heap Block is deallocating. Call
* our dispose helper.
*/
if (f & BLOCK_HAS_COPY_DISPOSE)
aBlock->copyDisposeDescriptor->BlockDisposeHelper(aBlock);
/* Tell the Objective-C runtime to remove all weak references to us. */
_Block_objc_delete_weak_refs(aBlock);
free((void *)aBlock);
}
/*
* Private routines for managing byrefs.
*/
[[clang::always_inline]]
inline static void
_Block_byref_assign(void **d, void *s)
{
BlockCopyDisposeByref *src, *fwd, *copy;
int f;
/* Get the real Byref from the forwarding pointer. */
src = (BlockCopyDisposeByref *)s;
fwd = src->forwarding;
copy = fwd;
f = atomic_load(&fwd->flags);
/*
* Heap Byrefs are reference counted. If given a stack Byref, make a
* copy. Give it two references, one for itself and another for the
* stack Byref pointing to it. The heap Byref is canonical.
*/
if (f & BLOCK_BYREF_NEEDS_FREE)
retainFlags(&fwd->flags);
else if ((f & BLOCK_REFCOUNT_MASK) == 0 && src == fwd) {
copy = malloc(src->size);
memcpy(copy, src, src->size);
copy->forwarding = copy;
atomic_init(©->flags, f | BLOCK_BYREF_NEEDS_FREE | 4);
if (f & BLOCK_HAS_COPY_DISPOSE)
src->ByrefCopyHelper(copy, src);
src->forwarding = copy;
} else
abort(); /* Unreachable. */
*d = (void *)copy;
}
[[clang::always_inline]]
inline static void
_Block_byref_dispose(void const *s)
{
BlockCopyDisposeByref *fwd;
int f;
/* Get the real Byref from the forwarding pointer. */
fwd = ((BlockCopyDisposeByref const *)s)->forwarding;
f = atomic_load(&fwd->flags);
if ((f & BLOCK_BYREF_NEEDS_FREE) == 0 ||
(f & BLOCK_REFCOUNT_MASK) == 0 ||
releaseFlags(&fwd->flags) == false)
return;
if (f & BLOCK_BYREF_HAS_COPY_DISPOSE)
fwd->ByrefDisposeHelper(fwd);
free((void *)fwd);
}
/*
* Compiler-called routines for managing captured Blocks, byrefs, and objects.
*/
void
_Block_object_assign(void **dest, void *src,
BlockCaptureFlags const captureFlags)
{
switch (captureFlags) {
/* Retain the object; assign in dest. */
case BLOCK_FIELD_IS_OBJECT:
_Block_objc_retain(src);
*dest = src;
return;
/* Copy stack Block or retain heap Block; assign in dest. */
case BLOCK_FIELD_IS_BLOCK:
*dest = _Block_copy(src);
return;
/* Copy stack Byref or retain heap Byref; assign in dest. */
case BLOCK_FIELD_IS_BYREF:
case BLOCK_FIELD_IS_WEAK_BYREF:
_Block_byref_assign(dest, src);
return;
/*
* Blocks and objects in a Byref are managed by the compiler; we only
* need to copy pointers.
*/
case BLOCK_BYREF_CALLER_FIELD_IS_BLOCK:
case BLOCK_BYREF_CALLER_FIELD_IS_OBJECT:
case BLOCK_BYREF_CALLER_FIELD_IS_WEAK_BLOCK:
case BLOCK_BYREF_CALLER_FIELD_IS_WEAK_OBJECT:
*dest = src;
return;
}
/* Unreachable. */
abort();
}
void
_Block_object_dispose(void *src, BlockCaptureFlags const captureFlags)
{
switch (captureFlags) {
case BLOCK_FIELD_IS_OBJECT:
_Block_objc_release(src);
return;
case BLOCK_FIELD_IS_BLOCK:
_Block_release(src);
return;
case BLOCK_FIELD_IS_BYREF:
case BLOCK_FIELD_IS_BYREF | BLOCK_FIELD_IS_WEAK:
_Block_byref_dispose(src);
return;
/* Blocks and objects in a Byref are managed by the compiler. */
case BLOCK_BYREF_CALLER_FIELD_IS_BLOCK:
case BLOCK_BYREF_CALLER_FIELD_IS_OBJECT:
case BLOCK_BYREF_CALLER_FIELD_IS_WEAK_BLOCK:
case BLOCK_BYREF_CALLER_FIELD_IS_WEAK_OBJECT:
return;
}
/* Unreachable. */
abort();
}
/*
* Callback hooks from libobjc2 for manipulating objects.
*/
void
_Block_use_RR2(Block_callbacks_RR const *callbacks)
{
_Block_objc_retain = callbacks->retain;
_Block_objc_release = callbacks->release;
_Block_objc_delete_weak_refs = callbacks->destructInstance;
}