1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
29*27b03b36SApple OSS Distributions
30*27b03b36SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*27b03b36SApple OSS Distributions
32*27b03b36SApple OSS Distributions #include <string.h>
33*27b03b36SApple OSS Distributions #include <sys/cdefs.h>
34*27b03b36SApple OSS Distributions
35*27b03b36SApple OSS Distributions #include <kern/locks.h>
36*27b03b36SApple OSS Distributions
37*27b03b36SApple OSS Distributions #include <libkern/c++/OSSymbol.h>
38*27b03b36SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
39*27b03b36SApple OSS Distributions #include <libkern/c++/OSLib.h>
40*27b03b36SApple OSS Distributions #include <os/cpp_util.h>
41*27b03b36SApple OSS Distributions #include <string.h>
42*27b03b36SApple OSS Distributions
43*27b03b36SApple OSS Distributions #define super OSString
44*27b03b36SApple OSS Distributions
45*27b03b36SApple OSS Distributions typedef struct { unsigned int i, j; } OSSymbolPoolState;
46*27b03b36SApple OSS Distributions
47*27b03b36SApple OSS Distributions #define INITIAL_POOL_SIZE ((unsigned int)((exp2ml(1 + log2(kInitBucketCount)))))
48*27b03b36SApple OSS Distributions
49*27b03b36SApple OSS Distributions #define GROW_FACTOR (1)
50*27b03b36SApple OSS Distributions #define SHRINK_FACTOR (3)
51*27b03b36SApple OSS Distributions
52*27b03b36SApple OSS Distributions #define GROW_POOL() do \
53*27b03b36SApple OSS Distributions if (count * GROW_FACTOR > nBuckets) { \
54*27b03b36SApple OSS Distributions reconstructSymbols(true); \
55*27b03b36SApple OSS Distributions } \
56*27b03b36SApple OSS Distributions while (0)
57*27b03b36SApple OSS Distributions
58*27b03b36SApple OSS Distributions #define SHRINK_POOL() do \
59*27b03b36SApple OSS Distributions if (count * SHRINK_FACTOR < nBuckets && \
60*27b03b36SApple OSS Distributions nBuckets > INITIAL_POOL_SIZE) { \
61*27b03b36SApple OSS Distributions reconstructSymbols(false); \
62*27b03b36SApple OSS Distributions } \
63*27b03b36SApple OSS Distributions while (0)
64*27b03b36SApple OSS Distributions
65*27b03b36SApple OSS Distributions class OSSymbolPool
66*27b03b36SApple OSS Distributions {
67*27b03b36SApple OSS Distributions private:
68*27b03b36SApple OSS Distributions static const unsigned int kInitBucketCount = 16;
69*27b03b36SApple OSS Distributions
70*27b03b36SApple OSS Distributions typedef struct { unsigned int count; OSSymbol **symbolP; } Bucket;
71*27b03b36SApple OSS Distributions
72*27b03b36SApple OSS Distributions Bucket *buckets;
73*27b03b36SApple OSS Distributions unsigned int nBuckets;
74*27b03b36SApple OSS Distributions unsigned int count;
75*27b03b36SApple OSS Distributions lck_rw_t *poolGate;
76*27b03b36SApple OSS Distributions
77*27b03b36SApple OSS Distributions static inline void
hashSymbol(const char * s,unsigned int * hashP,unsigned int * lenP)78*27b03b36SApple OSS Distributions hashSymbol(const char *s,
79*27b03b36SApple OSS Distributions unsigned int *hashP,
80*27b03b36SApple OSS Distributions unsigned int *lenP)
81*27b03b36SApple OSS Distributions {
82*27b03b36SApple OSS Distributions unsigned int hash = 0;
83*27b03b36SApple OSS Distributions unsigned int len = 0;
84*27b03b36SApple OSS Distributions
85*27b03b36SApple OSS Distributions /* Unroll the loop. */
86*27b03b36SApple OSS Distributions for (;;) {
87*27b03b36SApple OSS Distributions if (!*s) {
88*27b03b36SApple OSS Distributions break;
89*27b03b36SApple OSS Distributions }
90*27b03b36SApple OSS Distributions len++; hash ^= (unsigned int)(unsigned char) *s++;
91*27b03b36SApple OSS Distributions if (!*s) {
92*27b03b36SApple OSS Distributions break;
93*27b03b36SApple OSS Distributions }
94*27b03b36SApple OSS Distributions len++; hash ^= ((unsigned int)(unsigned char) *s++) << 8;
95*27b03b36SApple OSS Distributions if (!*s) {
96*27b03b36SApple OSS Distributions break;
97*27b03b36SApple OSS Distributions }
98*27b03b36SApple OSS Distributions len++; hash ^= ((unsigned int)(unsigned char) *s++) << 16;
99*27b03b36SApple OSS Distributions if (!*s) {
100*27b03b36SApple OSS Distributions break;
101*27b03b36SApple OSS Distributions }
102*27b03b36SApple OSS Distributions len++; hash ^= ((unsigned int)(unsigned char) *s++) << 24;
103*27b03b36SApple OSS Distributions }
104*27b03b36SApple OSS Distributions *lenP = len;
105*27b03b36SApple OSS Distributions *hashP = hash;
106*27b03b36SApple OSS Distributions }
107*27b03b36SApple OSS Distributions
108*27b03b36SApple OSS Distributions static unsigned long log2(unsigned int x);
109*27b03b36SApple OSS Distributions static unsigned long exp2ml(unsigned long x);
110*27b03b36SApple OSS Distributions
111*27b03b36SApple OSS Distributions void reconstructSymbols(void);
112*27b03b36SApple OSS Distributions void reconstructSymbols(bool grow);
113*27b03b36SApple OSS Distributions
114*27b03b36SApple OSS Distributions public:
115*27b03b36SApple OSS Distributions static void *operator new(size_t size);
116*27b03b36SApple OSS Distributions static void operator delete(void *mem, size_t size);
117*27b03b36SApple OSS Distributions
OSSymbolPool()118*27b03b36SApple OSS Distributions OSSymbolPool()
119*27b03b36SApple OSS Distributions {
120*27b03b36SApple OSS Distributions }
121*27b03b36SApple OSS Distributions OSSymbolPool(const OSSymbolPool *old);
122*27b03b36SApple OSS Distributions virtual
123*27b03b36SApple OSS Distributions ~OSSymbolPool();
124*27b03b36SApple OSS Distributions
125*27b03b36SApple OSS Distributions bool init();
126*27b03b36SApple OSS Distributions
127*27b03b36SApple OSS Distributions inline void
closeReadGate()128*27b03b36SApple OSS Distributions closeReadGate()
129*27b03b36SApple OSS Distributions {
130*27b03b36SApple OSS Distributions lck_rw_lock(poolGate, LCK_RW_TYPE_SHARED);
131*27b03b36SApple OSS Distributions }
132*27b03b36SApple OSS Distributions
133*27b03b36SApple OSS Distributions inline void
openReadGate()134*27b03b36SApple OSS Distributions openReadGate()
135*27b03b36SApple OSS Distributions {
136*27b03b36SApple OSS Distributions lck_rw_unlock(poolGate, LCK_RW_TYPE_SHARED);
137*27b03b36SApple OSS Distributions }
138*27b03b36SApple OSS Distributions
139*27b03b36SApple OSS Distributions
140*27b03b36SApple OSS Distributions inline void
closeWriteGate()141*27b03b36SApple OSS Distributions closeWriteGate()
142*27b03b36SApple OSS Distributions {
143*27b03b36SApple OSS Distributions lck_rw_lock(poolGate, LCK_RW_TYPE_EXCLUSIVE);
144*27b03b36SApple OSS Distributions }
145*27b03b36SApple OSS Distributions
146*27b03b36SApple OSS Distributions inline void
openWriteGate()147*27b03b36SApple OSS Distributions openWriteGate()
148*27b03b36SApple OSS Distributions {
149*27b03b36SApple OSS Distributions lck_rw_unlock(poolGate, LCK_RW_TYPE_EXCLUSIVE);
150*27b03b36SApple OSS Distributions }
151*27b03b36SApple OSS Distributions
152*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> findSymbol(const char *cString) const;
153*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> insertSymbol(OSSymbol *sym);
154*27b03b36SApple OSS Distributions void removeSymbol(OSSymbol *sym);
155*27b03b36SApple OSS Distributions
156*27b03b36SApple OSS Distributions OSSymbolPoolState initHashState();
157*27b03b36SApple OSS Distributions LIBKERN_RETURNS_NOT_RETAINED OSSymbol * nextHashState(OSSymbolPoolState *stateP);
158*27b03b36SApple OSS Distributions };
159*27b03b36SApple OSS Distributions
160*27b03b36SApple OSS Distributions void *
operator new(__assert_only size_t size)161*27b03b36SApple OSS Distributions OSSymbolPool::operator new(__assert_only size_t size)
162*27b03b36SApple OSS Distributions {
163*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(size);
164*27b03b36SApple OSS Distributions assert(size == sizeof(OSSymbolPool));
165*27b03b36SApple OSS Distributions
166*27b03b36SApple OSS Distributions return (void *)kalloc_type_tag(OSSymbolPool, Z_WAITOK_ZERO_NOFAIL,
167*27b03b36SApple OSS Distributions VM_KERN_MEMORY_LIBKERN);
168*27b03b36SApple OSS Distributions }
169*27b03b36SApple OSS Distributions
170*27b03b36SApple OSS Distributions void
operator delete(void * mem,__assert_only size_t size)171*27b03b36SApple OSS Distributions OSSymbolPool::operator delete(void *mem, __assert_only size_t size)
172*27b03b36SApple OSS Distributions {
173*27b03b36SApple OSS Distributions assert(size == sizeof(OSSymbolPool));
174*27b03b36SApple OSS Distributions kfree_type(OSSymbolPool, mem);
175*27b03b36SApple OSS Distributions }
176*27b03b36SApple OSS Distributions
177*27b03b36SApple OSS Distributions extern lck_grp_t *IOLockGroup;
178*27b03b36SApple OSS Distributions
179*27b03b36SApple OSS Distributions bool
init()180*27b03b36SApple OSS Distributions OSSymbolPool::init()
181*27b03b36SApple OSS Distributions {
182*27b03b36SApple OSS Distributions count = 0;
183*27b03b36SApple OSS Distributions nBuckets = INITIAL_POOL_SIZE;
184*27b03b36SApple OSS Distributions buckets = kalloc_type_tag(Bucket, nBuckets, Z_WAITOK_ZERO,
185*27b03b36SApple OSS Distributions VM_KERN_MEMORY_LIBKERN);
186*27b03b36SApple OSS Distributions if (!buckets) {
187*27b03b36SApple OSS Distributions return false;
188*27b03b36SApple OSS Distributions }
189*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(nBuckets * sizeof(Bucket));
190*27b03b36SApple OSS Distributions
191*27b03b36SApple OSS Distributions poolGate = lck_rw_alloc_init(IOLockGroup, LCK_ATTR_NULL);
192*27b03b36SApple OSS Distributions
193*27b03b36SApple OSS Distributions return poolGate != NULL;
194*27b03b36SApple OSS Distributions }
195*27b03b36SApple OSS Distributions
OSSymbolPool(const OSSymbolPool * old)196*27b03b36SApple OSS Distributions OSSymbolPool::OSSymbolPool(const OSSymbolPool *old)
197*27b03b36SApple OSS Distributions {
198*27b03b36SApple OSS Distributions count = old->count;
199*27b03b36SApple OSS Distributions nBuckets = old->nBuckets;
200*27b03b36SApple OSS Distributions buckets = old->buckets;
201*27b03b36SApple OSS Distributions
202*27b03b36SApple OSS Distributions poolGate = NULL; // Do not duplicate the poolGate
203*27b03b36SApple OSS Distributions }
204*27b03b36SApple OSS Distributions
~OSSymbolPool()205*27b03b36SApple OSS Distributions OSSymbolPool::~OSSymbolPool()
206*27b03b36SApple OSS Distributions {
207*27b03b36SApple OSS Distributions if (buckets) {
208*27b03b36SApple OSS Distributions Bucket *thisBucket;
209*27b03b36SApple OSS Distributions for (thisBucket = &buckets[0]; thisBucket < &buckets[nBuckets]; thisBucket++) {
210*27b03b36SApple OSS Distributions if (thisBucket->count > 1) {
211*27b03b36SApple OSS Distributions kfree_type(OSSymbol *, thisBucket->count, thisBucket->symbolP);
212*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(-(thisBucket->count * sizeof(OSSymbol *)));
213*27b03b36SApple OSS Distributions }
214*27b03b36SApple OSS Distributions }
215*27b03b36SApple OSS Distributions kfree_type(Bucket, nBuckets, buckets);
216*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(-(nBuckets * sizeof(Bucket)));
217*27b03b36SApple OSS Distributions }
218*27b03b36SApple OSS Distributions
219*27b03b36SApple OSS Distributions if (poolGate) {
220*27b03b36SApple OSS Distributions lck_rw_free(poolGate, IOLockGroup);
221*27b03b36SApple OSS Distributions }
222*27b03b36SApple OSS Distributions }
223*27b03b36SApple OSS Distributions
224*27b03b36SApple OSS Distributions unsigned long
log2(unsigned int x)225*27b03b36SApple OSS Distributions OSSymbolPool::log2(unsigned int x)
226*27b03b36SApple OSS Distributions {
227*27b03b36SApple OSS Distributions unsigned long i;
228*27b03b36SApple OSS Distributions
229*27b03b36SApple OSS Distributions for (i = 0; x > 1; i++) {
230*27b03b36SApple OSS Distributions x >>= 1;
231*27b03b36SApple OSS Distributions }
232*27b03b36SApple OSS Distributions return i;
233*27b03b36SApple OSS Distributions }
234*27b03b36SApple OSS Distributions
235*27b03b36SApple OSS Distributions unsigned long
exp2ml(unsigned long x)236*27b03b36SApple OSS Distributions OSSymbolPool::exp2ml(unsigned long x)
237*27b03b36SApple OSS Distributions {
238*27b03b36SApple OSS Distributions return (1 << x) - 1;
239*27b03b36SApple OSS Distributions }
240*27b03b36SApple OSS Distributions
241*27b03b36SApple OSS Distributions OSSymbolPoolState
initHashState()242*27b03b36SApple OSS Distributions OSSymbolPool::initHashState()
243*27b03b36SApple OSS Distributions {
244*27b03b36SApple OSS Distributions OSSymbolPoolState newState = { nBuckets, 0 };
245*27b03b36SApple OSS Distributions return newState;
246*27b03b36SApple OSS Distributions }
247*27b03b36SApple OSS Distributions
248*27b03b36SApple OSS Distributions OSSymbol *
nextHashState(OSSymbolPoolState * stateP)249*27b03b36SApple OSS Distributions OSSymbolPool::nextHashState(OSSymbolPoolState *stateP)
250*27b03b36SApple OSS Distributions {
251*27b03b36SApple OSS Distributions Bucket *thisBucket = &buckets[stateP->i];
252*27b03b36SApple OSS Distributions
253*27b03b36SApple OSS Distributions while (!stateP->j) {
254*27b03b36SApple OSS Distributions if (!stateP->i) {
255*27b03b36SApple OSS Distributions return NULL;
256*27b03b36SApple OSS Distributions }
257*27b03b36SApple OSS Distributions stateP->i--;
258*27b03b36SApple OSS Distributions thisBucket--;
259*27b03b36SApple OSS Distributions stateP->j = thisBucket->count;
260*27b03b36SApple OSS Distributions }
261*27b03b36SApple OSS Distributions
262*27b03b36SApple OSS Distributions stateP->j--;
263*27b03b36SApple OSS Distributions if (thisBucket->count == 1) {
264*27b03b36SApple OSS Distributions return (OSSymbol *) thisBucket->symbolP;
265*27b03b36SApple OSS Distributions } else {
266*27b03b36SApple OSS Distributions return thisBucket->symbolP[stateP->j];
267*27b03b36SApple OSS Distributions }
268*27b03b36SApple OSS Distributions }
269*27b03b36SApple OSS Distributions
270*27b03b36SApple OSS Distributions void
reconstructSymbols(void)271*27b03b36SApple OSS Distributions OSSymbolPool::reconstructSymbols(void)
272*27b03b36SApple OSS Distributions {
273*27b03b36SApple OSS Distributions this->reconstructSymbols(true);
274*27b03b36SApple OSS Distributions }
275*27b03b36SApple OSS Distributions
276*27b03b36SApple OSS Distributions void
reconstructSymbols(bool grow)277*27b03b36SApple OSS Distributions OSSymbolPool::reconstructSymbols(bool grow)
278*27b03b36SApple OSS Distributions {
279*27b03b36SApple OSS Distributions unsigned int new_nBuckets = nBuckets;
280*27b03b36SApple OSS Distributions OSSymbol *insert;
281*27b03b36SApple OSS Distributions OSSymbolPoolState state;
282*27b03b36SApple OSS Distributions
283*27b03b36SApple OSS Distributions if (grow) {
284*27b03b36SApple OSS Distributions new_nBuckets += new_nBuckets + 1;
285*27b03b36SApple OSS Distributions } else {
286*27b03b36SApple OSS Distributions /* Don't shrink the pool below the default initial size.
287*27b03b36SApple OSS Distributions */
288*27b03b36SApple OSS Distributions if (nBuckets <= INITIAL_POOL_SIZE) {
289*27b03b36SApple OSS Distributions return;
290*27b03b36SApple OSS Distributions }
291*27b03b36SApple OSS Distributions new_nBuckets = (new_nBuckets - 1) / 2;
292*27b03b36SApple OSS Distributions }
293*27b03b36SApple OSS Distributions
294*27b03b36SApple OSS Distributions /* Create old pool to iterate after doing above check, cause it
295*27b03b36SApple OSS Distributions * gets finalized at return.
296*27b03b36SApple OSS Distributions */
297*27b03b36SApple OSS Distributions OSSymbolPool old(this);
298*27b03b36SApple OSS Distributions
299*27b03b36SApple OSS Distributions count = 0;
300*27b03b36SApple OSS Distributions nBuckets = new_nBuckets;
301*27b03b36SApple OSS Distributions buckets = kalloc_type_tag(Bucket, nBuckets, Z_WAITOK_ZERO,
302*27b03b36SApple OSS Distributions VM_KERN_MEMORY_LIBKERN);
303*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(nBuckets * sizeof(Bucket));
304*27b03b36SApple OSS Distributions /* @@@ gvdl: Zero test and panic if can't set up pool */
305*27b03b36SApple OSS Distributions
306*27b03b36SApple OSS Distributions state = old.initHashState();
307*27b03b36SApple OSS Distributions while ((insert = old.nextHashState(&state))) {
308*27b03b36SApple OSS Distributions insertSymbol(insert);
309*27b03b36SApple OSS Distributions }
310*27b03b36SApple OSS Distributions }
311*27b03b36SApple OSS Distributions
312*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol>
findSymbol(const char * cString) const313*27b03b36SApple OSS Distributions OSSymbolPool::findSymbol(const char *cString) const
314*27b03b36SApple OSS Distributions {
315*27b03b36SApple OSS Distributions Bucket *thisBucket;
316*27b03b36SApple OSS Distributions unsigned int j, inLen, hash;
317*27b03b36SApple OSS Distributions OSSymbol *probeSymbol, **list;
318*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> ret;
319*27b03b36SApple OSS Distributions
320*27b03b36SApple OSS Distributions hashSymbol(cString, &hash, &inLen); inLen++;
321*27b03b36SApple OSS Distributions thisBucket = &buckets[hash % nBuckets];
322*27b03b36SApple OSS Distributions j = thisBucket->count;
323*27b03b36SApple OSS Distributions
324*27b03b36SApple OSS Distributions if (!j) {
325*27b03b36SApple OSS Distributions return NULL;
326*27b03b36SApple OSS Distributions }
327*27b03b36SApple OSS Distributions
328*27b03b36SApple OSS Distributions if (j == 1) {
329*27b03b36SApple OSS Distributions probeSymbol = (OSSymbol *) thisBucket->symbolP;
330*27b03b36SApple OSS Distributions
331*27b03b36SApple OSS Distributions if (inLen == probeSymbol->length
332*27b03b36SApple OSS Distributions && strncmp(probeSymbol->string, cString, probeSymbol->length) == 0
333*27b03b36SApple OSS Distributions && probeSymbol->taggedTryRetain(nullptr)) {
334*27b03b36SApple OSS Distributions ret.reset(probeSymbol, OSNoRetain);
335*27b03b36SApple OSS Distributions return ret;
336*27b03b36SApple OSS Distributions }
337*27b03b36SApple OSS Distributions return NULL;
338*27b03b36SApple OSS Distributions }
339*27b03b36SApple OSS Distributions
340*27b03b36SApple OSS Distributions for (list = thisBucket->symbolP; j--; list++) {
341*27b03b36SApple OSS Distributions probeSymbol = *list;
342*27b03b36SApple OSS Distributions if (inLen == probeSymbol->length
343*27b03b36SApple OSS Distributions && strncmp(probeSymbol->string, cString, probeSymbol->length) == 0
344*27b03b36SApple OSS Distributions && probeSymbol->taggedTryRetain(nullptr)) {
345*27b03b36SApple OSS Distributions ret.reset(probeSymbol, OSNoRetain);
346*27b03b36SApple OSS Distributions return ret;
347*27b03b36SApple OSS Distributions }
348*27b03b36SApple OSS Distributions }
349*27b03b36SApple OSS Distributions
350*27b03b36SApple OSS Distributions return NULL;
351*27b03b36SApple OSS Distributions }
352*27b03b36SApple OSS Distributions
353*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol>
insertSymbol(OSSymbol * sym)354*27b03b36SApple OSS Distributions OSSymbolPool::insertSymbol(OSSymbol *sym)
355*27b03b36SApple OSS Distributions {
356*27b03b36SApple OSS Distributions const char *cString = sym->string;
357*27b03b36SApple OSS Distributions Bucket *thisBucket;
358*27b03b36SApple OSS Distributions unsigned int j, inLen, hash;
359*27b03b36SApple OSS Distributions OSSymbol *probeSymbol, **list;
360*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> ret;
361*27b03b36SApple OSS Distributions
362*27b03b36SApple OSS Distributions hashSymbol(cString, &hash, &inLen); inLen++;
363*27b03b36SApple OSS Distributions thisBucket = &buckets[hash % nBuckets];
364*27b03b36SApple OSS Distributions j = thisBucket->count;
365*27b03b36SApple OSS Distributions
366*27b03b36SApple OSS Distributions if (!j) {
367*27b03b36SApple OSS Distributions thisBucket->symbolP = (OSSymbol **) sym;
368*27b03b36SApple OSS Distributions thisBucket->count++;
369*27b03b36SApple OSS Distributions count++;
370*27b03b36SApple OSS Distributions return nullptr;
371*27b03b36SApple OSS Distributions }
372*27b03b36SApple OSS Distributions
373*27b03b36SApple OSS Distributions if (j == 1) {
374*27b03b36SApple OSS Distributions probeSymbol = (OSSymbol *) thisBucket->symbolP;
375*27b03b36SApple OSS Distributions
376*27b03b36SApple OSS Distributions if (inLen == probeSymbol->length
377*27b03b36SApple OSS Distributions && strncmp(probeSymbol->string, cString, probeSymbol->length) == 0
378*27b03b36SApple OSS Distributions && probeSymbol->taggedTryRetain(nullptr)) {
379*27b03b36SApple OSS Distributions ret.reset(probeSymbol, OSNoRetain);
380*27b03b36SApple OSS Distributions return ret;
381*27b03b36SApple OSS Distributions }
382*27b03b36SApple OSS Distributions
383*27b03b36SApple OSS Distributions list = kalloc_type_tag(OSSymbol *, 2, Z_WAITOK_ZERO_NOFAIL,
384*27b03b36SApple OSS Distributions VM_KERN_MEMORY_LIBKERN);
385*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(2 * sizeof(OSSymbol *));
386*27b03b36SApple OSS Distributions /* @@@ gvdl: Zero test and panic if can't set up pool */
387*27b03b36SApple OSS Distributions list[0] = sym;
388*27b03b36SApple OSS Distributions list[1] = probeSymbol;
389*27b03b36SApple OSS Distributions thisBucket->symbolP = list;
390*27b03b36SApple OSS Distributions thisBucket->count++;
391*27b03b36SApple OSS Distributions count++;
392*27b03b36SApple OSS Distributions GROW_POOL();
393*27b03b36SApple OSS Distributions
394*27b03b36SApple OSS Distributions return nullptr;
395*27b03b36SApple OSS Distributions }
396*27b03b36SApple OSS Distributions
397*27b03b36SApple OSS Distributions for (list = thisBucket->symbolP; j--; list++) {
398*27b03b36SApple OSS Distributions probeSymbol = *list;
399*27b03b36SApple OSS Distributions if (inLen == probeSymbol->length
400*27b03b36SApple OSS Distributions && strncmp(probeSymbol->string, cString, probeSymbol->length) == 0
401*27b03b36SApple OSS Distributions && probeSymbol->taggedTryRetain(nullptr)) {
402*27b03b36SApple OSS Distributions ret.reset(probeSymbol, OSNoRetain);
403*27b03b36SApple OSS Distributions return ret;
404*27b03b36SApple OSS Distributions }
405*27b03b36SApple OSS Distributions }
406*27b03b36SApple OSS Distributions
407*27b03b36SApple OSS Distributions j = thisBucket->count++;
408*27b03b36SApple OSS Distributions count++;
409*27b03b36SApple OSS Distributions list = kalloc_type_tag(OSSymbol *, thisBucket->count, Z_WAITOK_ZERO,
410*27b03b36SApple OSS Distributions VM_KERN_MEMORY_LIBKERN);
411*27b03b36SApple OSS Distributions /* @@@ gvdl: Zero test and panic if can't set up pool */
412*27b03b36SApple OSS Distributions list[0] = sym;
413*27b03b36SApple OSS Distributions bcopy(thisBucket->symbolP, list + 1, j * sizeof(OSSymbol *));
414*27b03b36SApple OSS Distributions kfree_type(OSSymbol *, j, thisBucket->symbolP);
415*27b03b36SApple OSS Distributions
416*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE((thisBucket->count - j) * sizeof(OSSymbol *));
417*27b03b36SApple OSS Distributions thisBucket->symbolP = list;
418*27b03b36SApple OSS Distributions GROW_POOL();
419*27b03b36SApple OSS Distributions
420*27b03b36SApple OSS Distributions return nullptr;
421*27b03b36SApple OSS Distributions }
422*27b03b36SApple OSS Distributions
423*27b03b36SApple OSS Distributions void
removeSymbol(OSSymbol * sym)424*27b03b36SApple OSS Distributions OSSymbolPool::removeSymbol(OSSymbol *sym)
425*27b03b36SApple OSS Distributions {
426*27b03b36SApple OSS Distributions Bucket *thisBucket;
427*27b03b36SApple OSS Distributions unsigned int j, inLen, hash;
428*27b03b36SApple OSS Distributions OSSymbol *probeSymbol, **list;
429*27b03b36SApple OSS Distributions
430*27b03b36SApple OSS Distributions hashSymbol(sym->string, &hash, &inLen); inLen++;
431*27b03b36SApple OSS Distributions thisBucket = &buckets[hash % nBuckets];
432*27b03b36SApple OSS Distributions j = thisBucket->count;
433*27b03b36SApple OSS Distributions list = thisBucket->symbolP;
434*27b03b36SApple OSS Distributions
435*27b03b36SApple OSS Distributions if (!j) {
436*27b03b36SApple OSS Distributions // couldn't find the symbol; probably means string hash changed
437*27b03b36SApple OSS Distributions panic("removeSymbol %s count %d ", sym->string ? sym->string : "no string", count);
438*27b03b36SApple OSS Distributions return;
439*27b03b36SApple OSS Distributions }
440*27b03b36SApple OSS Distributions
441*27b03b36SApple OSS Distributions if (j == 1) {
442*27b03b36SApple OSS Distributions probeSymbol = (OSSymbol *) list;
443*27b03b36SApple OSS Distributions
444*27b03b36SApple OSS Distributions if (probeSymbol == sym) {
445*27b03b36SApple OSS Distributions thisBucket->symbolP = NULL;
446*27b03b36SApple OSS Distributions count--;
447*27b03b36SApple OSS Distributions thisBucket->count--;
448*27b03b36SApple OSS Distributions SHRINK_POOL();
449*27b03b36SApple OSS Distributions return;
450*27b03b36SApple OSS Distributions }
451*27b03b36SApple OSS Distributions // couldn't find the symbol; probably means string hash changed
452*27b03b36SApple OSS Distributions panic("removeSymbol %s count %d ", sym->string ? sym->string : "no string", count);
453*27b03b36SApple OSS Distributions return;
454*27b03b36SApple OSS Distributions }
455*27b03b36SApple OSS Distributions
456*27b03b36SApple OSS Distributions if (j == 2) {
457*27b03b36SApple OSS Distributions probeSymbol = list[0];
458*27b03b36SApple OSS Distributions if (probeSymbol == sym) {
459*27b03b36SApple OSS Distributions thisBucket->symbolP = (OSSymbol **) list[1];
460*27b03b36SApple OSS Distributions kfree_type(OSSymbol *, 2, list);
461*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(-(2 * sizeof(OSSymbol *)));
462*27b03b36SApple OSS Distributions count--;
463*27b03b36SApple OSS Distributions thisBucket->count--;
464*27b03b36SApple OSS Distributions SHRINK_POOL();
465*27b03b36SApple OSS Distributions return;
466*27b03b36SApple OSS Distributions }
467*27b03b36SApple OSS Distributions
468*27b03b36SApple OSS Distributions probeSymbol = list[1];
469*27b03b36SApple OSS Distributions if (probeSymbol == sym) {
470*27b03b36SApple OSS Distributions thisBucket->symbolP = (OSSymbol **) list[0];
471*27b03b36SApple OSS Distributions kfree_type(OSSymbol *, 2, list);
472*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(-(2 * sizeof(OSSymbol *)));
473*27b03b36SApple OSS Distributions count--;
474*27b03b36SApple OSS Distributions thisBucket->count--;
475*27b03b36SApple OSS Distributions SHRINK_POOL();
476*27b03b36SApple OSS Distributions return;
477*27b03b36SApple OSS Distributions }
478*27b03b36SApple OSS Distributions // couldn't find the symbol; probably means string hash changed
479*27b03b36SApple OSS Distributions panic("removeSymbol %s count %d ", sym->string ? sym->string : "no string", count);
480*27b03b36SApple OSS Distributions return;
481*27b03b36SApple OSS Distributions }
482*27b03b36SApple OSS Distributions
483*27b03b36SApple OSS Distributions for (; j--; list++) {
484*27b03b36SApple OSS Distributions probeSymbol = *list;
485*27b03b36SApple OSS Distributions if (probeSymbol == sym) {
486*27b03b36SApple OSS Distributions list = kalloc_type_tag(OSSymbol *, thisBucket->count - 1,
487*27b03b36SApple OSS Distributions Z_WAITOK, VM_KERN_MEMORY_LIBKERN);
488*27b03b36SApple OSS Distributions if (thisBucket->count - 1 != j) {
489*27b03b36SApple OSS Distributions bcopy(thisBucket->symbolP, list,
490*27b03b36SApple OSS Distributions (thisBucket->count - 1 - j) * sizeof(OSSymbol *));
491*27b03b36SApple OSS Distributions }
492*27b03b36SApple OSS Distributions if (j) {
493*27b03b36SApple OSS Distributions bcopy(thisBucket->symbolP + thisBucket->count - j,
494*27b03b36SApple OSS Distributions list + thisBucket->count - 1 - j,
495*27b03b36SApple OSS Distributions j * sizeof(OSSymbol *));
496*27b03b36SApple OSS Distributions }
497*27b03b36SApple OSS Distributions kfree_type(OSSymbol *, thisBucket->count, thisBucket->symbolP);
498*27b03b36SApple OSS Distributions OSMETA_ACCUMSIZE(-sizeof(OSSymbol *));
499*27b03b36SApple OSS Distributions thisBucket->symbolP = list;
500*27b03b36SApple OSS Distributions count--;
501*27b03b36SApple OSS Distributions thisBucket->count--;
502*27b03b36SApple OSS Distributions return;
503*27b03b36SApple OSS Distributions }
504*27b03b36SApple OSS Distributions }
505*27b03b36SApple OSS Distributions // couldn't find the symbol; probably means string hash changed
506*27b03b36SApple OSS Distributions panic("removeSymbol %s count %d ", sym->string ? sym->string : "no string", count);
507*27b03b36SApple OSS Distributions }
508*27b03b36SApple OSS Distributions
509*27b03b36SApple OSS Distributions /*
510*27b03b36SApple OSS Distributions *********************************************************************
511*27b03b36SApple OSS Distributions * From here on we are actually implementing the OSSymbol class
512*27b03b36SApple OSS Distributions *********************************************************************
513*27b03b36SApple OSS Distributions */
514*27b03b36SApple OSS Distributions OSDefineMetaClassAndStructorsWithInitAndZone(OSSymbol, OSString,
515*27b03b36SApple OSS Distributions OSSymbol::initialize(), ZC_ZFREE_CLEARMEM)
516*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 0);
517*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 1);
518*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 2);
519*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 3);
520*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 4);
521*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 5);
522*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 6);
523*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSSymbol, 7);
524*27b03b36SApple OSS Distributions
525*27b03b36SApple OSS Distributions static OSSymbolPool *pool;
526*27b03b36SApple OSS Distributions
527*27b03b36SApple OSS Distributions void
initialize()528*27b03b36SApple OSS Distributions OSSymbol::initialize()
529*27b03b36SApple OSS Distributions {
530*27b03b36SApple OSS Distributions pool = new OSSymbolPool;
531*27b03b36SApple OSS Distributions assert(pool);
532*27b03b36SApple OSS Distributions
533*27b03b36SApple OSS Distributions if (pool && !pool->init()) {
534*27b03b36SApple OSS Distributions delete pool;
535*27b03b36SApple OSS Distributions assert(false);
536*27b03b36SApple OSS Distributions }
537*27b03b36SApple OSS Distributions ;
538*27b03b36SApple OSS Distributions }
539*27b03b36SApple OSS Distributions
540*27b03b36SApple OSS Distributions bool
initWithCStringNoCopy(const char *)541*27b03b36SApple OSS Distributions OSSymbol::initWithCStringNoCopy(const char *)
542*27b03b36SApple OSS Distributions {
543*27b03b36SApple OSS Distributions return false;
544*27b03b36SApple OSS Distributions }
545*27b03b36SApple OSS Distributions bool
initWithCString(const char *)546*27b03b36SApple OSS Distributions OSSymbol::initWithCString(const char *)
547*27b03b36SApple OSS Distributions {
548*27b03b36SApple OSS Distributions return false;
549*27b03b36SApple OSS Distributions }
550*27b03b36SApple OSS Distributions bool
initWithString(const OSString *)551*27b03b36SApple OSS Distributions OSSymbol::initWithString(const OSString *)
552*27b03b36SApple OSS Distributions {
553*27b03b36SApple OSS Distributions return false;
554*27b03b36SApple OSS Distributions }
555*27b03b36SApple OSS Distributions
556*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol>
withString(const OSString * aString)557*27b03b36SApple OSS Distributions OSSymbol::withString(const OSString *aString)
558*27b03b36SApple OSS Distributions {
559*27b03b36SApple OSS Distributions // This string may be a OSSymbol already, cheap check.
560*27b03b36SApple OSS Distributions if (OSDynamicCast(OSSymbol, aString)) {
561*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
562*27b03b36SApple OSS Distributions return aStringNew;
563*27b03b36SApple OSS Distributions } else if (((const OSSymbol *) aString)->flags & kOSStringNoCopy) {
564*27b03b36SApple OSS Distributions return OSSymbol::withCStringNoCopy(aString->getCStringNoCopy());
565*27b03b36SApple OSS Distributions } else {
566*27b03b36SApple OSS Distributions return OSSymbol::withCString(aString->getCStringNoCopy());
567*27b03b36SApple OSS Distributions }
568*27b03b36SApple OSS Distributions }
569*27b03b36SApple OSS Distributions
570*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCString(const char * cString)571*27b03b36SApple OSS Distributions OSSymbol::withCString(const char *cString)
572*27b03b36SApple OSS Distributions {
573*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol> symbol;
574*27b03b36SApple OSS Distributions
575*27b03b36SApple OSS Distributions // Check if the symbol exists already, we don't need to take a lock here,
576*27b03b36SApple OSS Distributions // since existingSymbolForCString will take the shared lock.
577*27b03b36SApple OSS Distributions symbol = OSSymbol::existingSymbolForCString(cString);
578*27b03b36SApple OSS Distributions if (symbol) {
579*27b03b36SApple OSS Distributions return symbol;
580*27b03b36SApple OSS Distributions }
581*27b03b36SApple OSS Distributions
582*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> newSymb = OSMakeShared<OSSymbol>();
583*27b03b36SApple OSS Distributions if (!newSymb) {
584*27b03b36SApple OSS Distributions return os::move(newSymb);
585*27b03b36SApple OSS Distributions }
586*27b03b36SApple OSS Distributions
587*27b03b36SApple OSS Distributions if (newSymb->OSString::initWithCString(cString)) {
588*27b03b36SApple OSS Distributions pool->closeWriteGate();
589*27b03b36SApple OSS Distributions symbol = pool->insertSymbol(newSymb.get());
590*27b03b36SApple OSS Distributions pool->openWriteGate();
591*27b03b36SApple OSS Distributions
592*27b03b36SApple OSS Distributions if (symbol) {
593*27b03b36SApple OSS Distributions // Somebody must have inserted the new symbol so free our copy
594*27b03b36SApple OSS Distributions newSymb.detach()->OSString::free();
595*27b03b36SApple OSS Distributions return symbol;
596*27b03b36SApple OSS Distributions }
597*27b03b36SApple OSS Distributions }
598*27b03b36SApple OSS Distributions
599*27b03b36SApple OSS Distributions return os::move(newSymb); // return the newly created & inserted symbol.
600*27b03b36SApple OSS Distributions }
601*27b03b36SApple OSS Distributions
602*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol>
withCStringNoCopy(const char * cString)603*27b03b36SApple OSS Distributions OSSymbol::withCStringNoCopy(const char *cString)
604*27b03b36SApple OSS Distributions {
605*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol> symbol;
606*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> newSymb;
607*27b03b36SApple OSS Distributions
608*27b03b36SApple OSS Distributions // Check if the symbol exists already, we don't need to take a lock here,
609*27b03b36SApple OSS Distributions // since existingSymbolForCString will take the shared lock.
610*27b03b36SApple OSS Distributions symbol = OSSymbol::existingSymbolForCString(cString);
611*27b03b36SApple OSS Distributions if (symbol) {
612*27b03b36SApple OSS Distributions return symbol;
613*27b03b36SApple OSS Distributions }
614*27b03b36SApple OSS Distributions
615*27b03b36SApple OSS Distributions newSymb = OSMakeShared<OSSymbol>();
616*27b03b36SApple OSS Distributions if (!newSymb) {
617*27b03b36SApple OSS Distributions return os::move(newSymb);
618*27b03b36SApple OSS Distributions }
619*27b03b36SApple OSS Distributions
620*27b03b36SApple OSS Distributions if (newSymb->OSString::initWithCStringNoCopy(cString)) {
621*27b03b36SApple OSS Distributions pool->closeWriteGate();
622*27b03b36SApple OSS Distributions symbol = pool->insertSymbol(newSymb.get());
623*27b03b36SApple OSS Distributions pool->openWriteGate();
624*27b03b36SApple OSS Distributions
625*27b03b36SApple OSS Distributions if (symbol) {
626*27b03b36SApple OSS Distributions newSymb.detach()->OSString::free();
627*27b03b36SApple OSS Distributions // Somebody must have inserted the new symbol so free our copy
628*27b03b36SApple OSS Distributions return symbol;
629*27b03b36SApple OSS Distributions }
630*27b03b36SApple OSS Distributions }
631*27b03b36SApple OSS Distributions
632*27b03b36SApple OSS Distributions return os::move(newSymb); // return the newly created & inserted symbol.
633*27b03b36SApple OSS Distributions }
634*27b03b36SApple OSS Distributions
635*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForString(const OSString * aString)636*27b03b36SApple OSS Distributions OSSymbol::existingSymbolForString(const OSString *aString)
637*27b03b36SApple OSS Distributions {
638*27b03b36SApple OSS Distributions if (OSDynamicCast(OSSymbol, aString)) {
639*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol> aStringNew((const OSSymbol *)aString, OSRetain);
640*27b03b36SApple OSS Distributions return aStringNew;
641*27b03b36SApple OSS Distributions }
642*27b03b36SApple OSS Distributions
643*27b03b36SApple OSS Distributions return OSSymbol::existingSymbolForCString(aString->getCStringNoCopy());
644*27b03b36SApple OSS Distributions }
645*27b03b36SApple OSS Distributions
646*27b03b36SApple OSS Distributions OSSharedPtr<const OSSymbol>
existingSymbolForCString(const char * cString)647*27b03b36SApple OSS Distributions OSSymbol::existingSymbolForCString(const char *cString)
648*27b03b36SApple OSS Distributions {
649*27b03b36SApple OSS Distributions OSSharedPtr<OSSymbol> symbol;
650*27b03b36SApple OSS Distributions
651*27b03b36SApple OSS Distributions pool->closeReadGate();
652*27b03b36SApple OSS Distributions symbol = pool->findSymbol(cString);
653*27b03b36SApple OSS Distributions pool->openReadGate();
654*27b03b36SApple OSS Distributions
655*27b03b36SApple OSS Distributions return os::move(symbol);
656*27b03b36SApple OSS Distributions }
657*27b03b36SApple OSS Distributions
658*27b03b36SApple OSS Distributions void
checkForPageUnload(void * startAddr,void * endAddr)659*27b03b36SApple OSS Distributions OSSymbol::checkForPageUnload(void *startAddr, void *endAddr)
660*27b03b36SApple OSS Distributions {
661*27b03b36SApple OSS Distributions OSSymbol *probeSymbol;
662*27b03b36SApple OSS Distributions OSSymbolPoolState state;
663*27b03b36SApple OSS Distributions
664*27b03b36SApple OSS Distributions pool->closeWriteGate();
665*27b03b36SApple OSS Distributions state = pool->initHashState();
666*27b03b36SApple OSS Distributions while ((probeSymbol = pool->nextHashState(&state))) {
667*27b03b36SApple OSS Distributions if (probeSymbol->string >= startAddr && probeSymbol->string < endAddr) {
668*27b03b36SApple OSS Distributions probeSymbol->OSString::initWithCString(probeSymbol->string);
669*27b03b36SApple OSS Distributions }
670*27b03b36SApple OSS Distributions }
671*27b03b36SApple OSS Distributions pool->openWriteGate();
672*27b03b36SApple OSS Distributions }
673*27b03b36SApple OSS Distributions
674*27b03b36SApple OSS Distributions void
taggedRelease(const void * tag) const675*27b03b36SApple OSS Distributions OSSymbol::taggedRelease(const void *tag) const
676*27b03b36SApple OSS Distributions {
677*27b03b36SApple OSS Distributions super::taggedRelease(tag);
678*27b03b36SApple OSS Distributions }
679*27b03b36SApple OSS Distributions
680*27b03b36SApple OSS Distributions void
taggedRelease(const void * tag,const int when) const681*27b03b36SApple OSS Distributions OSSymbol::taggedRelease(const void *tag, const int when) const
682*27b03b36SApple OSS Distributions {
683*27b03b36SApple OSS Distributions super::taggedRelease(tag, when);
684*27b03b36SApple OSS Distributions }
685*27b03b36SApple OSS Distributions
686*27b03b36SApple OSS Distributions void
free()687*27b03b36SApple OSS Distributions OSSymbol::free()
688*27b03b36SApple OSS Distributions {
689*27b03b36SApple OSS Distributions pool->closeWriteGate();
690*27b03b36SApple OSS Distributions pool->removeSymbol(this);
691*27b03b36SApple OSS Distributions pool->openWriteGate();
692*27b03b36SApple OSS Distributions super::free();
693*27b03b36SApple OSS Distributions }
694*27b03b36SApple OSS Distributions
695*27b03b36SApple OSS Distributions bool
isEqualTo(const char * aCString) const696*27b03b36SApple OSS Distributions OSSymbol::isEqualTo(const char *aCString) const
697*27b03b36SApple OSS Distributions {
698*27b03b36SApple OSS Distributions return super::isEqualTo(aCString);
699*27b03b36SApple OSS Distributions }
700*27b03b36SApple OSS Distributions
701*27b03b36SApple OSS Distributions bool
isEqualTo(const OSSymbol * aSymbol) const702*27b03b36SApple OSS Distributions OSSymbol::isEqualTo(const OSSymbol *aSymbol) const
703*27b03b36SApple OSS Distributions {
704*27b03b36SApple OSS Distributions return aSymbol == this;
705*27b03b36SApple OSS Distributions }
706*27b03b36SApple OSS Distributions
707*27b03b36SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const708*27b03b36SApple OSS Distributions OSSymbol::isEqualTo(const OSMetaClassBase *obj) const
709*27b03b36SApple OSS Distributions {
710*27b03b36SApple OSS Distributions OSSymbol * sym;
711*27b03b36SApple OSS Distributions OSString * str;
712*27b03b36SApple OSS Distributions
713*27b03b36SApple OSS Distributions if ((sym = OSDynamicCast(OSSymbol, obj))) {
714*27b03b36SApple OSS Distributions return isEqualTo(sym);
715*27b03b36SApple OSS Distributions } else if ((str = OSDynamicCast(OSString, obj))) {
716*27b03b36SApple OSS Distributions return super::isEqualTo(str);
717*27b03b36SApple OSS Distributions } else {
718*27b03b36SApple OSS Distributions return false;
719*27b03b36SApple OSS Distributions }
720*27b03b36SApple OSS Distributions }
721*27b03b36SApple OSS Distributions
722*27b03b36SApple OSS Distributions unsigned int
bsearch(const void * key,const void * array,unsigned int arrayCount,size_t memberSize)723*27b03b36SApple OSS Distributions OSSymbol::bsearch(
724*27b03b36SApple OSS Distributions const void * key,
725*27b03b36SApple OSS Distributions const void * array,
726*27b03b36SApple OSS Distributions unsigned int arrayCount,
727*27b03b36SApple OSS Distributions size_t memberSize)
728*27b03b36SApple OSS Distributions {
729*27b03b36SApple OSS Distributions const void **p;
730*27b03b36SApple OSS Distributions unsigned int baseIdx = 0;
731*27b03b36SApple OSS Distributions unsigned int lim;
732*27b03b36SApple OSS Distributions
733*27b03b36SApple OSS Distributions for (lim = arrayCount; lim; lim >>= 1) {
734*27b03b36SApple OSS Distributions p = (typeof(p))(((uintptr_t) array) + (baseIdx + (lim >> 1)) * memberSize);
735*27b03b36SApple OSS Distributions if (key == *p) {
736*27b03b36SApple OSS Distributions return baseIdx + (lim >> 1);
737*27b03b36SApple OSS Distributions }
738*27b03b36SApple OSS Distributions if (key > *p) {
739*27b03b36SApple OSS Distributions // move right
740*27b03b36SApple OSS Distributions baseIdx += (lim >> 1) + 1;
741*27b03b36SApple OSS Distributions lim--;
742*27b03b36SApple OSS Distributions }
743*27b03b36SApple OSS Distributions // else move left
744*27b03b36SApple OSS Distributions }
745*27b03b36SApple OSS Distributions // not found, insertion point here
746*27b03b36SApple OSS Distributions return baseIdx + (lim >> 1);
747*27b03b36SApple OSS Distributions }
748