]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.mm
objc4-787.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
31 static objc::ExplicitInitDenseSet<const char *> namedSelectors;
32 static SEL search_builtins(const char *key);
33
34
35 /***********************************************************************
36 * sel_init
37 * Initialize selector tables and register selectors used internally.
38 **********************************************************************/
39 void sel_init(size_t selrefCount)
40 {
41 #if SUPPORT_PREOPT
42 if (PrintPreopt) {
43 _objc_inform("PREOPTIMIZATION: using dyld selector opt");
44 }
45 #endif
46
47 namedSelectors.init((unsigned)selrefCount);
48
49 // Register selectors used by libobjc
50
51 mutex_locker_t lock(selLock);
52
53 SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO);
54 SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO);
55 }
56
57
58 static SEL sel_alloc(const char *name, bool copy)
59 {
60 selLock.assertLocked();
61 return (SEL)(copy ? strdupIfMutable(name) : name);
62 }
63
64
65 const char *sel_getName(SEL sel)
66 {
67 if (!sel) return "<null selector>";
68 return (const char *)(const void*)sel;
69 }
70
71
72 BOOL sel_isMapped(SEL sel)
73 {
74 if (!sel) return NO;
75
76 const char *name = (const char *)(void *)sel;
77
78 if (sel == search_builtins(name)) return YES;
79
80 mutex_locker_t lock(selLock);
81 auto it = namedSelectors.get().find(name);
82 return it != namedSelectors.get().end() && (SEL)*it == sel;
83 }
84
85
86 static SEL search_builtins(const char *name)
87 {
88 #if SUPPORT_PREOPT
89 if (SEL result = (SEL)_dyld_get_objc_selector(name))
90 return result;
91 #endif
92 return nil;
93 }
94
95
96 static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
97 {
98 SEL result = 0;
99
100 if (shouldLock) selLock.assertUnlocked();
101 else selLock.assertLocked();
102
103 if (!name) return (SEL)0;
104
105 result = search_builtins(name);
106 if (result) return result;
107
108 conditional_mutex_locker_t lock(selLock, shouldLock);
109 auto it = namedSelectors.get().insert(name);
110 if (it.second) {
111 // No match. Insert.
112 *it.first = (const char *)sel_alloc(name, copy);
113 }
114 return (SEL)*it.first;
115 }
116
117
118 SEL sel_registerName(const char *name) {
119 return __sel_registerName(name, 1, 1); // YES lock, YES copy
120 }
121
122 SEL sel_registerNameNoLock(const char *name, bool copy) {
123 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
124 }
125
126
127 // 2001/1/24
128 // the majority of uses of this function (which used to return NULL if not found)
129 // did not check for NULL, so, in fact, never return NULL
130 //
131 SEL sel_getUid(const char *name) {
132 return __sel_registerName(name, 2, 1); // YES lock, YES copy
133 }
134
135
136 BOOL sel_isEqual(SEL lhs, SEL rhs)
137 {
138 return bool(lhs == rhs);
139 }
140
141
142 #endif