GPBRootObject.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <CoreFoundation/CoreFoundation.h>
  33. #import "GPBDescriptor.h"
  34. #import "GPBExtensionField.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. @interface GPBExtensionDescriptor (GPBRootObject)
  37. // Get singletonName as a c string.
  38. - (const char *)singletonNameC;
  39. @end
  40. @implementation GPBRootObject
  41. // Taken from http://www.burtleburtle.net/bob/hash/doobs.html
  42. // Public Domain
  43. static uint32_t jenkins_one_at_a_time_hash(const char *key) {
  44. uint32_t hash = 0;
  45. for (uint32_t i = 0; key[i] != '\0'; ++i) {
  46. hash += key[i];
  47. hash += (hash << 10);
  48. hash ^= (hash >> 6);
  49. }
  50. hash += (hash << 3);
  51. hash ^= (hash >> 11);
  52. hash += (hash << 15);
  53. return hash;
  54. }
  55. // Key methods for our custom CFDictionary.
  56. // Note that the dictionary lasts for the lifetime of our app, so no need
  57. // to worry about deallocation. All of the items are added to it at
  58. // startup, and so the keys don't need to be retained/released.
  59. // Keys are NULL terminated char *.
  60. static const void *GPBRootExtensionKeyRetain(CFAllocatorRef allocator,
  61. const void *value) {
  62. #pragma unused(allocator)
  63. return value;
  64. }
  65. static void GPBRootExtensionKeyRelease(CFAllocatorRef allocator,
  66. const void *value) {
  67. #pragma unused(allocator)
  68. #pragma unused(value)
  69. }
  70. static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) {
  71. const char *key = (const char *)value;
  72. return CFStringCreateWithCString(kCFAllocatorDefault, key,
  73. kCFStringEncodingUTF8);
  74. }
  75. static Boolean GPBRootExtensionKeyEqual(const void *value1,
  76. const void *value2) {
  77. const char *key1 = (const char *)value1;
  78. const char *key2 = (const char *)value2;
  79. return strcmp(key1, key2) == 0;
  80. }
  81. static CFHashCode GPBRootExtensionKeyHash(const void *value) {
  82. const char *key = (const char *)value;
  83. return jenkins_one_at_a_time_hash(key);
  84. }
  85. static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL;
  86. + (void)initialize {
  87. if (!gExtensionSingletonDictionary) {
  88. CFDictionaryKeyCallBacks keyCallBacks = {
  89. // See description above for reason for using custom dictionary.
  90. 0,
  91. GPBRootExtensionKeyRetain,
  92. GPBRootExtensionKeyRelease,
  93. GPBRootExtensionCopyKeyDescription,
  94. GPBRootExtensionKeyEqual,
  95. GPBRootExtensionKeyHash,
  96. };
  97. gExtensionSingletonDictionary =
  98. CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks,
  99. &kCFTypeDictionaryValueCallBacks);
  100. }
  101. }
  102. + (GPBExtensionRegistry *)extensionRegistry {
  103. // Is overridden in all the subclasses that provide extensions to provide the
  104. // per class one.
  105. return nil;
  106. }
  107. + (void)globallyRegisterExtension:(GPBExtensionField *)field {
  108. const char *key = [field.descriptor singletonNameC];
  109. // Register happens at startup, so there is no thread safety issue in
  110. // modifying the dictionary.
  111. CFDictionarySetValue(gExtensionSingletonDictionary, key, field);
  112. }
  113. static id ExtensionForName(id self, SEL _cmd) {
  114. // Really fast way of doing "classname_selName".
  115. // This came up as a hotspot (creation of NSString *) when accessing a
  116. // lot of extensions.
  117. const char *className = class_getName(self);
  118. const char *selName = sel_getName(_cmd);
  119. size_t classNameLen = strlen(className);
  120. size_t selNameLen = strlen(selName);
  121. char key[classNameLen + selNameLen + 2];
  122. memcpy(key, className, classNameLen);
  123. key[classNameLen] = '_';
  124. memcpy(&key[classNameLen + 1], selName, selNameLen);
  125. key[classNameLen + 1 + selNameLen] = '\0';
  126. id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key);
  127. // We can't remove the key from the dictionary here (as an optimization),
  128. // because resolveClassMethod can happen on any thread and we'd then need
  129. // a lock.
  130. return extension;
  131. }
  132. + (BOOL)resolveClassMethod:(SEL)sel {
  133. // Another option would be to register the extensions with the class at
  134. // globallyRegisterExtension:
  135. // Timing the two solutions, this solution turned out to be much faster
  136. // and reduced startup time, and runtime memory.
  137. // On an iPhone 5s:
  138. // ResolveClassMethod: 1515583 nanos
  139. // globallyRegisterExtension: 2453083 nanos
  140. // The advantage to globallyRegisterExtension is that it would reduce the
  141. // size of the protos somewhat because the singletonNameC wouldn't need
  142. // to include the class name. For a class with a lot of extensions it
  143. // can add up. You could also significantly reduce the code complexity of this
  144. // file.
  145. id extension = ExtensionForName(self, sel);
  146. if (extension != nil) {
  147. const char *encoding =
  148. GPBMessageEncodingForSelector(@selector(getClassValue), NO);
  149. Class metaClass = objc_getMetaClass(class_getName(self));
  150. IMP imp = imp_implementationWithBlock(^(id obj) {
  151. #pragma unused(obj)
  152. return extension;
  153. });
  154. return class_addMethod(metaClass, sel, imp, encoding);
  155. }
  156. return [super resolveClassMethod:sel];
  157. }
  158. @end