2 * Copyright (c) 2012 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@
26 #include "objc-private.h"
27 #include "objc-cache.h"
30 static const objc_selopt_t *builtins = NULL;
34 static size_t SelrefCount = 0;
36 static NXMapTable *namedSelectors;
38 static SEL search_builtins(const char *key);
41 /***********************************************************************
43 * Initialize selector tables and register selectors used internally.
44 **********************************************************************/
45 void sel_init(size_t selrefCount)
47 // save this value for later
48 SelrefCount = selrefCount;
51 builtins = preoptimizedSelectors();
53 if (PrintPreopt && builtins) {
54 uint32_t occupied = builtins->occupied;
55 uint32_t capacity = builtins->capacity;
57 _objc_inform("PREOPTIMIZATION: using selopt at %p", builtins);
58 _objc_inform("PREOPTIMIZATION: %u selectors", occupied);
59 _objc_inform("PREOPTIMIZATION: %u/%u (%u%%) hash table occupancy",
61 (unsigned)(occupied/(double)capacity*100));
65 // Register selectors used by libobjc
67 #define s(x) SEL_##x = sel_registerNameNoLock(#x, NO)
68 #define t(x,y) SEL_##y = sel_registerNameNoLock(#x, NO)
70 mutex_locker_t lock(selLock);
74 t(resolveInstanceMethod:, resolveInstanceMethod);
75 t(resolveClassMethod:, resolveClassMethod);
76 t(.cxx_construct, cxx_construct);
77 t(.cxx_destruct, cxx_destruct);
83 t(allocWithZone:, allocWithZone);
87 t(forwardInvocation:, forwardInvocation);
88 t(_tryRetain, tryRetain);
89 t(_isDeallocating, isDeallocating);
90 s(retainWeakReference);
91 s(allowsWeakReference);
98 static SEL sel_alloc(const char *name, bool copy)
100 selLock.assertLocked();
101 return (SEL)(copy ? strdupIfMutable(name) : name);
105 const char *sel_getName(SEL sel)
107 if (!sel) return "<null selector>";
108 return (const char *)(const void*)sel;
112 BOOL sel_isMapped(SEL sel)
116 const char *name = (const char *)(void *)sel;
118 if (sel == search_builtins(name)) return YES;
120 mutex_locker_t lock(selLock);
121 if (namedSelectors) {
122 return (sel == (SEL)NXMapGet(namedSelectors, name));
128 static SEL search_builtins(const char *name)
131 if (builtins) return (SEL)builtins->get(name);
137 static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
141 if (shouldLock) selLock.assertUnlocked();
142 else selLock.assertLocked();
144 if (!name) return (SEL)0;
146 result = search_builtins(name);
147 if (result) return result;
149 conditional_mutex_locker_t lock(selLock, shouldLock);
150 if (namedSelectors) {
151 result = (SEL)NXMapGet(namedSelectors, name);
153 if (result) return result;
157 if (!namedSelectors) {
158 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
159 (unsigned)SelrefCount);
162 result = sel_alloc(name, copy);
163 // fixme choose a better container (hash not map for starters)
164 NXMapInsert(namedSelectors, sel_getName(result), result);
171 SEL sel_registerName(const char *name) {
172 return __sel_registerName(name, 1, 1); // YES lock, YES copy
175 SEL sel_registerNameNoLock(const char *name, bool copy) {
176 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
181 // the majority of uses of this function (which used to return NULL if not found)
182 // did not check for NULL, so, in fact, never return NULL
184 SEL sel_getUid(const char *name) {
185 return __sel_registerName(name, 2, 1); // YES lock, YES copy
189 BOOL sel_isEqual(SEL lhs, SEL rhs)
191 return bool(lhs == rhs);