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