2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* IOArray.m created by rsulack on Fri 12-Sep-1997 */
31 /* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
34 #include <libkern/c++/OSArray.h>
35 #include <libkern/c++/OSDictionary.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSLib.h>
39 #define super OSCollection
41 OSDefineMetaClassAndStructors(OSArray
, OSCollection
)
42 OSMetaClassDefineReservedUnused(OSArray
, 0);
43 OSMetaClassDefineReservedUnused(OSArray
, 1);
44 OSMetaClassDefineReservedUnused(OSArray
, 2);
45 OSMetaClassDefineReservedUnused(OSArray
, 3);
46 OSMetaClassDefineReservedUnused(OSArray
, 4);
47 OSMetaClassDefineReservedUnused(OSArray
, 5);
48 OSMetaClassDefineReservedUnused(OSArray
, 6);
49 OSMetaClassDefineReservedUnused(OSArray
, 7);
53 extern int debug_container_malloc_size
;
55 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
60 #define EXT_CAST(obj) \
61 reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
63 bool OSArray::initWithCapacity(unsigned int inCapacity
)
70 size
= sizeof(const OSMetaClassBase
*) * inCapacity
;
71 array
= (const OSMetaClassBase
**) kalloc(size
);
76 capacity
= inCapacity
;
77 capacityIncrement
= (inCapacity
)? inCapacity
: 16;
85 bool OSArray::initWithObjects(const OSObject
*objects
[],
86 unsigned int theCount
,
87 unsigned int theCapacity
)
89 unsigned int initCapacity
;
92 initCapacity
= theCount
;
93 else if (theCount
> theCapacity
)
96 initCapacity
= theCapacity
;
98 if (!objects
|| !initWithCapacity(initCapacity
))
101 for ( unsigned int i
= 0; i
< theCount
; i
++ ) {
102 const OSMetaClassBase
*newObject
= *objects
++;
107 array
[count
++] = newObject
;
108 newObject
->taggedRetain(OSTypeID(OSCollection
));
114 bool OSArray::initWithArray(const OSArray
*anArray
,
115 unsigned int theCapacity
)
120 return initWithObjects((const OSObject
**) anArray
->array
,
121 anArray
->count
, theCapacity
);
124 OSArray
*OSArray::withCapacity(unsigned int capacity
)
126 OSArray
*me
= new OSArray
;
128 if (me
&& !me
->initWithCapacity(capacity
)) {
136 OSArray
*OSArray::withObjects(const OSObject
*objects
[],
138 unsigned int capacity
)
140 OSArray
*me
= new OSArray
;
142 if (me
&& !me
->initWithObjects(objects
, count
, capacity
)) {
150 OSArray
*OSArray::withArray(const OSArray
*array
,
151 unsigned int capacity
)
153 OSArray
*me
= new OSArray
;
155 if (me
&& !me
->initWithArray(array
, capacity
)) {
165 // Clear immutability - assumes the container is doing the right thing
166 (void) super::setOptions(0, kImmutable
);
171 kfree(array
, sizeof(const OSMetaClassBase
*) * capacity
);
172 ACCUMSIZE( -(sizeof(const OSMetaClassBase
*) * capacity
) );
179 unsigned int OSArray::getCount() const { return count
; }
180 unsigned int OSArray::getCapacity() const { return capacity
; }
181 unsigned int OSArray::getCapacityIncrement() const { return capacityIncrement
; }
182 unsigned int OSArray::setCapacityIncrement(unsigned int increment
)
184 capacityIncrement
= (increment
)? increment
: 16;
186 return capacityIncrement
;
189 unsigned int OSArray::ensureCapacity(unsigned int newCapacity
)
191 const OSMetaClassBase
**newArray
;
192 int oldSize
, newSize
;
194 if (newCapacity
<= capacity
)
198 newCapacity
= (((newCapacity
- 1) / capacityIncrement
) + 1)
200 newSize
= sizeof(const OSMetaClassBase
*) * newCapacity
;
202 newArray
= (const OSMetaClassBase
**) kalloc(newSize
);
204 oldSize
= sizeof(const OSMetaClassBase
*) * capacity
;
206 ACCUMSIZE(newSize
- oldSize
);
208 bcopy(array
, newArray
, oldSize
);
209 bzero(&newArray
[capacity
], newSize
- oldSize
);
210 kfree(array
, oldSize
);
212 capacity
= newCapacity
;
218 void OSArray::flushCollection()
223 for (i
= 0; i
< count
; i
++)
224 array
[i
]->taggedRelease(OSTypeID(OSCollection
));
228 bool OSArray::setObject(const OSMetaClassBase
*anObject
)
230 return setObject(count
, anObject
);
233 bool OSArray::setObject(unsigned int index
, const OSMetaClassBase
*anObject
)
236 unsigned int newCount
= count
+ 1;
238 if ((index
> count
) || !anObject
)
241 // do we need more space?
242 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
246 if (index
!= count
) {
247 for (i
= count
; i
> index
; i
--)
248 array
[i
] = array
[i
-1];
250 array
[index
] = anObject
;
251 anObject
->taggedRetain(OSTypeID(OSCollection
));
257 bool OSArray::merge(const OSArray
* otherArray
)
259 unsigned int otherCount
= otherArray
->getCount();
260 unsigned int newCount
= count
+ otherCount
;
265 // do we need more space?
266 if (newCount
> capacity
&& newCount
> ensureCapacity(newCount
))
270 for (unsigned int i
= 0; i
< otherCount
; i
++) {
271 const OSMetaClassBase
*newObject
= otherArray
->getObject(i
);
273 array
[count
++] = newObject
;
274 newObject
->taggedRetain(OSTypeID(OSCollection
));
281 replaceObject(unsigned int index
, const OSMetaClassBase
*anObject
)
283 const OSMetaClassBase
*oldObject
;
285 if ((index
>= count
) || !anObject
)
289 oldObject
= array
[index
];
290 array
[index
] = anObject
;
291 anObject
->taggedRetain(OSTypeID(OSCollection
));
293 oldObject
->taggedRelease(OSTypeID(OSCollection
));
296 void OSArray::removeObject(unsigned int index
)
299 const OSMetaClassBase
*oldObject
;
305 oldObject
= array
[index
];
308 for (i
= index
; i
< count
; i
++)
309 array
[i
] = array
[i
+1];
311 oldObject
->taggedRelease(OSTypeID(OSCollection
));
314 bool OSArray::isEqualTo(const OSArray
*anArray
) const
318 if ( this == anArray
)
321 if ( count
!= anArray
->getCount() )
324 for ( i
= 0; i
< count
; i
++ ) {
325 if ( !array
[i
]->isEqualTo(anArray
->getObject(i
)) )
332 bool OSArray::isEqualTo(const OSMetaClassBase
*anObject
) const
336 otherArray
= OSDynamicCast(OSArray
, anObject
);
338 return isEqualTo(otherArray
);
343 OSObject
*OSArray::getObject(unsigned int index
) const
348 return (OSObject
*) (const_cast<OSMetaClassBase
*>(array
[index
]));
351 OSObject
*OSArray::getLastObject() const
356 return ( OSObject
*) (const_cast<OSMetaClassBase
*>(array
[count
- 1]));
359 unsigned int OSArray::getNextIndexOfObject(const OSMetaClassBase
* anObject
,
360 unsigned int index
) const
362 while ((index
< count
) && (array
[index
] != anObject
))
365 index
= (unsigned int)-1;
369 unsigned int OSArray::iteratorSize() const
371 return sizeof(unsigned int);
374 bool OSArray::initIterator(void *inIterator
) const
376 unsigned int *iteratorP
= (unsigned int *) inIterator
;
382 bool OSArray::getNextObjectForIterator(void *inIterator
, OSObject
**ret
) const
384 unsigned int *iteratorP
= (unsigned int *) inIterator
;
385 unsigned int index
= (*iteratorP
)++;
388 *ret
= (OSObject
*)(const_cast<OSMetaClassBase
*> (array
[index
]));
397 bool OSArray::serialize(OSSerialize
*s
) const
399 if (s
->previouslySerialized(this)) return true;
401 if (!s
->addXMLStartTag(this, "array")) return false;
403 for (unsigned i
= 0; i
< count
; i
++) {
404 if (!array
[i
]->serialize(s
)) return false;
407 return s
->addXMLEndTag("array");
410 unsigned OSArray::setOptions(unsigned options
, unsigned mask
, void *)
412 unsigned old
= super::setOptions(options
, mask
);
413 if ((old
^ options
) & mask
) {
415 // Value changed need to recurse over all of the child collections
416 for ( unsigned i
= 0; i
< count
; i
++ ) {
417 OSCollection
*coll
= OSDynamicCast(OSCollection
, array
[i
]);
419 coll
->setOptions(options
, mask
);
426 OSCollection
* OSArray::copyCollection(OSDictionary
*cycleDict
)
428 bool allocDict
= !cycleDict
;
429 OSCollection
*ret
= 0;
430 OSArray
*newArray
= 0;
433 cycleDict
= OSDictionary::withCapacity(16);
440 ret
= super::copyCollection(cycleDict
);
444 newArray
= OSArray::withArray(this);
448 // Insert object into cycle Dictionary
449 cycleDict
->setObject((const OSSymbol
*) this, newArray
);
451 for (unsigned int i
= 0; i
< count
; i
++) {
453 OSDynamicCast(OSCollection
, EXT_CAST(newArray
->array
[i
]));
456 OSCollection
*newColl
= coll
->copyCollection(cycleDict
);
460 newArray
->replaceObject(i
, newColl
);
475 cycleDict
->release();