]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
objc4-493.11.tar.gz
[apple/objc4.git] / runtime / objc-sel.mm
1 /*
2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
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
28 * (method name).
29 */
30
31 #include "objc.h"
32 #include "objc-private.h"
33 #include "objc-auto.h"
34 #include "objc-sel-set.h"
35
36 #if SUPPORT_BUILTINS
37 #include "objc-selopt.h"
38 #endif
39
40 __BEGIN_DECLS
41
42 #if SUPPORT_BUILTINS
43 // opt: the actual opt used at runtime
44 // builtins: the actual selector table used at runtime
45 // _objc_opt_data: opt data possibly written by dyld
46 // empty_opt_data: empty data to use if dyld didn't cooperate or DisablePreopt
47 using namespace objc_opt;
48 static const objc_selopt_t *builtins = NULL;
49 static const objc_opt_t *opt = NULL;
50 static BOOL preoptimized;
51
52 extern const objc_opt_t _objc_opt_data; // in __TEXT, __objc_selopt
53 static const uint32_t empty_opt_data[] = OPT_INITIALIZER;
54 #endif
55
56
57 #define NUM_NONBUILTIN_SELS 3500
58 // objc_sel_set grows at 3571, 5778, 9349.
59 // Most apps use 2000..7000 extra sels. Most apps will grow zero to two times.
60
61 static const char *_objc_empty_selector = "";
62 static struct __objc_sel_set *_objc_selectors = NULL;
63
64
65 #if SUPPORT_BUILTINS
66 PRIVATE_EXTERN void dump_builtins(void)
67 {
68 uint32_t occupied = builtins->occupied;
69 uint32_t capacity = builtins->capacity;
70
71 _objc_inform("BUILTIN SELECTORS: %d selectors", occupied);
72 _objc_inform("BUILTIN SELECTORS: %d/%d (%d%%) hash table occupancy",
73 occupied, capacity, (int)(occupied/(double)capacity * 100));
74 _objc_inform("BUILTIN SELECTORS: using __TEXT,__objc_selopt at %p",
75 builtins);
76 _objc_inform("BUILTIN SELECTORS: capacity: %u", builtins->capacity);
77 _objc_inform("BUILTIN SELECTORS: occupied: %u", builtins->occupied);
78 _objc_inform("BUILTIN SELECTORS: shift: %u", builtins->shift);
79 _objc_inform("BUILTIN SELECTORS: mask: 0x%x", builtins->mask);
80 _objc_inform("BUILTIN SELECTORS: zero: %u", builtins->zero);
81 _objc_inform("BUILTIN SELECTORS: salt: 0x%llx", builtins->salt);
82
83 const int32_t *offsets = builtins->offsets();
84 uint32_t i;
85 for (i = 0; i < capacity; i++) {
86 if (offsets[i] != offsetof(objc_selopt_t, zero)) {
87 const char *str = (const char *)builtins + offsets[i];
88 _objc_inform("BUILTIN SELECTORS: %6d: %+8d %s",
89 i, offsets[i], str);
90 } else {
91 _objc_inform("BUILTIN SELECTORS: %6d: ", i);
92 }
93 }
94 }
95 #endif
96
97
98 static SEL _objc_search_builtins(const char *key)
99 {
100 #if defined(DUMP_SELECTORS)
101 if (NULL != key) printf("\t\"%s\",\n", key);
102 #endif
103
104 if (!key) return (SEL)0;
105 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
106 if ((uintptr_t)key == kIgnore) return (SEL)kIgnore;
107 if (ignoreSelectorNamed(key)) return (SEL)kIgnore;
108 #endif
109 if ('\0' == *key) return (SEL)_objc_empty_selector;
110
111 #if SUPPORT_BUILTINS
112 return (SEL)builtins->get(key);
113 #endif
114
115 return (SEL)0;
116 }
117
118
119 const char *sel_getName(SEL sel) {
120 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
121 if ((uintptr_t)sel == kIgnore) return "<ignored selector>";
122 #endif
123 return sel ? (const char *)sel : "<null selector>";
124 }
125
126
127 BOOL sel_isMapped(SEL name)
128 {
129 SEL result;
130
131 if (!name) return NO;
132 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
133 if ((uintptr_t)name == kIgnore) return YES;
134 #endif
135
136 result = _objc_search_builtins((const char *)name);
137 if (result) return YES;
138
139 rwlock_read(&selLock);
140 if (_objc_selectors) {
141 result = __objc_sel_set_get(_objc_selectors, name);
142 }
143 rwlock_unlock_read(&selLock);
144 return result ? YES : NO;
145 }
146
147 static SEL __sel_registerName(const char *name, int lock, int copy)
148 {
149 SEL result = 0;
150
151 if (lock) rwlock_assert_unlocked(&selLock);
152 else rwlock_assert_writing(&selLock);
153
154 if (!name) return (SEL)0;
155 result = _objc_search_builtins(name);
156 if (result) return result;
157
158 if (lock) rwlock_read(&selLock);
159 if (_objc_selectors) {
160 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
161 }
162 if (lock) rwlock_unlock_read(&selLock);
163 if (result) return result;
164
165 // No match. Insert.
166
167 if (lock) rwlock_write(&selLock);
168
169 if (!_objc_selectors) {
170 _objc_selectors = __objc_sel_set_create(NUM_NONBUILTIN_SELS);
171 }
172 if (lock) {
173 // Rescan in case it was added while we dropped the lock
174 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
175 }
176 if (!result) {
177 result = (SEL)(copy ? _strdup_internal(name) : name);
178 __objc_sel_set_add(_objc_selectors, result);
179 #if defined(DUMP_UNKNOWN_SELECTORS)
180 printf("\t\"%s\",\n", name);
181 #endif
182 }
183
184 if (lock) rwlock_unlock_write(&selLock);
185 return result;
186 }
187
188
189 SEL sel_registerName(const char *name) {
190 return __sel_registerName(name, 1, 1); // YES lock, YES copy
191 }
192
193 PRIVATE_EXTERN SEL sel_registerNameNoLock(const char *name, BOOL copy) {
194 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
195 }
196
197 PRIVATE_EXTERN void sel_lock(void)
198 {
199 rwlock_write(&selLock);
200 }
201
202 PRIVATE_EXTERN void sel_unlock(void)
203 {
204 rwlock_unlock_write(&selLock);
205 }
206
207
208 // 2001/1/24
209 // the majority of uses of this function (which used to return NULL if not found)
210 // did not check for NULL, so, in fact, never return NULL
211 //
212 SEL sel_getUid(const char *name) {
213 return __sel_registerName(name, 2, 1); // YES lock, YES copy
214 }
215
216
217 BOOL sel_isEqual(SEL lhs, SEL rhs)
218 {
219 return (lhs == rhs) ? YES : NO;
220 }
221
222
223 /***********************************************************************
224 * sel_preoptimizationValid
225 * Return YES if this image's selector fixups are valid courtesy
226 * of the dyld shared cache.
227 **********************************************************************/
228 PRIVATE_EXTERN BOOL sel_preoptimizationValid(const header_info *hi)
229 {
230 #if !SUPPORT_BUILTINS
231
232 return NO;
233
234 #else
235
236 # if SUPPORT_IGNORED_SELECTOR_CONSTANT
237 // shared cache can't fix constant ignored selectors
238 if (UseGC) return NO;
239 # endif
240
241 // preoptimization disabled for some reason
242 if (!preoptimized) return NO;
243
244 // image not from shared cache, or not fixed inside shared cache
245 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
246
247 return YES;
248
249 #endif
250 }
251
252
253 /***********************************************************************
254 * sel_init
255 * Initialize selector tables and register selectors used internally.
256 **********************************************************************/
257 PRIVATE_EXTERN void sel_init(BOOL wantsGC)
258 {
259 #if !SUPPORT_BUILTINS
260
261 disableSharedCacheOptimizations();
262
263 #else
264 // not set at compile time in order to detect too-early selector operations
265 const char *failure = NULL;
266 opt = &_objc_opt_data;
267
268 if (DisablePreopt) {
269 // OBJC_DISABLE_PREOPTIMIZATION is set
270 // If opt->version != VERSION then you continue at your own risk.
271 failure = "(by OBJC_DISABLE_PREOPTIMIZATION)";
272 }
273 else if (opt->version != objc_opt::VERSION) {
274 // This shouldn't happen. You probably forgot to
275 // change OPT_INITIALIZER and objc-sel-table.s.
276 // If dyld really did write the wrong optimization version,
277 // then we must halt because we don't know what bits dyld twiddled.
278 _objc_fatal("bad objc opt version (want %d, got %d)",
279 objc_opt::VERSION, opt->version);
280 }
281 else if (!opt->selopt()) {
282 // No selector table. dyld must not have written one.
283 failure = "(dyld shared cache is absent or out of date)";
284 }
285 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
286 else if (UseGC) {
287 // GC is on, which renames some selectors
288 // Non-selector optimizations are still valid, but we don't have
289 // any of those yet
290 failure = "(GC is on)";
291 }
292 #endif
293
294 if (failure) {
295 // All preoptimized selector references are invalid.
296 preoptimized = NO;
297 opt = (objc_opt_t *)empty_opt_data;
298 builtins = opt->selopt();
299 disableSharedCacheOptimizations();
300
301 if (PrintPreopt) {
302 _objc_inform("PREOPTIMIZATION: is DISABLED %s", failure);
303 }
304 }
305 else {
306 // Valid optimization data written by dyld shared cache
307 preoptimized = YES;
308 builtins = opt->selopt();
309
310 if (PrintPreopt) {
311 _objc_inform("PREOPTIMIZATION: is ENABLED "
312 "(version %d)", opt->version);
313 }
314 }
315
316 #endif
317
318 // Register selectors used by libobjc
319
320 if (wantsGC) {
321 // Registering retain/release/autorelease requires GC decision first.
322 // sel_init doesn't actually need the wantsGC parameter, it just
323 // helps enforce the initialization order.
324 }
325
326 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
327 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
328
329 sel_lock();
330
331 s(load);
332 s(initialize);
333 t(resolveInstanceMethod:, resolveInstanceMethod);
334 t(resolveClassMethod:, resolveClassMethod);
335 t(.cxx_construct, cxx_construct);
336 t(.cxx_destruct, cxx_destruct);
337 s(retain);
338 s(release);
339 s(autorelease);
340 s(retainCount);
341 s(alloc);
342 s(copy);
343 s(new);
344 s(finalize);
345 t(forwardInvocation:, forwardInvocation);
346
347 sel_unlock();
348
349 #undef s
350 #undef t
351 }
352
353 __END_DECLS