]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
objc4-779.1.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 #include "DenseMapExtras.h"
29
30 #if SUPPORT_PREOPT
31 static const objc_selopt_t *builtins = NULL;
32 static bool useDyldSelectorLookup = false;
33 #endif
34
35
36 static objc::ExplicitInitDenseSet<const char *> namedSelectors;
37 static SEL search_builtins(const char *key);
38
39
40 /***********************************************************************
41 * sel_init
42 * Initialize selector tables and register selectors used internally.
43 **********************************************************************/
44 void sel_init(size_t selrefCount)
45 {
46 #if SUPPORT_PREOPT
47 // If dyld finds a known shared cache selector, then it must be also looking
48 // in the shared cache table.
49 if (_dyld_get_objc_selector("retain") != nil)
50 useDyldSelectorLookup = true;
51 else
52 builtins = preoptimizedSelectors();
53
54 if (PrintPreopt && useDyldSelectorLookup) {
55 _objc_inform("PREOPTIMIZATION: using dyld selector opt");
56 }
57
58 if (PrintPreopt && builtins) {
59 uint32_t occupied = builtins->occupied;
60 uint32_t capacity = builtins->capacity;
61
62 _objc_inform("PREOPTIMIZATION: using selopt at %p", builtins);
63 _objc_inform("PREOPTIMIZATION: %u selectors", occupied);
64 _objc_inform("PREOPTIMIZATION: %u/%u (%u%%) hash table occupancy",
65 occupied, capacity,
66 (unsigned)(occupied/(double)capacity*100));
67 }
68 namedSelectors.init(useDyldSelectorLookup ? 0 : (unsigned)selrefCount);
69 #else
70 namedSelectors.init((unsigned)selrefCount);
71 #endif
72
73 // Register selectors used by libobjc
74
75 mutex_locker_t lock(selLock);
76
77 SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO);
78 SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO);
79 }
80
81
82 static SEL sel_alloc(const char *name, bool copy)
83 {
84 selLock.assertLocked();
85 return (SEL)(copy ? strdupIfMutable(name) : name);
86 }
87
88
89 const char *sel_getName(SEL sel)
90 {
91 if (!sel) return "<null selector>";
92 return (const char *)(const void*)sel;
93 }
94
95
96 BOOL sel_isMapped(SEL sel)
97 {
98 if (!sel) return NO;
99
100 const char *name = (const char *)(void *)sel;
101
102 if (sel == search_builtins(name)) return YES;
103
104 mutex_locker_t lock(selLock);
105 auto it = namedSelectors.get().find(name);
106 return it != namedSelectors.get().end() && (SEL)*it == sel;
107 }
108
109
110 static SEL search_builtins(const char *name)
111 {
112 #if SUPPORT_PREOPT
113 if (builtins) {
114 SEL result = 0;
115 if ((result = (SEL)builtins->get(name)))
116 return result;
117
118 if ((result = (SEL)_dyld_get_objc_selector(name)))
119 return result;
120 } else if (useDyldSelectorLookup) {
121 if (SEL result = (SEL)_dyld_get_objc_selector(name))
122 return result;
123 }
124 #endif
125 return nil;
126 }
127
128
129 static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
130 {
131 SEL result = 0;
132
133 if (shouldLock) selLock.assertUnlocked();
134 else selLock.assertLocked();
135
136 if (!name) return (SEL)0;
137
138 result = search_builtins(name);
139 if (result) return result;
140
141 conditional_mutex_locker_t lock(selLock, shouldLock);
142 auto it = namedSelectors.get().insert(name);
143 if (it.second) {
144 // No match. Insert.
145 *it.first = (const char *)sel_alloc(name, copy);
146 }
147 return (SEL)*it.first;
148 }
149
150
151 SEL sel_registerName(const char *name) {
152 return __sel_registerName(name, 1, 1); // YES lock, YES copy
153 }
154
155 SEL sel_registerNameNoLock(const char *name, bool copy) {
156 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
157 }
158
159
160 // 2001/1/24
161 // the majority of uses of this function (which used to return NULL if not found)
162 // did not check for NULL, so, in fact, never return NULL
163 //
164 SEL sel_getUid(const char *name) {
165 return __sel_registerName(name, 2, 1); // YES lock, YES copy
166 }
167
168
169 BOOL sel_isEqual(SEL lhs, SEL rhs)
170 {
171 return bool(lhs == rhs);
172 }
173
174
175 #endif