]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSDictionary.cpp
a53f23ad730eb1889591f733c826e963f177e4f0
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);
52 #define EXT_CAST(obj) \
53 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
55 bool OSDictionary::initWithCapacity(unsigned int inCapacity
)
60 if (inCapacity
> (UINT_MAX
/ sizeof(dictEntry
)))
63 unsigned int size
= inCapacity
* sizeof(dictEntry
);
66 dictionary
= (dictEntry
*) kalloc_container(size
);
70 bzero(dictionary
, size
);
71 OSCONTAINER_ACCUMSIZE(size
);
74 capacity
= inCapacity
;
75 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
80 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
81 const OSSymbol
*keys
[],
82 unsigned int theCount
,
83 unsigned int theCapacity
)
85 unsigned int newCapacity
= theCount
;
87 if (!objects
|| !keys
)
91 if (theCount
> theCapacity
)
94 newCapacity
= theCapacity
;
97 if (!initWithCapacity(newCapacity
))
100 for (unsigned int i
= 0; i
< theCount
; i
++) {
101 const OSMetaClassBase
*newObject
= *objects
++;
103 if (!newObject
|| !keys
[i
] || !setObject(keys
[i
], newObject
))
110 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
111 const OSString
*keys
[],
112 unsigned int theCount
,
113 unsigned int theCapacity
)
115 unsigned int newCapacity
= theCount
;
117 if (!objects
|| !keys
)
121 if (theCount
> theCapacity
)
124 newCapacity
= theCapacity
;
127 if (!initWithCapacity(newCapacity
))
130 for (unsigned int i
= 0; i
< theCount
; i
++) {
131 const OSSymbol
*key
= OSSymbol::withString(*keys
++);
132 const OSMetaClassBase
*newObject
= *objects
++;
137 if (!newObject
|| !setObject(key
, newObject
)) {
148 bool OSDictionary::initWithDictionary(const OSDictionary
*dict
,
149 unsigned int theCapacity
)
151 unsigned int newCapacity
;
156 newCapacity
= dict
->count
;
159 if ( dict
->count
> theCapacity
)
162 newCapacity
= theCapacity
;
165 if (!initWithCapacity(newCapacity
))
168 if ((kSort
& fOptions
) && !(kSort
& dict
->fOptions
)) {
169 for (unsigned int i
= 0; i
< dict
->count
; i
++) {
170 if (!setObject(dict
->dictionary
[i
].key
, dict
->dictionary
[i
].value
)) {
178 bcopy(dict
->dictionary
, dictionary
, count
* sizeof(dictEntry
));
179 for (unsigned int i
= 0; i
< count
; i
++) {
180 dictionary
[i
].key
->taggedRetain(OSTypeID(OSCollection
));
181 dictionary
[i
].value
->taggedRetain(OSTypeID(OSCollection
));
187 OSDictionary
*OSDictionary::withCapacity(unsigned int capacity
)
189 OSDictionary
*me
= new OSDictionary
;
191 if (me
&& !me
->initWithCapacity(capacity
)) {
199 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
200 const OSSymbol
*keys
[],
202 unsigned int capacity
)
204 OSDictionary
*me
= new OSDictionary
;
206 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
214 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
215 const OSString
*keys
[],
217 unsigned int capacity
)
219 OSDictionary
*me
= new OSDictionary
;
221 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
229 OSDictionary
*OSDictionary::withDictionary(const OSDictionary
*dict
,
230 unsigned int capacity
)
232 OSDictionary
*me
= new OSDictionary
;
234 if (me
&& !me
->initWithDictionary(dict
, capacity
)) {
242 void OSDictionary::free()
244 (void) super::setOptions(0, kImmutable
);
247 kfree(dictionary
, capacity
* sizeof(dictEntry
));
248 OSCONTAINER_ACCUMSIZE( -(capacity
* sizeof(dictEntry
)) );
254 unsigned int OSDictionary::getCount() const { return count
; }
255 unsigned int OSDictionary::getCapacity() const { return capacity
; }
257 unsigned int OSDictionary::getCapacityIncrement() const
259 return capacityIncrement
;
262 unsigned int OSDictionary::setCapacityIncrement(unsigned int increment
)
264 capacityIncrement
= (increment
)? increment
: 16;
266 return capacityIncrement
;
269 unsigned int OSDictionary::ensureCapacity(unsigned int newCapacity
)
272 unsigned int finalCapacity
, oldSize
, newSize
;
274 if (newCapacity
<= capacity
)
278 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
281 // integer overflow check
282 if (finalCapacity
< newCapacity
|| (finalCapacity
> (UINT_MAX
/ sizeof(dictEntry
))))
285 newSize
= sizeof(dictEntry
) * finalCapacity
;
287 newDict
= (dictEntry
*) kalloc_container(newSize
);
289 oldSize
= sizeof(dictEntry
) * capacity
;
291 bcopy(dictionary
, newDict
, oldSize
);
292 bzero(&newDict
[capacity
], newSize
- oldSize
);
294 OSCONTAINER_ACCUMSIZE(((size_t)newSize
) - ((size_t)oldSize
));
295 kfree(dictionary
, oldSize
);
297 dictionary
= newDict
;
298 capacity
= finalCapacity
;
304 void OSDictionary::flushCollection()
308 for (unsigned int i
= 0; i
< count
; i
++) {
309 dictionary
[i
].key
->taggedRelease(OSTypeID(OSCollection
));
310 dictionary
[i
].value
->taggedRelease(OSTypeID(OSCollection
));
316 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
, bool onlyAdd
)
321 if (!anObject
|| !aKey
)
324 // if the key exists, replace the object
326 if (fOptions
& kSort
) {
327 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
328 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
329 } else for (exists
= false, i
= 0; i
< count
; i
++) {
330 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
335 if (onlyAdd
) return false;
337 const OSMetaClassBase
*oldObject
= dictionary
[i
].value
;
341 anObject
->taggedRetain(OSTypeID(OSCollection
));
342 dictionary
[i
].value
= anObject
;
344 oldObject
->taggedRelease(OSTypeID(OSCollection
));
348 // add new key, possibly extending our capacity
349 if (count
>= capacity
&& count
>= ensureCapacity(count
+1))
354 bcopy(&dictionary
[i
], &dictionary
[i
+1], (count
- i
) * sizeof(dictionary
[0]));
356 aKey
->taggedRetain(OSTypeID(OSCollection
));
357 anObject
->taggedRetain(OSTypeID(OSCollection
));
358 dictionary
[i
].key
= aKey
;
359 dictionary
[i
].value
= anObject
;
366 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
)
368 return (setObject(aKey
, anObject
, false));
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();