GPBRootObject.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. #import "GPBRootObject.h"
  8. #import "GPBRootObject_PackagePrivate.h"
  9. #import <CoreFoundation/CoreFoundation.h>
  10. #import <objc/runtime.h>
  11. #import <os/lock.h>
  12. #import "GPBDescriptor.h"
  13. #import "GPBExtensionRegistry.h"
  14. #import "GPBUtilities.h"
  15. #import "GPBUtilities_PackagePrivate.h"
  16. @interface GPBExtensionDescriptor (GPBRootObject)
  17. // Get singletonName as a c string.
  18. - (const char *)singletonNameC;
  19. @end
  20. // We need some object to conform to the MessageSignatureProtocol to make sure
  21. // the selectors in it are recorded in our Objective C runtime information.
  22. // GPBMessage is arguably the more "obvious" choice, but given that all messages
  23. // inherit from GPBMessage, conflicts seem likely, so we are using GPBRootObject
  24. // instead.
  25. @interface GPBRootObject () <GPBMessageSignatureProtocol>
  26. @end
  27. @implementation GPBRootObject
  28. // Taken from http://www.burtleburtle.net/bob/hash/doobs.html
  29. // Public Domain
  30. static uint32_t jenkins_one_at_a_time_hash(const char *key) {
  31. uint32_t hash = 0;
  32. for (uint32_t i = 0; key[i] != '\0'; ++i) {
  33. hash += key[i];
  34. hash += (hash << 10);
  35. hash ^= (hash >> 6);
  36. }
  37. hash += (hash << 3);
  38. hash ^= (hash >> 11);
  39. hash += (hash << 15);
  40. return hash;
  41. }
  42. // Key methods for our custom CFDictionary.
  43. // Note that the dictionary lasts for the lifetime of our app, so no need
  44. // to worry about deallocation. All of the items are added to it at
  45. // startup, and so the keys don't need to be retained/released.
  46. // Keys are NULL terminated char *.
  47. static const void *GPBRootExtensionKeyRetain(__unused CFAllocatorRef allocator, const void *value) {
  48. return value;
  49. }
  50. static void GPBRootExtensionKeyRelease(__unused CFAllocatorRef allocator,
  51. __unused const void *value) {}
  52. static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) {
  53. const char *key = (const char *)value;
  54. return CFStringCreateWithCString(kCFAllocatorDefault, key, kCFStringEncodingUTF8);
  55. }
  56. static Boolean GPBRootExtensionKeyEqual(const void *value1, const void *value2) {
  57. const char *key1 = (const char *)value1;
  58. const char *key2 = (const char *)value2;
  59. return strcmp(key1, key2) == 0;
  60. }
  61. static CFHashCode GPBRootExtensionKeyHash(const void *value) {
  62. const char *key = (const char *)value;
  63. return jenkins_one_at_a_time_hash(key);
  64. }
  65. // Long ago, this was an OSSpinLock, but then it came to light that there were issues for that on
  66. // iOS:
  67. // http://mjtsai.com/blog/2015/12/16/osspinlock-is-unsafe/
  68. // https://lists.swift.org/pipermail/swift-dev/Week-of-Mon-20151214/000372.html
  69. // It was changed to a dispatch_semaphore_t, but that has potential for priority inversion issues.
  70. // The minOS versions are now high enough that os_unfair_lock can be used, and should provide
  71. // all the support we need. For more information in the concurrency/locking space see:
  72. // https://gist.github.com/tclementdev/6af616354912b0347cdf6db159c37057
  73. // https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/PrioritizeWorkWithQoS.html
  74. // https://developer.apple.com/videos/play/wwdc2017/706/
  75. static os_unfair_lock gExtensionSingletonDictionaryLock = OS_UNFAIR_LOCK_INIT;
  76. static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL;
  77. static GPBExtensionRegistry *gDefaultExtensionRegistry = NULL;
  78. + (void)initialize {
  79. // Ensure the global is started up.
  80. if (!gExtensionSingletonDictionary) {
  81. CFDictionaryKeyCallBacks keyCallBacks = {
  82. // See description above for reason for using custom dictionary.
  83. 0,
  84. GPBRootExtensionKeyRetain,
  85. GPBRootExtensionKeyRelease,
  86. GPBRootExtensionCopyKeyDescription,
  87. GPBRootExtensionKeyEqual,
  88. GPBRootExtensionKeyHash,
  89. };
  90. gExtensionSingletonDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
  91. &kCFTypeDictionaryValueCallBacks);
  92. gDefaultExtensionRegistry = [[GPBExtensionRegistry alloc] init];
  93. }
  94. if ([self superclass] == [GPBRootObject class]) {
  95. // This is here to start up all the per file "Root" subclasses.
  96. // This must be done in initialize to enforce thread safety of start up of
  97. // the protocol buffer library.
  98. [self extensionRegistry];
  99. }
  100. }
  101. + (GPBExtensionRegistry *)extensionRegistry {
  102. // Is overridden in all the subclasses that provide extensions to provide the
  103. // per class one.
  104. return gDefaultExtensionRegistry;
  105. }
  106. + (void)globallyRegisterExtension:(GPBExtensionDescriptor *)field {
  107. const char *key = [field singletonNameC];
  108. os_unfair_lock_lock(&gExtensionSingletonDictionaryLock);
  109. CFDictionarySetValue(gExtensionSingletonDictionary, key, field);
  110. os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock);
  111. }
  112. static id ExtensionForName(id self, SEL _cmd) {
  113. // Really fast way of doing "classname_selName".
  114. // This came up as a hotspot (creation of NSString *) when accessing a
  115. // lot of extensions.
  116. const char *selName = sel_getName(_cmd);
  117. if (selName[0] == '_') {
  118. return nil; // Apple internal selector.
  119. }
  120. size_t selNameLen = 0;
  121. while (1) {
  122. char c = selName[selNameLen];
  123. if (c == '\0') { // String end.
  124. break;
  125. }
  126. if (c == ':') {
  127. return nil; // Selector took an arg, not one of the runtime methods.
  128. }
  129. ++selNameLen;
  130. }
  131. const char *className = class_getName(self);
  132. size_t classNameLen = strlen(className);
  133. char key[classNameLen + selNameLen + 2];
  134. memcpy(key, className, classNameLen);
  135. key[classNameLen] = '_';
  136. memcpy(&key[classNameLen + 1], selName, selNameLen);
  137. key[classNameLen + 1 + selNameLen] = '\0';
  138. // NOTE: Even though this method is called from another C function,
  139. // gExtensionSingletonDictionaryLock and gExtensionSingletonDictionary
  140. // will always be initialized. This is because this call flow is just to
  141. // lookup the Extension, meaning the code is calling an Extension class
  142. // message on a Message or Root class. This guarantees that the class was
  143. // initialized and Message classes ensure their Root was also initialized.
  144. NSAssert(gExtensionSingletonDictionary, @"Startup order broken!");
  145. os_unfair_lock_lock(&gExtensionSingletonDictionaryLock);
  146. id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key);
  147. // We can't remove the key from the dictionary here (as an optimization),
  148. // two threads could have gone into +resolveClassMethod: for the same method,
  149. // and ended up here; there's no way to ensure both return YES without letting
  150. // both try to wire in the method.
  151. os_unfair_lock_unlock(&gExtensionSingletonDictionaryLock);
  152. return extension;
  153. }
  154. BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) {
  155. // Another option would be to register the extensions with the class at
  156. // globallyRegisterExtension:
  157. // Timing the two solutions, this solution turned out to be much faster
  158. // and reduced startup time, and runtime memory.
  159. // The advantage to globallyRegisterExtension is that it would reduce the
  160. // size of the protos somewhat because the singletonNameC wouldn't need
  161. // to include the class name. For a class with a lot of extensions it
  162. // can add up. You could also significantly reduce the code complexity of this
  163. // file.
  164. id extension = ExtensionForName(self, sel);
  165. if (extension != nil) {
  166. const char *encoding = GPBMessageEncodingForSelector(@selector(getClassValue), NO);
  167. Class metaClass = objc_getMetaClass(class_getName(self));
  168. IMP imp = imp_implementationWithBlock(^(__unused id obj) {
  169. return extension;
  170. });
  171. BOOL methodAdded = class_addMethod(metaClass, sel, imp, encoding);
  172. // class_addMethod() is documented as also failing if the method was already
  173. // added; so we check if the method is already there and return success so
  174. // the method dispatch will still happen. Why would it already be added?
  175. // Two threads could cause the same method to be bound at the same time,
  176. // but only one will actually bind it; the other still needs to return true
  177. // so things will dispatch.
  178. if (!methodAdded) {
  179. methodAdded = GPBClassHasSel(metaClass, sel);
  180. }
  181. return methodAdded;
  182. }
  183. return NO;
  184. }
  185. + (BOOL)resolveClassMethod:(SEL)sel {
  186. if (GPBResolveExtensionClassMethod(self, sel)) {
  187. return YES;
  188. }
  189. return [super resolveClassMethod:sel];
  190. }
  191. @end