2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
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
33 #include <objc/objc.h>
34 #include <CoreFoundation/CFSet.h>
35 #import "objc-private.h"
37 // NUM_BUILTIN_SELS, LG_NUM_BUILTIN_SELS, _objc_builtin_selectors
38 #include "objc-sel-table.h"
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.
44 static const char *_objc_empty_selector = "";
46 static SEL _objc_search_builtins(const char *key) {
47 int c, idx, lg = LG_NUM_BUILTIN_SELS;
50 #if defined(DUMP_SELECTORS)
51 if (NULL != key) printf("\t\"%s\",\n", key);
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;
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);
70 static OBJC_DECLARE_LOCK(_objc_selector_lock);
71 static CFMutableSetRef _objc_selectors = NULL;
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;
79 static CFHashCode _objc_hash_selector(const void *v) {
81 return (CFHashCode)_objc_strhash(v);
84 const char *sel_getName(SEL sel) {
85 return sel ? (const char *)sel : "<null selector>";
89 BOOL sel_isMapped(SEL name) {
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)) {
100 OBJC_UNLOCK(&_objc_selector_lock);
101 return ((SEL)0 != (SEL)result) ? YES : NO;
104 // CoreFoundation private API
105 extern void _CFSetSetCapacity(CFMutableSetRef set, CFIndex cap);
107 static SEL __sel_registerName(const char *name, int lockAndCopy) {
110 if (NULL == name) return (SEL)0;
111 result = _objc_search_builtins(name);
112 if ((SEL)0 != result) return result;
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);
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);
131 if (lockAndCopy) OBJC_UNLOCK(&_objc_selector_lock);
135 SEL sel_registerName(const char *name) {
136 return __sel_registerName(name, 1);
139 __private_extern__ SEL sel_registerNameNoCopyNoLock(const char *name) {
140 return __sel_registerName(name, 0);
143 __private_extern__ void sel_lock(void)
145 OBJC_LOCK(&_objc_selector_lock);
148 __private_extern__ void sel_unlock(void)
150 OBJC_UNLOCK(&_objc_selector_lock);
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
158 SEL sel_getUid(const char *name) {
159 return __sel_registerName(name, 2);