2 * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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
31 #include <objc/objc.h>
32 #import "objc-private.h"
35 #import "objc-sel-set.h"
37 // _objc_builtin_selectors[]
38 #include "objc-sel-table.h"
41 extern uint32_t phash(const char *key, int len);
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.
47 static const char *_objc_empty_selector = "";
48 static OBJC_DECLARE_LOCK(_objc_selector_lock);
49 static struct __objc_sel_set *_objc_selectors = NULL;
52 static inline int ignore_selector(const char *sel)
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).
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 ))
64 (_objc_strcmp(sel, "dealloc") == 0)
66 (sel[0] == 'a' && sel[1] == 'u' &&
67 _objc_strcmp(&sel[2], "torelease") == 0)));
71 static SEL _objc_search_builtins(const char *key) {
75 #if defined(DUMP_SELECTORS)
76 if (NULL != key) printf("\t\"%s\",\n", key);
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;
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;
93 const char *sel_getName(SEL sel) {
94 if ((intptr_t)sel == kIgnore) return "<ignored selector>";
95 return sel ? (const char *)sel : "<null selector>";
99 BOOL sel_isMapped(SEL name) {
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);
109 OBJC_UNLOCK(&_objc_selector_lock);
110 return ((SEL)0 != (SEL)result) ? YES : NO;
113 static SEL __sel_registerName(const char *name, int lock, int copy) {
116 if (NULL == name) return (SEL)0;
117 result = _objc_search_builtins(name);
118 if (result != NULL) return result;
120 if (lock) OBJC_LOCK(&_objc_selector_lock);
122 if (_objc_selectors) {
123 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
125 if (result == NULL) {
126 if (!_objc_selectors) {
127 _objc_selectors = __objc_sel_set_create(NUM_NONBUILTIN_SELS);
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);
136 if (lock) OBJC_UNLOCK(&_objc_selector_lock);
141 SEL sel_registerName(const char *name) {
142 return __sel_registerName(name, 1, 1); // YES lock, YES copy
145 __private_extern__ SEL sel_registerNameNoLock(const char *name, BOOL copy) {
146 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
149 __private_extern__ void sel_lock(void)
151 OBJC_LOCK(&_objc_selector_lock);
154 __private_extern__ void sel_unlock(void)
156 OBJC_UNLOCK(&_objc_selector_lock);
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
164 SEL sel_getUid(const char *name) {
165 return __sel_registerName(name, 2, 1); // YES lock, YES copy
169 BOOL sel_isEqual(SEL lhs, SEL rhs)
171 return (lhs == rhs) ? YES : NO;