-
Notifications
You must be signed in to change notification settings - Fork 118
/
blocks_runtime.m
324 lines (304 loc) · 9.62 KB
/
blocks_runtime.m
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
/*
* Copyright (c) 2009 Remy Demarest
* Portions Copyright (c) 2009 David Chisnall
*
* 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 "objc/blocks_runtime.h"
#include "objc/blocks_private.h"
#import "objc/runtime.h"
#import "objc/objc-arc.h"
#include "blocks_runtime.h"
#include "gc_ops.h"
#include "visibility.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
static void *_HeapBlockByRef = (void*)1;
OBJC_PUBLIC bool _Block_has_signature(void *b)
{
const struct Block_layout *block = (struct Block_layout*)b;
return ((NULL != block) && (block->flags & BLOCK_HAS_SIGNATURE));
}
/**
* Returns the Objective-C type encoding for the block.
*/
OBJC_PUBLIC const char * _Block_signature(void *b)
{
const struct Block_layout *block = (struct Block_layout*)b;
if ((NULL == block) || !(block->flags & BLOCK_HAS_SIGNATURE))
{
return NULL;
}
if (!(block->flags & BLOCK_HAS_COPY_DISPOSE))
{
return ((struct Block_descriptor_basic*)block->descriptor)->encoding;
}
return block->descriptor->encoding;
}
static int increment24(int *ref)
{
int old = *ref;
int val = old & BLOCK_REFCOUNT_MASK;
if (val == BLOCK_REFCOUNT_MASK)
{
return val;
}
assert(val < BLOCK_REFCOUNT_MASK);
if (!__sync_bool_compare_and_swap(ref, old, old+1))
{
return increment24(ref);
}
return val + 1;
}
static int decrement24(int *ref)
{
int old = *ref;
int val = old & BLOCK_REFCOUNT_MASK;
if (val == BLOCK_REFCOUNT_MASK)
{
return val;
}
assert(val > 0);
if (!__sync_bool_compare_and_swap(ref, old, old-1))
{
return decrement24(ref);
}
return val - 1;
}
// This is a really ugly hack that works around a buggy register allocator in
// GCC. Compiling nontrivial code using __sync_bool_compare_and_swap() with
// GCC (4.2.1, at least), causes the register allocator to run out of registers
// and fall over and die. We work around this by wrapping this CAS in a
// function, which means the register allocator can trivially handle it. Do
// not remove the noinline attribute - without it, gcc will inline it early on
// and then crash later.
#ifndef __clang__
__attribute__((noinline))
static int cas(void *ptr, void *old, void *new)
{
return __sync_bool_compare_and_swap((void**)ptr, old, new);
}
#define __sync_bool_compare_and_swap cas
#endif
/* Certain field types require runtime assistance when being copied to the
* heap. The following function is used to copy fields of types: blocks,
* pointers to byref structures, and objects (including
* __attribute__((NSObject)) pointers. BLOCK_FIELD_IS_WEAK is orthogonal to
* the other choices which are mutually exclusive. Only in a Block copy helper
* will one see BLOCK_FIELD_IS_BYREF.
*/
OBJC_PUBLIC void _Block_object_assign(void *destAddr, const void *object, const int flags)
{
//printf("Copying %x to %x with flags %x\n", object, destAddr, flags);
// FIXME: Needs to be implemented
//if(flags & BLOCK_FIELD_IS_WEAK)
{
}
//else
{
if (IS_SET(flags, BLOCK_FIELD_IS_BYREF))
{
struct block_byref_obj *src = (struct block_byref_obj *)object;
struct block_byref_obj **dst = destAddr;
src = src->forwarding;
if ((src->flags & BLOCK_REFCOUNT_MASK) == 0)
{
*dst = gc->malloc(src->size);
memcpy(*dst, src, src->size);
(*dst)->isa = _HeapBlockByRef;
// Refcount must be two; one for the copy and one for the
// on-stack version that will point to it.
(*dst)->flags += 2;
if (IS_SET(src->flags, BLOCK_HAS_COPY_DISPOSE))
{
src->byref_keep(*dst, src);
}
(*dst)->forwarding = *dst;
// Concurrency. If we try copying the same byref structure
// from two threads simultaneously, we could end up with two
// versions on the heap that are unaware of each other. That
// would be bad. So we first set up the copy, then try to do
// an atomic compare-and-exchange to point the old version at
// it. If the forwarding pointer in src has changed, then we
// recover - clean up and then return the structure that the
// other thread created.
if (!__sync_bool_compare_and_swap(&src->forwarding, src, *dst))
{
if((size_t)src->size >= sizeof(struct block_byref_obj))
{
src->byref_dispose(*dst);
}
gc->free(*dst);
*dst = src->forwarding;
}
}
else
{
*dst = (struct block_byref_obj*)src;
increment24(&(*dst)->flags);
}
}
else if (IS_SET(flags, BLOCK_FIELD_IS_BLOCK))
{
struct Block_layout *src = (struct Block_layout*)object;
struct Block_layout **dst = destAddr;
*dst = Block_copy(src);
}
else if (IS_SET(flags, BLOCK_FIELD_IS_OBJECT) &&
!IS_SET(flags, BLOCK_BYREF_CALLER))
{
id src = (id)object;
void **dst = destAddr;
*dst = src;
*dst = objc_retain(src);
}
}
}
/* Similarly a compiler generated dispose helper needs to call back for each
* field of the byref data structure. (Currently the implementation only packs
* one field into the byref structure but in principle there could be more).
* The same flags used in the copy helper should be used for each call
* generated to this function:
*/
OBJC_PUBLIC void _Block_object_dispose(const void *object, const int flags)
{
// FIXME: Needs to be implemented
//if(flags & BLOCK_FIELD_IS_WEAK)
{
}
//else
{
if (IS_SET(flags, BLOCK_FIELD_IS_BYREF))
{
struct block_byref_obj *src =
(struct block_byref_obj*)object;
src = src->forwarding;
if (src->isa == _HeapBlockByRef)
{
int refcount = (src->flags & BLOCK_REFCOUNT_MASK) == 0 ? 0 : decrement24(&src->flags);
if (refcount == 0)
{
if(IS_SET(src->flags, BLOCK_HAS_COPY_DISPOSE) && (0 != src->byref_dispose))
{
src->byref_dispose(src);
}
gc->free(src);
}
}
}
else if (IS_SET(flags, BLOCK_FIELD_IS_BLOCK))
{
struct Block_layout *src = (struct Block_layout*)object;
Block_release(src);
}
else if (IS_SET(flags, BLOCK_FIELD_IS_OBJECT) &&
!IS_SET(flags, BLOCK_BYREF_CALLER))
{
id src = (id)object;
objc_release(src);
}
}
}
// Copy a block to the heap if it's still on the stack or increments its retain count.
OBJC_PUBLIC void *_Block_copy(const void *src)
{
if (NULL == src) { return NULL; }
struct Block_layout *self = (struct Block_layout*)src;
struct Block_layout *ret = self;
extern void _NSConcreteStackBlock;
extern void _NSConcreteMallocBlock;
// If the block is Global, there's no need to copy it on the heap.
if(self->isa == &_NSConcreteStackBlock)
{
ret = gc->malloc(self->descriptor->size);
memcpy(ret, self, self->descriptor->size);
ret->isa = &_NSConcreteMallocBlock;
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
{
self->descriptor->copy_helper(ret, self);
}
// We don't need any atomic operations here, because on-stack blocks
// can not be aliased across threads (unless you've done something
// badly wrong).
ret->reserved = 1;
}
else if (self->isa == &_NSConcreteMallocBlock)
{
// We need an atomic increment for malloc'd blocks, because they may be
// shared.
__sync_fetch_and_add(&ret->reserved, 1);
}
return ret;
}
// Release a block and frees the memory when the retain count hits zero.
OBJC_PUBLIC void _Block_release(const void *src)
{
if (NULL == src) { return; }
struct Block_layout *self = (struct Block_layout*)src;
extern void _NSConcreteStackBlock;
extern void _NSConcreteMallocBlock;
if (&_NSConcreteStackBlock == self->isa)
{
fprintf(stderr, "Block_release called upon a stack Block: %p, ignored\n", self);
}
else if (&_NSConcreteMallocBlock == self->isa)
{
if (__sync_sub_and_fetch(&self->reserved, 1) == 0)
{
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
self->descriptor->dispose_helper(self);
objc_delete_weak_refs((id)self);
gc->free(self);
}
}
}
OBJC_PUBLIC bool _Block_isDeallocating(const void* arg)
{
struct Block_layout *block = (struct Block_layout*)arg;
int *refCountPtr = &((struct Block_layout*)arg)->reserved;
int refCount = __sync_fetch_and_add(refCountPtr, 0);
return refCount == 0;
}
OBJC_PUBLIC bool _Block_tryRetain(const void* arg)
{
/* This is used by the weak reference management in ARC. The implementation
* follows the reasoning of `retain_fast()` in arc.mm: We want to abandon the
* retain operation if another thread has started deallocating the object between
* loading the weak pointer and executing the retain operation.
*/
struct Block_layout *block = (struct Block_layout*)arg;
int *refCountPtr = &block->reserved;
int refCountVal = __sync_fetch_and_add(refCountPtr, 0);
int newVal = refCountVal;
do {
refCountVal = newVal;
if (refCountVal <= 0)
{
return false;
}
newVal = __sync_val_compare_and_swap(refCountPtr, refCountVal, newVal + 1);
} while (newVal != refCountVal);
return true;
}