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
, OSSymbol
***replace
) 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
, OSSymbol
***replace
) 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
;
229 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
231 if (inLen
== probeSymbol
->length
232 && (strcmp(probeSymbol
->string
, cString
) == 0)) {
233 probeSymbol
->retain();
234 if (probeSymbol
->getRetainCount() != 0xffff)
238 *replace
= (OSSymbol
**) &thisBucket
->symbolP
;
243 for (list
= thisBucket
->symbolP
; j
--; list
++) {
245 if (inLen
== probeSymbol
->length
246 && (strcmp(probeSymbol
->string
, cString
) == 0)) {
247 probeSymbol
->retain();
248 if (probeSymbol
->getRetainCount() != 0xffff)
259 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
261 const char *cString
= sym
->string
;
263 unsigned int j
, inLen
, hash
;
264 OSSymbol
*probeSymbol
, **list
;
266 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
267 thisBucket
= &buckets
[hash
% nBuckets
];
268 j
= thisBucket
->count
;
271 thisBucket
->symbolP
= (OSSymbol
**) sym
;
278 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
280 if (inLen
== probeSymbol
->length
281 && strcmp(probeSymbol
->string
, cString
) == 0)
284 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
285 ACCUMSIZE(2 * sizeof(OSSymbol
*));
286 /* @@@ gvdl: Zero test and panic if can't set up pool */
288 list
[1] = probeSymbol
;
289 thisBucket
->symbolP
= list
;
292 if (count
> nBuckets
)
293 reconstructSymbols();
298 for (list
= thisBucket
->symbolP
; j
--; list
++) {
300 if (inLen
== probeSymbol
->length
301 && strcmp(probeSymbol
->string
, cString
) == 0)
305 j
= thisBucket
->count
++;
307 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
308 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
309 /* @@@ gvdl: Zero test and panic if can't set up pool */
311 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
312 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
313 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
314 thisBucket
->symbolP
= list
;
315 if (count
> nBuckets
)
316 reconstructSymbols();
321 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
324 unsigned int j
, inLen
, hash
;
325 OSSymbol
*probeSymbol
, **list
;
327 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
328 thisBucket
= &buckets
[hash
% nBuckets
];
329 j
= thisBucket
->count
;
330 list
= thisBucket
->symbolP
;
336 probeSymbol
= (OSSymbol
*) list
;
338 if (probeSymbol
== sym
) {
339 thisBucket
->symbolP
= 0;
348 probeSymbol
= list
[0];
349 if (probeSymbol
== sym
) {
350 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
351 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
352 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
358 probeSymbol
= list
[1];
359 if (probeSymbol
== sym
) {
360 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
361 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
362 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
370 for (; j
--; list
++) {
372 if (probeSymbol
== sym
) {
375 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
376 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
377 if (thisBucket
->count
-1 != j
)
378 bcopy(thisBucket
->symbolP
, list
,
379 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
381 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
382 list
+ thisBucket
->count
-1-j
,
383 j
* sizeof(OSSymbol
*));
384 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
385 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
386 thisBucket
->symbolP
= list
;
395 *********************************************************************
396 * From here on we are actually implementing the OSSymbol class
397 *********************************************************************
399 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
400 OSSymbol::initialize())
401 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
402 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
403 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
404 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
405 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
406 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
407 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
408 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
410 static OSSymbolPool
*pool
;
412 void OSSymbol::initialize()
414 pool
= new OSSymbolPool
;
423 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
424 bool OSSymbol::initWithCString(const char *) { return false; }
425 bool OSSymbol::initWithString(const OSString
*) { return false; }
427 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
429 // This string may be a OSSymbol already, cheap check.
430 if (OSDynamicCast(OSSymbol
, aString
)) {
432 return (const OSSymbol
*) aString
;
434 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
435 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
437 return OSSymbol::withCString(aString
->getCStringNoCopy());
440 const OSSymbol
*OSSymbol::withCString(const char *cString
)
446 OSSymbol
*newSymb
= pool
->findSymbol(cString
, &replace
);
447 if (!newSymb
&& (newSymb
= new OSSymbol
) ) {
448 if (newSymb
->OSString::initWithCString(cString
)) {
452 pool
->insertSymbol(newSymb
);
454 newSymb
->OSString::free();
463 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
469 OSSymbol
*newSymb
= pool
->findSymbol(cString
, &replace
);
470 if (!newSymb
&& (newSymb
= new OSSymbol
) ) {
471 if (newSymb
->OSString::initWithCStringNoCopy(cString
)) {
475 pool
->insertSymbol(newSymb
);
477 newSymb
->OSString::free();
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::free()
509 pool
->removeSymbol(this);
515 bool OSSymbol::isEqualTo(const char *aCString
) const
517 return super::isEqualTo(aCString
);
520 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
522 return aSymbol
== this;
525 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
530 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
531 return isEqualTo(sym
);
532 else if ((str
= OSDynamicCast(OSString
, obj
)))
533 return super::isEqualTo(str
);