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)
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);
100 static SEL sel_alloc(const char *name, bool copy)
102 selLock.assertWriting();
103 return (SEL)(copy ? strdupIfMutable(name) : name);
107 const char *sel_getName(SEL sel)
109 if (!sel) return "<null selector>";
110 return (const char *)(const void*)sel;
114 BOOL sel_isMapped(SEL sel)
118 const char *name = (const char *)(void *)sel;
120 if (sel == search_builtins(name)) return YES;
122 rwlock_reader_t lock(selLock);
123 if (namedSelectors) {
124 return (sel == (SEL)NXMapGet(namedSelectors, name));
130 static SEL search_builtins(const char *name)
133 if (builtins) return (SEL)builtins->get(name);
139 static SEL __sel_registerName(const char *name, int lock, int copy)
143 if (lock) selLock.assertUnlocked();
144 else selLock.assertWriting();
146 if (!name) return (SEL)0;
148 result = search_builtins(name);
149 if (result) return result;
151 if (lock) selLock.read();
152 if (namedSelectors) {
153 result = (SEL)NXMapGet(namedSelectors, name);
155 if (lock) selLock.unlockRead();
156 if (result) return result;
160 if (lock) selLock.write();
162 if (!namedSelectors) {
163 namedSelectors = NXCreateMapTable(NXStrValueMapPrototype,
164 (unsigned)SelrefCount);
167 // Rescan in case it was added while we dropped the lock
168 result = (SEL)NXMapGet(namedSelectors, name);
171 result = sel_alloc(name, copy);
172 // fixme choose a better container (hash not map for starters)
173 NXMapInsert(namedSelectors, sel_getName(result), result);
176 if (lock) selLock.unlockWrite();
181 SEL sel_registerName(const char *name) {
182 return __sel_registerName(name, 1, 1); // YES lock, YES copy
185 SEL sel_registerNameNoLock(const char *name, bool copy) {
186 return __sel_registerName(name, 0, copy); // NO lock, maybe copy
194 void sel_unlock(void)
196 selLock.unlockWrite();
201 // the majority of uses of this function (which used to return NULL if not found)
202 // did not check for NULL, so, in fact, never return NULL
204 SEL sel_getUid(const char *name) {
205 return __sel_registerName(name, 2, 1); // YES lock, YES copy
209 BOOL sel_isEqual(SEL lhs, SEL rhs)
211 return bool(lhs == rhs);