]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel-old.mm
objc4-787.1.tar.gz
[apple/objc4.git] / runtime / objc-sel-old.mm
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 #if !__OBJC2__
32
33 #include "objc-private.h"
34 #include "objc-sel-set.h"
35
36 __BEGIN_DECLS
37
38 static size_t SelrefCount = 0;
39
40 static const char *_objc_empty_selector = "";
41 static struct __objc_sel_set *_objc_selectors = NULL;
42
43
44 static SEL _objc_search_builtins(const char *key)
45 {
46 #if defined(DUMP_SELECTORS)
47 if (NULL != key) printf("\t\"%s\",\n", key);
48 #endif
49
50 if (!key) return (SEL)0;
51 if ('\0' == *key) return (SEL)_objc_empty_selector;
52
53 return (SEL)0;
54 }
55
56
57 const char *sel_getName(SEL sel) {
58 return sel ? (const char *)sel : "<null selector>";
59 }
60
61
62 BOOL sel_isMapped(SEL name)
63 {
64 SEL sel;
65
66 if (!name) return NO;
67
68 sel = _objc_search_builtins((const char *)name);
69 if (sel) return YES;
70
71 mutex_locker_t lock(selLock);
72 if (_objc_selectors) {
73 sel = __objc_sel_set_get(_objc_selectors, name);
74 }
75 return bool(sel);
76 }
77
78 static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
79 {
80 SEL result = 0;
81
82 if (shouldLock) selLock.assertUnlocked();
83 else selLock.assertLocked();
84
85 if (!name) return (SEL)0;
86 result = _objc_search_builtins(name);
87 if (result) return result;
88
89 conditional_mutex_locker_t lock(selLock, shouldLock);
90 if (_objc_selectors) {
91 result = __objc_sel_set_get(_objc_selectors, (SEL)name);
92 }
93 if (result) return result;
94
95 // No match. Insert.
96
97 if (!_objc_selectors) {
98 _objc_selectors = __objc_sel_set_create(SelrefCount);
99 }
100 if (!result) {
101 result = (SEL)(copy ? strdup(name) : name);
102 __objc_sel_set_add(_objc_selectors, result);
103 #if defined(DUMP_UNKNOWN_SELECTORS)
104 printf("\t\"%s\",\n", name);
105 #endif
106 }
107
108 return result;
109 }
110
111
112 SEL sel_registerName(const char *name) {
113 return __sel_registerName(name, 1, 1); // YES lock, YES copy
114 }
115
116 SEL sel_registerNameNoLock(const char *name, bool copy) {
117 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
118 }
119
120
121 // 2001/1/24
122 // the majority of uses of this function (which used to return NULL if not found)
123 // did not check for NULL, so, in fact, never return NULL
124 //
125 SEL sel_getUid(const char *name) {
126 return __sel_registerName(name, 2, 1); // YES lock, YES copy
127 }
128
129
130 BOOL sel_isEqual(SEL lhs, SEL rhs)
131 {
132 return bool(lhs == rhs);
133 }
134
135
136 /***********************************************************************
137 * sel_init
138 * Initialize selector tables and register selectors used internally.
139 **********************************************************************/
140 void sel_init(size_t selrefCount)
141 {
142 // save this value for later
143 SelrefCount = selrefCount;
144
145 // Register selectors used by libobjc
146
147 mutex_locker_t lock(selLock);
148
149 SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO);
150 SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO);
151
152 extern SEL FwdSel;
153 FwdSel = sel_registerNameNoLock("forward::", NO);
154 }
155
156 __END_DECLS
157
158 #endif