24

While answering this question I noted that modern Objective-C runtime uses tagged pointers. The article by Mike Ash and its comments note that they are used for some NSNumber and NSDate instances.

Which got me thinking about the complete table of scenarios for different platforms:

Where does OSX/iOS 32/64-bit Objective-C runtime use tagged pointers?

5
  • Greg Parker has interesting information on the runtime's use of the isa pointer in ARM64. It's not exactly the same as tagged pointers but similar non-pointer data in the bits normally used for the address. Dec 3, 2013 at 21:59
  • It is an implementation detail and the exact set of objects that may be represented by tagged pointers can vary from platform to platform and version to version. It isn't hard to interrogate the the system at runtime to find out what classes are registered (and either Ash's or Parker's articles should give enough info to do so).
    – bbum
    Dec 3, 2013 at 23:25
  • 2
    implementation detail -- you're right, but it's still interesting; isn't hard -- perhaps, but I don't have access to ARMv8 devices.
    – ilya n.
    Dec 4, 2013 at 0:04
  • It should be noted that the term "tagged pointer" has multiple meanings. Historically the term was used for "real" pointers that contained some additional type or authorization information, and usually the main tag bit in the pointer cannot be manipulated by user code. On the IBM S/38 & its successors, eg, the tag bit (bit 65) must be on in a quadword for it to be considered a valid pointer to anything.
    – Hot Licks
    Dec 4, 2013 at 1:44
  • Re using the low order bits in a "regular" pointer for this purpose: On some architectures certain storage reference instructions will interrupt if presented with an unaligned address. This provides a convenient way to "trap" tagged pointers in cases where they are rarely used (though using such an interrupt-driven mechanism is too slow for, eg, NSNumber pointers).
    – Hot Licks
    Dec 4, 2013 at 1:47

1 Answer 1

28

OS X and iOS both use tagged pointer objects in 64-bit code. Neither currently uses any tagged pointer objects in 32-bit code, though in principle it's not impossible. The specific set of optimized classes and optimized values changes frequently. Open-source objc4/runtime/objc-internal.h describes this set of classes that was used in at least one OS version:

OBJC_TAG_NSAtom            = 0, 
OBJC_TAG_1                 = 1, 
OBJC_TAG_NSString          = 2, 
OBJC_TAG_NSNumber          = 3, 
OBJC_TAG_NSIndexPath       = 4, 
OBJC_TAG_NSManagedObjectID = 5, 
OBJC_TAG_NSDate            = 6, 
OBJC_TAG_7                 = 7
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.