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