2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
31 #include <sys/cdefs.h>
34 #include <kern/lock.h>
37 #include <libkern/c++/OSSymbol.h>
38 #include <libkern/c++/OSLib.h>
41 #define super OSString
43 typedef struct { int i
, j
; } OSSymbolPoolState
;
47 extern int debug_container_malloc_size
;
49 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
54 #define INITIAL_POOL_SIZE (exp2ml(1 + log2(kInitBucketCount)))
56 #define GROW_FACTOR (1)
57 #define SHRINK_FACTOR (3)
59 #define GROW_POOL() do \
60 if (count * GROW_FACTOR > nBuckets) { \
61 reconstructSymbols(true); \
65 #define SHRINK_POOL() do \
66 if (count * SHRINK_FACTOR < nBuckets && \
67 nBuckets > INITIAL_POOL_SIZE) { \
68 reconstructSymbols(false); \
75 static const unsigned int kInitBucketCount
= 16;
77 typedef struct { unsigned int count
; OSSymbol
**symbolP
; } Bucket
;
80 unsigned int nBuckets
;
84 static inline void hashSymbol(const char *s
,
88 unsigned int hash
= 0;
91 /* Unroll the loop. */
93 if (!*s
) break; len
++; hash
^= *s
++;
94 if (!*s
) break; len
++; hash
^= *s
++ << 8;
95 if (!*s
) break; len
++; hash
^= *s
++ << 16;
96 if (!*s
) break; len
++; hash
^= *s
++ << 24;
102 static unsigned long log2(unsigned int x
);
103 static unsigned long exp2ml(unsigned int x
);
105 void reconstructSymbols(void);
106 void reconstructSymbols(bool grow
);
109 static void *operator new(size_t size
);
110 static void operator delete(void *mem
, size_t size
);
113 OSSymbolPool(const OSSymbolPool
*old
);
114 virtual ~OSSymbolPool();
118 inline void closeGate() { mutex_lock(poolGate
); };
119 inline void openGate() { mutex_unlock(poolGate
); };
121 OSSymbol
*findSymbol(const char *cString
) const;
122 OSSymbol
*insertSymbol(OSSymbol
*sym
);
123 void removeSymbol(OSSymbol
*sym
);
125 OSSymbolPoolState
initHashState();
126 OSSymbol
*nextHashState(OSSymbolPoolState
*stateP
);
129 void * OSSymbolPool::operator new(size_t size
)
131 void *mem
= (void *)kalloc(size
);
139 void OSSymbolPool::operator delete(void *mem
, size_t size
)
145 bool OSSymbolPool::init()
148 nBuckets
= INITIAL_POOL_SIZE
;
149 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
150 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
154 bzero(buckets
, nBuckets
* sizeof(Bucket
));
156 poolGate
= mutex_alloc(0);
158 return poolGate
!= 0;
161 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
164 nBuckets
= old
->nBuckets
;
165 buckets
= old
->buckets
;
167 poolGate
= 0; // Do not duplicate the poolGate
170 OSSymbolPool::~OSSymbolPool()
173 kfree(buckets
, nBuckets
* sizeof(Bucket
));
174 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
178 kfree(poolGate
, 36 * 4);
181 unsigned long OSSymbolPool::log2(unsigned int x
)
185 for (i
= 0; x
> 1 ; i
++)
190 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
195 OSSymbolPoolState
OSSymbolPool::initHashState()
197 OSSymbolPoolState newState
= { nBuckets
, 0 };
201 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
203 Bucket
*thisBucket
= &buckets
[stateP
->i
];
210 stateP
->j
= thisBucket
->count
;
214 if (thisBucket
->count
== 1)
215 return (OSSymbol
*) thisBucket
->symbolP
;
217 return thisBucket
->symbolP
[stateP
->j
];
220 void OSSymbolPool::reconstructSymbols(void)
222 this->reconstructSymbols(true);
225 void OSSymbolPool::reconstructSymbols(bool grow
)
227 unsigned int new_nBuckets
= nBuckets
;
229 OSSymbolPoolState state
;
232 new_nBuckets
+= new_nBuckets
+ 1;
234 /* Don't shrink the pool below the default initial size.
236 if (nBuckets
<= INITIAL_POOL_SIZE
) {
239 new_nBuckets
= (new_nBuckets
- 1) / 2;
242 /* Create old pool to iterate after doing above check, cause it
243 * gets finalized at return.
245 OSSymbolPool
old(this);
248 nBuckets
= new_nBuckets
;
249 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
250 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
251 /* @@@ gvdl: Zero test and panic if can't set up pool */
252 bzero(buckets
, nBuckets
* sizeof(Bucket
));
254 state
= old
.initHashState();
255 while ( (insert
= old
.nextHashState(&state
)) )
256 insertSymbol(insert
);
259 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
262 unsigned int j
, inLen
, hash
;
263 OSSymbol
*probeSymbol
, **list
;
265 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
266 thisBucket
= &buckets
[hash
% nBuckets
];
267 j
= thisBucket
->count
;
273 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
275 if (inLen
== probeSymbol
->length
276 && (strcmp(probeSymbol
->string
, cString
) == 0))
281 for (list
= thisBucket
->symbolP
; j
--; list
++) {
283 if (inLen
== probeSymbol
->length
284 && (strcmp(probeSymbol
->string
, cString
) == 0))
291 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
293 const char *cString
= sym
->string
;
295 unsigned int j
, inLen
, hash
;
296 OSSymbol
*probeSymbol
, **list
;
298 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
299 thisBucket
= &buckets
[hash
% nBuckets
];
300 j
= thisBucket
->count
;
303 thisBucket
->symbolP
= (OSSymbol
**) sym
;
310 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
312 if (inLen
== probeSymbol
->length
313 && strcmp(probeSymbol
->string
, cString
) == 0)
316 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
317 ACCUMSIZE(2 * sizeof(OSSymbol
*));
318 /* @@@ gvdl: Zero test and panic if can't set up pool */
320 list
[1] = probeSymbol
;
321 thisBucket
->symbolP
= list
;
329 for (list
= thisBucket
->symbolP
; j
--; list
++) {
331 if (inLen
== probeSymbol
->length
332 && strcmp(probeSymbol
->string
, cString
) == 0)
336 j
= thisBucket
->count
++;
338 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
339 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
340 /* @@@ gvdl: Zero test and panic if can't set up pool */
342 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
343 kfree(thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
344 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
345 thisBucket
->symbolP
= list
;
351 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
354 unsigned int j
, inLen
, hash
;
355 OSSymbol
*probeSymbol
, **list
;
357 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
358 thisBucket
= &buckets
[hash
% nBuckets
];
359 j
= thisBucket
->count
;
360 list
= thisBucket
->symbolP
;
366 probeSymbol
= (OSSymbol
*) list
;
368 if (probeSymbol
== sym
) {
369 thisBucket
->symbolP
= 0;
379 probeSymbol
= list
[0];
380 if (probeSymbol
== sym
) {
381 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
382 kfree(list
, 2 * sizeof(OSSymbol
*));
383 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
390 probeSymbol
= list
[1];
391 if (probeSymbol
== sym
) {
392 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
393 kfree(list
, 2 * sizeof(OSSymbol
*));
394 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
403 for (; j
--; list
++) {
405 if (probeSymbol
== sym
) {
408 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
409 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
410 if (thisBucket
->count
-1 != j
)
411 bcopy(thisBucket
->symbolP
, list
,
412 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
414 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
415 list
+ thisBucket
->count
-1-j
,
416 j
* sizeof(OSSymbol
*));
417 kfree(thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
418 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
419 thisBucket
->symbolP
= list
;
428 *********************************************************************
429 * From here on we are actually implementing the OSSymbol class
430 *********************************************************************
432 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
433 OSSymbol::initialize())
434 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
435 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
436 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
437 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
438 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
439 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
440 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
441 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
443 static OSSymbolPool
*pool
;
445 void OSSymbol::initialize()
447 pool
= new OSSymbolPool
;
456 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
457 bool OSSymbol::initWithCString(const char *) { return false; }
458 bool OSSymbol::initWithString(const OSString
*) { return false; }
460 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
462 // This string may be a OSSymbol already, cheap check.
463 if (OSDynamicCast(OSSymbol
, aString
)) {
465 return (const OSSymbol
*) aString
;
467 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
468 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
470 return OSSymbol::withCString(aString
->getCStringNoCopy());
473 const OSSymbol
*OSSymbol::withCString(const char *cString
)
477 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
479 OSSymbol
*newSymb
= new OSSymbol
;
485 if (newSymb
->OSString::initWithCString(cString
))
486 oldSymb
= pool
->insertSymbol(newSymb
);
488 if (newSymb
== oldSymb
) {
490 return newSymb
; // return the newly created & inserted symbol.
493 // Somebody else inserted the new symbol so free our copy
494 newSymb
->OSString::free();
497 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
503 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
507 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
509 OSSymbol
*newSymb
= new OSSymbol
;
515 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
516 oldSymb
= pool
->insertSymbol(newSymb
);
518 if (newSymb
== oldSymb
) {
520 return newSymb
; // return the newly created & inserted symbol.
523 // Somebody else inserted the new symbol so free our copy
524 newSymb
->OSString::free();
527 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
533 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
535 OSSymbol
*probeSymbol
;
536 OSSymbolPoolState state
;
539 state
= pool
->initHashState();
540 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
541 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
542 const char *oldString
= probeSymbol
->string
;
544 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
545 ACCUMSIZE(probeSymbol
->length
);
546 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
547 probeSymbol
->flags
&= ~kOSStringNoCopy
;
553 void OSSymbol::taggedRelease(const void *tag
) const
555 super::taggedRelease(tag
);
558 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
561 super::taggedRelease(tag
, when
);
565 void OSSymbol::free()
567 pool
->removeSymbol(this);
571 bool OSSymbol::isEqualTo(const char *aCString
) const
573 return super::isEqualTo(aCString
);
576 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
578 return aSymbol
== this;
581 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
586 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
587 return isEqualTo(sym
);
588 else if ((str
= OSDynamicCast(OSString
, obj
)))
589 return super::isEqualTo(str
);