GPBRootObject.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBRootObject_PackagePrivate.h"
  31. #import <objc/runtime.h>
  32. #import <libkern/OSAtomic.h>
  33. #import <CoreFoundation/CoreFoundation.h>
  34. #import "GPBDescriptor.h"
  35. #import "GPBExtensionField.h"
  36. #import "GPBUtilities_PackagePrivate.h"
  37. @interface GPBExtensionDescriptor (GPBRootObject)
  38. // Get singletonName as a c string.
  39. - (const char *)singletonNameC;
  40. @end
  41. @implementation GPBRootObject
  42. // Taken from http://www.burtleburtle.net/bob/hash/doobs.html
  43. // Public Domain
  44. static uint32_t jenkins_one_at_a_time_hash(const char *key) {
  45. uint32_t hash = 0;
  46. for (uint32_t i = 0; key[i] != '\0'; ++i) {
  47. hash += key[i];
  48. hash += (hash << 10);
  49. hash ^= (hash >> 6);
  50. }
  51. hash += (hash << 3);
  52. hash ^= (hash >> 11);
  53. hash += (hash << 15);
  54. return hash;
  55. }
  56. // Key methods for our custom CFDictionary.
  57. // Note that the dictionary lasts for the lifetime of our app, so no need
  58. // to worry about deallocation. All of the items are added to it at
  59. // startup, and so the keys don't need to be retained/released.
  60. // Keys are NULL terminated char *.
  61. static const void *GPBRootExtensionKeyRetain(CFAllocatorRef allocator,
  62. const void *value) {
  63. #pragma unused(allocator)
  64. return value;
  65. }
  66. static void GPBRootExtensionKeyRelease(CFAllocatorRef allocator,
  67. const void *value) {
  68. #pragma unused(allocator)
  69. #pragma unused(value)
  70. }
  71. static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) {
  72. const char *key = (const char *)value;
  73. return CFStringCreateWithCString(kCFAllocatorDefault, key,
  74. kCFStringEncodingUTF8);
  75. }
  76. static Boolean GPBRootExtensionKeyEqual(const void *value1,
  77. const void *value2) {
  78. const char *key1 = (const char *)value1;
  79. const char *key2 = (const char *)value2;
  80. return strcmp(key1, key2) == 0;
  81. }
  82. static CFHashCode GPBRootExtensionKeyHash(const void *value) {
  83. const char *key = (const char *)value;
  84. return jenkins_one_at_a_time_hash(key);
  85. }
  86. static OSSpinLock gExtensionSingletonDictionaryLock_ = OS_SPINLOCK_INIT;
  87. static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL;
  88. + (void)initialize {
  89. // Ensure the global is started up.
  90. if (!gExtensionSingletonDictionary) {
  91. CFDictionaryKeyCallBacks keyCallBacks = {
  92. // See description above for reason for using custom dictionary.
  93. 0,
  94. GPBRootExtensionKeyRetain,
  95. GPBRootExtensionKeyRelease,
  96. GPBRootExtensionCopyKeyDescription,
  97. GPBRootExtensionKeyEqual,
  98. GPBRootExtensionKeyHash,
  99. };
  100. gExtensionSingletonDictionary =
  101. CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
  102. &kCFTypeDictionaryValueCallBacks);
  103. }
  104. if ([self superclass] == [GPBRootObject class]) {
  105. // This is here to start up all the per file "Root" subclasses.
  106. // This must be done in initialize to enforce thread safety of start up of
  107. // the protocol buffer library.
  108. [self extensionRegistry];
  109. }
  110. }
  111. + (GPBExtensionRegistry *)extensionRegistry {
  112. // Is overridden in all the subclasses that provide extensions to provide the
  113. // per class one.
  114. return nil;
  115. }
  116. + (void)globallyRegisterExtension:(GPBExtensionField *)field {
  117. const char *key = [field.descriptor singletonNameC];
  118. OSSpinLockLock(&gExtensionSingletonDictionaryLock_);
  119. CFDictionarySetValue(gExtensionSingletonDictionary, key, field);
  120. OSSpinLockUnlock(&gExtensionSingletonDictionaryLock_);
  121. }
  122. GPB_INLINE id ExtensionForName(id self, SEL _cmd) {
  123. // Really fast way of doing "classname_selName".
  124. // This came up as a hotspot (creation of NSString *) when accessing a
  125. // lot of extensions.
  126. const char *selName = sel_getName(_cmd);
  127. if (selName[0] == '_') {
  128. return nil; // Apple internal selector.
  129. }
  130. size_t selNameLen = 0;
  131. while (1) {
  132. char c = selName[selNameLen];
  133. if (c == '\0') { // String end.
  134. break;
  135. }
  136. if (c == ':') {
  137. return nil; // Selector took an arg, not one of the runtime methods.
  138. }
  139. ++selNameLen;
  140. }
  141. const char *className = class_getName(self);
  142. size_t classNameLen = strlen(className);
  143. char key[classNameLen + selNameLen + 2];
  144. memcpy(key, className, classNameLen);
  145. key[classNameLen] = '_';
  146. memcpy(&key[classNameLen + 1], selName, selNameLen);
  147. key[classNameLen + 1 + selNameLen] = '\0';
  148. OSSpinLockLock(&gExtensionSingletonDictionaryLock_);
  149. id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key);
  150. if (extension) {
  151. // The method is getting wired in to the class, so no need to keep it in
  152. // the dictionary.
  153. CFDictionaryRemoveValue(gExtensionSingletonDictionary, key);
  154. }
  155. OSSpinLockUnlock(&gExtensionSingletonDictionaryLock_);
  156. return extension;
  157. }
  158. BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) {
  159. // Another option would be to register the extensions with the class at
  160. // globallyRegisterExtension:
  161. // Timing the two solutions, this solution turned out to be much faster
  162. // and reduced startup time, and runtime memory.
  163. // The advantage to globallyRegisterExtension is that it would reduce the
  164. // size of the protos somewhat because the singletonNameC wouldn't need
  165. // to include the class name. For a class with a lot of extensions it
  166. // can add up. You could also significantly reduce the code complexity of this
  167. // file.
  168. id extension = ExtensionForName(self, sel);
  169. if (extension != nil) {
  170. const char *encoding =
  171. GPBMessageEncodingForSelector(@selector(getClassValue), NO);
  172. Class metaClass = objc_getMetaClass(class_getName(self));
  173. IMP imp = imp_implementationWithBlock(^(id obj) {
  174. #pragma unused(obj)
  175. return extension;
  176. });
  177. if (class_addMethod(metaClass, sel, imp, encoding)) {
  178. return YES;
  179. }
  180. }
  181. return NO;
  182. }
  183. + (BOOL)resolveClassMethod:(SEL)sel {
  184. if (GPBResolveExtensionClassMethod(self, sel)) {
  185. return YES;
  186. }
  187. return [super resolveClassMethod:sel];
  188. }
  189. @end