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 /* OSDictionary.m created by rsulack on Fri 12-Sep-1997 */
29 /* OSDictionary.cpp converted to C++ by gvdl on Fri 1998-10-30 */
30 /* OSDictionary.cpp rewritten by gvdl on Fri 1998-10-30 */
33 #include <libkern/c++/OSDictionary.h>
34 #include <libkern/c++/OSArray.h>
35 #include <libkern/c++/OSSymbol.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSLib.h>
38 #include <libkern/c++/OSCollectionIterator.h>
40 #define super OSCollection
42 OSDefineMetaClassAndStructors(OSDictionary
, OSCollection
)
43 OSMetaClassDefineReservedUnused(OSDictionary
, 0);
44 OSMetaClassDefineReservedUnused(OSDictionary
, 1);
45 OSMetaClassDefineReservedUnused(OSDictionary
, 2);
46 OSMetaClassDefineReservedUnused(OSDictionary
, 3);
47 OSMetaClassDefineReservedUnused(OSDictionary
, 4);
48 OSMetaClassDefineReservedUnused(OSDictionary
, 5);
49 OSMetaClassDefineReservedUnused(OSDictionary
, 6);
50 OSMetaClassDefineReservedUnused(OSDictionary
, 7);
54 extern int debug_container_malloc_size
;
56 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
61 #define EXT_CAST(obj) \
62 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
64 bool OSDictionary::initWithCapacity(unsigned int inCapacity
)
69 if (inCapacity
> (UINT_MAX
/ sizeof(dictEntry
)))
72 unsigned int size
= inCapacity
* sizeof(dictEntry
);
75 dictionary
= (dictEntry
*) kalloc(size
);
79 bzero(dictionary
, size
);
83 capacity
= inCapacity
;
84 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
89 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
90 const OSSymbol
*keys
[],
91 unsigned int theCount
,
92 unsigned int theCapacity
)
94 unsigned int newCapacity
= theCount
;
96 if (!objects
|| !keys
)
100 if (theCount
> theCapacity
)
103 newCapacity
= theCapacity
;
106 if (!initWithCapacity(newCapacity
))
109 for (unsigned int i
= 0; i
< theCount
; i
++) {
110 const OSMetaClassBase
*newObject
= *objects
++;
112 if (!newObject
|| !keys
[i
] || !setObject(keys
[i
], newObject
))
119 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
120 const OSString
*keys
[],
121 unsigned int theCount
,
122 unsigned int theCapacity
)
124 unsigned int newCapacity
= theCount
;
126 if (!objects
|| !keys
)
130 if (theCount
> theCapacity
)
133 newCapacity
= theCapacity
;
136 if (!initWithCapacity(newCapacity
))
139 for (unsigned int i
= 0; i
< theCount
; i
++) {
140 const OSSymbol
*key
= OSSymbol::withString(*keys
++);
141 const OSMetaClassBase
*newObject
= *objects
++;
146 if (!newObject
|| !setObject(key
, newObject
)) {
157 bool OSDictionary::initWithDictionary(const OSDictionary
*dict
,
158 unsigned int theCapacity
)
160 unsigned int newCapacity
;
165 newCapacity
= dict
->count
;
168 if ( dict
->count
> theCapacity
)
171 newCapacity
= theCapacity
;
174 if (!initWithCapacity(newCapacity
))
177 if ((kSort
& fOptions
) && !(kSort
& dict
->fOptions
)) {
178 for (unsigned int i
= 0; i
< dict
->count
; i
++) {
179 if (!setObject(dict
->dictionary
[i
].key
, dict
->dictionary
[i
].value
)) {
187 bcopy(dict
->dictionary
, dictionary
, count
* sizeof(dictEntry
));
188 for (unsigned int i
= 0; i
< count
; i
++) {
189 dictionary
[i
].key
->taggedRetain(OSTypeID(OSCollection
));
190 dictionary
[i
].value
->taggedRetain(OSTypeID(OSCollection
));
196 OSDictionary
*OSDictionary::withCapacity(unsigned int capacity
)
198 OSDictionary
*me
= new OSDictionary
;
200 if (me
&& !me
->initWithCapacity(capacity
)) {
208 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
209 const OSSymbol
*keys
[],
211 unsigned int capacity
)
213 OSDictionary
*me
= new OSDictionary
;
215 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
223 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
224 const OSString
*keys
[],
226 unsigned int capacity
)
228 OSDictionary
*me
= new OSDictionary
;
230 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
238 OSDictionary
*OSDictionary::withDictionary(const OSDictionary
*dict
,
239 unsigned int capacity
)
241 OSDictionary
*me
= new OSDictionary
;
243 if (me
&& !me
->initWithDictionary(dict
, capacity
)) {
251 void OSDictionary::free()
253 (void) super::setOptions(0, kImmutable
);
256 kfree(dictionary
, capacity
* sizeof(dictEntry
));
257 ACCUMSIZE( -(capacity
* sizeof(dictEntry
)) );
263 unsigned int OSDictionary::getCount() const { return count
; }
264 unsigned int OSDictionary::getCapacity() const { return capacity
; }
266 unsigned int OSDictionary::getCapacityIncrement() const
268 return capacityIncrement
;
271 unsigned int OSDictionary::setCapacityIncrement(unsigned int increment
)
273 capacityIncrement
= (increment
)? increment
: 16;
275 return capacityIncrement
;
278 unsigned int OSDictionary::ensureCapacity(unsigned int newCapacity
)
281 unsigned int finalCapacity
, oldSize
, newSize
;
283 if (newCapacity
<= capacity
)
287 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
290 // integer overflow check
291 if (finalCapacity
< newCapacity
|| (finalCapacity
> (UINT_MAX
/ sizeof(dictEntry
))))
294 newSize
= sizeof(dictEntry
) * finalCapacity
;
296 newDict
= (dictEntry
*) kalloc(newSize
);
298 oldSize
= sizeof(dictEntry
) * capacity
;
300 bcopy(dictionary
, newDict
, oldSize
);
301 bzero(&newDict
[capacity
], newSize
- oldSize
);
303 ACCUMSIZE(newSize
- oldSize
);
304 kfree(dictionary
, oldSize
);
306 dictionary
= newDict
;
307 capacity
= finalCapacity
;
313 void OSDictionary::flushCollection()
317 for (unsigned int i
= 0; i
< count
; i
++) {
318 dictionary
[i
].key
->taggedRelease(OSTypeID(OSCollection
));
319 dictionary
[i
].value
->taggedRelease(OSTypeID(OSCollection
));
325 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
)
330 if (!anObject
|| !aKey
)
333 // if the key exists, replace the object
335 if (fOptions
& kSort
) {
336 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
337 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
338 } else for (exists
= false, i
= 0; i
< count
; i
++) {
339 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
343 const OSMetaClassBase
*oldObject
= dictionary
[i
].value
;
347 anObject
->taggedRetain(OSTypeID(OSCollection
));
348 dictionary
[i
].value
= anObject
;
350 oldObject
->taggedRelease(OSTypeID(OSCollection
));
354 // add new key, possibly extending our capacity
355 if (count
>= capacity
&& count
>= ensureCapacity(count
+1))
360 bcopy(&dictionary
[i
], &dictionary
[i
+1], (count
- i
) * sizeof(dictionary
[0]));
362 aKey
->taggedRetain(OSTypeID(OSCollection
));
363 anObject
->taggedRetain(OSTypeID(OSCollection
));
364 dictionary
[i
].key
= aKey
;
365 dictionary
[i
].value
= anObject
;
371 void OSDictionary::removeObject(const OSSymbol
*aKey
)
379 // if the key exists, remove the object
381 if (fOptions
& kSort
) {
382 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
383 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
384 } else for (exists
= false, i
= 0; i
< count
; i
++) {
385 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
389 dictEntry oldEntry
= dictionary
[i
];
394 bcopy(&dictionary
[i
+1], &dictionary
[i
], (count
- i
) * sizeof(dictionary
[0]));
396 oldEntry
.key
->taggedRelease(OSTypeID(OSCollection
));
397 oldEntry
.value
->taggedRelease(OSTypeID(OSCollection
));
403 // Returns true on success, false on an error condition.
404 bool OSDictionary::merge(const OSDictionary
*srcDict
)
406 const OSSymbol
* sym
;
407 OSCollectionIterator
* iter
;
409 if ( !OSDynamicCast(OSDictionary
, srcDict
) )
412 iter
= OSCollectionIterator::withCollection(const_cast<OSDictionary
*>(srcDict
));
416 while ( (sym
= (const OSSymbol
*)iter
->getNextObject()) ) {
417 const OSMetaClassBase
* obj
;
419 obj
= srcDict
->getObject(sym
);
420 if ( !setObject(sym
, obj
) ) {
430 OSObject
*OSDictionary::getObject(const OSSymbol
*aKey
) const
438 // if the key exists, return the object
440 if (fOptions
& kSort
) {
441 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
442 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
443 } else for (exists
= false, i
= 0; i
< count
; i
++) {
444 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
448 return (const_cast<OSObject
*> ((const OSObject
*)dictionary
[i
].value
));
455 #define OBJECT_WRAP_1(cmd, k) \
457 const OSSymbol *tmpKey = k; \
458 OSObject *retObj = cmd(tmpKey); \
464 #define OBJECT_WRAP_2(cmd, k, o) \
466 const OSSymbol *tmpKey = k; \
467 bool ret = cmd(tmpKey, o); \
473 #define OBJECT_WRAP_3(cmd, k) \
475 const OSSymbol *tmpKey = k; \
481 bool OSDictionary::setObject(const OSString
*aKey
, const OSMetaClassBase
*anObject
)
482 OBJECT_WRAP_2(setObject
, OSSymbol::withString(aKey
), anObject
)
483 bool OSDictionary::setObject(const char *aKey
, const OSMetaClassBase
*anObject
)
484 OBJECT_WRAP_2(setObject
, OSSymbol::withCString(aKey
), anObject
)
486 OSObject
*OSDictionary::getObject(const OSString
*aKey
) const
487 OBJECT_WRAP_1(getObject
, OSSymbol::withString(aKey
))
488 OSObject
*OSDictionary::getObject(const char *aKey
) const
489 OBJECT_WRAP_1(getObject
, OSSymbol::withCString(aKey
))
491 void OSDictionary::removeObject(const OSString
*aKey
)
492 OBJECT_WRAP_3(removeObject
, OSSymbol::withString(aKey
))
493 void OSDictionary::removeObject(const char *aKey
)
494 OBJECT_WRAP_3(removeObject
, OSSymbol::withCString(aKey
))
497 OSDictionary::isEqualTo(const OSDictionary
*srcDict
, const OSCollection
*keys
) const
499 OSCollectionIterator
* iter
;
500 unsigned int keysCount
;
501 const OSMetaClassBase
* obj1
;
502 const OSMetaClassBase
* obj2
;
506 if ( this == srcDict
)
509 keysCount
= keys
->getCount();
510 if ( (count
< keysCount
) || (srcDict
->getCount() < keysCount
) )
513 iter
= OSCollectionIterator::withCollection(keys
);
518 while ( (aKey
= OSDynamicCast(OSString
, iter
->getNextObject())) ) {
519 obj1
= getObject(aKey
);
520 obj2
= srcDict
->getObject(aKey
);
521 if ( !obj1
|| !obj2
) {
526 if ( !obj1
->isEqualTo(obj2
) ) {
536 bool OSDictionary::isEqualTo(const OSDictionary
*srcDict
) const
539 const OSMetaClassBase
* obj
;
541 if ( this == srcDict
)
544 if ( count
!= srcDict
->getCount() )
547 for ( i
= 0; i
< count
; i
++ ) {
548 obj
= srcDict
->getObject(dictionary
[i
].key
);
552 if ( !dictionary
[i
].value
->isEqualTo(obj
) )
559 bool OSDictionary::isEqualTo(const OSMetaClassBase
*anObject
) const
563 dict
= OSDynamicCast(OSDictionary
, anObject
);
565 return isEqualTo(dict
);
570 unsigned int OSDictionary::iteratorSize() const
572 return sizeof(unsigned int);
575 bool OSDictionary::initIterator(void *inIterator
) const
577 unsigned int *iteratorP
= (unsigned int *) inIterator
;
583 bool OSDictionary::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
585 unsigned int *iteratorP
= (unsigned int *) inIterator
;
586 unsigned int index
= (*iteratorP
)++;
589 *ret
= (OSObject
*) dictionary
[index
].key
;
596 bool OSDictionary::serialize(OSSerialize
*s
) const
598 if (s
->previouslySerialized(this)) return true;
600 if (!s
->addXMLStartTag(this, "dict")) return false;
602 for (unsigned i
= 0; i
< count
; i
++) {
603 const OSSymbol
*key
= dictionary
[i
].key
;
605 // due the nature of the XML syntax, this must be a symbol
606 if (!key
->metaCast("OSSymbol")) {
609 if (!s
->addString("<key>")) return false;
610 const char *c
= key
->getCStringNoCopy();
613 if (!s
->addString("<")) return false;
614 } else if (*c
== '>') {
615 if (!s
->addString(">")) return false;
616 } else if (*c
== '&') {
617 if (!s
->addString("&")) return false;
619 if (!s
->addChar(*c
)) return false;
623 if (!s
->addXMLEndTag("key")) return false;
625 if (!dictionary
[i
].value
->serialize(s
)) return false;
628 return s
->addXMLEndTag("dict");
631 unsigned OSDictionary::setOptions(unsigned options
, unsigned mask
, void *)
633 unsigned old
= super::setOptions(options
, mask
);
634 if ((old
^ options
) & mask
) {
636 // Value changed need to recurse over all of the child collections
637 for ( unsigned i
= 0; i
< count
; i
++ ) {
638 OSCollection
*v
= OSDynamicCast(OSCollection
, dictionary
[i
].value
);
640 v
->setOptions(options
, mask
);
647 OSCollection
* OSDictionary::copyCollection(OSDictionary
*cycleDict
)
649 bool allocDict
= !cycleDict
;
650 OSCollection
*ret
= 0;
651 OSDictionary
*newDict
= 0;
654 cycleDict
= OSDictionary::withCapacity(16);
661 ret
= super::copyCollection(cycleDict
);
665 newDict
= OSDictionary::withDictionary(this);
669 // Insert object into cycle Dictionary
670 cycleDict
->setObject((const OSSymbol
*) this, newDict
);
672 for (unsigned int i
= 0; i
< count
; i
++) {
673 const OSMetaClassBase
*obj
= dictionary
[i
].value
;
674 OSCollection
*coll
= OSDynamicCast(OSCollection
, EXT_CAST(obj
));
677 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
681 newDict
->dictionary
[i
].value
= newColl
;
683 coll
->taggedRelease(OSTypeID(OSCollection
));
684 newColl
->taggedRetain(OSTypeID(OSCollection
));
699 cycleDict
->release();