2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* OSDictionary.m created by rsulack on Fri 12-Sep-1997 */
23 /* OSDictionary.cpp converted to C++ by gvdl on Fri 1998-10-30 */
24 /* OSDictionary.cpp rewritten by gvdl on Fri 1998-10-30 */
27 #include <libkern/c++/OSDictionary.h>
28 #include <libkern/c++/OSArray.h>
29 #include <libkern/c++/OSSymbol.h>
30 #include <libkern/c++/OSSerialize.h>
31 #include <libkern/c++/OSLib.h>
32 #include <libkern/c++/OSCollectionIterator.h>
34 #define super OSCollection
36 OSDefineMetaClassAndStructors(OSDictionary
, OSCollection
)
37 OSMetaClassDefineReservedUnused(OSDictionary
, 0);
38 OSMetaClassDefineReservedUnused(OSDictionary
, 1);
39 OSMetaClassDefineReservedUnused(OSDictionary
, 2);
40 OSMetaClassDefineReservedUnused(OSDictionary
, 3);
41 OSMetaClassDefineReservedUnused(OSDictionary
, 4);
42 OSMetaClassDefineReservedUnused(OSDictionary
, 5);
43 OSMetaClassDefineReservedUnused(OSDictionary
, 6);
44 OSMetaClassDefineReservedUnused(OSDictionary
, 7);
48 extern int debug_container_malloc_size
;
50 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
55 #define EXT_CAST(obj) \
56 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
58 bool OSDictionary::initWithCapacity(unsigned int inCapacity
)
63 int size
= inCapacity
* sizeof(dictEntry
);
65 dictionary
= (dictEntry
*) kalloc(size
);
69 bzero(dictionary
, size
);
73 capacity
= inCapacity
;
74 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
79 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
80 const OSSymbol
*keys
[],
81 unsigned int theCount
,
82 unsigned int theCapacity
)
84 unsigned int capacity
= theCount
;
86 if (!objects
|| !keys
)
90 if (theCount
> theCapacity
)
93 capacity
= theCapacity
;
96 if (!initWithCapacity(capacity
))
99 for (unsigned int i
= 0; i
< theCount
; i
++) {
100 const OSMetaClassBase
*newObject
= *objects
++;
102 if (!newObject
|| !keys
[i
] || !setObject(keys
[i
], newObject
))
109 bool OSDictionary::initWithObjects(const OSObject
*objects
[],
110 const OSString
*keys
[],
111 unsigned int theCount
,
112 unsigned int theCapacity
)
114 unsigned int capacity
= theCount
;
116 if (!objects
|| !keys
)
120 if (theCount
> theCapacity
)
123 capacity
= theCapacity
;
126 if (!initWithCapacity(capacity
))
129 for (unsigned int i
= 0; i
< theCount
; i
++) {
130 const OSSymbol
*key
= OSSymbol::withString(*keys
++);
131 const OSMetaClassBase
*newObject
= *objects
++;
136 if (!newObject
|| !setObject(key
, newObject
)) {
147 bool OSDictionary::initWithDictionary(const OSDictionary
*dict
,
148 unsigned int theCapacity
)
150 unsigned int capacity
;
155 capacity
= dict
->count
;
158 if ( dict
->count
> theCapacity
)
161 capacity
= theCapacity
;
164 if (!initWithCapacity(capacity
))
168 bcopy(dict
->dictionary
, dictionary
, count
* sizeof(dictEntry
));
169 for (unsigned int i
= 0; i
< count
; i
++) {
170 dictionary
[i
].key
->taggedRetain(OSTypeID(OSCollection
));
171 dictionary
[i
].value
->taggedRetain(OSTypeID(OSCollection
));
177 OSDictionary
*OSDictionary::withCapacity(unsigned int capacity
)
179 OSDictionary
*me
= new OSDictionary
;
181 if (me
&& !me
->initWithCapacity(capacity
)) {
189 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
190 const OSSymbol
*keys
[],
192 unsigned int capacity
)
194 OSDictionary
*me
= new OSDictionary
;
196 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
204 OSDictionary
*OSDictionary::withObjects(const OSObject
*objects
[],
205 const OSString
*keys
[],
207 unsigned int capacity
)
209 OSDictionary
*me
= new OSDictionary
;
211 if (me
&& !me
->initWithObjects(objects
, keys
, count
, capacity
)) {
219 OSDictionary
*OSDictionary::withDictionary(const OSDictionary
*dict
,
220 unsigned int capacity
)
222 OSDictionary
*me
= new OSDictionary
;
224 if (me
&& !me
->initWithDictionary(dict
, capacity
)) {
232 void OSDictionary::free()
234 (void) super::setOptions(0, kImmutable
);
237 kfree((vm_offset_t
)dictionary
, capacity
* sizeof(dictEntry
));
238 ACCUMSIZE( -(capacity
* sizeof(dictEntry
)) );
244 unsigned int OSDictionary::getCount() const { return count
; }
245 unsigned int OSDictionary::getCapacity() const { return capacity
; }
247 unsigned int OSDictionary::getCapacityIncrement() const
249 return capacityIncrement
;
252 unsigned int OSDictionary::setCapacityIncrement(unsigned int increment
)
254 capacityIncrement
= (increment
)? increment
: 16;
256 return capacityIncrement
;
259 unsigned int OSDictionary::ensureCapacity(unsigned int newCapacity
)
262 int oldSize
, newSize
;
264 if (newCapacity
<= capacity
)
268 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
270 newSize
= sizeof(dictEntry
) * newCapacity
;
272 newDict
= (dictEntry
*) kalloc(newSize
);
274 oldSize
= sizeof(dictEntry
) * capacity
;
276 bcopy(dictionary
, newDict
, oldSize
);
277 bzero(&newDict
[capacity
], newSize
- oldSize
);
279 ACCUMSIZE(newSize
- oldSize
);
280 kfree((vm_offset_t
)dictionary
, oldSize
);
282 dictionary
= newDict
;
283 capacity
= newCapacity
;
289 void OSDictionary::flushCollection()
293 for (unsigned int i
= 0; i
< count
; i
++) {
294 dictionary
[i
].key
->taggedRelease(OSTypeID(OSCollection
));
295 dictionary
[i
].value
->taggedRelease(OSTypeID(OSCollection
));
301 setObject(const OSSymbol
*aKey
, const OSMetaClassBase
*anObject
)
303 if (!anObject
|| !aKey
)
306 // if the key exists, replace the object
307 for (unsigned int i
= 0; i
< count
; i
++) {
308 if (aKey
== dictionary
[i
].key
) {
309 const OSMetaClassBase
*oldObject
= dictionary
[i
].value
;
313 anObject
->taggedRetain(OSTypeID(OSCollection
));
314 dictionary
[i
].value
= anObject
;
316 oldObject
->taggedRelease(OSTypeID(OSCollection
));
321 // add new key, possibly extending our capacity
322 if (count
>= capacity
&& count
>= ensureCapacity(count
+1))
327 aKey
->taggedRetain(OSTypeID(OSCollection
));
328 anObject
->taggedRetain(OSTypeID(OSCollection
));
329 dictionary
[count
].key
= aKey
;
330 dictionary
[count
].value
= anObject
;
336 void OSDictionary::removeObject(const OSSymbol
*aKey
)
341 // if the key exists, remove the object
342 for (unsigned int i
= 0; i
< count
; i
++)
343 if (aKey
== dictionary
[i
].key
) {
344 dictEntry oldEntry
= dictionary
[i
];
349 for (; i
< count
; i
++)
350 dictionary
[i
] = dictionary
[i
+1];
352 oldEntry
.key
->taggedRelease(OSTypeID(OSCollection
));
353 oldEntry
.value
->taggedRelease(OSTypeID(OSCollection
));
359 // Returns true on success, false on an error condition.
360 bool OSDictionary::merge(const OSDictionary
*srcDict
)
362 const OSSymbol
* sym
;
363 OSCollectionIterator
* iter
;
365 if ( !OSDynamicCast(OSDictionary
, srcDict
) )
368 iter
= OSCollectionIterator::withCollection((OSDictionary
*)srcDict
);
372 while ( (sym
= (const OSSymbol
*)iter
->getNextObject()) ) {
373 const OSMetaClassBase
* obj
;
375 obj
= srcDict
->getObject(sym
);
376 if ( !setObject(sym
, obj
) ) {
386 OSObject
*OSDictionary::getObject(const OSSymbol
*aKey
) const
391 // if the key exists, remove the object
392 for (unsigned int i
= 0; i
< count
; i
++)
393 if (aKey
== dictionary
[i
].key
)
394 return (OSObject
*) dictionary
[i
].value
;
400 #define OBJECT_WRAP_1(cmd, k) \
402 const OSSymbol *tmpKey = k; \
403 OSObject *retObj = cmd(tmpKey); \
409 #define OBJECT_WRAP_2(cmd, k, o) \
411 const OSSymbol *tmpKey = k; \
412 bool ret = cmd(tmpKey, o); \
418 #define OBJECT_WRAP_3(cmd, k) \
420 const OSSymbol *tmpKey = k; \
426 bool OSDictionary::setObject(const OSString
*aKey
, const OSMetaClassBase
*anObject
)
427 OBJECT_WRAP_2(setObject
, OSSymbol::withString(aKey
), anObject
)
428 bool OSDictionary::setObject(const char *aKey
, const OSMetaClassBase
*anObject
)
429 OBJECT_WRAP_2(setObject
, OSSymbol::withCString(aKey
), anObject
)
431 OSObject
*OSDictionary::getObject(const OSString
*aKey
) const
432 OBJECT_WRAP_1(getObject
, OSSymbol::withString(aKey
))
433 OSObject
*OSDictionary::getObject(const char *aKey
) const
434 OBJECT_WRAP_1(getObject
, OSSymbol::withCString(aKey
))
436 void OSDictionary::removeObject(const OSString
*aKey
)
437 OBJECT_WRAP_3(removeObject
, OSSymbol::withString(aKey
))
438 void OSDictionary::removeObject(const char *aKey
)
439 OBJECT_WRAP_3(removeObject
, OSSymbol::withCString(aKey
))
442 OSDictionary::isEqualTo(const OSDictionary
*srcDict
, const OSCollection
*keys
) const
444 OSCollectionIterator
* iter
;
445 unsigned int keysCount
;
446 const OSMetaClassBase
* obj1
;
447 const OSMetaClassBase
* obj2
;
451 if ( this == srcDict
)
454 keysCount
= keys
->getCount();
455 if ( (count
< keysCount
) || (srcDict
->getCount() < keysCount
) )
458 iter
= OSCollectionIterator::withCollection(keys
);
463 while ( (aKey
= OSDynamicCast(OSString
, iter
->getNextObject())) ) {
464 obj1
= getObject(aKey
);
465 obj2
= srcDict
->getObject(aKey
);
466 if ( !obj1
|| !obj2
) {
471 if ( !obj1
->isEqualTo(obj2
) ) {
481 bool OSDictionary::isEqualTo(const OSDictionary
*srcDict
) const
484 const OSMetaClassBase
* obj
;
486 if ( this == srcDict
)
489 if ( count
!= srcDict
->getCount() )
492 for ( i
= 0; i
< count
; i
++ ) {
493 obj
= srcDict
->getObject(dictionary
[i
].key
);
497 if ( !dictionary
[i
].value
->isEqualTo(obj
) )
504 bool OSDictionary::isEqualTo(const OSMetaClassBase
*anObject
) const
508 dict
= OSDynamicCast(OSDictionary
, anObject
);
510 return isEqualTo(dict
);
515 unsigned int OSDictionary::iteratorSize() const
517 return sizeof(unsigned int);
520 bool OSDictionary::initIterator(void *inIterator
) const
522 unsigned int *iteratorP
= (unsigned int *) inIterator
;
528 bool OSDictionary::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
530 unsigned int *iteratorP
= (unsigned int *) inIterator
;
531 unsigned int index
= (*iteratorP
)++;
534 *ret
= (OSObject
*) dictionary
[index
].key
;
541 bool OSDictionary::serialize(OSSerialize
*s
) const
543 if (s
->previouslySerialized(this)) return true;
545 if (!s
->addXMLStartTag(this, "dict")) return false;
547 for (unsigned i
= 0; i
< count
; i
++) {
548 const OSSymbol
*key
= dictionary
[i
].key
;
550 // due the nature of the XML syntax, this must be a symbol
551 if (!key
->metaCast("OSSymbol")) {
554 if (!s
->addString("<key>")) return false;
555 const char *c
= key
->getCStringNoCopy();
558 if (!s
->addString("<")) return false;
559 } else if (*c
== '>') {
560 if (!s
->addString(">")) return false;
561 } else if (*c
== '&') {
562 if (!s
->addString("&")) return false;
564 if (!s
->addChar(*c
)) return false;
568 if (!s
->addXMLEndTag("key")) return false;
570 if (!dictionary
[i
].value
->serialize(s
)) return false;
573 return s
->addXMLEndTag("dict");
576 unsigned OSDictionary::setOptions(unsigned options
, unsigned mask
, void *)
578 unsigned old
= super::setOptions(options
, mask
);
579 if ((old
^ options
) & mask
) {
581 // Value changed need to recurse over all of the child collections
582 for ( unsigned i
= 0; i
< count
; i
++ ) {
583 OSCollection
*v
= OSDynamicCast(OSCollection
, dictionary
[i
].value
);
585 v
->setOptions(options
, mask
);
592 OSCollection
* OSDictionary::copyCollection(OSDictionary
*cycleDict
)
594 bool allocDict
= !cycleDict
;
595 OSCollection
*ret
= 0;
596 OSDictionary
*newDict
= 0;
599 cycleDict
= OSDictionary::withCapacity(16);
606 ret
= super::copyCollection(cycleDict
);
610 newDict
= OSDictionary::withDictionary(this);
614 // Insert object into cycle Dictionary
615 cycleDict
->setObject((const OSSymbol
*) this, newDict
);
617 for (unsigned int i
= 0; i
< count
; i
++) {
618 const OSMetaClassBase
*obj
= dictionary
[i
].value
;
619 OSCollection
*coll
= OSDynamicCast(OSCollection
, EXT_CAST(obj
));
622 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
626 newDict
->dictionary
[i
].value
= newColl
;
628 coll
->taggedRelease(OSTypeID(OSCollection
));
629 newColl
->taggedRetain(OSTypeID(OSCollection
));
644 cycleDict
->release();