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 /* IOArray.m created by rsulack on Fri 12-Sep-1997 */
29 /* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
32 #include <libkern/c++/OSArray.h>
33 #include <libkern/c++/OSDictionary.h>
34 #include <libkern/c++/OSSerialize.h>
35 #include <libkern/c++/OSLib.h>
37 #define super OSCollection
39 OSDefineMetaClassAndStructors(OSArray
, OSCollection
)
40 OSMetaClassDefineReservedUnused(OSArray
, 0);
41 OSMetaClassDefineReservedUnused(OSArray
, 1);
42 OSMetaClassDefineReservedUnused(OSArray
, 2);
43 OSMetaClassDefineReservedUnused(OSArray
, 3);
44 OSMetaClassDefineReservedUnused(OSArray
, 4);
45 OSMetaClassDefineReservedUnused(OSArray
, 5);
46 OSMetaClassDefineReservedUnused(OSArray
, 6);
47 OSMetaClassDefineReservedUnused(OSArray
, 7);
51 extern int debug_container_malloc_size
;
53 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
58 #define EXT_CAST(obj) \
59 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
61 bool OSArray::initWithCapacity(unsigned int inCapacity
)
68 // integer overflow check
69 if (inCapacity
> (UINT_MAX
/ sizeof(const OSMetaClassBase
*)))
72 size
= sizeof(const OSMetaClassBase
*) * inCapacity
;
73 array
= (const OSMetaClassBase
**) kalloc(size
);
78 capacity
= inCapacity
;
79 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
87 bool OSArray::initWithObjects(const OSObject
*objects
[],
88 unsigned int theCount
,
89 unsigned int theCapacity
)
91 unsigned int initCapacity
;
94 initCapacity
= theCount
;
95 else if (theCount
> theCapacity
)
98 initCapacity
= theCapacity
;
100 if (!objects
|| !initWithCapacity(initCapacity
))
103 for ( unsigned int i
= 0; i
< theCount
; i
++ ) {
104 const OSMetaClassBase
*newObject
= *objects
++;
109 array
[count
++] = newObject
;
110 newObject
->taggedRetain(OSTypeID(OSCollection
));
116 bool OSArray::initWithArray(const OSArray
*anArray
,
117 unsigned int theCapacity
)
122 return initWithObjects((const OSObject
**) anArray
->array
,
123 anArray
->count
, theCapacity
);
126 OSArray
*OSArray::withCapacity(unsigned int capacity
)
128 OSArray
*me
= new OSArray
;
130 if (me
&& !me
->initWithCapacity(capacity
)) {
138 OSArray
*OSArray::withObjects(const OSObject
*objects
[],
140 unsigned int capacity
)
142 OSArray
*me
= new OSArray
;
144 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
152 OSArray
*OSArray::withArray(const OSArray
*array
,
153 unsigned int capacity
)
155 OSArray
*me
= new OSArray
;
157 if (me
&& !me
->initWithArray(array
, capacity
)) {
167 // Clear immutability - assumes the container is doing the right thing
168 (void) super::setOptions(0, kImmutable
);
173 kfree(array
, sizeof(const OSMetaClassBase
*) * capacity
);
174 ACCUMSIZE( -(sizeof(const OSMetaClassBase
*) * capacity
) );
181 unsigned int OSArray::getCount() const { return count
; }
182 unsigned int OSArray::getCapacity() const { return capacity
; }
183 unsigned int OSArray::getCapacityIncrement() const { return capacityIncrement
; }
184 unsigned int OSArray::setCapacityIncrement(unsigned int increment
)
186 capacityIncrement
= (increment
)? increment
: 16;
188 return capacityIncrement
;
191 unsigned int OSArray::ensureCapacity(unsigned int newCapacity
)
193 const OSMetaClassBase
**newArray
;
194 unsigned int finalCapacity
;
195 unsigned int oldSize
, newSize
;
197 if (newCapacity
<= capacity
)
201 finalCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
204 // integer overflow check
205 if ((finalCapacity
< newCapacity
) || (finalCapacity
> (UINT_MAX
/ sizeof(const OSMetaClassBase
*))))
208 newSize
= sizeof(const OSMetaClassBase
*) * finalCapacity
;
210 newArray
= (const OSMetaClassBase
**) kalloc(newSize
);
212 oldSize
= sizeof(const OSMetaClassBase
*) * capacity
;
214 ACCUMSIZE(newSize
- oldSize
);
216 bcopy(array
, newArray
, oldSize
);
217 bzero(&newArray
[capacity
], newSize
- oldSize
);
218 kfree(array
, oldSize
);
220 capacity
= finalCapacity
;
226 void OSArray::flushCollection()
231 for (i
= 0; i
< count
; i
++) {
232 array
[i
]->taggedRelease(OSTypeID(OSCollection
));
237 bool OSArray::setObject(const OSMetaClassBase
*anObject
)
239 return setObject(count
, anObject
);
242 bool OSArray::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
245 unsigned int newCount
= count
+ 1;
247 if ((index
> count
) || !anObject
)
250 // do we need more space?
251 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
255 if (index
!= count
) {
256 for (i
= count
; i
> index
; i
--)
257 array
[i
] = array
[i
-1];
259 array
[index
] = anObject
;
260 anObject
->taggedRetain(OSTypeID(OSCollection
));
266 bool OSArray::merge(const OSArray
* otherArray
)
268 unsigned int otherCount
= otherArray
->getCount();
269 unsigned int newCount
= count
+ otherCount
;
274 // do we need more space?
275 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
279 for (unsigned int i
= 0; i
< otherCount
; i
++) {
280 const OSMetaClassBase
*newObject
= otherArray
->getObject(i
);
282 array
[count
++] = newObject
;
283 newObject
->taggedRetain(OSTypeID(OSCollection
));
290 replaceObject(unsigned int index
, const OSMetaClassBase
*anObject
)
292 const OSMetaClassBase
*oldObject
;
294 if ((index
>= count
) || !anObject
)
298 oldObject
= array
[index
];
299 array
[index
] = anObject
;
300 anObject
->taggedRetain(OSTypeID(OSCollection
));
302 oldObject
->taggedRelease(OSTypeID(OSCollection
));
305 void OSArray::removeObject(unsigned int index
)
308 const OSMetaClassBase
*oldObject
;
314 oldObject
= array
[index
];
317 for (i
= index
; i
< count
; i
++)
318 array
[i
] = array
[i
+1];
320 oldObject
->taggedRelease(OSTypeID(OSCollection
));
323 bool OSArray::isEqualTo(const OSArray
*anArray
) const
327 if ( this == anArray
)
330 if ( count
!= anArray
->getCount() )
333 for ( i
= 0; i
< count
; i
++ ) {
334 if ( !array
[i
]->isEqualTo(anArray
->getObject(i
)) )
341 bool OSArray::isEqualTo(const OSMetaClassBase
*anObject
) const
345 otherArray
= OSDynamicCast(OSArray
, anObject
);
347 return isEqualTo(otherArray
);
352 OSObject
*OSArray::getObject(unsigned int index
) const
357 return (OSObject
*) (const_cast<OSMetaClassBase
*>(array
[index
]));
360 OSObject
*OSArray::getLastObject() const
365 return ( OSObject
*) (const_cast<OSMetaClassBase
*>(array
[count
- 1]));
368 unsigned int OSArray::getNextIndexOfObject(const OSMetaClassBase
* anObject
,
369 unsigned int index
) const
371 while ((index
< count
) && (array
[index
] != anObject
))
374 index
= (unsigned int)-1;
378 unsigned int OSArray::iteratorSize() const
380 return sizeof(unsigned int);
383 bool OSArray::initIterator(void *inIterator
) const
385 unsigned int *iteratorP
= (unsigned int *) inIterator
;
391 bool OSArray::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
393 unsigned int *iteratorP
= (unsigned int *) inIterator
;
394 unsigned int index
= (*iteratorP
)++;
397 *ret
= (OSObject
*)(const_cast<OSMetaClassBase
*> (array
[index
]));
406 bool OSArray::serialize(OSSerialize
*s
) const
408 if (s
->previouslySerialized(this)) return true;
410 if (!s
->addXMLStartTag(this, "array")) return false;
412 for (unsigned i
= 0; i
< count
; i
++) {
413 if (array
[i
] == NULL
|| !array
[i
]->serialize(s
)) return false;
416 return s
->addXMLEndTag("array");
419 unsigned OSArray::setOptions(unsigned options
, unsigned mask
, void *)
421 unsigned old
= super::setOptions(options
, mask
);
422 if ((old
^ options
) & mask
) {
424 // Value changed need to recurse over all of the child collections
425 for ( unsigned i
= 0; i
< count
; i
++ ) {
426 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
]);
428 coll
->setOptions(options
, mask
);
435 OSCollection
* OSArray::copyCollection(OSDictionary
*cycleDict
)
437 bool allocDict
= !cycleDict
;
438 OSCollection
*ret
= 0;
439 OSArray
*newArray
= 0;
442 cycleDict
= OSDictionary::withCapacity(16);
449 ret
= super::copyCollection(cycleDict
);
453 newArray
= OSArray::withArray(this);
457 // Insert object into cycle Dictionary
458 cycleDict
->setObject((const OSSymbol
*) this, newArray
);
460 for (unsigned int i
= 0; i
< count
; i
++) {
462 OSDynamicCast(OSCollection
, EXT_CAST(newArray
->array
[i
]));
465 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
469 newArray
->replaceObject(i
, newColl
);
484 cycleDict
->release();