2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOSymbol.cpp created by gvdl on Fri 1998-11-17 */
31 #include <sys/cdefs.h>
34 #include <kern/lock.h>
37 #include <libkern/c++/OSSymbol.h>
38 #include <libkern/c++/OSLib.h>
41 #define super OSString
43 typedef struct { int i
, j
; } OSSymbolPoolState
;
47 extern int debug_container_malloc_size
;
49 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
57 static const unsigned int kInitBucketCount
= 16;
59 typedef struct { unsigned int count
; OSSymbol
**symbolP
; } Bucket
;
62 unsigned int nBuckets
;
66 static inline void hashSymbol(const char *s
,
70 unsigned int hash
= 0;
73 /* Unroll the loop. */
75 if (!*s
) break; len
++; hash
^= *s
++;
76 if (!*s
) break; len
++; hash
^= *s
++ << 8;
77 if (!*s
) break; len
++; hash
^= *s
++ << 16;
78 if (!*s
) break; len
++; hash
^= *s
++ << 24;
84 static unsigned long log2(unsigned int x
);
85 static unsigned long exp2ml(unsigned int x
);
87 void reconstructSymbols();
90 static void *operator new(size_t size
);
91 static void operator delete(void *mem
, size_t size
);
94 OSSymbolPool(const OSSymbolPool
*old
);
95 virtual ~OSSymbolPool();
99 inline void closeGate() { mutex_lock(poolGate
); };
100 inline void openGate() { mutex_unlock(poolGate
); };
102 OSSymbol
*findSymbol(const char *cString
) const;
103 OSSymbol
*insertSymbol(OSSymbol
*sym
);
104 void removeSymbol(OSSymbol
*sym
);
106 OSSymbolPoolState
initHashState();
107 OSSymbol
*nextHashState(OSSymbolPoolState
*stateP
);
110 void * OSSymbolPool::operator new(size_t size
)
112 void *mem
= (void *)kalloc(size
);
120 void OSSymbolPool::operator delete(void *mem
, size_t size
)
122 kfree((vm_offset_t
)mem
, size
);
126 bool OSSymbolPool::init()
129 nBuckets
= exp2ml(1 + log2(kInitBucketCount
));
130 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
131 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
135 bzero(buckets
, nBuckets
* sizeof(Bucket
));
137 poolGate
= mutex_alloc(0);
139 return poolGate
!= 0;
142 OSSymbolPool::OSSymbolPool(const OSSymbolPool
*old
)
145 nBuckets
= old
->nBuckets
;
146 buckets
= old
->buckets
;
148 poolGate
= 0; // Do not duplicate the poolGate
151 OSSymbolPool::~OSSymbolPool()
154 kfree((vm_offset_t
)buckets
, nBuckets
* sizeof(Bucket
));
155 ACCUMSIZE(-(nBuckets
* sizeof(Bucket
)));
159 kfree((vm_offset_t
) poolGate
, 36 * 4);
162 unsigned long OSSymbolPool::log2(unsigned int x
)
166 for (i
= 0; x
> 1 ; i
++)
171 unsigned long OSSymbolPool::exp2ml(unsigned int x
)
176 OSSymbolPoolState
OSSymbolPool::initHashState()
178 OSSymbolPoolState newState
= { nBuckets
, 0 };
182 OSSymbol
*OSSymbolPool::nextHashState(OSSymbolPoolState
*stateP
)
184 Bucket
*thisBucket
= &buckets
[stateP
->i
];
191 stateP
->j
= thisBucket
->count
;
195 if (thisBucket
->count
== 1)
196 return (OSSymbol
*) thisBucket
->symbolP
;
198 return thisBucket
->symbolP
[stateP
->j
];
201 void OSSymbolPool::reconstructSymbols()
203 OSSymbolPool
old(this);
205 OSSymbolPoolState state
;
207 nBuckets
+= nBuckets
+ 1;
209 buckets
= (Bucket
*) kalloc(nBuckets
* sizeof(Bucket
));
210 ACCUMSIZE(nBuckets
* sizeof(Bucket
));
211 /* @@@ gvdl: Zero test and panic if can't set up pool */
212 bzero(buckets
, nBuckets
* sizeof(Bucket
));
214 state
= old
.initHashState();
215 while ( (insert
= old
.nextHashState(&state
)) )
216 insertSymbol(insert
);
219 OSSymbol
*OSSymbolPool::findSymbol(const char *cString
) const
222 unsigned int j
, inLen
, hash
;
223 OSSymbol
*probeSymbol
, **list
;
225 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
226 thisBucket
= &buckets
[hash
% nBuckets
];
227 j
= thisBucket
->count
;
233 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
235 if (inLen
== probeSymbol
->length
236 && (strcmp(probeSymbol
->string
, cString
) == 0))
241 for (list
= thisBucket
->symbolP
; j
--; list
++) {
243 if (inLen
== probeSymbol
->length
244 && (strcmp(probeSymbol
->string
, cString
) == 0))
251 OSSymbol
*OSSymbolPool::insertSymbol(OSSymbol
*sym
)
253 const char *cString
= sym
->string
;
255 unsigned int j
, inLen
, hash
;
256 OSSymbol
*probeSymbol
, **list
;
258 hashSymbol(cString
, &hash
, &inLen
); inLen
++;
259 thisBucket
= &buckets
[hash
% nBuckets
];
260 j
= thisBucket
->count
;
263 thisBucket
->symbolP
= (OSSymbol
**) sym
;
270 probeSymbol
= (OSSymbol
*) thisBucket
->symbolP
;
272 if (inLen
== probeSymbol
->length
273 && strcmp(probeSymbol
->string
, cString
) == 0)
276 list
= (OSSymbol
**) kalloc(2 * sizeof(OSSymbol
*));
277 ACCUMSIZE(2 * sizeof(OSSymbol
*));
278 /* @@@ gvdl: Zero test and panic if can't set up pool */
280 list
[1] = probeSymbol
;
281 thisBucket
->symbolP
= list
;
284 if (count
> nBuckets
)
285 reconstructSymbols();
290 for (list
= thisBucket
->symbolP
; j
--; list
++) {
292 if (inLen
== probeSymbol
->length
293 && strcmp(probeSymbol
->string
, cString
) == 0)
297 j
= thisBucket
->count
++;
299 list
= (OSSymbol
**) kalloc(thisBucket
->count
* sizeof(OSSymbol
*));
300 ACCUMSIZE(thisBucket
->count
* sizeof(OSSymbol
*));
301 /* @@@ gvdl: Zero test and panic if can't set up pool */
303 bcopy(thisBucket
->symbolP
, list
+ 1, j
* sizeof(OSSymbol
*));
304 kfree((vm_offset_t
)thisBucket
->symbolP
, j
* sizeof(OSSymbol
*));
305 ACCUMSIZE(-(j
* sizeof(OSSymbol
*)));
306 thisBucket
->symbolP
= list
;
307 if (count
> nBuckets
)
308 reconstructSymbols();
313 void OSSymbolPool::removeSymbol(OSSymbol
*sym
)
316 unsigned int j
, inLen
, hash
;
317 OSSymbol
*probeSymbol
, **list
;
319 hashSymbol(sym
->string
, &hash
, &inLen
); inLen
++;
320 thisBucket
= &buckets
[hash
% nBuckets
];
321 j
= thisBucket
->count
;
322 list
= thisBucket
->symbolP
;
328 probeSymbol
= (OSSymbol
*) list
;
330 if (probeSymbol
== sym
) {
331 thisBucket
->symbolP
= 0;
340 probeSymbol
= list
[0];
341 if (probeSymbol
== sym
) {
342 thisBucket
->symbolP
= (OSSymbol
**) list
[1];
343 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
344 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
350 probeSymbol
= list
[1];
351 if (probeSymbol
== sym
) {
352 thisBucket
->symbolP
= (OSSymbol
**) list
[0];
353 kfree((vm_offset_t
)list
, 2 * sizeof(OSSymbol
*));
354 ACCUMSIZE(-(2 * sizeof(OSSymbol
*)));
362 for (; j
--; list
++) {
364 if (probeSymbol
== sym
) {
367 kalloc((thisBucket
->count
-1) * sizeof(OSSymbol
*));
368 ACCUMSIZE((thisBucket
->count
-1) * sizeof(OSSymbol
*));
369 if (thisBucket
->count
-1 != j
)
370 bcopy(thisBucket
->symbolP
, list
,
371 (thisBucket
->count
-1-j
) * sizeof(OSSymbol
*));
373 bcopy(thisBucket
->symbolP
+ thisBucket
->count
-j
,
374 list
+ thisBucket
->count
-1-j
,
375 j
* sizeof(OSSymbol
*));
376 kfree((vm_offset_t
)thisBucket
->symbolP
, thisBucket
->count
* sizeof(OSSymbol
*));
377 ACCUMSIZE(-(thisBucket
->count
* sizeof(OSSymbol
*)));
378 thisBucket
->symbolP
= list
;
387 *********************************************************************
388 * From here on we are actually implementing the OSSymbol class
389 *********************************************************************
391 OSDefineMetaClassAndStructorsWithInit(OSSymbol
, OSString
,
392 OSSymbol::initialize())
393 OSMetaClassDefineReservedUnused(OSSymbol
, 0);
394 OSMetaClassDefineReservedUnused(OSSymbol
, 1);
395 OSMetaClassDefineReservedUnused(OSSymbol
, 2);
396 OSMetaClassDefineReservedUnused(OSSymbol
, 3);
397 OSMetaClassDefineReservedUnused(OSSymbol
, 4);
398 OSMetaClassDefineReservedUnused(OSSymbol
, 5);
399 OSMetaClassDefineReservedUnused(OSSymbol
, 6);
400 OSMetaClassDefineReservedUnused(OSSymbol
, 7);
402 static OSSymbolPool
*pool
;
404 void OSSymbol::initialize()
406 pool
= new OSSymbolPool
;
415 bool OSSymbol::initWithCStringNoCopy(const char *) { return false; }
416 bool OSSymbol::initWithCString(const char *) { return false; }
417 bool OSSymbol::initWithString(const OSString
*) { return false; }
419 const OSSymbol
*OSSymbol::withString(const OSString
*aString
)
421 // This string may be a OSSymbol already, cheap check.
422 if (OSDynamicCast(OSSymbol
, aString
)) {
424 return (const OSSymbol
*) aString
;
426 else if (((const OSSymbol
*) aString
)->flags
& kOSStringNoCopy
)
427 return OSSymbol::withCStringNoCopy(aString
->getCStringNoCopy());
429 return OSSymbol::withCString(aString
->getCStringNoCopy());
432 const OSSymbol
*OSSymbol::withCString(const char *cString
)
436 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
438 OSSymbol
*newSymb
= new OSSymbol
;
444 if (newSymb
->OSString::initWithCString(cString
))
445 oldSymb
= pool
->insertSymbol(newSymb
);
447 if (newSymb
== oldSymb
) {
449 return newSymb
; // return the newly created & inserted symbol.
452 // Somebody else inserted the new symbol so free our copy
453 newSymb
->OSString::free();
456 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
462 const OSSymbol
*OSSymbol::withCStringNoCopy(const char *cString
)
466 OSSymbol
*oldSymb
= pool
->findSymbol(cString
);
468 OSSymbol
*newSymb
= new OSSymbol
;
474 if (newSymb
->OSString::initWithCStringNoCopy(cString
))
475 oldSymb
= pool
->insertSymbol(newSymb
);
477 if (newSymb
== oldSymb
) {
479 return newSymb
; // return the newly created & inserted symbol.
482 // Somebody else inserted the new symbol so free our copy
483 newSymb
->OSString::free();
486 oldSymb
->retain(); // Retain the old symbol before releasing the lock.
492 void OSSymbol::checkForPageUnload(void *startAddr
, void *endAddr
)
494 OSSymbol
*probeSymbol
;
495 OSSymbolPoolState state
;
498 state
= pool
->initHashState();
499 while ( (probeSymbol
= pool
->nextHashState(&state
)) ) {
500 if (probeSymbol
->string
>= startAddr
&& probeSymbol
->string
< endAddr
) {
501 const char *oldString
= probeSymbol
->string
;
503 probeSymbol
->string
= (char *) kalloc(probeSymbol
->length
);
504 ACCUMSIZE(probeSymbol
->length
);
505 bcopy(oldString
, probeSymbol
->string
, probeSymbol
->length
);
506 probeSymbol
->flags
&= ~kOSStringNoCopy
;
512 void OSSymbol::taggedRelease(const void *tag
) const
514 super::taggedRelease(tag
);
517 void OSSymbol::taggedRelease(const void *tag
, const int when
) const
520 super::taggedRelease(tag
, when
);
524 void OSSymbol::free()
526 pool
->removeSymbol(this);
530 bool OSSymbol::isEqualTo(const char *aCString
) const
532 return super::isEqualTo(aCString
);
535 bool OSSymbol::isEqualTo(const OSSymbol
*aSymbol
) const
537 return aSymbol
== this;
540 bool OSSymbol::isEqualTo(const OSMetaClassBase
*obj
) const
545 if ((sym
= OSDynamicCast(OSSymbol
, obj
)))
546 return isEqualTo(sym
);
547 else if ((str
= OSDynamicCast(OSString
, obj
)))
548 return super::isEqualTo(str
);