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 int size
= inCapacity
* sizeof(dictEntry
);
73 dictionary
= (dictEntry
*) kalloc(size
);
77 bzero(dictionary
, size
);
81 capacity
= inCapacity
;
82 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
87 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
88 const OSSymbol
*keys
[],
89 unsigned int theCount
,
90 unsigned int theCapacity
)
92 unsigned int newCapacity
= theCount
;
94 if (!objects
|| !keys
)
98 if (theCount
> theCapacity
)
101 newCapacity
= theCapacity
;
104 if (!initWithCapacity(newCapacity
))
107 for (unsigned int i
= 0; i
< theCount
; i
++) {
108 const OSMetaClassBase
*newObject
= *objects
++;
110 if (!newObject
|| !keys
[i
] || !setObject(keys
[i
], newObject
))
117 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
118 const OSString
*keys
[],
119 unsigned int theCount
,
120 unsigned int theCapacity
)
122 unsigned int newCapacity
= theCount
;
124 if (!objects
|| !keys
)
128 if (theCount
> theCapacity
)
131 newCapacity
= theCapacity
;
134 if (!initWithCapacity(newCapacity
))
137 for (unsigned int i
= 0; i
< theCount
; i
++) {
138 const OSSymbol
*key
= OSSymbol::withString(*keys
++);
139 const OSMetaClassBase
*newObject
= *objects
++;
144 if (!newObject
|| !setObject(key
, newObject
)) {
155 bool OSDictionary::initWithDictionary(const OSDictionary
*dict
,
156 unsigned int theCapacity
)
158 unsigned int newCapacity
;
163 newCapacity
= dict
->count
;
166 if ( dict
->count
> theCapacity
)
169 newCapacity
= theCapacity
;
172 if (!initWithCapacity(newCapacity
))
175 if ((kSort
& fOptions
) && !(kSort
& dict
->fOptions
)) {
176 for (unsigned int i
= 0; i
< dict
->count
; i
++) {
177 if (!setObject(dict
->dictionary
[i
].key
, dict
->dictionary
[i
].value
)) {
185 bcopy(dict
->dictionary
, dictionary
, count
* sizeof(dictEntry
));
186 for (unsigned int i
= 0; i
< count
; i
++) {
187 dictionary
[i
].key
->taggedRetain(OSTypeID(OSCollection
));
188 dictionary
[i
].value
->taggedRetain(OSTypeID(OSCollection
));
194 OSDictionary
*OSDictionary::withCapacity(unsigned int capacity
)
196 OSDictionary
*me
= new OSDictionary
;
198 if (me
&& !me
->initWithCapacity(capacity
)) {
206 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
207 const OSSymbol
*keys
[],
209 unsigned int capacity
)
211 OSDictionary
*me
= new OSDictionary
;
213 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
221 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
222 const OSString
*keys
[],
224 unsigned int capacity
)
226 OSDictionary
*me
= new OSDictionary
;
228 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
236 OSDictionary
*OSDictionary::withDictionary(const OSDictionary
*dict
,
237 unsigned int capacity
)
239 OSDictionary
*me
= new OSDictionary
;
241 if (me
&& !me
->initWithDictionary(dict
, capacity
)) {
249 void OSDictionary::free()
251 (void) super::setOptions(0, kImmutable
);
254 kfree(dictionary
, capacity
* sizeof(dictEntry
));
255 ACCUMSIZE( -(capacity
* sizeof(dictEntry
)) );
261 unsigned int OSDictionary::getCount() const { return count
; }
262 unsigned int OSDictionary::getCapacity() const { return capacity
; }
264 unsigned int OSDictionary::getCapacityIncrement() const
266 return capacityIncrement
;
269 unsigned int OSDictionary::setCapacityIncrement(unsigned int increment
)
271 capacityIncrement
= (increment
)? increment
: 16;
273 return capacityIncrement
;
276 unsigned int OSDictionary::ensureCapacity(unsigned int newCapacity
)
279 int oldSize
, newSize
;
281 if (newCapacity
<= capacity
)
285 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
287 newSize
= sizeof(dictEntry
) * newCapacity
;
289 newDict
= (dictEntry
*) kalloc(newSize
);
291 oldSize
= sizeof(dictEntry
) * capacity
;
293 bcopy(dictionary
, newDict
, oldSize
);
294 bzero(&newDict
[capacity
], newSize
- oldSize
);
296 ACCUMSIZE(newSize
- oldSize
);
297 kfree(dictionary
, oldSize
);
299 dictionary
= newDict
;
300 capacity
= newCapacity
;
306 void OSDictionary::flushCollection()
310 for (unsigned int i
= 0; i
< count
; i
++) {
311 dictionary
[i
].key
->taggedRelease(OSTypeID(OSCollection
));
312 dictionary
[i
].value
->taggedRelease(OSTypeID(OSCollection
));
318 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
)
323 if (!anObject
|| !aKey
)
326 // if the key exists, replace the object
328 if (fOptions
& kSort
) {
329 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
330 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
331 } else for (exists
= false, i
= 0; i
< count
; i
++) {
332 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
336 const OSMetaClassBase
*oldObject
= dictionary
[i
].value
;
340 anObject
->taggedRetain(OSTypeID(OSCollection
));
341 dictionary
[i
].value
= anObject
;
343 oldObject
->taggedRelease(OSTypeID(OSCollection
));
347 // add new key, possibly extending our capacity
348 if (count
>= capacity
&& count
>= ensureCapacity(count
+1))
353 bcopy(&dictionary
[i
], &dictionary
[i
+1], (count
- i
) * sizeof(dictionary
[0]));
355 aKey
->taggedRetain(OSTypeID(OSCollection
));
356 anObject
->taggedRetain(OSTypeID(OSCollection
));
357 dictionary
[i
].key
= aKey
;
358 dictionary
[i
].value
= anObject
;
364 void OSDictionary::removeObject(const OSSymbol
*aKey
)
372 // if the key exists, remove the object
374 if (fOptions
& kSort
) {
375 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
376 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
377 } else for (exists
= false, i
= 0; i
< count
; i
++) {
378 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
382 dictEntry oldEntry
= dictionary
[i
];
387 bcopy(&dictionary
[i
+1], &dictionary
[i
], (count
- i
) * sizeof(dictionary
[0]));
389 oldEntry
.key
->taggedRelease(OSTypeID(OSCollection
));
390 oldEntry
.value
->taggedRelease(OSTypeID(OSCollection
));
396 // Returns true on success, false on an error condition.
397 bool OSDictionary::merge(const OSDictionary
*srcDict
)
399 const OSSymbol
* sym
;
400 OSCollectionIterator
* iter
;
402 if ( !OSDynamicCast(OSDictionary
, srcDict
) )
405 iter
= OSCollectionIterator::withCollection(const_cast<OSDictionary
*>(srcDict
));
409 while ( (sym
= (const OSSymbol
*)iter
->getNextObject()) ) {
410 const OSMetaClassBase
* obj
;
412 obj
= srcDict
->getObject(sym
);
413 if ( !setObject(sym
, obj
) ) {
423 OSObject
*OSDictionary::getObject(const OSSymbol
*aKey
) const
431 // if the key exists, return the object
433 if (fOptions
& kSort
) {
434 i
= OSSymbol::bsearch(aKey
, &dictionary
[0], count
, sizeof(dictionary
[0]));
435 exists
= (i
< count
) && (aKey
== dictionary
[i
].key
);
436 } else for (exists
= false, i
= 0; i
< count
; i
++) {
437 if ((exists
= (aKey
== dictionary
[i
].key
))) break;
441 return (const_cast<OSObject
*> ((const OSObject
*)dictionary
[i
].value
));
448 #define OBJECT_WRAP_1(cmd, k) \
450 const OSSymbol *tmpKey = k; \
451 OSObject *retObj = cmd(tmpKey); \
457 #define OBJECT_WRAP_2(cmd, k, o) \
459 const OSSymbol *tmpKey = k; \
460 bool ret = cmd(tmpKey, o); \
466 #define OBJECT_WRAP_3(cmd, k) \
468 const OSSymbol *tmpKey = k; \
474 bool OSDictionary::setObject(const OSString
*aKey
, const OSMetaClassBase
*anObject
)
475 OBJECT_WRAP_2(setObject
, OSSymbol::withString(aKey
), anObject
)
476 bool OSDictionary::setObject(const char *aKey
, const OSMetaClassBase
*anObject
)
477 OBJECT_WRAP_2(setObject
, OSSymbol::withCString(aKey
), anObject
)
479 OSObject
*OSDictionary::getObject(const OSString
*aKey
) const
480 OBJECT_WRAP_1(getObject
, OSSymbol::withString(aKey
))
481 OSObject
*OSDictionary::getObject(const char *aKey
) const
482 OBJECT_WRAP_1(getObject
, OSSymbol::withCString(aKey
))
484 void OSDictionary::removeObject(const OSString
*aKey
)
485 OBJECT_WRAP_3(removeObject
, OSSymbol::withString(aKey
))
486 void OSDictionary::removeObject(const char *aKey
)
487 OBJECT_WRAP_3(removeObject
, OSSymbol::withCString(aKey
))
490 OSDictionary::isEqualTo(const OSDictionary
*srcDict
, const OSCollection
*keys
) const
492 OSCollectionIterator
* iter
;
493 unsigned int keysCount
;
494 const OSMetaClassBase
* obj1
;
495 const OSMetaClassBase
* obj2
;
499 if ( this == srcDict
)
502 keysCount
= keys
->getCount();
503 if ( (count
< keysCount
) || (srcDict
->getCount() < keysCount
) )
506 iter
= OSCollectionIterator::withCollection(keys
);
511 while ( (aKey
= OSDynamicCast(OSString
, iter
->getNextObject())) ) {
512 obj1
= getObject(aKey
);
513 obj2
= srcDict
->getObject(aKey
);
514 if ( !obj1
|| !obj2
) {
519 if ( !obj1
->isEqualTo(obj2
) ) {
529 bool OSDictionary::isEqualTo(const OSDictionary
*srcDict
) const
532 const OSMetaClassBase
* obj
;
534 if ( this == srcDict
)
537 if ( count
!= srcDict
->getCount() )
540 for ( i
= 0; i
< count
; i
++ ) {
541 obj
= srcDict
->getObject(dictionary
[i
].key
);
545 if ( !dictionary
[i
].value
->isEqualTo(obj
) )
552 bool OSDictionary::isEqualTo(const OSMetaClassBase
*anObject
) const
556 dict
= OSDynamicCast(OSDictionary
, anObject
);
558 return isEqualTo(dict
);
563 unsigned int OSDictionary::iteratorSize() const
565 return sizeof(unsigned int);
568 bool OSDictionary::initIterator(void *inIterator
) const
570 unsigned int *iteratorP
= (unsigned int *) inIterator
;
576 bool OSDictionary::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
578 unsigned int *iteratorP
= (unsigned int *) inIterator
;
579 unsigned int index
= (*iteratorP
)++;
582 *ret
= (OSObject
*) dictionary
[index
].key
;
589 bool OSDictionary::serialize(OSSerialize
*s
) const
591 if (s
->previouslySerialized(this)) return true;
593 if (!s
->addXMLStartTag(this, "dict")) return false;
595 for (unsigned i
= 0; i
< count
; i
++) {
596 const OSSymbol
*key
= dictionary
[i
].key
;
598 // due the nature of the XML syntax, this must be a symbol
599 if (!key
->metaCast("OSSymbol")) {
602 if (!s
->addString("<key>")) return false;
603 const char *c
= key
->getCStringNoCopy();
606 if (!s
->addString("<")) return false;
607 } else if (*c
== '>') {
608 if (!s
->addString(">")) return false;
609 } else if (*c
== '&') {
610 if (!s
->addString("&")) return false;
612 if (!s
->addChar(*c
)) return false;
616 if (!s
->addXMLEndTag("key")) return false;
618 if (!dictionary
[i
].value
->serialize(s
)) return false;
621 return s
->addXMLEndTag("dict");
624 unsigned OSDictionary::setOptions(unsigned options
, unsigned mask
, void *)
626 unsigned old
= super::setOptions(options
, mask
);
627 if ((old
^ options
) & mask
) {
629 // Value changed need to recurse over all of the child collections
630 for ( unsigned i
= 0; i
< count
; i
++ ) {
631 OSCollection
*v
= OSDynamicCast(OSCollection
, dictionary
[i
].value
);
633 v
->setOptions(options
, mask
);
640 OSCollection
* OSDictionary::copyCollection(OSDictionary
*cycleDict
)
642 bool allocDict
= !cycleDict
;
643 OSCollection
*ret
= 0;
644 OSDictionary
*newDict
= 0;
647 cycleDict
= OSDictionary::withCapacity(16);
654 ret
= super::copyCollection(cycleDict
);
658 newDict
= OSDictionary::withDictionary(this);
662 // Insert object into cycle Dictionary
663 cycleDict
->setObject((const OSSymbol
*) this, newDict
);
665 for (unsigned int i
= 0; i
< count
; i
++) {
666 const OSMetaClassBase
*obj
= dictionary
[i
].value
;
667 OSCollection
*coll
= OSDynamicCast(OSCollection
, EXT_CAST(obj
));
670 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
674 newDict
->dictionary
[i
].value
= newColl
;
676 coll
->taggedRelease(OSTypeID(OSCollection
));
677 newColl
->taggedRetain(OSTypeID(OSCollection
));
692 cycleDict
->release();