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