]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel.m
objc4-235.tar.gz
[apple/objc4.git] / runtime / objc-sel.m
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Utilities for registering and looking up selectors. The sole
28 * purpose of the selector tables is a registry whereby there is
29 * exactly one address (selector) associated with a given string
30 * (method name).
31 */
32
33 #include <objc/objc.h>
34 #include <CoreFoundation/CFSet.h>
35 #import "objc-private.h"
36
37 // NUM_BUILTIN_SELS, LG_NUM_BUILTIN_SELS, _objc_builtin_selectors
38 #include "objc-sel-table.h"
39
40 #define NUM_NONBUILTIN_SELS 3500
41 // Panther CFSet grows at 3571, 5778, 9349.
42 // Most apps use 2000..7000 extra sels. Most apps will grow zero to two times.
43
44 static const char *_objc_empty_selector = "";
45
46 static SEL _objc_search_builtins(const char *key) {
47 int c, idx, lg = LG_NUM_BUILTIN_SELS;
48 const char *s;
49
50 #if defined(DUMP_SELECTORS)
51 if (NULL != key) printf("\t\"%s\",\n", key);
52 #endif
53 /* The builtin table contains only sels starting with '[A-z]', including '_' */
54 if (!key) return (SEL)0;
55 if ('\0' == *key) return (SEL)_objc_empty_selector;
56 if (*key < 'A' || 'z' < *key) return (SEL)0;
57 s = _objc_builtin_selectors[-1 + (1 << lg)];
58 c = _objc_strcmp(s, key);
59 if (c == 0) return (SEL)s;
60 idx = (c < 0) ? NUM_BUILTIN_SELS - (1 << lg) : -1;
61 while (--lg >= 0) {
62 s = _objc_builtin_selectors[idx + (1 << lg)];
63 c = _objc_strcmp(s, key);
64 if (c == 0) return (SEL)s;
65 if (c < 0) idx += (1 << lg);
66 }
67 return (SEL)0;
68 }
69
70 static OBJC_DECLARE_LOCK(_objc_selector_lock);
71 static CFMutableSetRef _objc_selectors = NULL;
72
73 static Boolean _objc_equal_selector(const void *v1, const void *v2) {
74 if (v1 == v2) return TRUE;
75 if ((v1 == NULL) || (v2 == NULL)) return FALSE;
76 return _objc_strcmp((const unsigned char *)v1, (const unsigned char *)v2) == 0;
77 }
78
79 static CFHashCode _objc_hash_selector(const void *v) {
80 if (!v) return 0;
81 return (CFHashCode)_objc_strhash(v);
82 }
83
84 const char *sel_getName(SEL sel) {
85 return sel ? (const char *)sel : "<null selector>";
86 }
87
88
89 BOOL sel_isMapped(SEL name) {
90 SEL result;
91 const void *value;
92
93 if (NULL == name) return NO;
94 result = _objc_search_builtins((const char *)name);
95 if ((SEL)0 != result) return YES;
96 OBJC_LOCK(&_objc_selector_lock);
97 if (_objc_selectors && CFSetGetValueIfPresent(_objc_selectors, name, &value)) {
98 result = (SEL)value;
99 }
100 OBJC_UNLOCK(&_objc_selector_lock);
101 return ((SEL)0 != (SEL)result) ? YES : NO;
102 }
103
104 // CoreFoundation private API
105 extern void _CFSetSetCapacity(CFMutableSetRef set, CFIndex cap);
106
107 static SEL __sel_registerName(const char *name, int lockAndCopy) {
108 SEL result = 0;
109 const void *value;
110 if (NULL == name) return (SEL)0;
111 result = _objc_search_builtins(name);
112 if ((SEL)0 != result) return result;
113
114 if (lockAndCopy) OBJC_LOCK(&_objc_selector_lock);
115 if (!_objc_selectors || !CFSetGetValueIfPresent(_objc_selectors, name, &value)) {
116 if (!_objc_selectors) {
117 CFSetCallBacks cb = {0, NULL, NULL, NULL,
118 _objc_equal_selector, _objc_hash_selector};
119 _objc_selectors = CFSetCreateMutable(kCFAllocatorDefault, 0, &cb);
120 _CFSetSetCapacity(_objc_selectors, NUM_NONBUILTIN_SELS);
121 CFSetAddValue(_objc_selectors, (void *)NULL);
122 }
123 //if (lockAndCopy > 1) printf("registering %s for sel_getUid\n",name);
124 value = lockAndCopy ? strdup(name) : name;
125 CFSetAddValue(_objc_selectors, (void *)value);
126 #if defined(DUMP_UNKNOWN_SELECTORS)
127 printf("\t\"%s\",\n", value);
128 #endif
129 }
130 result = (SEL)value;
131 if (lockAndCopy) OBJC_UNLOCK(&_objc_selector_lock);
132 return result;
133 }
134
135 SEL sel_registerName(const char *name) {
136 return __sel_registerName(name, 1);
137 }
138
139 __private_extern__ SEL sel_registerNameNoCopyNoLock(const char *name) {
140 return __sel_registerName(name, 0);
141 }
142
143 __private_extern__ void sel_lock(void)
144 {
145 OBJC_LOCK(&_objc_selector_lock);
146 }
147
148 __private_extern__ void sel_unlock(void)
149 {
150 OBJC_UNLOCK(&_objc_selector_lock);
151 }
152
153
154 // 2001/1/24
155 // the majority of uses of this function (which used to return NULL if not found)
156 // did not check for NULL, so, in fact, never return NULL
157 //
158 SEL sel_getUid(const char *name) {
159 return __sel_registerName(name, 2);
160 }