]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.m
objc4-371.2.tar.gz
[apple/objc4.git] / runtime / objc-sel.m
1 /*
2 * Copyright (c) 1999-2007 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 /*
25 * Utilities for registering and looking up selectors. The sole
26 * purpose of the selector tables is a registry whereby there is
27 * exactly one address (selector) associated with a given string
28 * (method name).
29 */
30
31 #include <objc/objc.h>
32 #import "objc-private.h"
33 #import "objc-auto.h"
34 #import "objc-rtp.h"
35 #import "objc-sel-set.h"
36
37 // _objc_builtin_selectors[]
38 #include "objc-sel-table.h"
39
40 // from phash.c
41 extern uint32_t phash(const char *key, int len);
42
43 #define NUM_NONBUILTIN_SELS 3500
44 // objc_sel_set grows at 3571, 5778, 9349.
45 // Most apps use 2000..7000 extra sels. Most apps will grow zero to two times.
46
47 static const char *_objc_empty_selector = "";
48 static OBJC_DECLARE_LOCK(_objc_selector_lock);
49 static struct __objc_sel_set *_objc_selectors = NULL;
50
51
52 static inline int ignore_selector(const char *sel)
53 {
54 // force retain/release/autorelease to be a constant value when GC is on
55 // note that the selectors for "Protocol" are registered before we can
56 // see the executable image header that sets _WantsGC, so we can't cache
57 // this result (sigh).
58 return (UseGC &&
59 ( (sel[0] == 'r' && sel[1] == 'e' &&
60 (_objc_strcmp(&sel[2], "lease") == 0 ||
61 _objc_strcmp(&sel[2], "tain") == 0 ||
62 _objc_strcmp(&sel[2], "tainCount") == 0 ))
63 ||
64 (_objc_strcmp(sel, "dealloc") == 0)
65 ||
66 (sel[0] == 'a' && sel[1] == 'u' &&
67 _objc_strcmp(&sel[2], "torelease") == 0)));
68 }
69
70
71 static SEL _objc_search_builtins(const char *key) {
72 const char *sel;
73 uint32_t hash;
74
75 #if defined(DUMP_SELECTORS)
76 if (NULL != key) printf("\t\"%s\",\n", key);
77 #endif
78
79 /* The builtin table contains only sels starting with '[.A-z]', including '_' */
80 if (!key) return (SEL)0;
81 if ((intptr_t)key == kIgnore) return (SEL)kIgnore;
82 if ('\0' == *key) return (SEL)_objc_empty_selector;
83 if ((*key < 'A' || 'z' < *key) && *key != '.') return (SEL)0;
84 if (ignore_selector(key)) return (SEL)kIgnore;
85
86 hash = phash(key, (int)__builtin_strlen(key));
87 sel = _objc_builtin_selectors[hash];
88 if (sel && 0 == __builtin_strcmp(key, sel)) return (SEL)sel;
89 else return (SEL)0;
90 }
91
92
93 const char *sel_getName(SEL sel) {
94 if ((intptr_t)sel == kIgnore) return "<ignored selector>";
95 return sel ? (const char *)sel : "<null selector>";
96 }
97
98
99 BOOL sel_isMapped(SEL name) {
100 SEL result;
101
102 if (NULL == name) return NO;
103 result = _objc_search_builtins((const char *)name);
104 if ((SEL)0 != result) return YES;
105 OBJC_LOCK(&_objc_selector_lock);
106 if (_objc_selectors) {
107 result = __objc_sel_set_get(_objc_selectors, name);
108 }
109 OBJC_UNLOCK(&_objc_selector_lock);
110 return ((SEL)0 != (SEL)result) ? YES : NO;
111 }
112
113 static SEL __sel_registerName(const char *name, int lock, int copy) {
114 SEL result = 0;
115
116 if (NULL == name) return (SEL)0;
117 result = _objc_search_builtins(name);
118 if (result != NULL) return result;
119
120 if (lock) OBJC_LOCK(&_objc_selector_lock);
121
122 if (_objc_selectors) {
123 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
124 }
125 if (result == NULL) {
126 if (!_objc_selectors) {
127 _objc_selectors = __objc_sel_set_create(NUM_NONBUILTIN_SELS);
128 }
129 result = (SEL)(copy ? _strdup_internal(name) : name);
130 __objc_sel_set_add(_objc_selectors, result);
131 #if defined(DUMP_UNKNOWN_SELECTORS)
132 printf("\t\"%s\",\n", name);
133 #endif
134 }
135
136 if (lock) OBJC_UNLOCK(&_objc_selector_lock);
137 return result;
138 }
139
140
141 SEL sel_registerName(const char *name) {
142 return __sel_registerName(name, 1, 1); // YES lock, YES copy
143 }
144
145 __private_extern__ SEL sel_registerNameNoLock(const char *name, BOOL copy) {
146 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
147 }
148
149 __private_extern__ void sel_lock(void)
150 {
151 OBJC_LOCK(&_objc_selector_lock);
152 }
153
154 __private_extern__ void sel_unlock(void)
155 {
156 OBJC_UNLOCK(&_objc_selector_lock);
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 (lhs == rhs) ? YES : NO;
172 }