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
);
71 dictionary
= (dictEntry
*) kalloc(size
);
75 bzero(dictionary
, size
);
79 capacity
= inCapacity
;
80 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
85 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
86 const OSSymbol
*keys
[],
87 unsigned int theCount
,
88 unsigned int theCapacity
)
90 unsigned int capacity
= theCount
;
92 if (!objects
|| !keys
)
96 if (theCount
> theCapacity
)
99 capacity
= theCapacity
;
102 if (!initWithCapacity(capacity
))
105 for (unsigned int i
= 0; i
< theCount
; i
++) {
106 const OSMetaClassBase
*newObject
= *objects
++;
108 if (!newObject
|| !keys
[i
] || !setObject(keys
[i
], newObject
))
115 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
116 const OSString
*keys
[],
117 unsigned int theCount
,
118 unsigned int theCapacity
)
120 unsigned int capacity
= theCount
;
122 if (!objects
|| !keys
)
126 if (theCount
> theCapacity
)
129 capacity
= theCapacity
;
132 if (!initWithCapacity(capacity
))
135 for (unsigned int i
= 0; i
< theCount
; i
++) {
136 const OSSymbol
*key
= OSSymbol::withString(*keys
++);
137 const OSMetaClassBase
*newObject
= *objects
++;
142 if (!newObject
|| !setObject(key
, newObject
)) {
153 bool OSDictionary::initWithDictionary(const OSDictionary
*dict
,
154 unsigned int theCapacity
)
156 unsigned int capacity
;
161 capacity
= dict
->count
;
164 if ( dict
->count
> theCapacity
)
167 capacity
= theCapacity
;
170 if (!initWithCapacity(capacity
))
174 bcopy(dict
->dictionary
, dictionary
, count
* sizeof(dictEntry
));
175 for (unsigned int i
= 0; i
< count
; i
++) {
176 dictionary
[i
].key
->taggedRetain(OSTypeID(OSCollection
));
177 dictionary
[i
].value
->taggedRetain(OSTypeID(OSCollection
));
183 OSDictionary
*OSDictionary::withCapacity(unsigned int capacity
)
185 OSDictionary
*me
= new OSDictionary
;
187 if (me
&& !me
->initWithCapacity(capacity
)) {
195 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
196 const OSSymbol
*keys
[],
198 unsigned int capacity
)
200 OSDictionary
*me
= new OSDictionary
;
202 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
210 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
211 const OSString
*keys
[],
213 unsigned int capacity
)
215 OSDictionary
*me
= new OSDictionary
;
217 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
225 OSDictionary
*OSDictionary::withDictionary(const OSDictionary
*dict
,
226 unsigned int capacity
)
228 OSDictionary
*me
= new OSDictionary
;
230 if (me
&& !me
->initWithDictionary(dict
, capacity
)) {
238 void OSDictionary::free()
240 (void) super::setOptions(0, kImmutable
);
243 kfree((vm_offset_t
)dictionary
, capacity
* sizeof(dictEntry
));
244 ACCUMSIZE( -(capacity
* sizeof(dictEntry
)) );
250 unsigned int OSDictionary::getCount() const { return count
; }
251 unsigned int OSDictionary::getCapacity() const { return capacity
; }
253 unsigned int OSDictionary::getCapacityIncrement() const
255 return capacityIncrement
;
258 unsigned int OSDictionary::setCapacityIncrement(unsigned int increment
)
260 capacityIncrement
= (increment
)? increment
: 16;
262 return capacityIncrement
;
265 unsigned int OSDictionary::ensureCapacity(unsigned int newCapacity
)
268 int oldSize
, newSize
;
270 if (newCapacity
<= capacity
)
274 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
276 newSize
= sizeof(dictEntry
) * newCapacity
;
278 newDict
= (dictEntry
*) kalloc(newSize
);
280 oldSize
= sizeof(dictEntry
) * capacity
;
282 bcopy(dictionary
, newDict
, oldSize
);
283 bzero(&newDict
[capacity
], newSize
- oldSize
);
285 ACCUMSIZE(newSize
- oldSize
);
286 kfree((vm_offset_t
)dictionary
, oldSize
);
288 dictionary
= newDict
;
289 capacity
= newCapacity
;
295 void OSDictionary::flushCollection()
299 for (unsigned int i
= 0; i
< count
; i
++) {
300 dictionary
[i
].key
->taggedRelease(OSTypeID(OSCollection
));
301 dictionary
[i
].value
->taggedRelease(OSTypeID(OSCollection
));
307 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
)
309 if (!anObject
|| !aKey
)
312 // if the key exists, replace the object
313 for (unsigned int i
= 0; i
< count
; i
++) {
314 if (aKey
== dictionary
[i
].key
) {
315 const OSMetaClassBase
*oldObject
= dictionary
[i
].value
;
319 anObject
->taggedRetain(OSTypeID(OSCollection
));
320 dictionary
[i
].value
= anObject
;
322 oldObject
->taggedRelease(OSTypeID(OSCollection
));
327 // add new key, possibly extending our capacity
328 if (count
>= capacity
&& count
>= ensureCapacity(count
+1))
333 aKey
->taggedRetain(OSTypeID(OSCollection
));
334 anObject
->taggedRetain(OSTypeID(OSCollection
));
335 dictionary
[count
].key
= aKey
;
336 dictionary
[count
].value
= anObject
;
342 void OSDictionary::removeObject(const OSSymbol
*aKey
)
347 // if the key exists, remove the object
348 for (unsigned int i
= 0; i
< count
; i
++)
349 if (aKey
== dictionary
[i
].key
) {
350 dictEntry oldEntry
= dictionary
[i
];
355 for (; i
< count
; i
++)
356 dictionary
[i
] = dictionary
[i
+1];
358 oldEntry
.key
->taggedRelease(OSTypeID(OSCollection
));
359 oldEntry
.value
->taggedRelease(OSTypeID(OSCollection
));
365 // Returns true on success, false on an error condition.
366 bool OSDictionary::merge(const OSDictionary
*srcDict
)
368 const OSSymbol
* sym
;
369 OSCollectionIterator
* iter
;
371 if ( !OSDynamicCast(OSDictionary
, srcDict
) )
374 iter
= OSCollectionIterator::withCollection((OSDictionary
*)srcDict
);
378 while ( (sym
= (const OSSymbol
*)iter
->getNextObject()) ) {
379 const OSMetaClassBase
* obj
;
381 obj
= srcDict
->getObject(sym
);
382 if ( !setObject(sym
, obj
) ) {
392 OSObject
*OSDictionary::getObject(const OSSymbol
*aKey
) const
397 // if the key exists, remove the object
398 for (unsigned int i
= 0; i
< count
; i
++)
399 if (aKey
== dictionary
[i
].key
)
400 return (OSObject
*) dictionary
[i
].value
;
406 #define OBJECT_WRAP_1(cmd, k) \
408 const OSSymbol *tmpKey = k; \
409 OSObject *retObj = cmd(tmpKey); \
415 #define OBJECT_WRAP_2(cmd, k, o) \
417 const OSSymbol *tmpKey = k; \
418 bool ret = cmd(tmpKey, o); \
424 #define OBJECT_WRAP_3(cmd, k) \
426 const OSSymbol *tmpKey = k; \
432 bool OSDictionary::setObject(const OSString
*aKey
, const OSMetaClassBase
*anObject
)
433 OBJECT_WRAP_2(setObject
, OSSymbol::withString(aKey
), anObject
)
434 bool OSDictionary::setObject(const char *aKey
, const OSMetaClassBase
*anObject
)
435 OBJECT_WRAP_2(setObject
, OSSymbol::withCString(aKey
), anObject
)
437 OSObject
*OSDictionary::getObject(const OSString
*aKey
) const
438 OBJECT_WRAP_1(getObject
, OSSymbol::withString(aKey
))
439 OSObject
*OSDictionary::getObject(const char *aKey
) const
440 OBJECT_WRAP_1(getObject
, OSSymbol::withCString(aKey
))
442 void OSDictionary::removeObject(const OSString
*aKey
)
443 OBJECT_WRAP_3(removeObject
, OSSymbol::withString(aKey
))
444 void OSDictionary::removeObject(const char *aKey
)
445 OBJECT_WRAP_3(removeObject
, OSSymbol::withCString(aKey
))
448 OSDictionary::isEqualTo(const OSDictionary
*srcDict
, const OSCollection
*keys
) const
450 OSCollectionIterator
* iter
;
451 unsigned int keysCount
;
452 const OSMetaClassBase
* obj1
;
453 const OSMetaClassBase
* obj2
;
457 if ( this == srcDict
)
460 keysCount
= keys
->getCount();
461 if ( (count
< keysCount
) || (srcDict
->getCount() < keysCount
) )
464 iter
= OSCollectionIterator::withCollection(keys
);
469 while ( (aKey
= OSDynamicCast(OSString
, iter
->getNextObject())) ) {
470 obj1
= getObject(aKey
);
471 obj2
= srcDict
->getObject(aKey
);
472 if ( !obj1
|| !obj2
) {
477 if ( !obj1
->isEqualTo(obj2
) ) {
487 bool OSDictionary::isEqualTo(const OSDictionary
*srcDict
) const
490 const OSMetaClassBase
* obj
;
492 if ( this == srcDict
)
495 if ( count
!= srcDict
->getCount() )
498 for ( i
= 0; i
< count
; i
++ ) {
499 obj
= srcDict
->getObject(dictionary
[i
].key
);
503 if ( !dictionary
[i
].value
->isEqualTo(obj
) )
510 bool OSDictionary::isEqualTo(const OSMetaClassBase
*anObject
) const
514 dict
= OSDynamicCast(OSDictionary
, anObject
);
516 return isEqualTo(dict
);
521 unsigned int OSDictionary::iteratorSize() const
523 return sizeof(unsigned int);
526 bool OSDictionary::initIterator(void *inIterator
) const
528 unsigned int *iteratorP
= (unsigned int *) inIterator
;
534 bool OSDictionary::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
536 unsigned int *iteratorP
= (unsigned int *) inIterator
;
537 unsigned int index
= (*iteratorP
)++;
540 *ret
= (OSObject
*) dictionary
[index
].key
;
547 bool OSDictionary::serialize(OSSerialize
*s
) const
549 if (s
->previouslySerialized(this)) return true;
551 if (!s
->addXMLStartTag(this, "dict")) return false;
553 for (unsigned i
= 0; i
< count
; i
++) {
554 const OSSymbol
*key
= dictionary
[i
].key
;
556 // due the nature of the XML syntax, this must be a symbol
557 if (!key
->metaCast("OSSymbol")) {
560 if (!s
->addString("<key>")) return false;
561 const char *c
= key
->getCStringNoCopy();
564 if (!s
->addString("<")) return false;
565 } else if (*c
== '>') {
566 if (!s
->addString(">")) return false;
567 } else if (*c
== '&') {
568 if (!s
->addString("&")) return false;
570 if (!s
->addChar(*c
)) return false;
574 if (!s
->addXMLEndTag("key")) return false;
576 if (!dictionary
[i
].value
->serialize(s
)) return false;
579 return s
->addXMLEndTag("dict");
582 unsigned OSDictionary::setOptions(unsigned options
, unsigned mask
, void *)
584 unsigned old
= super::setOptions(options
, mask
);
585 if ((old
^ options
) & mask
) {
587 // Value changed need to recurse over all of the child collections
588 for ( unsigned i
= 0; i
< count
; i
++ ) {
589 OSCollection
*v
= OSDynamicCast(OSCollection
, dictionary
[i
].value
);
591 v
->setOptions(options
, mask
);
598 OSCollection
* OSDictionary::copyCollection(OSDictionary
*cycleDict
)
600 bool allocDict
= !cycleDict
;
601 OSCollection
*ret
= 0;
602 OSDictionary
*newDict
= 0;
605 cycleDict
= OSDictionary::withCapacity(16);
612 ret
= super::copyCollection(cycleDict
);
616 newDict
= OSDictionary::withDictionary(this);
620 // Insert object into cycle Dictionary
621 cycleDict
->setObject((const OSSymbol
*) this, newDict
);
623 for (unsigned int i
= 0; i
< count
; i
++) {
624 const OSMetaClassBase
*obj
= dictionary
[i
].value
;
625 OSCollection
*coll
= OSDynamicCast(OSCollection
, EXT_CAST(obj
));
628 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
632 newDict
->dictionary
[i
].value
= newColl
;
634 coll
->taggedRelease(OSTypeID(OSCollection
));
635 newColl
->taggedRetain(OSTypeID(OSCollection
));
650 cycleDict
->release();