]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
31182f531a9b307e8834894c784887942b7857a1
[apple/objc4.git] / runtime / objc-sel.mm
1 /*
2 * Copyright (c) 2012 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 #if __OBJC2__
25
26 #include "objc-private.h"
27 #include "objc-cache.h"
28
29 #if SUPPORT_PREOPT
30 #include <objc-shared-cache.h>
31 using namespace objc_opt;
32 static const objc_selopt_t *builtins = NULL;
33 #endif
34
35 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
36 #error sorry
37 #endif
38
39
40 static size_t SelrefCount = 0;
41
42 static NXMapTable *namedSelectors;
43
44 static SEL search_builtins(const char *key);
45
46
47 /***********************************************************************
48 * sel_init
49 * Initialize selector tables and register selectors used internally.
50 **********************************************************************/
51 void sel_init(BOOL wantsGC, size_t selrefCount)
52 {
53 // save this value for later
54 SelrefCount = selrefCount;
55
56 #if SUPPORT_PREOPT
57 builtins = preoptimizedSelectors();
58 #endif
59
60 // Register selectors used by libobjc
61
62 if (wantsGC) {
63 // Registering retain/release/autorelease requires GC decision first.
64 // sel_init doesn't actually need the wantsGC parameter, it just
65 // helps enforce the initialization order.
66 }
67
68 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
69 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
70
71 sel_lock();
72
73 s(load);
74 s(initialize);
75 t(resolveInstanceMethod:, resolveInstanceMethod);
76 t(resolveClassMethod:, resolveClassMethod);
77 t(.cxx_construct, cxx_construct);
78 t(.cxx_destruct, cxx_destruct);
79 s(retain);
80 s(release);
81 s(autorelease);
82 s(retainCount);
83 s(alloc);
84 t(allocWithZone:, allocWithZone);
85 s(copy);
86 s(new);
87 s(finalize);
88 t(forwardInvocation:, forwardInvocation);
89
90 sel_unlock();
91
92 #undef s
93 #undef t
94 }
95
96
97 static SEL sel_alloc(const char *name, bool copy)
98 {
99 rwlock_assert_writing(&selLock);
100 return (SEL)(copy ? _strdup_internal(name) : name);
101 }
102
103
104 const char *sel_getName(SEL sel)
105 {
106 if (!sel) return "<null selector>";
107 return (const char *)(const void*)sel;
108 }
109
110
111 BOOL sel_isMapped(SEL sel)
112 {
113 if (!sel) return NO;
114
115 const char *name = (const char *)sel;
116
117 if (sel == search_builtins(name)) return YES;
118
119 rwlock_read(&selLock);
120 bool result = (sel == (SEL)NXMapGet(namedSelectors, name));
121 rwlock_unlock_read(&selLock);
122
123 return result;
124 }
125
126
127 static SEL search_builtins(const char *name)
128 {
129 #if SUPPORT_PREOPT
130 if (builtins) return (SEL)builtins->get(name);
131 #endif
132 return nil;
133 }
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
145 result = search_builtins(name);
146 if (result) return result;
147
148 if (lock) rwlock_read(&selLock);
149 if (namedSelectors) {
150 result = (SEL)NXMapGet(namedSelectors, name);
151 }
152 if (lock) rwlock_unlock_read(&selLock);
153 if (result) return result;
154
155 // No match. Insert.
156
157 if (lock) rwlock_write(&selLock);
158
159 if (!namedSelectors) {
160 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
161 (unsigned)SelrefCount);
162 }
163 if (lock) {
164 // Rescan in case it was added while we dropped the lock
165 result = (SEL)NXMapGet(namedSelectors, name);
166 }
167 if (!result) {
168 result = sel_alloc(name, copy);
169 // fixme choose a better container (hash not map for starters)
170 NXMapInsert(namedSelectors, sel_getName(result), result);
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 // preoptimization disabled for some reason
226 if (!isPreoptimized()) return NO;
227
228 // image not from shared cache, or not fixed inside shared cache
229 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
230
231 return YES;
232
233 #endif
234 }
235
236
237 #endif