]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
objc4-750.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 mutex_locker_t lock(selLock);
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 #undef s
94 #undef t
95 }
96
97
98 static SEL sel_alloc(const char *name, bool copy)
99 {
100 selLock.assertLocked();
101 return (SEL)(copy ? strdupIfMutable(name) : name);
102 }
103
104
105 const char *sel_getName(SEL sel)
106 {
107 if (!sel) return "<null selector>";
108 return (const char *)(const void*)sel;
109 }
110
111
112 BOOL sel_isMapped(SEL sel)
113 {
114 if (!sel) return NO;
115
116 const char *name = (const char *)(void *)sel;
117
118 if (sel == search_builtins(name)) return YES;
119
120 mutex_locker_t lock(selLock);
121 if (namedSelectors) {
122 return (sel == (SEL)NXMapGet(namedSelectors, name));
123 }
124 return false;
125 }
126
127
128 static SEL search_builtins(const char *name)
129 {
130 #if SUPPORT_PREOPT
131 if (builtins) return (SEL)builtins->get(name);
132 #endif
133 return nil;
134 }
135
136
137 static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
138 {
139 SEL result = 0;
140
141 if (shouldLock) selLock.assertUnlocked();
142 else selLock.assertLocked();
143
144 if (!name) return (SEL)0;
145
146 result = search_builtins(name);
147 if (result) return result;
148
149 conditional_mutex_locker_t lock(selLock, shouldLock);
150 if (namedSelectors) {
151 result = (SEL)NXMapGet(namedSelectors, name);
152 }
153 if (result) return result;
154
155 // No match. Insert.
156
157 if (!namedSelectors) {
158 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
159 (unsigned)SelrefCount);
160 }
161 if (!result) {
162 result = sel_alloc(name, copy);
163 // fixme choose a better container (hash not map for starters)
164 NXMapInsert(namedSelectors, sel_getName(result), result);
165 }
166
167 return result;
168 }
169
170
171 SEL sel_registerName(const char *name) {
172 return __sel_registerName(name, 1, 1); // YES lock, YES copy
173 }
174
175 SEL sel_registerNameNoLock(const char *name, bool copy) {
176 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
177 }
178
179
180 // 2001/1/24
181 // the majority of uses of this function (which used to return NULL if not found)
182 // did not check for NULL, so, in fact, never return NULL
183 //
184 SEL sel_getUid(const char *name) {
185 return __sel_registerName(name, 2, 1); // YES lock, YES copy
186 }
187
188
189 BOOL sel_isEqual(SEL lhs, SEL rhs)
190 {
191 return bool(lhs == rhs);
192 }
193
194
195 #endif