]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel-old.mm
objc4-551.1.tar.gz
[apple/objc4.git] / runtime / objc-sel-old.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 #if !__OBJC2__
32
33 #include "objc-private.h"
34 #include "objc-sel-set.h"
35
36 #if SUPPORT_PREOPT
37 #include <objc-shared-cache.h>
38 using namespace objc_opt;
39 static const objc_selopt_t *builtins = NULL;
40 #endif
41
42 __BEGIN_DECLS
43
44 static size_t SelrefCount = 0;
45
46 static const char *_objc_empty_selector = "";
47 static struct __objc_sel_set *_objc_selectors = NULL;
48
49
50 #if SUPPORT_PREOPT
51 void dump_builtins(void)
52 {
53 uint32_t occupied = builtins->occupied;
54 uint32_t capacity = builtins->capacity;
55
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",
60 builtins);
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);
67
68 const int32_t *offsets = builtins->offsets();
69 uint32_t i;
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",
74 i, offsets[i], str);
75 if ((const char *)sel_registerName(str) != str) {
76 _objc_fatal("bogus");
77 }
78 } else {
79 _objc_inform("BUILTIN SELECTORS: %6d: ", i);
80 }
81 }
82 }
83 #endif
84
85
86 static SEL _objc_search_builtins(const char *key)
87 {
88 #if defined(DUMP_SELECTORS)
89 if (NULL != key) printf("\t\"%s\",\n", key);
90 #endif
91
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;
96 #endif
97 if ('\0' == *key) return (SEL)_objc_empty_selector;
98
99 #if SUPPORT_PREOPT
100 assert(builtins);
101 return (SEL)builtins->get(key);
102 #endif
103
104 return (SEL)0;
105 }
106
107
108 const char *sel_getName(SEL sel) {
109 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
110 if ((uintptr_t)sel == kIgnore) return "<ignored selector>";
111 #endif
112 return sel ? (const char *)sel : "<null selector>";
113 }
114
115
116 BOOL sel_isMapped(SEL name)
117 {
118 SEL result;
119
120 if (!name) return NO;
121 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
122 if ((uintptr_t)name == kIgnore) return YES;
123 #endif
124
125 result = _objc_search_builtins((const char *)name);
126 if (result) return YES;
127
128 rwlock_read(&selLock);
129 if (_objc_selectors) {
130 result = __objc_sel_set_get(_objc_selectors, name);
131 }
132 rwlock_unlock_read(&selLock);
133 return result ? YES : NO;
134 }
135
136 static SEL __sel_registerName(const char *name, int lock, int copy)
137 {
138 SEL result = 0;
139
140 if (lock) rwlock_assert_unlocked(&selLock);
141 else rwlock_assert_writing(&selLock);
142
143 if (!name) return (SEL)0;
144 result = _objc_search_builtins(name);
145 if (result) return result;
146
147 if (lock) rwlock_read(&selLock);
148 if (_objc_selectors) {
149 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
150 }
151 if (lock) rwlock_unlock_read(&selLock);
152 if (result) return result;
153
154 // No match. Insert.
155
156 if (lock) rwlock_write(&selLock);
157
158 if (!_objc_selectors) {
159 _objc_selectors = __objc_sel_set_create(SelrefCount);
160 }
161 if (lock) {
162 // Rescan in case it was added while we dropped the lock
163 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
164 }
165 if (!result) {
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);
170 #endif
171 }
172
173 if (lock) rwlock_unlock_write(&selLock);
174 return result;
175 }
176
177
178 SEL sel_registerName(const char *name) {
179 return __sel_registerName(name, 1, 1); // YES lock, YES copy
180 }
181
182 SEL sel_registerNameNoLock(const char *name, BOOL copy) {
183 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
184 }
185
186 void sel_lock(void)
187 {
188 rwlock_write(&selLock);
189 }
190
191 void sel_unlock(void)
192 {
193 rwlock_unlock_write(&selLock);
194 }
195
196
197 // 2001/1/24
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
200 //
201 SEL sel_getUid(const char *name) {
202 return __sel_registerName(name, 2, 1); // YES lock, YES copy
203 }
204
205
206 BOOL sel_isEqual(SEL lhs, SEL rhs)
207 {
208 return (lhs == rhs) ? YES : NO;
209 }
210
211
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)
218 {
219 #if !SUPPORT_PREOPT
220
221 return NO;
222
223 #else
224
225 # if SUPPORT_IGNORED_SELECTOR_CONSTANT
226 // shared cache can't fix constant ignored selectors
227 if (UseGC) return NO;
228 # endif
229
230 // preoptimization disabled for some reason
231 if (!isPreoptimized()) return NO;
232
233 // image not from shared cache, or not fixed inside shared cache
234 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
235
236 return YES;
237
238 #endif
239 }
240
241
242 /***********************************************************************
243 * sel_init
244 * Initialize selector tables and register selectors used internally.
245 **********************************************************************/
246 void sel_init(BOOL wantsGC, size_t selrefCount)
247 {
248 // save this value for later
249 SelrefCount = selrefCount;
250
251 #if SUPPORT_PREOPT
252 builtins = preoptimizedSelectors();
253 #endif
254
255 // Register selectors used by libobjc
256
257 if (wantsGC) {
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.
261 }
262
263 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
264 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
265
266 sel_lock();
267
268 s(load);
269 s(initialize);
270 t(resolveInstanceMethod:, resolveInstanceMethod);
271 t(resolveClassMethod:, resolveClassMethod);
272 t(.cxx_construct, cxx_construct);
273 t(.cxx_destruct, cxx_destruct);
274 s(retain);
275 s(release);
276 s(autorelease);
277 s(retainCount);
278 s(alloc);
279 t(allocWithZone:, allocWithZone);
280 s(copy);
281 s(new);
282 s(finalize);
283 t(forwardInvocation:, forwardInvocation);
284
285 sel_unlock();
286
287 #undef s
288 #undef t
289 }
290
291 __END_DECLS
292
293 #endif