Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Test/FastPathAlloc.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ @interface NoAlloc : Test @end
@interface NoInit : Test @end
@interface NoInit2 : NoInit @end

@interface NilAlloc : NoInit @end

@interface ShouldInitSubclassed : NoInit @end

@implementation ShouldAlloc
Expand Down Expand Up @@ -100,6 +102,13 @@ + (instancetype) alloc
}
@end

@implementation NilAlloc
+ (instancetype)alloc
{
return nil;
}
@end

Class getClassNamed(char *name)
{
return nil;
Expand Down Expand Up @@ -147,6 +156,11 @@ int main(void)
// implementations can handle receivers that are nil
[getClassNamed("flibble") alloc];
[[getClassNamed("flibble") alloc] init];

// Check that we can handle fast-path init if alloc returns nil.
assert([[NilAlloc alloc] init] == nil);

return 0;
}

#endif
22 changes: 17 additions & 5 deletions fast_paths.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,23 @@ - (id)init;
{
return nil;
}
id instance = objc_alloc(cls);
// If +alloc was overwritten, it is not guaranteed that it returns
// an instance of cls.
cls = classForObject(instance);
if (objc_test_class_flag(cls, objc_class_flag_fast_alloc_init))
id instance;
if (LIKELY(objc_test_class_flag(cls->isa, objc_class_flag_fast_alloc_init)))
{
instance = class_createInstance(cls, 0);
}
else
{
instance = [cls alloc];
if (UNLIKELY(instance == nil))
{
return nil;
}
// If +alloc was overwritten, it is not guaranteed that it returns
// an instance of cls.
cls = classForObject(instance);
}
if (LIKELY(objc_test_class_flag(cls, objc_class_flag_fast_alloc_init)))
{
return instance;
}
Expand Down
Loading