-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocks_runtime.m
427 lines (370 loc) · 13 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
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
#include "blocks_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
struct StackBlockClass;
struct GlobalBlockClass;
#if !defined(NSINTEGER_DEFINED) || ! NSINTEGER_DEFINED
typedef unsigned int NSUInteger;
#endif
#ifndef _OBJC_OBJC_H_
typedef struct objc_selector *SEL;
typedef struct objc_object {
void *isa;
} *id;
#endif
#ifdef __OBJC__
/* Makes the compiler happy even without Foundation */
@interface Dummy
- (id)retain;
- (void)release;
@end
#endif
void *__Block_copy(void *);
void __Block_release(void *);
// Descriptor attributes
enum {
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_HAS_DESCRIPTOR = (1 << 29), // interim until complete world build is accomplished
};
// _Block_object_assign() and _Block_object_dispose() flag helpers.
enum {
BLOCK_FIELD_IS_OBJECT = 3, // id, NSObject, __attribute__((NSObject)), block, ...
BLOCK_FIELD_IS_BLOCK = 7, // a block variable
BLOCK_FIELD_IS_BYREF = 8, // the on stack structure holding the __block variable
BLOCK_FIELD_IS_WEAK = 16, // declared __weak
BLOCK_BYREF_CALLER = 128, // called from byref copy/dispose helpers
};
// Helper structure
struct psy_block_literal {
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
int flags;
int reserved;
void (*invoke)(void *, ...);
struct {
unsigned long int reserved; // NULL
unsigned long int size; // sizeof(struct Block_literal_1)
// optional helper functions
void (*copy_helper)(void *dst, void *src);
void (*dispose_helper)(void *src);
} *descriptor;
};
// Helper structure
struct psy_block_byref_obj {
void *isa; // uninitialized
struct psy_block_byref_obj *forwarding;
int flags; //refcount;
int size;
void (*byref_keep)(struct psy_block_byref_obj *dst, struct psy_block_byref_obj *src);
void (*byref_dispose)(struct psy_block_byref_obj *);
};
/* 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.
*/
void _Block_object_assign(void *destAddr, void *object, const int flags)
{
// FIXME: Needs to be implemented
if(flags & BLOCK_FIELD_IS_WEAK)
{
}
else
{
if(flags & BLOCK_FIELD_IS_BYREF)
{
struct psy_block_byref_obj *src = object;
struct psy_block_byref_obj **dst = destAddr;
/* I followed Apple's specs saying byref's "flags" field should represent the refcount
* but it still contains real flag, so this is a little hack...
*/
if((src->flags & ~BLOCK_HAS_COPY_DISPOSE) == 0)
{
*dst = malloc(src->size);
memcpy(*dst, src, src->size);
if(src->size >= sizeof(struct psy_block_byref_obj))
src->byref_keep(*dst, src);
}
else *dst = src;
(*dst)->flags++;
}
else if((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_BLOCK)
{
struct psy_block_literal *src = object;
struct psy_block_literal **dst = destAddr;
*dst = __Block_copy(src);
}
#ifdef __OBJC__
else if((flags & BLOCK_FIELD_IS_BLOCK) == BLOCK_FIELD_IS_OBJECT)
{
id src = object;
id *dst = destAddr;
*dst = [src retain];
}
#endif
}
}
/* 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:
*/
void _Block_object_dispose(void *object, const int flags)
{
// FIXME: Needs to be implemented
if(flags & BLOCK_FIELD_IS_WEAK)
{
}
else
{
if(flags & BLOCK_FIELD_IS_BYREF)
{
struct psy_block_byref_obj *src = object;
src->flags--;
if((src->flags & ~BLOCK_HAS_COPY_DISPOSE) == 0)
{
if(src->size >= sizeof(struct psy_block_byref_obj))
src->byref_dispose(src);
free(src);
}
}
else if((flags & ~BLOCK_BYREF_CALLER) == BLOCK_FIELD_IS_BLOCK)
{
struct psy_block_literal *src = object;
__Block_release(src);
}
#ifdef __OBJC__
else if((flags & ~BLOCK_BYREF_CALLER) == BLOCK_FIELD_IS_OBJECT)
{
id src = object;
[src release];
}
#endif
}
}
// The following code is generated with clang-cc -rewrite-objc and provides Blocks with an isa
// pointer compatible with Apple's ObjC runtime.
#ifndef _REWRITER_typedef_StackBlockClass
#define _REWRITER_typedef_StackBlockClass
typedef struct objc_object StackBlockClass;
#endif
struct StackBlockClass {
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
int flags;
int reserved;
void (*invoke)(void *, ...);
struct {
unsigned long int reserved; // NULL
unsigned long int size; // sizeof(struct Block_literal_1)
// optional helper functions
void (*copy_helper)(void *dst, void *src);
void (*dispose_helper)(void *src);
} *descriptor;
};
// @end
// @implementation StackBlockClass
static id _I_StackBlockClass_retain(struct StackBlockClass * self, SEL _cmd)
{
return __Block_copy(self);
}
static id _I_StackBlockClass_copyWithZone_(struct StackBlockClass * self, SEL _cmd, NSZone *aZone)
{
return _I_StackBlockClass_retain(self, (SEL)"retain");
}
static id _I_StackBlockClass_copy(struct StackBlockClass * self, SEL _cmd)
{
return _I_StackBlockClass_copyWithZone_(self, (SEL)"copyWithZone:", NULL);
}
static void _I_StackBlockClass_dealloc(struct StackBlockClass * self, SEL _cmd)
{
__Block_release(self);
}
static void _I_StackBlockClass_release(struct StackBlockClass * self, SEL _cmd)
{
_I_StackBlockClass_dealloc(self, (SEL)"dealloc");
}
static NSUInteger _I_StackBlockClass_retainCount(struct StackBlockClass * self, SEL _cmd)
{
return self->reserved;
}
/*
static NSString * _I_StackBlockClass_description(struct StackBlockClass * self, SEL _cmd)
{
return [NSString stringWithFormat:@"Stack Block object=%p address=%p", self, self->invoke];
}
*/
// @end
#ifndef _REWRITER_typedef_GlobalBlockClass
#define _REWRITER_typedef_GlobalBlockClass
typedef struct objc_object GlobalBlockClass;
#endif
struct GlobalBlockClass {
void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock
int flags;
int reserved;
void (*invoke)(void *, ...);
struct {
unsigned long int reserved; // NULL
unsigned long int size; // sizeof(struct Block_literal_1)
} *descriptor;
// no imported variables
};
// @end
// @implementation GlobalBlockClass
static id _I_GlobalBlockClass_copyWithZone_(struct StackBlockClass * self, SEL _cmd, NSZone *aZone)
{
return (id)self;
}
static id _I_GlobalBlockClass_copy(struct StackBlockClass * self, SEL _cmd)
{
return (id)self;
}
static id _I_GlobalBlockClass_retain(struct GlobalBlockClass * self, SEL _cmd)
{
return (id)self;
}
static void _I_GlobalBlockClass_release(struct GlobalBlockClass * self, SEL _cmd)
{
}
static void _I_GlobalBlockClass_dealloc(struct GlobalBlockClass * self, SEL _cmd)
{
}
static NSUInteger _I_GlobalBlockClass_retainCount(struct GlobalBlockClass * self, SEL _cmd)
{
return UINT_MAX;
}
/*
static NSString * _I_GlobalBlockClass_description(struct GlobalBlockClass * self, SEL _cmd)
{
return [NSString stringWithFormat:@"Global Block object=%p address=%p", self, self->invoke];
}
*/
// @end
#define __OFFSETOFIVAR__(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
struct psy_objc_method {
SEL _cmd;
char *method_types;
void *_imp;
};
static struct {
struct _objc_method_list *next_method;
int method_count;
struct psy_objc_method method_list[6];
} _OBJC_INSTANCE_METHODS_StackBlockClass __attribute__ ((used, section ("__OBJC, __inst_meth")))= {
0, 6
,{{(SEL)"copyWithZone:", "@12@0:4^{_NSZone=}8", (void *)_I_StackBlockClass_copyWithZone_}
,{(SEL)"copy", "@8@0:4", (void *)_I_StackBlockClass_copy}
,{(SEL)"retain", "@8@0:4", (void *)_I_StackBlockClass_retain}
,{(SEL)"release", "Vv8@0:4", (void *)_I_StackBlockClass_release}
,{(SEL)"dealloc", "v8@0:4", (void *)_I_StackBlockClass_dealloc}
,{(SEL)"retainCount", "I8@0:4", (void *)_I_StackBlockClass_retainCount}
//,{(SEL)"description", "@8@0:4", (void *)_I_StackBlockClass_description}
}
};
struct psy_objc_class {
struct psy_objc_class *isa;
const char *super_class_name;
char *name;
long version;
long info;
long instance_size;
struct _objc_ivar_list *ivars;
struct _objc_method_list *methods;
struct objc_cache *cache;
struct _objc_protocol_list *protocols;
const char *ivar_layout;
struct _objc_class_ext *ext;
};
static struct psy_objc_class _OBJC_METACLASS_StackBlockClass __attribute__ ((used, section ("__OBJC, __meta_class")))= {
(struct psy_objc_class *)"NSObject", "NSObject", "StackBlockClass", 0,2, sizeof(struct psy_objc_class), 0, 0
,0,0,0,0
};
#define _OBJC_CLASS_StackBlockClass _NSConcreteStackBlock
struct psy_objc_class _NSConcreteStackBlock __attribute__ ((used, section ("__OBJC, __class")))= {
&_OBJC_METACLASS_StackBlockClass, "NSObject", "StackBlockClass", 0,1,sizeof(struct StackBlockClass),0, (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_StackBlockClass, 0
,0,0,0
};
static struct {
struct _objc_method_list *next_method;
int method_count;
struct psy_objc_method method_list[6];
} _OBJC_INSTANCE_METHODS_GlobalBlockClass __attribute__ ((used, section ("__OBJC, __inst_meth")))= {
0, 6
,{{(SEL)"copyWithZone:", "@12@0:4^{_NSZone=}8", (void *)_I_GlobalBlockClass_copyWithZone_}
,{(SEL)"copy", "@8@0:4", (void *)_I_GlobalBlockClass_copy}
,{(SEL)"retain", "@8@0:4", (void *)_I_GlobalBlockClass_retain}
,{(SEL)"release", "Vv8@0:4", (void *)_I_GlobalBlockClass_release}
,{(SEL)"dealloc", "v8@0:4", (void *)_I_GlobalBlockClass_dealloc}
,{(SEL)"retainCount", "I8@0:4", (void *)_I_GlobalBlockClass_retainCount}
//,{(SEL)"description", "@8@0:4", (void *)_I_GlobalBlockClass_description}
}
};
static struct psy_objc_class _OBJC_METACLASS_GlobalBlockClass __attribute__ ((used, section ("__OBJC, __meta_class")))= {
(struct psy_objc_class *)"NSObject", "NSObject", "GlobalBlockClass", 0,2, sizeof(struct psy_objc_class), 0, 0
,0,0,0,0
};
#define _OBJC_CLASS_GlobalBlockClass _NSConcreteGlobalBlock
struct psy_objc_class _NSConcreteGlobalBlock __attribute__ ((used, section ("__OBJC, __class")))= {
&_OBJC_METACLASS_GlobalBlockClass, "NSObject", "GlobalBlockClass", 0,1,sizeof(struct GlobalBlockClass),0, (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_GlobalBlockClass, 0
,0,0,0
};
struct psy_objc_symtab {
long sel_ref_cnt;
SEL *refs;
short cls_def_cnt;
short cat_def_cnt;
void *defs[2];
};
static struct psy_objc_symtab PSY_OBJC_SYMBOLS __attribute__((used, section ("__OBJC, __symbols")))= {
0, 0, 2, 0
,&_OBJC_CLASS_StackBlockClass
,&_OBJC_CLASS_GlobalBlockClass
};
struct psy_objc_module {
long version;
long size;
const char *name;
struct psy_objc_symtab *symtab;
};
static struct psy_objc_module PSY_OBJC_MODULES __attribute__ ((used, section ("__OBJC, __module_info")))= {
7, sizeof(struct psy_objc_module), "", &PSY_OBJC_SYMBOLS
};
// Copy a block to the heap if it's still on the stack or increments its retain count.
// The block is considered on the stack if self->descriptor->reserved == 0.
void *__Block_copy(void *src)
{
struct StackBlockClass *self = src;
struct StackBlockClass *ret = self;
// If the block is Global, there's no need to copy it on the heap.
if(self->isa == &_NSConcreteStackBlock && self->flags & BLOCK_HAS_DESCRIPTOR)
{
if(self->reserved == 0)
{
ret = malloc(self->descriptor->size);
memcpy(ret, self, self->descriptor->size);
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
self->descriptor->copy_helper(ret, self);
}
ret->reserved++;
}
return ret;
}
// Release a block and frees the memory when the retain count hits zero.
void __Block_release(void *src)
{
struct StackBlockClass *self = src;
if(self->isa == &_NSConcreteStackBlock && // A Global block doesn't need to be released
self->flags & BLOCK_HAS_DESCRIPTOR && // Should always be true...
self->reserved > 0) // If false, then it's not allocated on the heap, we won't release auto memory !
{
self->reserved--;
if(self->reserved == 0)
{
if(self->flags & BLOCK_HAS_COPY_DISPOSE)
self->descriptor->dispose_helper(self);
free(self);
}
}
}