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 /* IOArray.m created by rsulack on Fri 12-Sep-1997 */
23 /* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
26 #include <libkern/c++/OSArray.h>
27 #include <libkern/c++/OSDictionary.h>
28 #include <libkern/c++/OSSerialize.h>
29 #include <libkern/c++/OSLib.h>
31 #define super OSCollection
33 OSDefineMetaClassAndStructors(OSArray
, OSCollection
)
34 OSMetaClassDefineReservedUnused(OSArray
, 0);
35 OSMetaClassDefineReservedUnused(OSArray
, 1);
36 OSMetaClassDefineReservedUnused(OSArray
, 2);
37 OSMetaClassDefineReservedUnused(OSArray
, 3);
38 OSMetaClassDefineReservedUnused(OSArray
, 4);
39 OSMetaClassDefineReservedUnused(OSArray
, 5);
40 OSMetaClassDefineReservedUnused(OSArray
, 6);
41 OSMetaClassDefineReservedUnused(OSArray
, 7);
45 extern int debug_container_malloc_size
;
47 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
52 #define EXT_CAST(obj) \
53 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
55 bool OSArray::initWithCapacity(unsigned int inCapacity
)
62 size
= sizeof(const OSMetaClassBase
*) * inCapacity
;
63 array
= (const OSMetaClassBase
**) kalloc(size
);
68 capacity
= inCapacity
;
69 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
77 bool OSArray::initWithObjects(const OSObject
*objects
[],
78 unsigned int theCount
,
79 unsigned int theCapacity
)
81 unsigned int initCapacity
;
84 initCapacity
= theCount
;
85 else if (theCount
> theCapacity
)
88 initCapacity
= theCapacity
;
90 if (!objects
|| !initWithCapacity(initCapacity
))
93 for ( unsigned int i
= 0; i
< theCount
; i
++ ) {
94 const OSMetaClassBase
*newObject
= *objects
++;
99 array
[count
++] = newObject
;
100 newObject
->taggedRetain(OSTypeID(OSCollection
));
106 bool OSArray::initWithArray(const OSArray
*anArray
,
107 unsigned int theCapacity
)
112 return initWithObjects((const OSObject
**) anArray
->array
,
113 anArray
->count
, theCapacity
);
116 OSArray
*OSArray::withCapacity(unsigned int capacity
)
118 OSArray
*me
= new OSArray
;
120 if (me
&& !me
->initWithCapacity(capacity
)) {
128 OSArray
*OSArray::withObjects(const OSObject
*objects
[],
130 unsigned int capacity
)
132 OSArray
*me
= new OSArray
;
134 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
142 OSArray
*OSArray::withArray(const OSArray
*array
,
143 unsigned int capacity
)
145 OSArray
*me
= new OSArray
;
147 if (me
&& !me
->initWithArray(array
, capacity
)) {
157 // Clear immutability - assumes the container is doing the right thing
158 (void) super::setOptions(0, kImmutable
);
163 kfree(array
, sizeof(const OSMetaClassBase
*) * capacity
);
164 ACCUMSIZE( -(sizeof(const OSMetaClassBase
*) * capacity
) );
171 unsigned int OSArray::getCount() const { return count
; }
172 unsigned int OSArray::getCapacity() const { return capacity
; }
173 unsigned int OSArray::getCapacityIncrement() const { return capacityIncrement
; }
174 unsigned int OSArray::setCapacityIncrement(unsigned int increment
)
176 capacityIncrement
= (increment
)? increment
: 16;
178 return capacityIncrement
;
181 unsigned int OSArray::ensureCapacity(unsigned int newCapacity
)
183 const OSMetaClassBase
**newArray
;
184 int oldSize
, newSize
;
186 if (newCapacity
<= capacity
)
190 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
192 newSize
= sizeof(const OSMetaClassBase
*) * newCapacity
;
194 newArray
= (const OSMetaClassBase
**) kalloc(newSize
);
196 oldSize
= sizeof(const OSMetaClassBase
*) * capacity
;
198 ACCUMSIZE(newSize
- oldSize
);
200 bcopy(array
, newArray
, oldSize
);
201 bzero(&newArray
[capacity
], newSize
- oldSize
);
202 kfree(array
, oldSize
);
204 capacity
= newCapacity
;
210 void OSArray::flushCollection()
215 for (i
= 0; i
< count
; i
++)
216 array
[i
]->taggedRelease(OSTypeID(OSCollection
));
220 bool OSArray::setObject(const OSMetaClassBase
*anObject
)
222 return setObject(count
, anObject
);
225 bool OSArray::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
228 unsigned int newCount
= count
+ 1;
230 if ((index
> count
) || !anObject
)
233 // do we need more space?
234 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
238 if (index
!= count
) {
239 for (i
= count
; i
> index
; i
--)
240 array
[i
] = array
[i
-1];
242 array
[index
] = anObject
;
243 anObject
->taggedRetain(OSTypeID(OSCollection
));
249 bool OSArray::merge(const OSArray
* otherArray
)
251 unsigned int otherCount
= otherArray
->getCount();
252 unsigned int newCount
= count
+ otherCount
;
257 // do we need more space?
258 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
262 for (unsigned int i
= 0; i
< otherCount
; i
++) {
263 const OSMetaClassBase
*newObject
= otherArray
->getObject(i
);
265 array
[count
++] = newObject
;
266 newObject
->taggedRetain(OSTypeID(OSCollection
));
273 replaceObject(unsigned int index
, const OSMetaClassBase
*anObject
)
275 const OSMetaClassBase
*oldObject
;
277 if ((index
>= count
) || !anObject
)
281 oldObject
= array
[index
];
282 array
[index
] = anObject
;
283 anObject
->taggedRetain(OSTypeID(OSCollection
));
285 oldObject
->taggedRelease(OSTypeID(OSCollection
));
288 void OSArray::removeObject(unsigned int index
)
291 const OSMetaClassBase
*oldObject
;
297 oldObject
= array
[index
];
300 for (i
= index
; i
< count
; i
++)
301 array
[i
] = array
[i
+1];
303 oldObject
->taggedRelease(OSTypeID(OSCollection
));
306 bool OSArray::isEqualTo(const OSArray
*anArray
) const
310 if ( this == anArray
)
313 if ( count
!= anArray
->getCount() )
316 for ( i
= 0; i
< count
; i
++ ) {
317 if ( !array
[i
]->isEqualTo(anArray
->getObject(i
)) )
324 bool OSArray::isEqualTo(const OSMetaClassBase
*anObject
) const
328 otherArray
= OSDynamicCast(OSArray
, anObject
);
330 return isEqualTo(otherArray
);
335 OSObject
*OSArray::getObject(unsigned int index
) const
340 return (OSObject
*) (const_cast<OSMetaClassBase
*>(array
[index
]));
343 OSObject
*OSArray::getLastObject() const
348 return ( OSObject
*) (const_cast<OSMetaClassBase
*>(array
[count
- 1]));
351 unsigned int OSArray::getNextIndexOfObject(const OSMetaClassBase
* anObject
,
352 unsigned int index
) const
354 while ((index
< count
) && (array
[index
] != anObject
))
357 index
= (unsigned int)-1;
361 unsigned int OSArray::iteratorSize() const
363 return sizeof(unsigned int);
366 bool OSArray::initIterator(void *inIterator
) const
368 unsigned int *iteratorP
= (unsigned int *) inIterator
;
374 bool OSArray::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
376 unsigned int *iteratorP
= (unsigned int *) inIterator
;
377 unsigned int index
= (*iteratorP
)++;
380 *ret
= (OSObject
*)(const_cast<OSMetaClassBase
*> (array
[index
]));
389 bool OSArray::serialize(OSSerialize
*s
) const
391 if (s
->previouslySerialized(this)) return true;
393 if (!s
->addXMLStartTag(this, "array")) return false;
395 for (unsigned i
= 0; i
< count
; i
++) {
396 if (!array
[i
]->serialize(s
)) return false;
399 return s
->addXMLEndTag("array");
402 unsigned OSArray::setOptions(unsigned options
, unsigned mask
, void *)
404 unsigned old
= super::setOptions(options
, mask
);
405 if ((old
^ options
) & mask
) {
407 // Value changed need to recurse over all of the child collections
408 for ( unsigned i
= 0; i
< count
; i
++ ) {
409 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
]);
411 coll
->setOptions(options
, mask
);
418 OSCollection
* OSArray::copyCollection(OSDictionary
*cycleDict
)
420 bool allocDict
= !cycleDict
;
421 OSCollection
*ret
= 0;
422 OSArray
*newArray
= 0;
425 cycleDict
= OSDictionary::withCapacity(16);
432 ret
= super::copyCollection(cycleDict
);
436 newArray
= OSArray::withArray(this);
440 // Insert object into cycle Dictionary
441 cycleDict
->setObject((const OSSymbol
*) this, newArray
);
443 for (unsigned int i
= 0; i
< count
; i
++) {
445 OSDynamicCast(OSCollection
, EXT_CAST(newArray
->array
[i
]));
448 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
452 newArray
->replaceObject(i
, newColl
);
467 cycleDict
->release();