]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
objc4-647.tar.gz
[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 static const objc_selopt_t *builtins = NULL;
31 #endif
32
33 #if SUPPORT_IGNORED_SELECTOR_CONSTANT
34 #error sorry
35 #endif
36
37
38 static size_t SelrefCount = 0;
39
40 static NXMapTable *namedSelectors;
41
42 static SEL search_builtins(const char *key);
43
44
45 /***********************************************************************
46 * sel_init
47 * Initialize selector tables and register selectors used internally.
48 **********************************************************************/
49 void sel_init(BOOL wantsGC, size_t selrefCount)
50 {
51 // save this value for later
52 SelrefCount = selrefCount;
53
54 #if SUPPORT_PREOPT
55 builtins = preoptimizedSelectors();
56
57 if (PrintPreopt && builtins) {
58 uint32_t occupied = builtins->occupied;
59 uint32_t capacity = builtins->capacity;
60
61 _objc_inform("PREOPTIMIZATION: using selopt at %p", builtins);
62 _objc_inform("PREOPTIMIZATION: %u selectors", occupied);
63 _objc_inform("PREOPTIMIZATION: %u/%u (%u%%) hash table occupancy",
64 occupied, capacity,
65 (unsigned)(occupied/(double)capacity*100));
66 }
67 #endif
68
69 // Register selectors used by libobjc
70
71 if (wantsGC) {
72 // Registering retain/release/autorelease requires GC decision first.
73 // sel_init doesn't actually need the wantsGC parameter, it just
74 // helps enforce the initialization order.
75 }
76
77 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
78 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
79
80 sel_lock();
81
82 s(load);
83 s(initialize);
84 t(resolveInstanceMethod:, resolveInstanceMethod);
85 t(resolveClassMethod:, resolveClassMethod);
86 t(.cxx_construct, cxx_construct);
87 t(.cxx_destruct, cxx_destruct);
88 s(retain);
89 s(release);
90 s(autorelease);
91 s(retainCount);
92 s(alloc);
93 t(allocWithZone:, allocWithZone);
94 s(dealloc);
95 s(copy);
96 s(new);
97 s(finalize);
98 t(forwardInvocation:, forwardInvocation);
99 t(_tryRetain, tryRetain);
100 t(_isDeallocating, isDeallocating);
101 s(retainWeakReference);
102 s(allowsWeakReference);
103
104 sel_unlock();
105
106 #undef s
107 #undef t
108 }
109
110
111 static SEL sel_alloc(const char *name, bool copy)
112 {
113 rwlock_assert_writing(&selLock);
114 return (SEL)(copy ? _strdup_internal(name) : name);
115 }
116
117
118 const char *sel_getName(SEL sel)
119 {
120 if (!sel) return "<null selector>";
121 return (const char *)(const void*)sel;
122 }
123
124
125 BOOL sel_isMapped(SEL sel)
126 {
127 if (!sel) return NO;
128
129 const char *name = (const char *)(void *)sel;
130
131 if (sel == search_builtins(name)) return YES;
132
133 bool result = false;
134 rwlock_read(&selLock);
135 if (namedSelectors) result = (sel == (SEL)NXMapGet(namedSelectors, name));
136 rwlock_unlock_read(&selLock);
137
138 return result;
139 }
140
141
142 static SEL search_builtins(const char *name)
143 {
144 #if SUPPORT_PREOPT
145 if (builtins) return (SEL)builtins->get(name);
146 #endif
147 return nil;
148 }
149
150
151 static SEL __sel_registerName(const char *name, int lock, int copy)
152 {
153 SEL result = 0;
154
155 if (lock) rwlock_assert_unlocked(&selLock);
156 else rwlock_assert_writing(&selLock);
157
158 if (!name) return (SEL)0;
159
160 result = search_builtins(name);
161 if (result) return result;
162
163 if (lock) rwlock_read(&selLock);
164 if (namedSelectors) {
165 result = (SEL)NXMapGet(namedSelectors, name);
166 }
167 if (lock) rwlock_unlock_read(&selLock);
168 if (result) return result;
169
170 // No match. Insert.
171
172 if (lock) rwlock_write(&selLock);
173
174 if (!namedSelectors) {
175 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
176 (unsigned)SelrefCount);
177 }
178 if (lock) {
179 // Rescan in case it was added while we dropped the lock
180 result = (SEL)NXMapGet(namedSelectors, name);
181 }
182 if (!result) {
183 result = sel_alloc(name, copy);
184 // fixme choose a better container (hash not map for starters)
185 NXMapInsert(namedSelectors, sel_getName(result), result);
186 }
187
188 if (lock) rwlock_unlock_write(&selLock);
189 return result;
190 }
191
192
193 SEL sel_registerName(const char *name) {
194 return __sel_registerName(name, 1, 1); // YES lock, YES copy
195 }
196
197 SEL sel_registerNameNoLock(const char *name, BOOL copy) {
198 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
199 }
200
201 void sel_lock(void)
202 {
203 rwlock_write(&selLock);
204 }
205
206 void sel_unlock(void)
207 {
208 rwlock_unlock_write(&selLock);
209 }
210
211
212 // 2001/1/24
213 // the majority of uses of this function (which used to return NULL if not found)
214 // did not check for NULL, so, in fact, never return NULL
215 //
216 SEL sel_getUid(const char *name) {
217 return __sel_registerName(name, 2, 1); // YES lock, YES copy
218 }
219
220
221 BOOL sel_isEqual(SEL lhs, SEL rhs)
222 {
223 return (lhs == rhs) ? YES : NO;
224 }
225
226
227 /***********************************************************************
228 * sel_preoptimizationValid
229 * Return YES if this image's selector fixups are valid courtesy
230 * of the dyld shared cache.
231 **********************************************************************/
232 BOOL sel_preoptimizationValid(const header_info *hi)
233 {
234 #if !SUPPORT_PREOPT
235
236 return NO;
237
238 #else
239
240 // preoptimization disabled for some reason
241 if (!isPreoptimized()) return NO;
242
243 // image not from shared cache, or not fixed inside shared cache
244 if (!_objcHeaderOptimizedByDyld(hi)) return NO;
245
246 return YES;
247
248 #endif
249 }
250
251
252 #endif