Skip to content

Instantly share code, notes, and snippets.

@Catfish-Man
Last active April 19, 2018 21:56
Show Gist options
  • Select an option

  • Save Catfish-Man/ad9fe0ca44f9a5f31a44 to your computer and use it in GitHub Desktop.

Select an option

Save Catfish-Man/ad9fe0ca44f9a5f31a44 to your computer and use it in GitHub Desktop.

Revisions

  1. Catfish-Man revised this gist Mar 15, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tollfree.m
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@

    //These are equivalent in behavior
    NSNumber *first = [objects objectAtIndex:0]
    NSNumber *second = (NSNumber *)CFArrayGetValueAtIndex(0)
    NSNumber *second = (NSNumber *)CFArrayGetValueAtIndex(objects, 0)

    //But is the code that runs the same? Not so much… in the first one we do…
    objc_msgSend(objects, <selector reference>, 0)
  2. Catfish-Man created this gist Feb 7, 2016.
    31 changes: 31 additions & 0 deletions tollfree.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    //Given this:
    NSArray *objects = @[@1, @2, @3]

    //These are equivalent in behavior
    NSNumber *first = [objects objectAtIndex:0]
    NSNumber *second = (NSNumber *)CFArrayGetValueAtIndex(0)

    //But is the code that runs the same? Not so much… in the first one we do…
    objc_msgSend(objects, <selector reference>, 0)
    -> http://sealiesoftware.com/msg/x86-mavericks.html
    -> -[__NSArrayI objectAtIndex:]
    -> check if index is in bounds
    -> return _list[0]

    //In the second one we do…
    CFArrayGetValueAtIndex()
    -> CFArrayGetTypeID()
    -> dispatch_once()
    -> grab __kCFArrayTypeID
    -> CF_IS_OBJC() (see http://opensource.apple.com/source/CF/CF-744.19/CFInternal.h)
    -> check if objects is a tagged pointer (if (objects & 1))
    -> check if objects->isa is 0
    -> check if objects->isa is the constant string class
    -> check if __kCFArrayTypeID is small enough to be in the bridged classes array
    -> grab the class for __kCFArrayTypeID from the bridged classes array
    -> check if the class we grabbed is the same as objects->isa
    -> objc_msgSend(objects, <selector reference>, 0)
    -> http://sealiesoftware.com/msg/x86-mavericks.html
    -> -[__NSArrayI objectAtIndex:]
    -> check if index is in bounds
    -> return _list[0]