2 * Copyright (c) 2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include "objc-private.h"
27 #include "objc-cache.h"
30 static const objc_selopt_t *builtins = NULL;
33 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
38 static size_t SelrefCount = 0;
40 static NXMapTable *namedSelectors;
42 static SEL search_builtins(const char *key);
45 /***********************************************************************
47 * Initialize selector tables and register selectors used internally.
48 **********************************************************************/
49 void sel_init(BOOL wantsGC, size_t selrefCount)
51 // save this value for later
52 SelrefCount = selrefCount;
55 builtins = preoptimizedSelectors();
57 if (PrintPreopt && builtins) {
58 uint32_t occupied = builtins->occupied;
59 uint32_t capacity = builtins->capacity;
61 _objc_inform("PREOPTIMIZATION: using selopt at %p", builtins);
62 _objc_inform("PREOPTIMIZATION: %u selectors", occupied);
63 _objc_inform("PREOPTIMIZATION: %u/%u (%u%%) hash table occupancy",
65 (unsigned)(occupied/(double)capacity*100));
69 // Register selectors used by libobjc
72 // Registering retain/release/autorelease requires GC decision first.
73 // sel_init doesn't actually need the wantsGC parameter, it just
74 // helps enforce the initialization order.
77 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
78 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
84 t(resolveInstanceMethod:, resolveInstanceMethod);
85 t(resolveClassMethod:, resolveClassMethod);
86 t(.cxx_construct, cxx_construct);
87 t(.cxx_destruct, cxx_destruct);
93 t(allocWithZone:, allocWithZone);
98 t(forwardInvocation:, forwardInvocation);
99 t(_tryRetain, tryRetain);
100 t(_isDeallocating, isDeallocating);
101 s(retainWeakReference);
102 s(allowsWeakReference);
111 static SEL sel_alloc(const char *name, bool copy)
113 rwlock_assert_writing(&selLock);
114 return (SEL)(copy ? _strdup_internal(name) : name);
118 const char *sel_getName(SEL sel)
120 if (!sel) return "<null selector>";
121 return (const char *)(const void*)sel;
125 BOOL sel_isMapped(SEL sel)
129 const char *name = (const char *)(void *)sel;
131 if (sel == search_builtins(name)) return YES;
134 rwlock_read(&selLock);
135 if (namedSelectors) result = (sel == (SEL)NXMapGet(namedSelectors, name));
136 rwlock_unlock_read(&selLock);
142 static SEL search_builtins(const char *name)
145 if (builtins) return (SEL)builtins->get(name);
151 static SEL __sel_registerName(const char *name, int lock, int copy)
155 if (lock) rwlock_assert_unlocked(&selLock);
156 else rwlock_assert_writing(&selLock);
158 if (!name) return (SEL)0;
160 result = search_builtins(name);
161 if (result) return result;
163 if (lock) rwlock_read(&selLock);
164 if (namedSelectors) {
165 result = (SEL)NXMapGet(namedSelectors, name);
167 if (lock) rwlock_unlock_read(&selLock);
168 if (result) return result;
172 if (lock) rwlock_write(&selLock);
174 if (!namedSelectors) {
175 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
176 (unsigned)SelrefCount);
179 // Rescan in case it was added while we dropped the lock
180 result = (SEL)NXMapGet(namedSelectors, name);
183 result = sel_alloc(name, copy);
184 // fixme choose a better container (hash not map for starters)
185 NXMapInsert(namedSelectors, sel_getName(result), result);
188 if (lock) rwlock_unlock_write(&selLock);
193 SEL sel_registerName(const char *name) {
194 return __sel_registerName(name, 1, 1); // YES lock, YES copy
197 SEL sel_registerNameNoLock(const char *name, BOOL copy) {
198 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
203 rwlock_write(&selLock);
206 void sel_unlock(void)
208 rwlock_unlock_write(&selLock);
213 // the majority of uses of this function (which used to return NULL if not found)
214 // did not check for NULL, so, in fact, never return NULL
216 SEL sel_getUid(const char *name) {
217 return __sel_registerName(name, 2, 1); // YES lock, YES copy
221 BOOL sel_isEqual(SEL lhs, SEL rhs)
223 return (lhs == rhs) ? YES : NO;
227 /***********************************************************************
228 * sel_preoptimizationValid
229 * Return YES if this image's selector fixups are valid courtesy
230 * of the dyld shared cache.
231 **********************************************************************/
232 BOOL sel_preoptimizationValid(const header_info *hi)
240 // preoptimization disabled for some reason
241 if (!isPreoptimized()) return NO;
243 // image not from shared cache, or not fixed inside shared cache
244 if (!_objcHeaderOptimizedByDyld(hi)) return NO;