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 { unsigned 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()
176 for (thisBucket
= &buckets
[0]; thisBucket
< &buckets
[nBuckets
]; thisBucket
++) {
177 if (thisBucket
->count
> 1) {
178 kfree(thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
179 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
182 kfree(buckets
, nBuckets
* sizeof(Bucket
));
183 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
187 lck_mtx_free(poolGate
, IOLockGroup
);
190 unsigned long OSSymbolPool::log2(unsigned int x
)
194 for (i
= 0; x
> 1 ; i
++)
199 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
204 OSSymbolPoolState
OSSymbolPool::initHashState()
206 OSSymbolPoolState newState
= { nBuckets
, 0 };
210 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
212 Bucket
*thisBucket
= &buckets
[stateP
->i
];
219 stateP
->j
= thisBucket
->count
;
223 if (thisBucket
->count
== 1)
224 return (OSSymbol
*) thisBucket
->symbolP
;
226 return thisBucket
->symbolP
[stateP
->j
];
229 void OSSymbolPool::reconstructSymbols(void)
231 this->reconstructSymbols(true);
234 void OSSymbolPool::reconstructSymbols(bool grow
)
236 unsigned int new_nBuckets
= nBuckets
;
238 OSSymbolPoolState state
;
241 new_nBuckets
+= new_nBuckets
+ 1;
243 /* Don't shrink the pool below the default initial size.
245 if (nBuckets
<= INITIAL_POOL_SIZE
) {
248 new_nBuckets
= (new_nBuckets
- 1) / 2;
251 /* Create old pool to iterate after doing above check, cause it
252 * gets finalized at return.
254 OSSymbolPool
old(this);
257 nBuckets
= new_nBuckets
;
258 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
259 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
260 /* @@@ gvdl: Zero test and panic if can't set up pool */
261 bzero(buckets
, nBuckets
* sizeof(Bucket
));
263 state
= old
.initHashState();
264 while ( (insert
= old
.nextHashState(&state
)) )
265 insertSymbol(insert
);
268 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
271 unsigned int j
, inLen
, hash
;
272 OSSymbol
*probeSymbol
, **list
;
274 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
275 thisBucket
= &buckets
[hash
% nBuckets
];
276 j
= thisBucket
->count
;
282 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
284 if (inLen
== probeSymbol
->length
285 && (strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0))
290 for (list
= thisBucket
->symbolP
; j
--; list
++) {
292 if (inLen
== probeSymbol
->length
293 && (strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0))
300 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
302 const char *cString
= sym
->string
;
304 unsigned int j
, inLen
, hash
;
305 OSSymbol
*probeSymbol
, **list
;
307 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
308 thisBucket
= &buckets
[hash
% nBuckets
];
309 j
= thisBucket
->count
;
312 thisBucket
->symbolP
= (OSSymbol
**) sym
;
319 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
321 if (inLen
== probeSymbol
->length
322 && strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0)
325 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
326 ACCUMSIZE(2 * sizeof(OSSymbol
*));
327 /* @@@ gvdl: Zero test and panic if can't set up pool */
329 list
[1] = probeSymbol
;
330 thisBucket
->symbolP
= list
;
338 for (list
= thisBucket
->symbolP
; j
--; list
++) {
340 if (inLen
== probeSymbol
->length
341 && strncmp(probeSymbol
->string
, cString
, probeSymbol
->length
) == 0)
345 j
= thisBucket
->count
++;
347 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
348 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
349 /* @@@ gvdl: Zero test and panic if can't set up pool */
351 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
352 kfree(thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
353 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
354 thisBucket
->symbolP
= list
;
360 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
363 unsigned int j
, inLen
, hash
;
364 OSSymbol
*probeSymbol
, **list
;
366 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
367 thisBucket
= &buckets
[hash
% nBuckets
];
368 j
= thisBucket
->count
;
369 list
= thisBucket
->symbolP
;
372 // couldn't find the symbol; probably means string hash changed
373 panic("removeSymbol %s count %d ", sym
->string
? sym
->string
: "no string", count
);
378 probeSymbol
= (OSSymbol
*) list
;
380 if (probeSymbol
== sym
) {
381 thisBucket
->symbolP
= 0;
387 // couldn't find the symbol; probably means string hash changed
388 panic("removeSymbol %s count %d ", sym
->string
? sym
->string
: "no string", count
);
393 probeSymbol
= list
[0];
394 if (probeSymbol
== sym
) {
395 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
396 kfree(list
, 2 * sizeof(OSSymbol
*));
397 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
404 probeSymbol
= list
[1];
405 if (probeSymbol
== sym
) {
406 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
407 kfree(list
, 2 * sizeof(OSSymbol
*));
408 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
414 // couldn't find the symbol; probably means string hash changed
415 panic("removeSymbol %s count %d ", sym
->string
? sym
->string
: "no string", count
);
419 for (; j
--; list
++) {
421 if (probeSymbol
== sym
) {
424 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
425 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
426 if (thisBucket
->count
-1 != j
)
427 bcopy(thisBucket
->symbolP
, list
,
428 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
430 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
431 list
+ thisBucket
->count
-1-j
,
432 j
* sizeof(OSSymbol
*));
433 kfree(thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
434 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
435 thisBucket
->symbolP
= list
;
441 // couldn't find the symbol; probably means string hash changed
442 panic("removeSymbol %s count %d ", sym
->string
? sym
->string
: "no string", count
);
446 *********************************************************************
447 * From here on we are actually implementing the OSSymbol class
448 *********************************************************************
450 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
451 OSSymbol::initialize())
452 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
453 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
454 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
455 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
456 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
457 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
458 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
459 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
461 static OSSymbolPool
*pool
;
463 void OSSymbol::initialize()
465 pool
= new OSSymbolPool
;
474 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
475 bool OSSymbol::initWithCString(const char *) { return false; }
476 bool OSSymbol::initWithString(const OSString
*) { return false; }
478 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
480 // This string may be a OSSymbol already, cheap check.
481 if (OSDynamicCast(OSSymbol
, aString
)) {
483 return (const OSSymbol
*) aString
;
485 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
486 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
488 return OSSymbol::withCString(aString
->getCStringNoCopy());
491 const OSSymbol
*OSSymbol::withCString(const char *cString
)
495 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
497 OSSymbol
*newSymb
= new OSSymbol
;
503 if (newSymb
->OSString::initWithCString(cString
))
504 oldSymb
= pool
->insertSymbol(newSymb
);
506 if (newSymb
== oldSymb
) {
508 return newSymb
; // return the newly created & inserted symbol.
511 // Somebody else inserted the new symbol so free our copy
512 newSymb
->OSString::free();
515 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
521 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
525 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
527 OSSymbol
*newSymb
= new OSSymbol
;
533 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
534 oldSymb
= pool
->insertSymbol(newSymb
);
536 if (newSymb
== oldSymb
) {
538 return newSymb
; // return the newly created & inserted symbol.
541 // Somebody else inserted the new symbol so free our copy
542 newSymb
->OSString::free();
545 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
551 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
553 OSSymbol
*probeSymbol
;
554 OSSymbolPoolState state
;
557 state
= pool
->initHashState();
558 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
559 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
560 const char *oldString
= probeSymbol
->string
;
562 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
563 ACCUMSIZE(probeSymbol
->length
);
564 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
565 probeSymbol
->flags
&= ~kOSStringNoCopy
;
571 void OSSymbol::taggedRelease(const void *tag
) const
573 super::taggedRelease(tag
);
576 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
579 super::taggedRelease(tag
, when
);
583 void OSSymbol::free()
585 pool
->removeSymbol(this);
589 bool OSSymbol::isEqualTo(const char *aCString
) const
591 return super::isEqualTo(aCString
);
594 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
596 return aSymbol
== this;
599 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
604 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
605 return isEqualTo(sym
);
606 else if ((str
= OSDynamicCast(OSString
, obj
)))
607 return super::isEqualTo(str
);
616 unsigned int arrayCount
,
620 unsigned int baseIdx
= 0;
623 for (lim
= arrayCount
; lim
; lim
>>= 1)
625 p
= (typeof(p
)) (((uintptr_t) array
) + (baseIdx
+ (lim
>> 1)) * memberSize
);
628 return (baseIdx
+ (lim
>> 1));
633 baseIdx
+= (lim
>> 1) + 1;
638 // not found, insertion point here
639 return (baseIdx
+ (lim
>> 1));