2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* IOArray.m created by rsulack on Fri 12-Sep-1997 */
24 /* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
27 #include <libkern/c++/OSArray.h>
28 #include <libkern/c++/OSDictionary.h>
29 #include <libkern/c++/OSSerialize.h>
30 #include <libkern/c++/OSLib.h>
32 #define super OSCollection
34 OSDefineMetaClassAndStructors(OSArray
, OSCollection
)
35 OSMetaClassDefineReservedUnused(OSArray
, 0);
36 OSMetaClassDefineReservedUnused(OSArray
, 1);
37 OSMetaClassDefineReservedUnused(OSArray
, 2);
38 OSMetaClassDefineReservedUnused(OSArray
, 3);
39 OSMetaClassDefineReservedUnused(OSArray
, 4);
40 OSMetaClassDefineReservedUnused(OSArray
, 5);
41 OSMetaClassDefineReservedUnused(OSArray
, 6);
42 OSMetaClassDefineReservedUnused(OSArray
, 7);
46 extern int debug_container_malloc_size
;
48 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
53 #define EXT_CAST(obj) \
54 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
56 bool OSArray::initWithCapacity(unsigned int inCapacity
)
63 size
= sizeof(const OSMetaClassBase
*) * inCapacity
;
64 array
= (const OSMetaClassBase
**) kalloc(size
);
69 capacity
= inCapacity
;
70 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
78 bool OSArray::initWithObjects(const OSObject
*objects
[],
79 unsigned int theCount
,
80 unsigned int theCapacity
)
82 unsigned int initCapacity
;
85 initCapacity
= theCount
;
86 else if (theCount
> theCapacity
)
89 initCapacity
= theCapacity
;
91 if (!objects
|| !initWithCapacity(initCapacity
))
94 for ( unsigned int i
= 0; i
< theCount
; i
++ ) {
95 const OSMetaClassBase
*newObject
= *objects
++;
100 array
[count
++] = newObject
;
101 newObject
->taggedRetain(OSTypeID(OSCollection
));
107 bool OSArray::initWithArray(const OSArray
*anArray
,
108 unsigned int theCapacity
)
113 return initWithObjects((const OSObject
**) anArray
->array
,
114 anArray
->count
, theCapacity
);
117 OSArray
*OSArray::withCapacity(unsigned int capacity
)
119 OSArray
*me
= new OSArray
;
121 if (me
&& !me
->initWithCapacity(capacity
)) {
129 OSArray
*OSArray::withObjects(const OSObject
*objects
[],
131 unsigned int capacity
)
133 OSArray
*me
= new OSArray
;
135 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
143 OSArray
*OSArray::withArray(const OSArray
*array
,
144 unsigned int capacity
)
146 OSArray
*me
= new OSArray
;
148 if (me
&& !me
->initWithArray(array
, capacity
)) {
158 // Clear immutability - assumes the container is doing the right thing
159 (void) super::setOptions(0, kImmutable
);
164 kfree(array
, sizeof(const OSMetaClassBase
*) * capacity
);
165 ACCUMSIZE( -(sizeof(const OSMetaClassBase
*) * capacity
) );
172 unsigned int OSArray::getCount() const { return count
; }
173 unsigned int OSArray::getCapacity() const { return capacity
; }
174 unsigned int OSArray::getCapacityIncrement() const { return capacityIncrement
; }
175 unsigned int OSArray::setCapacityIncrement(unsigned int increment
)
177 capacityIncrement
= (increment
)? increment
: 16;
179 return capacityIncrement
;
182 unsigned int OSArray::ensureCapacity(unsigned int newCapacity
)
184 const OSMetaClassBase
**newArray
;
185 int oldSize
, newSize
;
187 if (newCapacity
<= capacity
)
191 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
193 newSize
= sizeof(const OSMetaClassBase
*) * newCapacity
;
195 newArray
= (const OSMetaClassBase
**) kalloc(newSize
);
197 oldSize
= sizeof(const OSMetaClassBase
*) * capacity
;
199 ACCUMSIZE(newSize
- oldSize
);
201 bcopy(array
, newArray
, oldSize
);
202 bzero(&newArray
[capacity
], newSize
- oldSize
);
203 kfree(array
, oldSize
);
205 capacity
= newCapacity
;
211 void OSArray::flushCollection()
216 for (i
= 0; i
< count
; i
++)
217 array
[i
]->taggedRelease(OSTypeID(OSCollection
));
221 bool OSArray::setObject(const OSMetaClassBase
*anObject
)
223 return setObject(count
, anObject
);
226 bool OSArray::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
229 unsigned int newCount
= count
+ 1;
231 if ((index
> count
) || !anObject
)
234 // do we need more space?
235 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
239 if (index
!= count
) {
240 for (i
= count
; i
> index
; i
--)
241 array
[i
] = array
[i
-1];
243 array
[index
] = anObject
;
244 anObject
->taggedRetain(OSTypeID(OSCollection
));
250 bool OSArray::merge(const OSArray
* otherArray
)
252 unsigned int otherCount
= otherArray
->getCount();
253 unsigned int newCount
= count
+ otherCount
;
258 // do we need more space?
259 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
263 for (unsigned int i
= 0; i
< otherCount
; i
++) {
264 const OSMetaClassBase
*newObject
= otherArray
->getObject(i
);
266 array
[count
++] = newObject
;
267 newObject
->taggedRetain(OSTypeID(OSCollection
));
274 replaceObject(unsigned int index
, const OSMetaClassBase
*anObject
)
276 const OSMetaClassBase
*oldObject
;
278 if ((index
>= count
) || !anObject
)
282 oldObject
= array
[index
];
283 array
[index
] = anObject
;
284 anObject
->taggedRetain(OSTypeID(OSCollection
));
286 oldObject
->taggedRelease(OSTypeID(OSCollection
));
289 void OSArray::removeObject(unsigned int index
)
292 const OSMetaClassBase
*oldObject
;
298 oldObject
= array
[index
];
301 for (i
= index
; i
< count
; i
++)
302 array
[i
] = array
[i
+1];
304 oldObject
->taggedRelease(OSTypeID(OSCollection
));
307 bool OSArray::isEqualTo(const OSArray
*anArray
) const
311 if ( this == anArray
)
314 if ( count
!= anArray
->getCount() )
317 for ( i
= 0; i
< count
; i
++ ) {
318 if ( !array
[i
]->isEqualTo(anArray
->getObject(i
)) )
325 bool OSArray::isEqualTo(const OSMetaClassBase
*anObject
) const
329 otherArray
= OSDynamicCast(OSArray
, anObject
);
331 return isEqualTo(otherArray
);
336 OSObject
*OSArray::getObject(unsigned int index
) const
341 return (OSObject
*) (const_cast<OSMetaClassBase
*>(array
[index
]));
344 OSObject
*OSArray::getLastObject() const
349 return ( OSObject
*) (const_cast<OSMetaClassBase
*>(array
[count
- 1]));
352 unsigned int OSArray::getNextIndexOfObject(const OSMetaClassBase
* anObject
,
353 unsigned int index
) const
355 while ((index
< count
) && (array
[index
] != anObject
))
358 index
= (unsigned int)-1;
362 unsigned int OSArray::iteratorSize() const
364 return sizeof(unsigned int);
367 bool OSArray::initIterator(void *inIterator
) const
369 unsigned int *iteratorP
= (unsigned int *) inIterator
;
375 bool OSArray::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
377 unsigned int *iteratorP
= (unsigned int *) inIterator
;
378 unsigned int index
= (*iteratorP
)++;
381 *ret
= (OSObject
*)(const_cast<OSMetaClassBase
*> (array
[index
]));
390 bool OSArray::serialize(OSSerialize
*s
) const
392 if (s
->previouslySerialized(this)) return true;
394 if (!s
->addXMLStartTag(this, "array")) return false;
396 for (unsigned i
= 0; i
< count
; i
++) {
397 if (!array
[i
]->serialize(s
)) return false;
400 return s
->addXMLEndTag("array");
403 unsigned OSArray::setOptions(unsigned options
, unsigned mask
, void *)
405 unsigned old
= super::setOptions(options
, mask
);
406 if ((old
^ options
) & mask
) {
408 // Value changed need to recurse over all of the child collections
409 for ( unsigned i
= 0; i
< count
; i
++ ) {
410 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
]);
412 coll
->setOptions(options
, mask
);
419 OSCollection
* OSArray::copyCollection(OSDictionary
*cycleDict
)
421 bool allocDict
= !cycleDict
;
422 OSCollection
*ret
= 0;
423 OSArray
*newArray
= 0;
426 cycleDict
= OSDictionary::withCapacity(16);
433 ret
= super::copyCollection(cycleDict
);
437 newArray
= OSArray::withArray(this);
441 // Insert object into cycle Dictionary
442 cycleDict
->setObject((const OSSymbol
*) this, newArray
);
444 for (unsigned int i
= 0; i
< count
; i
++) {
446 OSDynamicCast(OSCollection
, EXT_CAST(newArray
->array
[i
]));
449 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
453 newArray
->replaceObject(i
, newColl
);
468 cycleDict
->release();