2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
28 #include <sys/cdefs.h>
31 #include <kern/lock.h>
34 #include <libkern/c++/OSSymbol.h>
35 #include <libkern/c++/OSLib.h>
38 #define super OSString
40 typedef struct { int i
, j
; } OSSymbolPoolState
;
44 extern int debug_container_malloc_size
;
46 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
54 static const unsigned int kInitBucketCount
= 16;
56 typedef struct { unsigned int count
; OSSymbol
**symbolP
; } Bucket
;
59 unsigned int nBuckets
;
63 static inline void hashSymbol(const char *s
,
67 unsigned int hash
= 0;
70 /* Unroll the loop. */
72 if (!*s
) break; len
++; hash
^= *s
++;
73 if (!*s
) break; len
++; hash
^= *s
++ << 8;
74 if (!*s
) break; len
++; hash
^= *s
++ << 16;
75 if (!*s
) break; len
++; hash
^= *s
++ << 24;
81 static unsigned long log2(unsigned int x
);
82 static unsigned long exp2ml(unsigned int x
);
84 void reconstructSymbols();
87 static void *operator new(size_t size
);
88 static void operator delete(void *mem
, size_t size
);
91 OSSymbolPool(const OSSymbolPool
*old
);
92 virtual ~OSSymbolPool();
96 inline void closeGate() { mutex_lock(poolGate
); };
97 inline void openGate() { mutex_unlock(poolGate
); };
99 OSSymbol
*findSymbol(const char *cString
) const;
100 OSSymbol
*insertSymbol(OSSymbol
*sym
);
101 void removeSymbol(OSSymbol
*sym
);
103 OSSymbolPoolState
initHashState();
104 OSSymbol
*nextHashState(OSSymbolPoolState
*stateP
);
107 void * OSSymbolPool::operator new(size_t size
)
109 void *mem
= (void *)kalloc(size
);
117 void OSSymbolPool::operator delete(void *mem
, size_t size
)
119 kfree((vm_offset_t
)mem
, size
);
123 bool OSSymbolPool::init()
126 nBuckets
= exp2ml(1 + log2(kInitBucketCount
));
127 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
128 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
132 bzero(buckets
, nBuckets
* sizeof(Bucket
));
134 poolGate
= mutex_alloc(0);
136 return poolGate
!= 0;
139 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
142 nBuckets
= old
->nBuckets
;
143 buckets
= old
->buckets
;
145 poolGate
= 0; // Do not duplicate the poolGate
148 OSSymbolPool::~OSSymbolPool()
151 kfree((vm_offset_t
)buckets
, nBuckets
* sizeof(Bucket
));
152 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
156 kfree((vm_offset_t
) poolGate
, 36 * 4);
159 unsigned long OSSymbolPool::log2(unsigned int x
)
163 for (i
= 0; x
> 1 ; i
++)
168 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
173 OSSymbolPoolState
OSSymbolPool::initHashState()
175 OSSymbolPoolState newState
= { nBuckets
, 0 };
179 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
181 Bucket
*thisBucket
= &buckets
[stateP
->i
];
188 stateP
->j
= thisBucket
->count
;
192 if (thisBucket
->count
== 1)
193 return (OSSymbol
*) thisBucket
->symbolP
;
195 return thisBucket
->symbolP
[stateP
->j
];
198 void OSSymbolPool::reconstructSymbols()
200 OSSymbolPool
old(this);
202 OSSymbolPoolState state
;
204 nBuckets
+= nBuckets
+ 1;
206 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
207 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
208 /* @@@ gvdl: Zero test and panic if can't set up pool */
209 bzero(buckets
, nBuckets
* sizeof(Bucket
));
211 state
= old
.initHashState();
212 while ( (insert
= old
.nextHashState(&state
)) )
213 insertSymbol(insert
);
216 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
219 unsigned int j
, inLen
, hash
;
220 OSSymbol
*probeSymbol
, **list
;
222 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
223 thisBucket
= &buckets
[hash
% nBuckets
];
224 j
= thisBucket
->count
;
230 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
232 if (inLen
== probeSymbol
->length
233 && (strcmp(probeSymbol
->string
, cString
) == 0))
238 for (list
= thisBucket
->symbolP
; j
--; list
++) {
240 if (inLen
== probeSymbol
->length
241 && (strcmp(probeSymbol
->string
, cString
) == 0))
248 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
250 const char *cString
= sym
->string
;
252 unsigned int j
, inLen
, hash
;
253 OSSymbol
*probeSymbol
, **list
;
255 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
256 thisBucket
= &buckets
[hash
% nBuckets
];
257 j
= thisBucket
->count
;
260 thisBucket
->symbolP
= (OSSymbol
**) sym
;
267 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
269 if (inLen
== probeSymbol
->length
270 && strcmp(probeSymbol
->string
, cString
) == 0)
273 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
274 ACCUMSIZE(2 * sizeof(OSSymbol
*));
275 /* @@@ gvdl: Zero test and panic if can't set up pool */
277 list
[1] = probeSymbol
;
278 thisBucket
->symbolP
= list
;
281 if (count
> nBuckets
)
282 reconstructSymbols();
287 for (list
= thisBucket
->symbolP
; j
--; list
++) {
289 if (inLen
== probeSymbol
->length
290 && strcmp(probeSymbol
->string
, cString
) == 0)
294 j
= thisBucket
->count
++;
296 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
297 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
298 /* @@@ gvdl: Zero test and panic if can't set up pool */
300 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
301 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
302 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
303 thisBucket
->symbolP
= list
;
304 if (count
> nBuckets
)
305 reconstructSymbols();
310 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
313 unsigned int j
, inLen
, hash
;
314 OSSymbol
*probeSymbol
, **list
;
316 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
317 thisBucket
= &buckets
[hash
% nBuckets
];
318 j
= thisBucket
->count
;
319 list
= thisBucket
->symbolP
;
325 probeSymbol
= (OSSymbol
*) list
;
327 if (probeSymbol
== sym
) {
328 thisBucket
->symbolP
= 0;
337 probeSymbol
= list
[0];
338 if (probeSymbol
== sym
) {
339 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
340 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
341 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
347 probeSymbol
= list
[1];
348 if (probeSymbol
== sym
) {
349 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
350 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
351 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
359 for (; j
--; list
++) {
361 if (probeSymbol
== sym
) {
364 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
365 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
366 if (thisBucket
->count
-1 != j
)
367 bcopy(thisBucket
->symbolP
, list
,
368 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
370 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
371 list
+ thisBucket
->count
-1-j
,
372 j
* sizeof(OSSymbol
*));
373 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
374 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
375 thisBucket
->symbolP
= list
;
384 *********************************************************************
385 * From here on we are actually implementing the OSSymbol class
386 *********************************************************************
388 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
389 OSSymbol::initialize())
390 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
391 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
392 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
393 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
394 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
395 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
396 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
397 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
399 static OSSymbolPool
*pool
;
401 void OSSymbol::initialize()
403 pool
= new OSSymbolPool
;
412 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
413 bool OSSymbol::initWithCString(const char *) { return false; }
414 bool OSSymbol::initWithString(const OSString
*) { return false; }
416 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
418 // This string may be a OSSymbol already, cheap check.
419 if (OSDynamicCast(OSSymbol
, aString
)) {
421 return (const OSSymbol
*) aString
;
423 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
424 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
426 return OSSymbol::withCString(aString
->getCStringNoCopy());
429 const OSSymbol
*OSSymbol::withCString(const char *cString
)
433 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
435 OSSymbol
*newSymb
= new OSSymbol
;
441 if (newSymb
->OSString::initWithCString(cString
))
442 oldSymb
= pool
->insertSymbol(newSymb
);
444 if (newSymb
== oldSymb
) {
446 return newSymb
; // return the newly created & inserted symbol.
449 // Somebody else inserted the new symbol so free our copy
450 newSymb
->OSString::free();
453 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
459 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
463 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
465 OSSymbol
*newSymb
= new OSSymbol
;
471 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
472 oldSymb
= pool
->insertSymbol(newSymb
);
474 if (newSymb
== oldSymb
) {
476 return newSymb
; // return the newly created & inserted symbol.
479 // Somebody else inserted the new symbol so free our copy
480 newSymb
->OSString::free();
483 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
489 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
491 OSSymbol
*probeSymbol
;
492 OSSymbolPoolState state
;
495 state
= pool
->initHashState();
496 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
497 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
498 const char *oldString
= probeSymbol
->string
;
500 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
501 ACCUMSIZE(probeSymbol
->length
);
502 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
503 probeSymbol
->flags
&= ~kOSStringNoCopy
;
509 void OSSymbol::taggedRelease(const void *tag
) const
511 super::taggedRelease(tag
);
514 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
517 super::taggedRelease(tag
, when
);
521 void OSSymbol::free()
523 pool
->removeSymbol(this);
527 bool OSSymbol::isEqualTo(const char *aCString
) const
529 return super::isEqualTo(aCString
);
532 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
534 return aSymbol
== this;
537 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
542 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
543 return isEqualTo(sym
);
544 else if ((str
= OSDynamicCast(OSString
, obj
)))
545 return super::isEqualTo(str
);