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?

Community
  • 1
  • 1
ilya n.
  • 18,398
  • 15
  • 71
  • 89
  • 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 03 '13 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 04 '13 at 00: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 04 '13 at 01: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 04 '13 at 01:47
  • Greg Parker has [interesting information](http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html) 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. – Nikolai Ruhe Dec 03 '13 at 21:59

1 Answers1

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
Greg Parker
  • 7,972
  • 2
  • 24
  • 21
  • 3
    [Here's a link to macOS 10.12.4 `objc-internal.h`](https://opensource.apple.com/source/objc4/objc4-709/runtime/objc-internal.h.auto.html). – rob mayoff May 19 '17 at 19:24
  • fresh version of this enum could be found here: https://opensource.apple.com/source/objc4/objc4-818.2/runtime/objc-internal.h.auto.html – Maxim Kholyavkin Dec 08 '21 at 11:46