-
Notifications
You must be signed in to change notification settings - Fork 118
/
fast_paths.m
74 lines (70 loc) · 1.43 KB
/
fast_paths.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
#include "objc/runtime.h"
#include "class.h"
typedef struct _NSZone NSZone;
@interface RootMethods
- (id)alloc;
- (id)allocWithZone: (NSZone*)aZone;
- (id)init;
@end
#include <stdio.h>
/**
* Equivalent to [cls alloc]. If there's a fast path opt-in, then this skips the message send.
*/
OBJC_PUBLIC
id
objc_alloc(Class cls)
{
if (UNLIKELY(cls == nil))
{
return nil;
}
if (UNLIKELY(!objc_test_class_flag(cls->isa, objc_class_flag_initialized)))
{
objc_send_initialize(cls);
}
if (objc_test_class_flag(cls->isa, objc_class_flag_fast_alloc_init))
{
return class_createInstance(cls, 0);
}
return [cls alloc];
}
/**
* Equivalent to [cls allocWithZone: null]. If there's a fast path opt-in, then this skips the message send.
*/
OBJC_PUBLIC
id
objc_allocWithZone(Class cls)
{
if (UNLIKELY(cls == nil))
{
return nil;
}
if (UNLIKELY(!objc_test_class_flag(cls->isa, objc_class_flag_initialized)))
{
objc_send_initialize(cls);
}
if (objc_test_class_flag(cls->isa, objc_class_flag_fast_alloc_init))
{
return class_createInstance(cls, 0);
}
return [cls allocWithZone: NULL];
}
/**
* Equivalent to [[cls alloc] init]. If there's a fast path opt-in, then this
* skips the message send.
*/
OBJC_PUBLIC
id
objc_alloc_init(Class cls)
{
if (UNLIKELY(cls == nil))
{
return nil;
}
id instance = objc_alloc(cls);
if (objc_test_class_flag(cls, objc_class_flag_fast_alloc_init))
{
return instance;
}
return [instance init];
}