2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
25 #include <sys/cdefs.h>
28 #include <kern/lock.h>
31 #include <libkern/c++/OSSymbol.h>
32 #include <libkern/c++/OSLib.h>
35 #define super OSString
37 typedef struct { int i
, j
; } OSSymbolPoolState
;
41 extern int debug_container_malloc_size
;
43 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
51 static const unsigned int kInitBucketCount
= 16;
53 typedef struct { unsigned int count
; OSSymbol
**symbolP
; } Bucket
;
56 unsigned int nBuckets
;
60 static inline void hashSymbol(const char *s
,
64 unsigned int hash
= 0;
67 /* Unroll the loop. */
69 if (!*s
) break; len
++; hash
^= *s
++;
70 if (!*s
) break; len
++; hash
^= *s
++ << 8;
71 if (!*s
) break; len
++; hash
^= *s
++ << 16;
72 if (!*s
) break; len
++; hash
^= *s
++ << 24;
78 static unsigned long log2(unsigned int x
);
79 static unsigned long exp2ml(unsigned int x
);
81 void reconstructSymbols();
84 static void *operator new(size_t size
);
85 static void operator delete(void *mem
, size_t size
);
88 OSSymbolPool(const OSSymbolPool
*old
);
89 virtual ~OSSymbolPool();
93 inline void closeGate() { mutex_lock(poolGate
); };
94 inline void openGate() { mutex_unlock(poolGate
); };
96 OSSymbol
*findSymbol(const char *cString
) const;
97 OSSymbol
*insertSymbol(OSSymbol
*sym
);
98 void removeSymbol(OSSymbol
*sym
);
100 OSSymbolPoolState
initHashState();
101 OSSymbol
*nextHashState(OSSymbolPoolState
*stateP
);
104 void * OSSymbolPool::operator new(size_t size
)
106 void *mem
= (void *)kalloc(size
);
114 void OSSymbolPool::operator delete(void *mem
, size_t size
)
116 kfree((vm_offset_t
)mem
, size
);
120 bool OSSymbolPool::init()
123 nBuckets
= exp2ml(1 + log2(kInitBucketCount
));
124 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
125 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
129 bzero(buckets
, nBuckets
* sizeof(Bucket
));
131 poolGate
= mutex_alloc(0);
133 return poolGate
!= 0;
136 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
139 nBuckets
= old
->nBuckets
;
140 buckets
= old
->buckets
;
142 poolGate
= 0; // Do not duplicate the poolGate
145 OSSymbolPool::~OSSymbolPool()
148 kfree((vm_offset_t
)buckets
, nBuckets
* sizeof(Bucket
));
149 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
153 kfree((vm_offset_t
) poolGate
, 36 * 4);
156 unsigned long OSSymbolPool::log2(unsigned int x
)
160 for (i
= 0; x
> 1 ; i
++)
165 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
170 OSSymbolPoolState
OSSymbolPool::initHashState()
172 OSSymbolPoolState newState
= { nBuckets
, 0 };
176 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
178 Bucket
*thisBucket
= &buckets
[stateP
->i
];
185 stateP
->j
= thisBucket
->count
;
189 if (thisBucket
->count
== 1)
190 return (OSSymbol
*) thisBucket
->symbolP
;
192 return thisBucket
->symbolP
[stateP
->j
];
195 void OSSymbolPool::reconstructSymbols()
197 OSSymbolPool
old(this);
199 OSSymbolPoolState state
;
201 nBuckets
+= nBuckets
+ 1;
203 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
204 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
205 /* @@@ gvdl: Zero test and panic if can't set up pool */
206 bzero(buckets
, nBuckets
* sizeof(Bucket
));
208 state
= old
.initHashState();
209 while ( (insert
= old
.nextHashState(&state
)) )
210 insertSymbol(insert
);
213 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
216 unsigned int j
, inLen
, hash
;
217 OSSymbol
*probeSymbol
, **list
;
219 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
220 thisBucket
= &buckets
[hash
% nBuckets
];
221 j
= thisBucket
->count
;
227 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
229 if (inLen
== probeSymbol
->length
230 && (strcmp(probeSymbol
->string
, cString
) == 0))
235 for (list
= thisBucket
->symbolP
; j
--; list
++) {
237 if (inLen
== probeSymbol
->length
238 && (strcmp(probeSymbol
->string
, cString
) == 0))
245 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
247 const char *cString
= sym
->string
;
249 unsigned int j
, inLen
, hash
;
250 OSSymbol
*probeSymbol
, **list
;
252 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
253 thisBucket
= &buckets
[hash
% nBuckets
];
254 j
= thisBucket
->count
;
257 thisBucket
->symbolP
= (OSSymbol
**) sym
;
264 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
266 if (inLen
== probeSymbol
->length
267 && strcmp(probeSymbol
->string
, cString
) == 0)
270 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
271 ACCUMSIZE(2 * sizeof(OSSymbol
*));
272 /* @@@ gvdl: Zero test and panic if can't set up pool */
274 list
[1] = probeSymbol
;
275 thisBucket
->symbolP
= list
;
278 if (count
> nBuckets
)
279 reconstructSymbols();
284 for (list
= thisBucket
->symbolP
; j
--; list
++) {
286 if (inLen
== probeSymbol
->length
287 && strcmp(probeSymbol
->string
, cString
) == 0)
291 j
= thisBucket
->count
++;
293 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
294 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
295 /* @@@ gvdl: Zero test and panic if can't set up pool */
297 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
298 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
299 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
300 thisBucket
->symbolP
= list
;
301 if (count
> nBuckets
)
302 reconstructSymbols();
307 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
310 unsigned int j
, inLen
, hash
;
311 OSSymbol
*probeSymbol
, **list
;
313 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
314 thisBucket
= &buckets
[hash
% nBuckets
];
315 j
= thisBucket
->count
;
316 list
= thisBucket
->symbolP
;
322 probeSymbol
= (OSSymbol
*) list
;
324 if (probeSymbol
== sym
) {
325 thisBucket
->symbolP
= 0;
334 probeSymbol
= list
[0];
335 if (probeSymbol
== sym
) {
336 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
337 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
338 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
344 probeSymbol
= list
[1];
345 if (probeSymbol
== sym
) {
346 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
347 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
348 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
356 for (; j
--; list
++) {
358 if (probeSymbol
== sym
) {
361 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
362 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
363 if (thisBucket
->count
-1 != j
)
364 bcopy(thisBucket
->symbolP
, list
,
365 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
367 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
368 list
+ thisBucket
->count
-1-j
,
369 j
* sizeof(OSSymbol
*));
370 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
371 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
372 thisBucket
->symbolP
= list
;
381 *********************************************************************
382 * From here on we are actually implementing the OSSymbol class
383 *********************************************************************
385 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
386 OSSymbol::initialize())
387 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
388 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
389 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
390 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
391 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
392 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
393 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
394 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
396 static OSSymbolPool
*pool
;
398 void OSSymbol::initialize()
400 pool
= new OSSymbolPool
;
409 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
410 bool OSSymbol::initWithCString(const char *) { return false; }
411 bool OSSymbol::initWithString(const OSString
*) { return false; }
413 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
415 // This string may be a OSSymbol already, cheap check.
416 if (OSDynamicCast(OSSymbol
, aString
)) {
418 return (const OSSymbol
*) aString
;
420 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
421 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
423 return OSSymbol::withCString(aString
->getCStringNoCopy());
426 const OSSymbol
*OSSymbol::withCString(const char *cString
)
430 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
432 OSSymbol
*newSymb
= new OSSymbol
;
438 if (newSymb
->OSString::initWithCString(cString
))
439 oldSymb
= pool
->insertSymbol(newSymb
);
441 if (newSymb
== oldSymb
) {
443 return newSymb
; // return the newly created & inserted symbol.
446 // Somebody else inserted the new symbol so free our copy
447 newSymb
->OSString::free();
450 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
456 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
460 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
462 OSSymbol
*newSymb
= new OSSymbol
;
468 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
469 oldSymb
= pool
->insertSymbol(newSymb
);
471 if (newSymb
== oldSymb
) {
473 return newSymb
; // return the newly created & inserted symbol.
476 // Somebody else inserted the new symbol so free our copy
477 newSymb
->OSString::free();
480 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
486 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
488 OSSymbol
*probeSymbol
;
489 OSSymbolPoolState state
;
492 state
= pool
->initHashState();
493 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
494 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
495 const char *oldString
= probeSymbol
->string
;
497 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
498 ACCUMSIZE(probeSymbol
->length
);
499 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
500 probeSymbol
->flags
&= ~kOSStringNoCopy
;
506 void OSSymbol::taggedRelease(const void *tag
) const
508 super::taggedRelease(tag
);
511 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
514 super::taggedRelease(tag
, when
);
518 void OSSymbol::free()
520 pool
->removeSymbol(this);
524 bool OSSymbol::isEqualTo(const char *aCString
) const
526 return super::isEqualTo(aCString
);
529 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
531 return aSymbol
== this;
534 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
539 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
540 return isEqualTo(sym
);
541 else if ((str
= OSDynamicCast(OSString
, obj
)))
542 return super::isEqualTo(str
);