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