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() { lck_mtx_lock(poolGate
); };
119 inline void openGate() { lck_mtx_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 extern lck_grp_t
*IOLockGroup
;
147 bool OSSymbolPool::init()
150 nBuckets
= INITIAL_POOL_SIZE
;
151 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
152 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
156 bzero(buckets
, nBuckets
* sizeof(Bucket
));
158 poolGate
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
160 return poolGate
!= 0;
163 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
166 nBuckets
= old
->nBuckets
;
167 buckets
= old
->buckets
;
169 poolGate
= 0; // Do not duplicate the poolGate
172 OSSymbolPool::~OSSymbolPool()
175 kfree(buckets
, nBuckets
* sizeof(Bucket
));
176 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
180 lck_mtx_free(poolGate
, IOLockGroup
);
183 unsigned long OSSymbolPool::log2(unsigned int x
)
187 for (i
= 0; x
> 1 ; i
++)
192 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
197 OSSymbolPoolState
OSSymbolPool::initHashState()
199 OSSymbolPoolState newState
= { nBuckets
, 0 };
203 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
205 Bucket
*thisBucket
= &buckets
[stateP
->i
];
212 stateP
->j
= thisBucket
->count
;
216 if (thisBucket
->count
== 1)
217 return (OSSymbol
*) thisBucket
->symbolP
;
219 return thisBucket
->symbolP
[stateP
->j
];
222 void OSSymbolPool::reconstructSymbols(void)
224 this->reconstructSymbols(true);
227 void OSSymbolPool::reconstructSymbols(bool grow
)
229 unsigned int new_nBuckets
= nBuckets
;
231 OSSymbolPoolState state
;
234 new_nBuckets
+= new_nBuckets
+ 1;
236 /* Don't shrink the pool below the default initial size.
238 if (nBuckets
<= INITIAL_POOL_SIZE
) {
241 new_nBuckets
= (new_nBuckets
- 1) / 2;
244 /* Create old pool to iterate after doing above check, cause it
245 * gets finalized at return.
247 OSSymbolPool
old(this);
250 nBuckets
= new_nBuckets
;
251 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
252 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
253 /* @@@ gvdl: Zero test and panic if can't set up pool */
254 bzero(buckets
, nBuckets
* sizeof(Bucket
));
256 state
= old
.initHashState();
257 while ( (insert
= old
.nextHashState(&state
)) )
258 insertSymbol(insert
);
261 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
264 unsigned int j
, inLen
, hash
;
265 OSSymbol
*probeSymbol
, **list
;
267 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
268 thisBucket
= &buckets
[hash
% nBuckets
];
269 j
= thisBucket
->count
;
275 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
277 if (inLen
== probeSymbol
->length
278 && (strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0))
283 for (list
= thisBucket
->symbolP
; j
--; list
++) {
285 if (inLen
== probeSymbol
->length
286 && (strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0))
293 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
295 const char *cString
= sym
->string
;
297 unsigned int j
, inLen
, hash
;
298 OSSymbol
*probeSymbol
, **list
;
300 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
301 thisBucket
= &buckets
[hash
% nBuckets
];
302 j
= thisBucket
->count
;
305 thisBucket
->symbolP
= (OSSymbol
**) sym
;
312 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
314 if (inLen
== probeSymbol
->length
315 && strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0)
318 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
319 ACCUMSIZE(2 * sizeof(OSSymbol
*));
320 /* @@@ gvdl: Zero test and panic if can't set up pool */
322 list
[1] = probeSymbol
;
323 thisBucket
->symbolP
= list
;
331 for (list
= thisBucket
->symbolP
; j
--; list
++) {
333 if (inLen
== probeSymbol
->length
334 && strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0)
338 j
= thisBucket
->count
++;
340 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
341 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
342 /* @@@ gvdl: Zero test and panic if can't set up pool */
344 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
345 kfree(thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
346 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
347 thisBucket
->symbolP
= list
;
353 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
356 unsigned int j
, inLen
, hash
;
357 OSSymbol
*probeSymbol
, **list
;
359 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
360 thisBucket
= &buckets
[hash
% nBuckets
];
361 j
= thisBucket
->count
;
362 list
= thisBucket
->symbolP
;
365 // couldn't find the symbol; probably means string hash changed
366 panic("removeSymbol");
371 probeSymbol
= (OSSymbol
*) list
;
373 if (probeSymbol
== sym
) {
374 thisBucket
->symbolP
= 0;
380 // couldn't find the symbol; probably means string hash changed
381 panic("removeSymbol");
386 probeSymbol
= list
[0];
387 if (probeSymbol
== sym
) {
388 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
389 kfree(list
, 2 * sizeof(OSSymbol
*));
390 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
397 probeSymbol
= list
[1];
398 if (probeSymbol
== sym
) {
399 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
400 kfree(list
, 2 * sizeof(OSSymbol
*));
401 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
407 // couldn't find the symbol; probably means string hash changed
408 panic("removeSymbol");
412 for (; j
--; list
++) {
414 if (probeSymbol
== sym
) {
417 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
418 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
419 if (thisBucket
->count
-1 != j
)
420 bcopy(thisBucket
->symbolP
, list
,
421 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
423 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
424 list
+ thisBucket
->count
-1-j
,
425 j
* sizeof(OSSymbol
*));
426 kfree(thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
427 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
428 thisBucket
->symbolP
= list
;
434 // couldn't find the symbol; probably means string hash changed
435 panic("removeSymbol");
439 *********************************************************************
440 * From here on we are actually implementing the OSSymbol class
441 *********************************************************************
443 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
444 OSSymbol::initialize())
445 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
446 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
447 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
448 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
449 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
450 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
451 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
452 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
454 static OSSymbolPool
*pool
;
456 void OSSymbol::initialize()
458 pool
= new OSSymbolPool
;
467 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
468 bool OSSymbol::initWithCString(const char *) { return false; }
469 bool OSSymbol::initWithString(const OSString
*) { return false; }
471 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
473 // This string may be a OSSymbol already, cheap check.
474 if (OSDynamicCast(OSSymbol
, aString
)) {
476 return (const OSSymbol
*) aString
;
478 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
479 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
481 return OSSymbol::withCString(aString
->getCStringNoCopy());
484 const OSSymbol
*OSSymbol::withCString(const char *cString
)
488 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
490 OSSymbol
*newSymb
= new OSSymbol
;
496 if (newSymb
->OSString::initWithCString(cString
))
497 oldSymb
= pool
->insertSymbol(newSymb
);
499 if (newSymb
== oldSymb
) {
501 return newSymb
; // return the newly created & inserted symbol.
504 // Somebody else inserted the new symbol so free our copy
505 newSymb
->OSString::free();
508 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
514 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
518 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
520 OSSymbol
*newSymb
= new OSSymbol
;
526 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
527 oldSymb
= pool
->insertSymbol(newSymb
);
529 if (newSymb
== oldSymb
) {
531 return newSymb
; // return the newly created & inserted symbol.
534 // Somebody else inserted the new symbol so free our copy
535 newSymb
->OSString::free();
538 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
544 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
546 OSSymbol
*probeSymbol
;
547 OSSymbolPoolState state
;
550 state
= pool
->initHashState();
551 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
552 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
553 const char *oldString
= probeSymbol
->string
;
555 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
556 ACCUMSIZE(probeSymbol
->length
);
557 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
558 probeSymbol
->flags
&= ~kOSStringNoCopy
;
564 void OSSymbol::taggedRelease(const void *tag
) const
566 super::taggedRelease(tag
);
569 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
572 super::taggedRelease(tag
, when
);
576 void OSSymbol::free()
578 pool
->removeSymbol(this);
582 bool OSSymbol::isEqualTo(const char *aCString
) const
584 return super::isEqualTo(aCString
);
587 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
589 return aSymbol
== this;
592 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
597 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
598 return isEqualTo(sym
);
599 else if ((str
= OSDynamicCast(OSString
, obj
)))
600 return super::isEqualTo(str
);
609 unsigned int arrayCount
,
613 unsigned int baseIdx
= 0;
616 for (lim
= arrayCount
; lim
; lim
>>= 1)
618 p
= (typeof(p
)) (((uintptr_t) array
) + (baseIdx
+ (lim
>> 1)) * memberSize
);
621 return (baseIdx
+ (lim
>> 1));
626 baseIdx
+= (lim
>> 1) + 1;
631 // not found, insertion point here
632 return (baseIdx
+ (lim
>> 1));