-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathMABlockClosure.m
343 lines (291 loc) · 8.08 KB
/
MABlockClosure.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
#import "MABlockClosure.h"
#import <assert.h>
#import <objc/runtime.h>
#import <sys/mman.h>
#if TARGET_OS_IPHONE
#import <CoreGraphics/CoreGraphics.h>
#endif
@implementation MABlockClosure
struct BlockDescriptor
{
unsigned long reserved;
unsigned long size;
void *rest[1];
};
struct Block
{
void *isa;
int flags;
int reserved;
void *invoke;
struct BlockDescriptor *descriptor;
};
static void *BlockImpl(id block)
{
return ((struct Block *)block)->invoke;
}
static const char *BlockSig(id blockObj)
{
struct Block *block = (void *)blockObj;
struct BlockDescriptor *descriptor = block->descriptor;
int copyDisposeFlag = 1 << 25;
int signatureFlag = 1 << 30;
assert(block->flags & signatureFlag);
int index = 0;
if(block->flags & copyDisposeFlag)
index += 2;
return descriptor->rest[index];
}
static void BlockClosure(ffi_cif *cif, void *ret, void **args, void *userdata)
{
MABlockClosure *self = userdata;
int count = self->_closureArgCount;
void **innerArgs = malloc((count + 1) * sizeof(*innerArgs));
innerArgs[0] = &self->_block;
memcpy(innerArgs + 1, args, count * sizeof(*args));
ffi_call(&self->_innerCIF, BlockImpl(self->_block), ret, innerArgs);
free(innerArgs);
}
static void *AllocateClosure(void **codePtr)
{
#if USE_LIBFFI_CLOSURE_ALLOC
return ffi_closure_alloc(sizeof(ffi_closure), codePtr);
#else
ffi_closure *closure = mmap(NULL, sizeof(ffi_closure), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if(closure == (void *)-1)
{
perror("mmap");
return NULL;
}
*codePtr = closure;
return closure;
#endif
}
static void DeallocateClosure(void *closure)
{
#if USE_LIBFFI_CLOSURE_ALLOC
ffi_closure_free(closure);
#else
munmap(closure, sizeof(ffi_closure));
#endif
}
- (void *)_allocate: (size_t)howmuch
{
NSMutableData *data = [NSMutableData dataWithLength: howmuch];
[_allocations addObject: data];
return [data mutableBytes];
}
static const char *SizeAndAlignment(const char *str, NSUInteger *sizep, NSUInteger *alignp, int *len)
{
const char *out = NSGetSizeAndAlignment(str, sizep, alignp);
if(len)
*len = out - str;
while(isdigit(*out))
out++;
return out;
}
static int ArgCount(const char *str)
{
int argcount = -1; // return type is the first one
while(str && *str)
{
str = SizeAndAlignment(str, NULL, NULL, NULL);
argcount++;
}
return argcount;
}
- (ffi_type *)_ffiArgForEncode: (const char *)str
{
#define SINT(type) do { \
if(str[0] == @encode(type)[0]) \
{ \
if(sizeof(type) == 1) \
return &ffi_type_sint8; \
else if(sizeof(type) == 2) \
return &ffi_type_sint16; \
else if(sizeof(type) == 4) \
return &ffi_type_sint32; \
else if(sizeof(type) == 8) \
return &ffi_type_sint64; \
else \
{ \
NSLog(@"Unknown size for type %s", #type); \
abort(); \
} \
} \
} while(0)
#define UINT(type) do { \
if(str[0] == @encode(type)[0]) \
{ \
if(sizeof(type) == 1) \
return &ffi_type_uint8; \
else if(sizeof(type) == 2) \
return &ffi_type_uint16; \
else if(sizeof(type) == 4) \
return &ffi_type_uint32; \
else if(sizeof(type) == 8) \
return &ffi_type_uint64; \
else \
{ \
NSLog(@"Unknown size for type %s", #type); \
abort(); \
} \
} \
} while(0)
#define INT(type) do { \
SINT(type); \
UINT(unsigned type); \
} while(0)
#define COND(type, name) do { \
if(str[0] == @encode(type)[0]) \
return &ffi_type_ ## name; \
} while(0)
#define PTR(type) COND(type, pointer)
#define STRUCT(structType, ...) do { \
if(strncmp(str, @encode(structType), strlen(@encode(structType))) == 0) \
{ \
ffi_type *elementsLocal[] = { __VA_ARGS__, NULL }; \
ffi_type **elements = [self _allocate: sizeof(elementsLocal)]; \
memcpy(elements, elementsLocal, sizeof(elementsLocal)); \
\
ffi_type *structType = [self _allocate: sizeof(*structType)]; \
structType->type = FFI_TYPE_STRUCT; \
structType->elements = elements; \
return structType; \
} \
} while(0)
SINT(_Bool);
SINT(signed char);
UINT(unsigned char);
INT(short);
INT(int);
INT(long);
INT(long long);
PTR(id);
PTR(Class);
PTR(SEL);
PTR(void *);
PTR(char *);
PTR(void (*)(void));
COND(float, float);
COND(double, double);
COND(void, void);
ffi_type *CGFloatFFI = sizeof(CGFloat) == sizeof(float) ? &ffi_type_float : &ffi_type_double;
STRUCT(CGRect, CGFloatFFI, CGFloatFFI, CGFloatFFI, CGFloatFFI);
STRUCT(CGPoint, CGFloatFFI, CGFloatFFI);
STRUCT(CGSize, CGFloatFFI, CGFloatFFI);
#if !TARGET_OS_IPHONE
STRUCT(NSRect, CGFloatFFI, CGFloatFFI, CGFloatFFI, CGFloatFFI);
STRUCT(NSPoint, CGFloatFFI, CGFloatFFI);
STRUCT(NSSize, CGFloatFFI, CGFloatFFI);
#endif
NSLog(@"Unknown encode string %s", str);
abort();
}
- (ffi_type **)_argsWithEncodeString: (const char *)str getCount: (int *)outCount
{
int argCount = ArgCount(str);
ffi_type **argTypes = [self _allocate: argCount * sizeof(*argTypes)];
int i = -1;
while(str && *str)
{
const char *next = SizeAndAlignment(str, NULL, NULL, NULL);
if(i >= 0)
argTypes[i] = [self _ffiArgForEncode: str];
i++;
str = next;
}
*outCount = argCount;
return argTypes;
}
- (int)_prepCIF: (ffi_cif *)cif withEncodeString: (const char *)str skipArg: (BOOL)skip
{
int argCount;
ffi_type **argTypes = [self _argsWithEncodeString: str getCount: &argCount];
if(skip)
{
argTypes++;
argCount--;
}
ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, argCount, [self _ffiArgForEncode: str], argTypes);
if(status != FFI_OK)
{
NSLog(@"Got result %ld from ffi_prep_cif", (long)status);
abort();
}
return argCount;
}
- (void)_prepClosureCIF
{
_closureArgCount = [self _prepCIF: &_closureCIF withEncodeString: BlockSig(_block) skipArg: YES];
}
- (void)_prepInnerCIF
{
[self _prepCIF: &_innerCIF withEncodeString: BlockSig(_block) skipArg: NO];
}
- (void)_prepClosure
{
#if USE_LIBFFI_CLOSURE_ALLOC
ffi_status status = ffi_prep_closure_loc(_closure, &_closureCIF, BlockClosure, self, _closureFptr);
if(status != FFI_OK)
{
NSLog(@"ffi_prep_closure returned %d", (int)status);
abort();
}
#else
ffi_status status = ffi_prep_closure(_closure, &_closureCIF, BlockClosure, self);
if(status != FFI_OK)
{
NSLog(@"ffi_prep_closure returned %d", (int)status);
abort();
}
if(mprotect(_closure, sizeof(_closure), PROT_READ | PROT_EXEC) == -1)
{
perror("mprotect");
abort();
}
#endif
}
- (id)initWithBlock: (id)block
{
if((self = [self init]))
{
_allocations = [[NSMutableArray alloc] init];
_block = block;
_closure = AllocateClosure(&_closureFptr);
[self _prepClosureCIF];
[self _prepInnerCIF];
[self _prepClosure];
}
return self;
}
- (void)dealloc
{
if(_closure)
DeallocateClosure(_closure);
[_allocations release];
[super dealloc];
}
- (void *)fptr
{
return _closureFptr;
}
@end
void *BlockFptr(id block)
{
@synchronized(block)
{
MABlockClosure *closure = objc_getAssociatedObject(block, BlockFptr);
if(!closure)
{
closure = [[MABlockClosure alloc] initWithBlock: block];
objc_setAssociatedObject(block, BlockFptr, closure, OBJC_ASSOCIATION_RETAIN);
[closure release]; // retained by the associated object assignment
}
return [closure fptr];
}
}
void *BlockFptrAuto(id block)
{
return BlockFptr([[block copy] autorelease]);
}