2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
33 #include <sys/cdefs.h>
36 #include <kern/lock.h>
39 #include <libkern/c++/OSSymbol.h>
40 #include <libkern/c++/OSLib.h>
43 #define super OSString
45 typedef struct { int i
, j
; } OSSymbolPoolState
;
49 extern int debug_container_malloc_size
;
51 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
59 static const unsigned int kInitBucketCount
= 16;
61 typedef struct { unsigned int count
; OSSymbol
**symbolP
; } Bucket
;
64 unsigned int nBuckets
;
68 static inline void hashSymbol(const char *s
,
72 unsigned int hash
= 0;
75 /* Unroll the loop. */
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;
86 static unsigned long log2(unsigned int x
);
87 static unsigned long exp2ml(unsigned int x
);
89 void reconstructSymbols();
92 static void *operator new(size_t size
);
93 static void operator delete(void *mem
, size_t size
);
96 OSSymbolPool(const OSSymbolPool
*old
);
97 virtual ~OSSymbolPool();
101 inline void closeGate() { mutex_lock(poolGate
); };
102 inline void openGate() { mutex_unlock(poolGate
); };
104 OSSymbol
*findSymbol(const char *cString
) const;
105 OSSymbol
*insertSymbol(OSSymbol
*sym
);
106 void removeSymbol(OSSymbol
*sym
);
108 OSSymbolPoolState
initHashState();
109 OSSymbol
*nextHashState(OSSymbolPoolState
*stateP
);
112 void * OSSymbolPool::operator new(size_t size
)
114 void *mem
= (void *)kalloc(size
);
122 void OSSymbolPool::operator delete(void *mem
, size_t size
)
124 kfree((vm_offset_t
)mem
, size
);
128 bool OSSymbolPool::init()
131 nBuckets
= exp2ml(1 + log2(kInitBucketCount
));
132 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
133 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
137 bzero(buckets
, nBuckets
* sizeof(Bucket
));
139 poolGate
= mutex_alloc(0);
141 return poolGate
!= 0;
144 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
147 nBuckets
= old
->nBuckets
;
148 buckets
= old
->buckets
;
150 poolGate
= 0; // Do not duplicate the poolGate
153 OSSymbolPool::~OSSymbolPool()
156 kfree((vm_offset_t
)buckets
, nBuckets
* sizeof(Bucket
));
157 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
161 kfree((vm_offset_t
) poolGate
, 36 * 4);
164 unsigned long OSSymbolPool::log2(unsigned int x
)
168 for (i
= 0; x
> 1 ; i
++)
173 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
178 OSSymbolPoolState
OSSymbolPool::initHashState()
180 OSSymbolPoolState newState
= { nBuckets
, 0 };
184 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
186 Bucket
*thisBucket
= &buckets
[stateP
->i
];
193 stateP
->j
= thisBucket
->count
;
197 if (thisBucket
->count
== 1)
198 return (OSSymbol
*) thisBucket
->symbolP
;
200 return thisBucket
->symbolP
[stateP
->j
];
203 void OSSymbolPool::reconstructSymbols()
205 OSSymbolPool
old(this);
207 OSSymbolPoolState state
;
209 nBuckets
+= nBuckets
+ 1;
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
));
216 state
= old
.initHashState();
217 while ( (insert
= old
.nextHashState(&state
)) )
218 insertSymbol(insert
);
221 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
224 unsigned int j
, inLen
, hash
;
225 OSSymbol
*probeSymbol
, **list
;
227 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
228 thisBucket
= &buckets
[hash
% nBuckets
];
229 j
= thisBucket
->count
;
235 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
237 if (inLen
== probeSymbol
->length
238 && (strcmp(probeSymbol
->string
, cString
) == 0))
243 for (list
= thisBucket
->symbolP
; j
--; list
++) {
245 if (inLen
== probeSymbol
->length
246 && (strcmp(probeSymbol
->string
, cString
) == 0))
253 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
255 const char *cString
= sym
->string
;
257 unsigned int j
, inLen
, hash
;
258 OSSymbol
*probeSymbol
, **list
;
260 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
261 thisBucket
= &buckets
[hash
% nBuckets
];
262 j
= thisBucket
->count
;
265 thisBucket
->symbolP
= (OSSymbol
**) sym
;
272 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
274 if (inLen
== probeSymbol
->length
275 && strcmp(probeSymbol
->string
, cString
) == 0)
278 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
279 ACCUMSIZE(2 * sizeof(OSSymbol
*));
280 /* @@@ gvdl: Zero test and panic if can't set up pool */
282 list
[1] = probeSymbol
;
283 thisBucket
->symbolP
= list
;
286 if (count
> nBuckets
)
287 reconstructSymbols();
292 for (list
= thisBucket
->symbolP
; j
--; list
++) {
294 if (inLen
== probeSymbol
->length
295 && strcmp(probeSymbol
->string
, cString
) == 0)
299 j
= thisBucket
->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 */
305 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
306 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
307 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
308 thisBucket
->symbolP
= list
;
309 if (count
> nBuckets
)
310 reconstructSymbols();
315 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
318 unsigned int j
, inLen
, hash
;
319 OSSymbol
*probeSymbol
, **list
;
321 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
322 thisBucket
= &buckets
[hash
% nBuckets
];
323 j
= thisBucket
->count
;
324 list
= thisBucket
->symbolP
;
330 probeSymbol
= (OSSymbol
*) list
;
332 if (probeSymbol
== sym
) {
333 thisBucket
->symbolP
= 0;
342 probeSymbol
= list
[0];
343 if (probeSymbol
== sym
) {
344 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
345 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
346 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
352 probeSymbol
= list
[1];
353 if (probeSymbol
== sym
) {
354 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
355 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
356 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
364 for (; j
--; list
++) {
366 if (probeSymbol
== sym
) {
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
*));
375 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
376 list
+ thisBucket
->count
-1-j
,
377 j
* sizeof(OSSymbol
*));
378 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
379 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
380 thisBucket
->symbolP
= list
;
389 *********************************************************************
390 * From here on we are actually implementing the OSSymbol class
391 *********************************************************************
393 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
394 OSSymbol::initialize())
395 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
396 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
397 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
398 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
399 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
400 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
401 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
402 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
404 static OSSymbolPool
*pool
;
406 void OSSymbol::initialize()
408 pool
= new OSSymbolPool
;
417 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
418 bool OSSymbol::initWithCString(const char *) { return false; }
419 bool OSSymbol::initWithString(const OSString
*) { return false; }
421 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
423 // This string may be a OSSymbol already, cheap check.
424 if (OSDynamicCast(OSSymbol
, aString
)) {
426 return (const OSSymbol
*) aString
;
428 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
429 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
431 return OSSymbol::withCString(aString
->getCStringNoCopy());
434 const OSSymbol
*OSSymbol::withCString(const char *cString
)
438 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
440 OSSymbol
*newSymb
= new OSSymbol
;
446 if (newSymb
->OSString::initWithCString(cString
))
447 oldSymb
= pool
->insertSymbol(newSymb
);
449 if (newSymb
== oldSymb
) {
451 return newSymb
; // return the newly created & inserted symbol.
454 // Somebody else inserted the new symbol so free our copy
455 newSymb
->OSString::free();
458 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
464 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
468 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
470 OSSymbol
*newSymb
= new OSSymbol
;
476 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
477 oldSymb
= pool
->insertSymbol(newSymb
);
479 if (newSymb
== oldSymb
) {
481 return newSymb
; // return the newly created & inserted symbol.
484 // Somebody else inserted the new symbol so free our copy
485 newSymb
->OSString::free();
488 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
494 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
496 OSSymbol
*probeSymbol
;
497 OSSymbolPoolState state
;
500 state
= pool
->initHashState();
501 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
502 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
503 const char *oldString
= probeSymbol
->string
;
505 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
506 ACCUMSIZE(probeSymbol
->length
);
507 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
508 probeSymbol
->flags
&= ~kOSStringNoCopy
;
514 void OSSymbol::taggedRelease(const void *tag
) const
516 super::taggedRelease(tag
);
519 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
522 super::taggedRelease(tag
, when
);
526 void OSSymbol::free()
528 pool
->removeSymbol(this);
532 bool OSSymbol::isEqualTo(const char *aCString
) const
534 return super::isEqualTo(aCString
);
537 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
539 return aSymbol
== this;
542 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
547 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
548 return isEqualTo(sym
);
549 else if ((str
= OSDynamicCast(OSString
, obj
)))
550 return super::isEqualTo(str
);