]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel-old.mm
objc4-680.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 static const objc_selopt_t *builtins = NULL;
39 #endif
40
41 __BEGIN_DECLS
42
43 static size_t SelrefCount = 0;
44
45 static const char *_objc_empty_selector = "";
46 static struct __objc_sel_set *_objc_selectors = NULL;
47
48
49 static SEL _objc_search_builtins(const char *key)
50 {
51 #if defined(DUMP_SELECTORS)
52 if (NULL != key) printf("\t\"%s\",\n", key);
53 #endif
54
55 if (!key) return (SEL)0;
56 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
57 if ((uintptr_t)key == kIgnore) return (SEL)kIgnore;
58 if (ignoreSelectorNamed(key)) return (SEL)kIgnore;
59 #endif
60 if ('\0' == *key) return (SEL)_objc_empty_selector;
61
62 #if SUPPORT_PREOPT
63 if (builtins) return (SEL)builtins->get(key);
64 #endif
65
66 return (SEL)0;
67 }
68
69
70 const char *sel_getName(SEL sel) {
71 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
72 if ((uintptr_t)sel == kIgnore) return "<ignored selector>";
73 #endif
74 return sel ? (const char *)sel : "<null selector>";
75 }
76
77
78 BOOL sel_isMapped(SEL name)
79 {
80 SEL sel;
81
82 if (!name) return NO;
83 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
84 if ((uintptr_t)name == kIgnore) return YES;
85 #endif
86
87 sel = _objc_search_builtins((const char *)name);
88 if (sel) return YES;
89
90 rwlock_reader_t lock(selLock);
91 if (_objc_selectors) {
92 sel = __objc_sel_set_get(_objc_selectors, name);
93 }
94 return bool(sel);
95 }
96
97 static SEL __sel_registerName(const char *name, int lock, int copy)
98 {
99 SEL result = 0;
100
101 if (lock) selLock.assertUnlocked();
102 else selLock.assertWriting();
103
104 if (!name) return (SEL)0;
105 result = _objc_search_builtins(name);
106 if (result) return result;
107
108 if (lock) selLock.read();
109 if (_objc_selectors) {
110 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
111 }
112 if (lock) selLock.unlockRead();
113 if (result) return result;
114
115 // No match. Insert.
116
117 if (lock) selLock.write();
118
119 if (!_objc_selectors) {
120 _objc_selectors = __objc_sel_set_create(SelrefCount);
121 }
122 if (lock) {
123 // Rescan in case it was added while we dropped the lock
124 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
125 }
126 if (!result) {
127 result = (SEL)(copy ? strdup(name) : name);
128 __objc_sel_set_add(_objc_selectors, result);
129 #if defined(DUMP_UNKNOWN_SELECTORS)
130 printf("\t\"%s\",\n", name);
131 #endif
132 }
133
134 if (lock) selLock.unlockWrite();
135 return result;
136 }
137
138
139 SEL sel_registerName(const char *name) {
140 return __sel_registerName(name, 1, 1); // YES lock, YES copy
141 }
142
143 SEL sel_registerNameNoLock(const char *name, bool copy) {
144 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
145 }
146
147 void sel_lock(void)
148 {
149 selLock.write();
150 }
151
152 void sel_unlock(void)
153 {
154 selLock.unlockWrite();
155 }
156
157
158 // 2001/1/24
159 // the majority of uses of this function (which used to return NULL if not found)
160 // did not check for NULL, so, in fact, never return NULL
161 //
162 SEL sel_getUid(const char *name) {
163 return __sel_registerName(name, 2, 1); // YES lock, YES copy
164 }
165
166
167 BOOL sel_isEqual(SEL lhs, SEL rhs)
168 {
169 return bool(lhs == rhs);
170 }
171
172
173 /***********************************************************************
174 * sel_init
175 * Initialize selector tables and register selectors used internally.
176 **********************************************************************/
177 void sel_init(bool wantsGC, size_t selrefCount)
178 {
179 // save this value for later
180 SelrefCount = selrefCount;
181
182 #if SUPPORT_PREOPT
183 builtins = preoptimizedSelectors();
184 #endif
185
186 // Register selectors used by libobjc
187
188 if (wantsGC) {
189 // Registering retain/release/autorelease requires GC decision first.
190 // sel_init doesn't actually need the wantsGC parameter, it just
191 // helps enforce the initialization order.
192 }
193
194 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
195 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
196
197 sel_lock();
198
199 s(load);
200 s(initialize);
201 t(resolveInstanceMethod:, resolveInstanceMethod);
202 t(resolveClassMethod:, resolveClassMethod);
203 t(.cxx_construct, cxx_construct);
204 t(.cxx_destruct, cxx_destruct);
205 s(retain);
206 s(release);
207 s(autorelease);
208 s(retainCount);
209 s(alloc);
210 t(allocWithZone:, allocWithZone);
211 s(dealloc);
212 s(copy);
213 s(new);
214 s(finalize);
215 t(forwardInvocation:, forwardInvocation);
216 t(_tryRetain, tryRetain);
217 t(_isDeallocating, isDeallocating);
218 s(retainWeakReference);
219 s(allowsWeakReference);
220
221 extern SEL FwdSel;
222 FwdSel = sel_registerNameNoLock("forward::", NO);
223
224 sel_unlock();
225
226 #undef s
227 #undef t
228 }
229
230 __END_DECLS
231
232 #endif