2 * Copyright (c) 1999-2007 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@
25 * Utilities for registering and looking up selectors. The sole
26 * purpose of the selector tables is a registry whereby there is
27 * exactly one address (selector) associated with a given string
32 #include "objc-private.h"
33 #include "objc-auto.h"
34 #include "objc-sel-set.h"
37 #include <objc-shared-cache.h>
38 using namespace objc_opt;
39 static const objc_selopt_t *builtins = NULL;
44 static size_t SelrefCount = 0;
46 static const char *_objc_empty_selector = "";
47 static struct __objc_sel_set *_objc_selectors = NULL;
51 void dump_builtins(void)
53 uint32_t occupied = builtins->occupied;
54 uint32_t capacity = builtins->capacity;
56 _objc_inform("BUILTIN SELECTORS: %d selectors", occupied);
57 _objc_inform("BUILTIN SELECTORS: %d/%d (%d%%) hash table occupancy",
58 occupied, capacity, (int)(occupied/(double)capacity * 100));
59 _objc_inform("BUILTIN SELECTORS: using __TEXT,__objc_selopt at %p",
61 _objc_inform("BUILTIN SELECTORS: capacity: %u", builtins->capacity);
62 _objc_inform("BUILTIN SELECTORS: occupied: %u", builtins->occupied);
63 _objc_inform("BUILTIN SELECTORS: shift: %u", builtins->shift);
64 _objc_inform("BUILTIN SELECTORS: mask: 0x%x", builtins->mask);
65 _objc_inform("BUILTIN SELECTORS: zero: %u", builtins->zero);
66 _objc_inform("BUILTIN SELECTORS: salt: 0x%llx", builtins->salt);
68 const int32_t *offsets = builtins->offsets();
70 for (i = 0; i < capacity; i++) {
71 if (offsets[i] != offsetof(objc_stringhash_t, zero)) {
72 const char *str = (const char *)builtins + offsets[i];
73 _objc_inform("BUILTIN SELECTORS: %6d: %+8d %s",
75 if ((const char *)sel_registerName(str) != str) {
79 _objc_inform("BUILTIN SELECTORS: %6d: ", i);
86 static SEL _objc_search_builtins(const char *key)
88 #if defined(DUMP_SELECTORS)
89 if (NULL != key) printf("\t\"%s\",\n", key);
92 if (!key) return (SEL)0;
93 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
94 if ((uintptr_t)key == kIgnore) return (SEL)kIgnore;
95 if (ignoreSelectorNamed(key)) return (SEL)kIgnore;
97 if ('\0' == *key) return (SEL)_objc_empty_selector;
101 return (SEL)builtins->get(key);
108 const char *sel_getName(SEL sel) {
109 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
110 if ((uintptr_t)sel == kIgnore) return "<ignored selector>";
112 return sel ? (const char *)sel : "<null selector>";
116 BOOL sel_isMapped(SEL name)
120 if (!name) return NO;
121 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
122 if ((uintptr_t)name == kIgnore) return YES;
125 result = _objc_search_builtins((const char *)name);
126 if (result) return YES;
128 rwlock_read(&selLock);
129 if (_objc_selectors) {
130 result = __objc_sel_set_get(_objc_selectors, name);
132 rwlock_unlock_read(&selLock);
133 return result ? YES : NO;
136 static SEL __sel_registerName(const char *name, int lock, int copy)
140 if (lock) rwlock_assert_unlocked(&selLock);
141 else rwlock_assert_writing(&selLock);
143 if (!name) return (SEL)0;
144 result = _objc_search_builtins(name);
145 if (result) return result;
147 if (lock) rwlock_read(&selLock);
148 if (_objc_selectors) {
149 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
151 if (lock) rwlock_unlock_read(&selLock);
152 if (result) return result;
156 if (lock) rwlock_write(&selLock);
158 if (!_objc_selectors) {
159 _objc_selectors = __objc_sel_set_create(SelrefCount);
162 // Rescan in case it was added while we dropped the lock
163 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
166 result = (SEL)(copy ? _strdup_internal(name) : name);
167 __objc_sel_set_add(_objc_selectors, result);
168 #if defined(DUMP_UNKNOWN_SELECTORS)
169 printf("\t\"%s\",\n", name);
173 if (lock) rwlock_unlock_write(&selLock);
178 SEL sel_registerName(const char *name) {
179 return __sel_registerName(name, 1, 1); // YES lock, YES copy
182 SEL sel_registerNameNoLock(const char *name, BOOL copy) {
183 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
188 rwlock_write(&selLock);
191 void sel_unlock(void)
193 rwlock_unlock_write(&selLock);
198 // the majority of uses of this function (which used to return NULL if not found)
199 // did not check for NULL, so, in fact, never return NULL
201 SEL sel_getUid(const char *name) {
202 return __sel_registerName(name, 2, 1); // YES lock, YES copy
206 BOOL sel_isEqual(SEL lhs, SEL rhs)
208 return (lhs == rhs) ? YES : NO;
212 /***********************************************************************
213 * sel_preoptimizationValid
214 * Return YES if this image's selector fixups are valid courtesy
215 * of the dyld shared cache.
216 **********************************************************************/
217 BOOL sel_preoptimizationValid(const header_info *hi)
225 # if SUPPORT_IGNORED_SELECTOR_CONSTANT
226 // shared cache can't fix constant ignored selectors
227 if (UseGC) return NO;
230 // preoptimization disabled for some reason
231 if (!isPreoptimized()) return NO;
233 // image not from shared cache, or not fixed inside shared cache
234 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
242 /***********************************************************************
244 * Initialize selector tables and register selectors used internally.
245 **********************************************************************/
246 void sel_init(BOOL wantsGC, size_t selrefCount)
248 // save this value for later
249 SelrefCount = selrefCount;
252 builtins = preoptimizedSelectors();
255 // Register selectors used by libobjc
258 // Registering retain/release/autorelease requires GC decision first.
259 // sel_init doesn't actually need the wantsGC parameter, it just
260 // helps enforce the initialization order.
263 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
264 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
270 t(resolveInstanceMethod:, resolveInstanceMethod);
271 t(resolveClassMethod:, resolveClassMethod);
272 t(.cxx_construct, cxx_construct);
273 t(.cxx_destruct, cxx_destruct);
279 t(allocWithZone:, allocWithZone);
283 t(forwardInvocation:, forwardInvocation);