| OLD | NEW |
| (Empty) | |
| 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 |
| 31 #import "GPBRootObject_PackagePrivate.h" |
| 32 |
| 33 #import <objc/runtime.h> |
| 34 #import <libkern/OSAtomic.h> |
| 35 |
| 36 #import <CoreFoundation/CoreFoundation.h> |
| 37 |
| 38 #import "GPBDescriptor.h" |
| 39 #import "GPBExtensionRegistry.h" |
| 40 #import "GPBUtilities_PackagePrivate.h" |
| 41 |
| 42 @interface GPBExtensionDescriptor (GPBRootObject) |
| 43 // Get singletonName as a c string. |
| 44 - (const char *)singletonNameC; |
| 45 @end |
| 46 |
| 47 @implementation GPBRootObject |
| 48 |
| 49 // Taken from http://www.burtleburtle.net/bob/hash/doobs.html |
| 50 // Public Domain |
| 51 static uint32_t jenkins_one_at_a_time_hash(const char *key) { |
| 52 uint32_t hash = 0; |
| 53 for (uint32_t i = 0; key[i] != '\0'; ++i) { |
| 54 hash += key[i]; |
| 55 hash += (hash << 10); |
| 56 hash ^= (hash >> 6); |
| 57 } |
| 58 hash += (hash << 3); |
| 59 hash ^= (hash >> 11); |
| 60 hash += (hash << 15); |
| 61 return hash; |
| 62 } |
| 63 |
| 64 // Key methods for our custom CFDictionary. |
| 65 // Note that the dictionary lasts for the lifetime of our app, so no need |
| 66 // to worry about deallocation. All of the items are added to it at |
| 67 // startup, and so the keys don't need to be retained/released. |
| 68 // Keys are NULL terminated char *. |
| 69 static const void *GPBRootExtensionKeyRetain(CFAllocatorRef allocator, |
| 70 const void *value) { |
| 71 #pragma unused(allocator) |
| 72 return value; |
| 73 } |
| 74 |
| 75 static void GPBRootExtensionKeyRelease(CFAllocatorRef allocator, |
| 76 const void *value) { |
| 77 #pragma unused(allocator) |
| 78 #pragma unused(value) |
| 79 } |
| 80 |
| 81 static CFStringRef GPBRootExtensionCopyKeyDescription(const void *value) { |
| 82 const char *key = (const char *)value; |
| 83 return CFStringCreateWithCString(kCFAllocatorDefault, key, |
| 84 kCFStringEncodingUTF8); |
| 85 } |
| 86 |
| 87 static Boolean GPBRootExtensionKeyEqual(const void *value1, |
| 88 const void *value2) { |
| 89 const char *key1 = (const char *)value1; |
| 90 const char *key2 = (const char *)value2; |
| 91 return strcmp(key1, key2) == 0; |
| 92 } |
| 93 |
| 94 static CFHashCode GPBRootExtensionKeyHash(const void *value) { |
| 95 const char *key = (const char *)value; |
| 96 return jenkins_one_at_a_time_hash(key); |
| 97 } |
| 98 |
| 99 static OSSpinLock gExtensionSingletonDictionaryLock_ = OS_SPINLOCK_INIT; |
| 100 static CFMutableDictionaryRef gExtensionSingletonDictionary = NULL; |
| 101 static GPBExtensionRegistry *gDefaultExtensionRegistry = NULL; |
| 102 |
| 103 + (void)initialize { |
| 104 // Ensure the global is started up. |
| 105 if (!gExtensionSingletonDictionary) { |
| 106 CFDictionaryKeyCallBacks keyCallBacks = { |
| 107 // See description above for reason for using custom dictionary. |
| 108 0, |
| 109 GPBRootExtensionKeyRetain, |
| 110 GPBRootExtensionKeyRelease, |
| 111 GPBRootExtensionCopyKeyDescription, |
| 112 GPBRootExtensionKeyEqual, |
| 113 GPBRootExtensionKeyHash, |
| 114 }; |
| 115 gExtensionSingletonDictionary = |
| 116 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &keyCallBacks, |
| 117 &kCFTypeDictionaryValueCallBacks); |
| 118 gDefaultExtensionRegistry = [[GPBExtensionRegistry alloc] init]; |
| 119 } |
| 120 |
| 121 if ([self superclass] == [GPBRootObject class]) { |
| 122 // This is here to start up all the per file "Root" subclasses. |
| 123 // This must be done in initialize to enforce thread safety of start up of |
| 124 // the protocol buffer library. |
| 125 [self extensionRegistry]; |
| 126 } |
| 127 } |
| 128 |
| 129 + (GPBExtensionRegistry *)extensionRegistry { |
| 130 // Is overridden in all the subclasses that provide extensions to provide the |
| 131 // per class one. |
| 132 return gDefaultExtensionRegistry; |
| 133 } |
| 134 |
| 135 + (void)globallyRegisterExtension:(GPBExtensionDescriptor *)field { |
| 136 const char *key = [field singletonNameC]; |
| 137 OSSpinLockLock(&gExtensionSingletonDictionaryLock_); |
| 138 CFDictionarySetValue(gExtensionSingletonDictionary, key, field); |
| 139 OSSpinLockUnlock(&gExtensionSingletonDictionaryLock_); |
| 140 } |
| 141 |
| 142 static id ExtensionForName(id self, SEL _cmd) { |
| 143 // Really fast way of doing "classname_selName". |
| 144 // This came up as a hotspot (creation of NSString *) when accessing a |
| 145 // lot of extensions. |
| 146 const char *selName = sel_getName(_cmd); |
| 147 if (selName[0] == '_') { |
| 148 return nil; // Apple internal selector. |
| 149 } |
| 150 size_t selNameLen = 0; |
| 151 while (1) { |
| 152 char c = selName[selNameLen]; |
| 153 if (c == '\0') { // String end. |
| 154 break; |
| 155 } |
| 156 if (c == ':') { |
| 157 return nil; // Selector took an arg, not one of the runtime methods. |
| 158 } |
| 159 ++selNameLen; |
| 160 } |
| 161 |
| 162 const char *className = class_getName(self); |
| 163 size_t classNameLen = strlen(className); |
| 164 char key[classNameLen + selNameLen + 2]; |
| 165 memcpy(key, className, classNameLen); |
| 166 key[classNameLen] = '_'; |
| 167 memcpy(&key[classNameLen + 1], selName, selNameLen); |
| 168 key[classNameLen + 1 + selNameLen] = '\0'; |
| 169 OSSpinLockLock(&gExtensionSingletonDictionaryLock_); |
| 170 id extension = (id)CFDictionaryGetValue(gExtensionSingletonDictionary, key); |
| 171 if (extension) { |
| 172 // The method is getting wired in to the class, so no need to keep it in |
| 173 // the dictionary. |
| 174 CFDictionaryRemoveValue(gExtensionSingletonDictionary, key); |
| 175 } |
| 176 OSSpinLockUnlock(&gExtensionSingletonDictionaryLock_); |
| 177 return extension; |
| 178 } |
| 179 |
| 180 BOOL GPBResolveExtensionClassMethod(Class self, SEL sel) { |
| 181 // Another option would be to register the extensions with the class at |
| 182 // globallyRegisterExtension: |
| 183 // Timing the two solutions, this solution turned out to be much faster |
| 184 // and reduced startup time, and runtime memory. |
| 185 // The advantage to globallyRegisterExtension is that it would reduce the |
| 186 // size of the protos somewhat because the singletonNameC wouldn't need |
| 187 // to include the class name. For a class with a lot of extensions it |
| 188 // can add up. You could also significantly reduce the code complexity of this |
| 189 // file. |
| 190 id extension = ExtensionForName(self, sel); |
| 191 if (extension != nil) { |
| 192 const char *encoding = |
| 193 GPBMessageEncodingForSelector(@selector(getClassValue), NO); |
| 194 Class metaClass = objc_getMetaClass(class_getName(self)); |
| 195 IMP imp = imp_implementationWithBlock(^(id obj) { |
| 196 #pragma unused(obj) |
| 197 return extension; |
| 198 }); |
| 199 if (class_addMethod(metaClass, sel, imp, encoding)) { |
| 200 return YES; |
| 201 } |
| 202 } |
| 203 return NO; |
| 204 } |
| 205 |
| 206 |
| 207 + (BOOL)resolveClassMethod:(SEL)sel { |
| 208 if (GPBResolveExtensionClassMethod(self, sel)) { |
| 209 return YES; |
| 210 } |
| 211 return [super resolveClassMethod:sel]; |
| 212 } |
| 213 |
| 214 @end |
| OLD | NEW |