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
, OSSymbol
***replace
) 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
, OSSymbol
***replace
) 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
;
232 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
234 if (inLen
== probeSymbol
->length
235 && (strcmp(probeSymbol
->string
, cString
) == 0)) {
236 probeSymbol
->retain();
237 if (probeSymbol
->getRetainCount() != 0xffff)
241 *replace
= (OSSymbol
**) &thisBucket
->symbolP
;
246 for (list
= thisBucket
->symbolP
; j
--; list
++) {
248 if (inLen
== probeSymbol
->length
249 && (strcmp(probeSymbol
->string
, cString
) == 0)) {
250 probeSymbol
->retain();
251 if (probeSymbol
->getRetainCount() != 0xffff)
262 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
264 const char *cString
= sym
->string
;
266 unsigned int j
, inLen
, hash
;
267 OSSymbol
*probeSymbol
, **list
;
269 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
270 thisBucket
= &buckets
[hash
% nBuckets
];
271 j
= thisBucket
->count
;
274 thisBucket
->symbolP
= (OSSymbol
**) sym
;
281 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
283 if (inLen
== probeSymbol
->length
284 && strcmp(probeSymbol
->string
, cString
) == 0)
287 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
288 ACCUMSIZE(2 * sizeof(OSSymbol
*));
289 /* @@@ gvdl: Zero test and panic if can't set up pool */
291 list
[1] = probeSymbol
;
292 thisBucket
->symbolP
= list
;
295 if (count
> nBuckets
)
296 reconstructSymbols();
301 for (list
= thisBucket
->symbolP
; j
--; list
++) {
303 if (inLen
== probeSymbol
->length
304 && strcmp(probeSymbol
->string
, cString
) == 0)
308 j
= thisBucket
->count
++;
310 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
311 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
312 /* @@@ gvdl: Zero test and panic if can't set up pool */
314 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
315 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
316 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
317 thisBucket
->symbolP
= list
;
318 if (count
> nBuckets
)
319 reconstructSymbols();
324 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
327 unsigned int j
, inLen
, hash
;
328 OSSymbol
*probeSymbol
, **list
;
330 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
331 thisBucket
= &buckets
[hash
% nBuckets
];
332 j
= thisBucket
->count
;
333 list
= thisBucket
->symbolP
;
339 probeSymbol
= (OSSymbol
*) list
;
341 if (probeSymbol
== sym
) {
342 thisBucket
->symbolP
= 0;
351 probeSymbol
= list
[0];
352 if (probeSymbol
== sym
) {
353 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
354 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
355 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
361 probeSymbol
= list
[1];
362 if (probeSymbol
== sym
) {
363 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
364 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
365 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
373 for (; j
--; list
++) {
375 if (probeSymbol
== sym
) {
378 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
379 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
380 if (thisBucket
->count
-1 != j
)
381 bcopy(thisBucket
->symbolP
, list
,
382 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
384 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
385 list
+ thisBucket
->count
-1-j
,
386 j
* sizeof(OSSymbol
*));
387 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
388 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
389 thisBucket
->symbolP
= list
;
398 *********************************************************************
399 * From here on we are actually implementing the OSSymbol class
400 *********************************************************************
402 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
403 OSSymbol::initialize())
404 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
405 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
406 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
407 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
408 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
409 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
410 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
411 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
413 static OSSymbolPool
*pool
;
415 void OSSymbol::initialize()
417 pool
= new OSSymbolPool
;
426 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
427 bool OSSymbol::initWithCString(const char *) { return false; }
428 bool OSSymbol::initWithString(const OSString
*) { return false; }
430 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
432 // This string may be a OSSymbol already, cheap check.
433 if (OSDynamicCast(OSSymbol
, aString
)) {
435 return (const OSSymbol
*) aString
;
437 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
438 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
440 return OSSymbol::withCString(aString
->getCStringNoCopy());
443 const OSSymbol
*OSSymbol::withCString(const char *cString
)
449 OSSymbol
*newSymb
= pool
->findSymbol(cString
, &replace
);
450 if (!newSymb
&& (newSymb
= new OSSymbol
) ) {
451 if (newSymb
->OSString::initWithCString(cString
)) {
455 pool
->insertSymbol(newSymb
);
457 newSymb
->OSString::free();
466 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
472 OSSymbol
*newSymb
= pool
->findSymbol(cString
, &replace
);
473 if (!newSymb
&& (newSymb
= new OSSymbol
) ) {
474 if (newSymb
->OSString::initWithCStringNoCopy(cString
)) {
478 pool
->insertSymbol(newSymb
);
480 newSymb
->OSString::free();
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::free()
512 pool
->removeSymbol(this);
518 bool OSSymbol::isEqualTo(const char *aCString
) const
520 return super::isEqualTo(aCString
);
523 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
525 return aSymbol
== this;
528 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
533 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
534 return isEqualTo(sym
);
535 else if ((str
= OSDynamicCast(OSString
, obj
)))
536 return super::isEqualTo(str
);