]> git.saurik.com Git - apple/xnu.git/blame - libkern/c++/OSSymbol.cpp
xnu-792.12.6.tar.gz
[apple/xnu.git] / libkern / c++ / OSSymbol.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
31
9bccf70c 32#include <string.h>
1c79356b
A
33#include <sys/cdefs.h>
34
35__BEGIN_DECLS
36#include <kern/lock.h>
37__END_DECLS
38
39#include <libkern/c++/OSSymbol.h>
40#include <libkern/c++/OSLib.h>
9bccf70c 41#include <string.h>
1c79356b
A
42
43#define super OSString
44
45typedef struct { int i, j; } OSSymbolPoolState;
46
47#if OSALLOCDEBUG
48extern "C" {
49 extern int debug_container_malloc_size;
50};
51#define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
52#else
53#define ACCUMSIZE(s)
54#endif
55
56class OSSymbolPool
57{
58private:
59 static const unsigned int kInitBucketCount = 16;
60
61 typedef struct { unsigned int count; OSSymbol **symbolP; } Bucket;
62
63 Bucket *buckets;
64 unsigned int nBuckets;
65 unsigned int count;
66 mutex_t *poolGate;
67
68 static inline void hashSymbol(const char *s,
69 unsigned int *hashP,
70 unsigned int *lenP)
71 {
72 unsigned int hash = 0;
73 unsigned int len = 0;
74
75 /* Unroll the loop. */
76 for (;;) {
77 if (!*s) break; len++; hash ^= *s++;
78 if (!*s) break; len++; hash ^= *s++ << 8;
79 if (!*s) break; len++; hash ^= *s++ << 16;
80 if (!*s) break; len++; hash ^= *s++ << 24;
81 }
82 *lenP = len;
83 *hashP = hash;
84 }
85
86 static unsigned long log2(unsigned int x);
87 static unsigned long exp2ml(unsigned int x);
88
89 void reconstructSymbols();
90
91public:
92 static void *operator new(size_t size);
93 static void operator delete(void *mem, size_t size);
94
95 OSSymbolPool() { };
96 OSSymbolPool(const OSSymbolPool *old);
97 virtual ~OSSymbolPool();
98
99 bool init();
100
101 inline void closeGate() { mutex_lock(poolGate); };
102 inline void openGate() { mutex_unlock(poolGate); };
103
55e303ae 104 OSSymbol *findSymbol(const char *cString) const;
1c79356b 105 OSSymbol *insertSymbol(OSSymbol *sym);
9bccf70c 106 void removeSymbol(OSSymbol *sym);
1c79356b
A
107
108 OSSymbolPoolState initHashState();
109 OSSymbol *nextHashState(OSSymbolPoolState *stateP);
110};
111
112void * OSSymbolPool::operator new(size_t size)
113{
114 void *mem = (void *)kalloc(size);
115 ACCUMSIZE(size);
116 assert(mem);
117 bzero(mem, size);
118
119 return mem;
120}
121
122void OSSymbolPool::operator delete(void *mem, size_t size)
123{
8ad349bb 124 kfree((vm_offset_t)mem, size);
1c79356b
A
125 ACCUMSIZE(-size);
126}
127
128bool OSSymbolPool::init()
129{
130 count = 0;
131 nBuckets = exp2ml(1 + log2(kInitBucketCount));
132 buckets = (Bucket *) kalloc(nBuckets * sizeof(Bucket));
133 ACCUMSIZE(nBuckets * sizeof(Bucket));
134 if (!buckets)
135 return false;
136
137 bzero(buckets, nBuckets * sizeof(Bucket));
138
139 poolGate = mutex_alloc(0);
140
141 return poolGate != 0;
142}
143
144OSSymbolPool::OSSymbolPool(const OSSymbolPool *old)
145{
146 count = old->count;
147 nBuckets = old->nBuckets;
148 buckets = old->buckets;
149
150 poolGate = 0; // Do not duplicate the poolGate
151}
152
153OSSymbolPool::~OSSymbolPool()
154{
155 if (buckets) {
8ad349bb 156 kfree((vm_offset_t)buckets, nBuckets * sizeof(Bucket));
1c79356b
A
157 ACCUMSIZE(-(nBuckets * sizeof(Bucket)));
158 }
159
160 if (poolGate)
8ad349bb 161 kfree((vm_offset_t) poolGate, 36 * 4);
1c79356b
A
162}
163
164unsigned long OSSymbolPool::log2(unsigned int x)
165{
166 unsigned long i;
167
168 for (i = 0; x > 1 ; i++)
169 x >>= 1;
170 return i;
171}
172
173unsigned long OSSymbolPool::exp2ml(unsigned int x)
174{
175 return (1 << x) - 1;
176}
177
178OSSymbolPoolState OSSymbolPool::initHashState()
179{
180 OSSymbolPoolState newState = { nBuckets, 0 };
181 return newState;
182}
183
184OSSymbol *OSSymbolPool::nextHashState(OSSymbolPoolState *stateP)
185{
186 Bucket *thisBucket = &buckets[stateP->i];
187
188 while (!stateP->j) {
189 if (!stateP->i)
190 return 0;
191 stateP->i--;
192 thisBucket--;
193 stateP->j = thisBucket->count;
194 }
195
196 stateP->j--;
197 if (thisBucket->count == 1)
198 return (OSSymbol *) thisBucket->symbolP;
199 else
200 return thisBucket->symbolP[stateP->j];
201}
202
203void OSSymbolPool::reconstructSymbols()
204{
205 OSSymbolPool old(this);
206 OSSymbol *insert;
207 OSSymbolPoolState state;
208
209 nBuckets += nBuckets + 1;
210 count = 0;
211 buckets = (Bucket *) kalloc(nBuckets * sizeof(Bucket));
212 ACCUMSIZE(nBuckets * sizeof(Bucket));
213 /* @@@ gvdl: Zero test and panic if can't set up pool */
214 bzero(buckets, nBuckets * sizeof(Bucket));
215
216 state = old.initHashState();
217 while ( (insert = old.nextHashState(&state)) )
218 insertSymbol(insert);
219}
220
55e303ae 221OSSymbol *OSSymbolPool::findSymbol(const char *cString) const
1c79356b
A
222{
223 Bucket *thisBucket;
224 unsigned int j, inLen, hash;
225 OSSymbol *probeSymbol, **list;
226
227 hashSymbol(cString, &hash, &inLen); inLen++;
228 thisBucket = &buckets[hash % nBuckets];
229 j = thisBucket->count;
230
231 if (!j)
232 return 0;
233
234 if (j == 1) {
235 probeSymbol = (OSSymbol *) thisBucket->symbolP;
236
237 if (inLen == probeSymbol->length
55e303ae
A
238 && (strcmp(probeSymbol->string, cString) == 0))
239 return probeSymbol;
9bccf70c 240 return 0;
1c79356b
A
241 }
242
243 for (list = thisBucket->symbolP; j--; list++) {
244 probeSymbol = *list;
245 if (inLen == probeSymbol->length
55e303ae
A
246 && (strcmp(probeSymbol->string, cString) == 0))
247 return probeSymbol;
1c79356b
A
248 }
249
250 return 0;
251}
252
253OSSymbol *OSSymbolPool::insertSymbol(OSSymbol *sym)
254{
255 const char *cString = sym->string;
256 Bucket *thisBucket;
257 unsigned int j, inLen, hash;
258 OSSymbol *probeSymbol, **list;
259
260 hashSymbol(cString, &hash, &inLen); inLen++;
261 thisBucket = &buckets[hash % nBuckets];
262 j = thisBucket->count;
263
264 if (!j) {
265 thisBucket->symbolP = (OSSymbol **) sym;
266 thisBucket->count++;
267 count++;
55e303ae 268 return sym;
1c79356b
A
269 }
270
271 if (j == 1) {
272 probeSymbol = (OSSymbol *) thisBucket->symbolP;
273
274 if (inLen == probeSymbol->length
275 && strcmp(probeSymbol->string, cString) == 0)
276 return probeSymbol;
277
278 list = (OSSymbol **) kalloc(2 * sizeof(OSSymbol *));
279 ACCUMSIZE(2 * sizeof(OSSymbol *));
280 /* @@@ gvdl: Zero test and panic if can't set up pool */
281 list[0] = sym;
282 list[1] = probeSymbol;
283 thisBucket->symbolP = list;
284 thisBucket->count++;
285 count++;
286 if (count > nBuckets)
287 reconstructSymbols();
288
55e303ae 289 return sym;
1c79356b
A
290 }
291
292 for (list = thisBucket->symbolP; j--; list++) {
293 probeSymbol = *list;
294 if (inLen == probeSymbol->length
295 && strcmp(probeSymbol->string, cString) == 0)
296 return probeSymbol;
297 }
298
299 j = thisBucket->count++;
300 count++;
301 list = (OSSymbol **) kalloc(thisBucket->count * sizeof(OSSymbol *));
302 ACCUMSIZE(thisBucket->count * sizeof(OSSymbol *));
303 /* @@@ gvdl: Zero test and panic if can't set up pool */
304 list[0] = sym;
305 bcopy(thisBucket->symbolP, list + 1, j * sizeof(OSSymbol *));
8ad349bb 306 kfree((vm_offset_t)thisBucket->symbolP, j * sizeof(OSSymbol *));
1c79356b
A
307 ACCUMSIZE(-(j * sizeof(OSSymbol *)));
308 thisBucket->symbolP = list;
309 if (count > nBuckets)
310 reconstructSymbols();
311
55e303ae 312 return sym;
1c79356b
A
313}
314
9bccf70c 315void OSSymbolPool::removeSymbol(OSSymbol *sym)
1c79356b
A
316{
317 Bucket *thisBucket;
318 unsigned int j, inLen, hash;
319 OSSymbol *probeSymbol, **list;
320
9bccf70c 321 hashSymbol(sym->string, &hash, &inLen); inLen++;
1c79356b
A
322 thisBucket = &buckets[hash % nBuckets];
323 j = thisBucket->count;
324 list = thisBucket->symbolP;
325
326 if (!j)
327 return;
328
329 if (j == 1) {
330 probeSymbol = (OSSymbol *) list;
331
9bccf70c 332 if (probeSymbol == sym) {
1c79356b
A
333 thisBucket->symbolP = 0;
334 count--;
335 thisBucket->count--;
336 return;
337 }
338 return;
339 }
340
341 if (j == 2) {
342 probeSymbol = list[0];
9bccf70c 343 if (probeSymbol == sym) {
1c79356b 344 thisBucket->symbolP = (OSSymbol **) list[1];
8ad349bb 345 kfree((vm_offset_t)list, 2 * sizeof(OSSymbol *));
1c79356b
A
346 ACCUMSIZE(-(2 * sizeof(OSSymbol *)));
347 count--;
348 thisBucket->count--;
349 return;
350 }
351
352 probeSymbol = list[1];
9bccf70c 353 if (probeSymbol == sym) {
1c79356b 354 thisBucket->symbolP = (OSSymbol **) list[0];
8ad349bb 355 kfree((vm_offset_t)list, 2 * sizeof(OSSymbol *));
1c79356b
A
356 ACCUMSIZE(-(2 * sizeof(OSSymbol *)));
357 count--;
358 thisBucket->count--;
359 return;
360 }
361 return;
362 }
363
364 for (; j--; list++) {
365 probeSymbol = *list;
9bccf70c 366 if (probeSymbol == sym) {
1c79356b
A
367
368 list = (OSSymbol **)
369 kalloc((thisBucket->count-1) * sizeof(OSSymbol *));
370 ACCUMSIZE((thisBucket->count-1) * sizeof(OSSymbol *));
371 if (thisBucket->count-1 != j)
372 bcopy(thisBucket->symbolP, list,
373 (thisBucket->count-1-j) * sizeof(OSSymbol *));
374 if (j)
375 bcopy(thisBucket->symbolP + thisBucket->count-j,
376 list + thisBucket->count-1-j,
377 j * sizeof(OSSymbol *));
8ad349bb 378 kfree((vm_offset_t)thisBucket->symbolP, thisBucket->count * sizeof(OSSymbol *));
1c79356b
A
379 ACCUMSIZE(-(thisBucket->count * sizeof(OSSymbol *)));
380 thisBucket->symbolP = list;
381 count--;
382 thisBucket->count--;
383 return;
384 }
385 }
386}
387
388/*
389 *********************************************************************
390 * From here on we are actually implementing the OSSymbol class
391 *********************************************************************
392 */
393OSDefineMetaClassAndStructorsWithInit(OSSymbol, OSString,
394 OSSymbol::initialize())
395OSMetaClassDefineReservedUnused(OSSymbol, 0);
396OSMetaClassDefineReservedUnused(OSSymbol, 1);
397OSMetaClassDefineReservedUnused(OSSymbol, 2);
398OSMetaClassDefineReservedUnused(OSSymbol, 3);
399OSMetaClassDefineReservedUnused(OSSymbol, 4);
400OSMetaClassDefineReservedUnused(OSSymbol, 5);
401OSMetaClassDefineReservedUnused(OSSymbol, 6);
402OSMetaClassDefineReservedUnused(OSSymbol, 7);
403
404static OSSymbolPool *pool;
405
406void OSSymbol::initialize()
407{
408 pool = new OSSymbolPool;
409 assert(pool);
410
411 if (!pool->init()) {
412 delete pool;
413 assert(false);
414 };
415}
416
417bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
418bool OSSymbol::initWithCString(const char *) { return false; }
419bool OSSymbol::initWithString(const OSString *) { return false; }
420
421const OSSymbol *OSSymbol::withString(const OSString *aString)
422{
423 // This string may be a OSSymbol already, cheap check.
424 if (OSDynamicCast(OSSymbol, aString)) {
425 aString->retain();
426 return (const OSSymbol *) aString;
427 }
428 else if (((const OSSymbol *) aString)->flags & kOSStringNoCopy)
429 return OSSymbol::withCStringNoCopy(aString->getCStringNoCopy());
430 else
431 return OSSymbol::withCString(aString->getCStringNoCopy());
432}
433
434const OSSymbol *OSSymbol::withCString(const char *cString)
435{
436 pool->closeGate();
437
55e303ae
A
438 OSSymbol *oldSymb = pool->findSymbol(cString);
439 if (!oldSymb) {
440 OSSymbol *newSymb = new OSSymbol;
441 if (!newSymb) {
442 pool->openGate();
443 return newSymb;
444 }
445
446 if (newSymb->OSString::initWithCString(cString))
447 oldSymb = pool->insertSymbol(newSymb);
448
449 if (newSymb == oldSymb) {
450 pool->openGate();
451 return newSymb; // return the newly created & inserted symbol.
452 }
453 else
454 // Somebody else inserted the new symbol so free our copy
9bccf70c 455 newSymb->OSString::free();
1c79356b 456 }
55e303ae
A
457
458 oldSymb->retain(); // Retain the old symbol before releasing the lock.
1c79356b 459
55e303ae
A
460 pool->openGate();
461 return oldSymb;
1c79356b
A
462}
463
464const OSSymbol *OSSymbol::withCStringNoCopy(const char *cString)
465{
466 pool->closeGate();
467
55e303ae
A
468 OSSymbol *oldSymb = pool->findSymbol(cString);
469 if (!oldSymb) {
470 OSSymbol *newSymb = new OSSymbol;
471 if (!newSymb) {
472 pool->openGate();
473 return newSymb;
474 }
475
476 if (newSymb->OSString::initWithCStringNoCopy(cString))
477 oldSymb = pool->insertSymbol(newSymb);
478
479 if (newSymb == oldSymb) {
480 pool->openGate();
481 return newSymb; // return the newly created & inserted symbol.
482 }
483 else
484 // Somebody else inserted the new symbol so free our copy
9bccf70c 485 newSymb->OSString::free();
1c79356b 486 }
55e303ae
A
487
488 oldSymb->retain(); // Retain the old symbol before releasing the lock.
1c79356b 489
55e303ae
A
490 pool->openGate();
491 return oldSymb;
1c79356b
A
492}
493
494void OSSymbol::checkForPageUnload(void *startAddr, void *endAddr)
495{
496 OSSymbol *probeSymbol;
497 OSSymbolPoolState state;
498
499 pool->closeGate();
500 state = pool->initHashState();
501 while ( (probeSymbol = pool->nextHashState(&state)) ) {
502 if (probeSymbol->string >= startAddr && probeSymbol->string < endAddr) {
503 const char *oldString = probeSymbol->string;
504
505 probeSymbol->string = (char *) kalloc(probeSymbol->length);
506 ACCUMSIZE(probeSymbol->length);
507 bcopy(oldString, probeSymbol->string, probeSymbol->length);
508 probeSymbol->flags &= ~kOSStringNoCopy;
509 }
510 }
511 pool->openGate();
512}
513
55e303ae
A
514void OSSymbol::taggedRelease(const void *tag) const
515{
516 super::taggedRelease(tag);
517}
518
519void OSSymbol::taggedRelease(const void *tag, const int when) const
1c79356b
A
520{
521 pool->closeGate();
55e303ae 522 super::taggedRelease(tag, when);
1c79356b 523 pool->openGate();
55e303ae
A
524}
525
526void OSSymbol::free()
527{
528 pool->removeSymbol(this);
1c79356b
A
529 super::free();
530}
531
532bool OSSymbol::isEqualTo(const char *aCString) const
533{
534 return super::isEqualTo(aCString);
535}
536
537bool OSSymbol::isEqualTo(const OSSymbol *aSymbol) const
538{
539 return aSymbol == this;
540}
541
542bool OSSymbol::isEqualTo(const OSMetaClassBase *obj) const
543{
544 OSSymbol * sym;
545 OSString * str;
546
547 if ((sym = OSDynamicCast(OSSymbol, obj)))
548 return isEqualTo(sym);
549 else if ((str = OSDynamicCast(OSString, obj)))
550 return super::isEqualTo(str);
551 else
552 return false;
553}