xref: /xnu-10063.121.3/libkern/c++/OSArray.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions /* IOArray.m created by rsulack on Fri 12-Sep-1997 */
29*2c2f96dcSApple OSS Distributions /* IOArray.cpp converted to C++ by gvdl on Fri 1998-10-30 */
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSArray.h>
35*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
36*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSLib.h>
37*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
38*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
39*2c2f96dcSApple OSS Distributions #include <libkern/OSDebug.h>
40*2c2f96dcSApple OSS Distributions #include <os/cpp_util.h>
41*2c2f96dcSApple OSS Distributions 
42*2c2f96dcSApple OSS Distributions #define super OSCollection
43*2c2f96dcSApple OSS Distributions 
44*2c2f96dcSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSArray, OSCollection,
45*2c2f96dcSApple OSS Distributions     (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 0);
47*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 1);
48*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 2);
49*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 3);
50*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 4);
51*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 5);
52*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 6);
53*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSArray, 7);
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)56*2c2f96dcSApple OSS Distributions OSArray::initWithCapacity(unsigned int inCapacity)
57*2c2f96dcSApple OSS Distributions {
58*2c2f96dcSApple OSS Distributions 	if (!super::init()) {
59*2c2f96dcSApple OSS Distributions 		return false;
60*2c2f96dcSApple OSS Distributions 	}
61*2c2f96dcSApple OSS Distributions 
62*2c2f96dcSApple OSS Distributions 	// integer overflow check
63*2c2f96dcSApple OSS Distributions 	if (inCapacity > (UINT_MAX / sizeof(*array))) {
64*2c2f96dcSApple OSS Distributions 		return false;
65*2c2f96dcSApple OSS Distributions 	}
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions 	array = kallocp_type_container(ArrayPtrType, &inCapacity, Z_WAITOK_ZERO);
68*2c2f96dcSApple OSS Distributions 	if (!array) {
69*2c2f96dcSApple OSS Distributions 		return false;
70*2c2f96dcSApple OSS Distributions 	}
71*2c2f96dcSApple OSS Distributions 
72*2c2f96dcSApple OSS Distributions 	count = 0;
73*2c2f96dcSApple OSS Distributions 	capacity = inCapacity;
74*2c2f96dcSApple OSS Distributions 	capacityIncrement = (inCapacity)? inCapacity : 16;
75*2c2f96dcSApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(sizeof(*array) * inCapacity);
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions 	return true;
78*2c2f96dcSApple OSS Distributions }
79*2c2f96dcSApple OSS Distributions 
80*2c2f96dcSApple OSS Distributions bool
initWithObjects(const OSObject * objects[],unsigned int theCount,unsigned int theCapacity)81*2c2f96dcSApple OSS Distributions OSArray::initWithObjects(const OSObject *objects[],
82*2c2f96dcSApple OSS Distributions     unsigned int theCount,
83*2c2f96dcSApple OSS Distributions     unsigned int theCapacity)
84*2c2f96dcSApple OSS Distributions {
85*2c2f96dcSApple OSS Distributions 	unsigned int initCapacity;
86*2c2f96dcSApple OSS Distributions 
87*2c2f96dcSApple OSS Distributions 	if (!theCapacity) {
88*2c2f96dcSApple OSS Distributions 		initCapacity = theCount;
89*2c2f96dcSApple OSS Distributions 	} else if (theCount > theCapacity) {
90*2c2f96dcSApple OSS Distributions 		return false;
91*2c2f96dcSApple OSS Distributions 	} else {
92*2c2f96dcSApple OSS Distributions 		initCapacity = theCapacity;
93*2c2f96dcSApple OSS Distributions 	}
94*2c2f96dcSApple OSS Distributions 
95*2c2f96dcSApple OSS Distributions 	if (!objects || !initWithCapacity(initCapacity)) {
96*2c2f96dcSApple OSS Distributions 		return false;
97*2c2f96dcSApple OSS Distributions 	}
98*2c2f96dcSApple OSS Distributions 
99*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < theCount; i++) {
100*2c2f96dcSApple OSS Distributions 		const OSMetaClassBase *newObject = *objects++;
101*2c2f96dcSApple OSS Distributions 
102*2c2f96dcSApple OSS Distributions 		if (!newObject) {
103*2c2f96dcSApple OSS Distributions 			return false;
104*2c2f96dcSApple OSS Distributions 		}
105*2c2f96dcSApple OSS Distributions 
106*2c2f96dcSApple OSS Distributions 		array[count++].reset(newObject, OSRetain);
107*2c2f96dcSApple OSS Distributions 	}
108*2c2f96dcSApple OSS Distributions 
109*2c2f96dcSApple OSS Distributions 	return true;
110*2c2f96dcSApple OSS Distributions }
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions bool
initWithArray(const OSArray * anArray,unsigned int theCapacity)113*2c2f96dcSApple OSS Distributions OSArray::initWithArray(const OSArray *anArray,
114*2c2f96dcSApple OSS Distributions     unsigned int theCapacity)
115*2c2f96dcSApple OSS Distributions {
116*2c2f96dcSApple OSS Distributions 	if (!anArray) {
117*2c2f96dcSApple OSS Distributions 		return false;
118*2c2f96dcSApple OSS Distributions 	}
119*2c2f96dcSApple OSS Distributions 
120*2c2f96dcSApple OSS Distributions 	return initWithObjects((const OSObject **) anArray->array,
121*2c2f96dcSApple OSS Distributions 	           anArray->count, theCapacity);
122*2c2f96dcSApple OSS Distributions }
123*2c2f96dcSApple OSS Distributions 
124*2c2f96dcSApple OSS Distributions OSSharedPtr<OSArray>
withCapacity(unsigned int capacity)125*2c2f96dcSApple OSS Distributions OSArray::withCapacity(unsigned int capacity)
126*2c2f96dcSApple OSS Distributions {
127*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
128*2c2f96dcSApple OSS Distributions 
129*2c2f96dcSApple OSS Distributions 	if (me && !me->initWithCapacity(capacity)) {
130*2c2f96dcSApple OSS Distributions 		return nullptr;
131*2c2f96dcSApple OSS Distributions 	}
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions 	return me;
134*2c2f96dcSApple OSS Distributions }
135*2c2f96dcSApple OSS Distributions 
136*2c2f96dcSApple OSS Distributions OSSharedPtr<OSArray>
withObjects(const OSObject * objects[],unsigned int count,unsigned int capacity)137*2c2f96dcSApple OSS Distributions OSArray::withObjects(const OSObject *objects[],
138*2c2f96dcSApple OSS Distributions     unsigned int count,
139*2c2f96dcSApple OSS Distributions     unsigned int capacity)
140*2c2f96dcSApple OSS Distributions {
141*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
142*2c2f96dcSApple OSS Distributions 
143*2c2f96dcSApple OSS Distributions 	if (me && !me->initWithObjects(objects, count, capacity)) {
144*2c2f96dcSApple OSS Distributions 		return nullptr;
145*2c2f96dcSApple OSS Distributions 	}
146*2c2f96dcSApple OSS Distributions 
147*2c2f96dcSApple OSS Distributions 	return me;
148*2c2f96dcSApple OSS Distributions }
149*2c2f96dcSApple OSS Distributions 
150*2c2f96dcSApple OSS Distributions OSSharedPtr<OSArray>
withArray(const OSArray * array,unsigned int capacity)151*2c2f96dcSApple OSS Distributions OSArray::withArray(const OSArray *array,
152*2c2f96dcSApple OSS Distributions     unsigned int capacity)
153*2c2f96dcSApple OSS Distributions {
154*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> me = OSMakeShared<OSArray>();
155*2c2f96dcSApple OSS Distributions 
156*2c2f96dcSApple OSS Distributions 	if (me && !me->initWithArray(array, capacity)) {
157*2c2f96dcSApple OSS Distributions 		return nullptr;
158*2c2f96dcSApple OSS Distributions 	}
159*2c2f96dcSApple OSS Distributions 
160*2c2f96dcSApple OSS Distributions 	return me;
161*2c2f96dcSApple OSS Distributions }
162*2c2f96dcSApple OSS Distributions 
163*2c2f96dcSApple OSS Distributions void
free()164*2c2f96dcSApple OSS Distributions OSArray::free()
165*2c2f96dcSApple OSS Distributions {
166*2c2f96dcSApple OSS Distributions 	// Clear immutability - assumes the container is doing the right thing
167*2c2f96dcSApple OSS Distributions 	(void) super::setOptions(0, kImmutable);
168*2c2f96dcSApple OSS Distributions 
169*2c2f96dcSApple OSS Distributions 	flushCollection();
170*2c2f96dcSApple OSS Distributions 
171*2c2f96dcSApple OSS Distributions 	if (array) {
172*2c2f96dcSApple OSS Distributions 		os::destroy(array, array + capacity);
173*2c2f96dcSApple OSS Distributions 		kfree_type(ArrayPtrType, capacity, array);
174*2c2f96dcSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE( -(sizeof(*array) * capacity));
175*2c2f96dcSApple OSS Distributions 	}
176*2c2f96dcSApple OSS Distributions 
177*2c2f96dcSApple OSS Distributions 	super::free();
178*2c2f96dcSApple OSS Distributions }
179*2c2f96dcSApple OSS Distributions 
180*2c2f96dcSApple OSS Distributions 
181*2c2f96dcSApple OSS Distributions unsigned int
getCount() const182*2c2f96dcSApple OSS Distributions OSArray::getCount() const
183*2c2f96dcSApple OSS Distributions {
184*2c2f96dcSApple OSS Distributions 	return count;
185*2c2f96dcSApple OSS Distributions }
186*2c2f96dcSApple OSS Distributions unsigned int
getCapacity() const187*2c2f96dcSApple OSS Distributions OSArray::getCapacity() const
188*2c2f96dcSApple OSS Distributions {
189*2c2f96dcSApple OSS Distributions 	return capacity;
190*2c2f96dcSApple OSS Distributions }
191*2c2f96dcSApple OSS Distributions unsigned int
getCapacityIncrement() const192*2c2f96dcSApple OSS Distributions OSArray::getCapacityIncrement() const
193*2c2f96dcSApple OSS Distributions {
194*2c2f96dcSApple OSS Distributions 	return capacityIncrement;
195*2c2f96dcSApple OSS Distributions }
196*2c2f96dcSApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)197*2c2f96dcSApple OSS Distributions OSArray::setCapacityIncrement(unsigned int increment)
198*2c2f96dcSApple OSS Distributions {
199*2c2f96dcSApple OSS Distributions 	capacityIncrement = (increment)? increment : 16;
200*2c2f96dcSApple OSS Distributions 
201*2c2f96dcSApple OSS Distributions 	return capacityIncrement;
202*2c2f96dcSApple OSS Distributions }
203*2c2f96dcSApple OSS Distributions 
204*2c2f96dcSApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)205*2c2f96dcSApple OSS Distributions OSArray::ensureCapacity(unsigned int newCapacity)
206*2c2f96dcSApple OSS Distributions {
207*2c2f96dcSApple OSS Distributions 	ArraySharedPtrType *newArray;
208*2c2f96dcSApple OSS Distributions 	unsigned int        finalCapacity;
209*2c2f96dcSApple OSS Distributions 
210*2c2f96dcSApple OSS Distributions 	if (newCapacity <= capacity) {
211*2c2f96dcSApple OSS Distributions 		return capacity;
212*2c2f96dcSApple OSS Distributions 	}
213*2c2f96dcSApple OSS Distributions 
214*2c2f96dcSApple OSS Distributions 	// round up
215*2c2f96dcSApple OSS Distributions 	finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
216*2c2f96dcSApple OSS Distributions 	    * capacityIncrement;
217*2c2f96dcSApple OSS Distributions 
218*2c2f96dcSApple OSS Distributions 	// integer overflow check
219*2c2f96dcSApple OSS Distributions 	if (finalCapacity < newCapacity) {
220*2c2f96dcSApple OSS Distributions 		return capacity;
221*2c2f96dcSApple OSS Distributions 	}
222*2c2f96dcSApple OSS Distributions 
223*2c2f96dcSApple OSS Distributions 	newArray = kreallocp_type_container(ArrayPtrType, array,
224*2c2f96dcSApple OSS Distributions 	    capacity, &finalCapacity, Z_WAITOK_ZERO);
225*2c2f96dcSApple OSS Distributions 	if (newArray) {
226*2c2f96dcSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(sizeof(*array) * (finalCapacity - capacity));
227*2c2f96dcSApple OSS Distributions 		array = newArray;
228*2c2f96dcSApple OSS Distributions 		capacity = finalCapacity;
229*2c2f96dcSApple OSS Distributions 	}
230*2c2f96dcSApple OSS Distributions 
231*2c2f96dcSApple OSS Distributions 	return capacity;
232*2c2f96dcSApple OSS Distributions }
233*2c2f96dcSApple OSS Distributions 
234*2c2f96dcSApple OSS Distributions void
flushCollection()235*2c2f96dcSApple OSS Distributions OSArray::flushCollection()
236*2c2f96dcSApple OSS Distributions {
237*2c2f96dcSApple OSS Distributions 	unsigned int i;
238*2c2f96dcSApple OSS Distributions 
239*2c2f96dcSApple OSS Distributions 	haveUpdated();
240*2c2f96dcSApple OSS Distributions 	for (i = 0; i < count; i++) {
241*2c2f96dcSApple OSS Distributions 		array[i].reset();
242*2c2f96dcSApple OSS Distributions 	}
243*2c2f96dcSApple OSS Distributions 	count = 0;
244*2c2f96dcSApple OSS Distributions }
245*2c2f96dcSApple OSS Distributions 
246*2c2f96dcSApple OSS Distributions bool
setObject(const OSMetaClassBase * anObject)247*2c2f96dcSApple OSS Distributions OSArray::setObject(const OSMetaClassBase *anObject)
248*2c2f96dcSApple OSS Distributions {
249*2c2f96dcSApple OSS Distributions 	return setObject(count, anObject);
250*2c2f96dcSApple OSS Distributions }
251*2c2f96dcSApple OSS Distributions 
252*2c2f96dcSApple OSS Distributions bool
setObject(OSSharedPtr<const OSMetaClassBase> const & anObject)253*2c2f96dcSApple OSS Distributions OSArray::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
254*2c2f96dcSApple OSS Distributions {
255*2c2f96dcSApple OSS Distributions 	return setObject(count, anObject);
256*2c2f96dcSApple OSS Distributions }
257*2c2f96dcSApple OSS Distributions 
258*2c2f96dcSApple OSS Distributions bool
setObject(unsigned int index,const OSMetaClassBase * anObject)259*2c2f96dcSApple OSS Distributions OSArray::setObject(unsigned int index, const OSMetaClassBase *anObject)
260*2c2f96dcSApple OSS Distributions {
261*2c2f96dcSApple OSS Distributions 	unsigned int i;
262*2c2f96dcSApple OSS Distributions 	unsigned int newCount = count + 1;
263*2c2f96dcSApple OSS Distributions 
264*2c2f96dcSApple OSS Distributions 	if ((index > count) || !anObject) {
265*2c2f96dcSApple OSS Distributions 		return false;
266*2c2f96dcSApple OSS Distributions 	}
267*2c2f96dcSApple OSS Distributions 
268*2c2f96dcSApple OSS Distributions 	// do we need more space?
269*2c2f96dcSApple OSS Distributions 	if (newCount > capacity && newCount > ensureCapacity(newCount)) {
270*2c2f96dcSApple OSS Distributions 		return false;
271*2c2f96dcSApple OSS Distributions 	}
272*2c2f96dcSApple OSS Distributions 
273*2c2f96dcSApple OSS Distributions 	haveUpdated();
274*2c2f96dcSApple OSS Distributions 	if (index != count) {
275*2c2f96dcSApple OSS Distributions 		for (i = count; i > index; i--) {
276*2c2f96dcSApple OSS Distributions 			array[i] = os::move(array[i - 1]);
277*2c2f96dcSApple OSS Distributions 		}
278*2c2f96dcSApple OSS Distributions 	}
279*2c2f96dcSApple OSS Distributions 	array[index].reset(anObject, OSRetain);
280*2c2f96dcSApple OSS Distributions 	count++;
281*2c2f96dcSApple OSS Distributions 
282*2c2f96dcSApple OSS Distributions 	return true;
283*2c2f96dcSApple OSS Distributions }
284*2c2f96dcSApple OSS Distributions 
285*2c2f96dcSApple OSS Distributions bool
setObject(unsigned int index,OSSharedPtr<const OSMetaClassBase> const & anObject)286*2c2f96dcSApple OSS Distributions OSArray::setObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
287*2c2f96dcSApple OSS Distributions {
288*2c2f96dcSApple OSS Distributions 	return setObject(index, anObject.get());
289*2c2f96dcSApple OSS Distributions }
290*2c2f96dcSApple OSS Distributions 
291*2c2f96dcSApple OSS Distributions bool
merge(const OSArray * otherArray)292*2c2f96dcSApple OSS Distributions OSArray::merge(const OSArray * otherArray)
293*2c2f96dcSApple OSS Distributions {
294*2c2f96dcSApple OSS Distributions 	unsigned int otherCount = otherArray->getCount();
295*2c2f96dcSApple OSS Distributions 	unsigned int newCount = count + otherCount;
296*2c2f96dcSApple OSS Distributions 
297*2c2f96dcSApple OSS Distributions 	if (!otherCount) {
298*2c2f96dcSApple OSS Distributions 		return true;
299*2c2f96dcSApple OSS Distributions 	}
300*2c2f96dcSApple OSS Distributions 
301*2c2f96dcSApple OSS Distributions 	if (newCount < count) {
302*2c2f96dcSApple OSS Distributions 		return false;
303*2c2f96dcSApple OSS Distributions 	}
304*2c2f96dcSApple OSS Distributions 
305*2c2f96dcSApple OSS Distributions 	// do we need more space?
306*2c2f96dcSApple OSS Distributions 	if (newCount > capacity && newCount > ensureCapacity(newCount)) {
307*2c2f96dcSApple OSS Distributions 		return false;
308*2c2f96dcSApple OSS Distributions 	}
309*2c2f96dcSApple OSS Distributions 
310*2c2f96dcSApple OSS Distributions 	haveUpdated();
311*2c2f96dcSApple OSS Distributions 	for (unsigned int i = 0; i < otherCount; i++) {
312*2c2f96dcSApple OSS Distributions 		const OSMetaClassBase *newObject = otherArray->getObject(i);
313*2c2f96dcSApple OSS Distributions 
314*2c2f96dcSApple OSS Distributions 		array[count++].reset(newObject, OSRetain);
315*2c2f96dcSApple OSS Distributions 	}
316*2c2f96dcSApple OSS Distributions 
317*2c2f96dcSApple OSS Distributions 	return true;
318*2c2f96dcSApple OSS Distributions }
319*2c2f96dcSApple OSS Distributions 
320*2c2f96dcSApple OSS Distributions void
321*2c2f96dcSApple OSS Distributions OSArray::
replaceObject(unsigned int index,const OSMetaClassBase * anObject)322*2c2f96dcSApple OSS Distributions replaceObject(unsigned int index, const OSMetaClassBase *anObject)
323*2c2f96dcSApple OSS Distributions {
324*2c2f96dcSApple OSS Distributions 	if ((index >= count) || !anObject) {
325*2c2f96dcSApple OSS Distributions 		return;
326*2c2f96dcSApple OSS Distributions 	}
327*2c2f96dcSApple OSS Distributions 
328*2c2f96dcSApple OSS Distributions 	haveUpdated();
329*2c2f96dcSApple OSS Distributions 
330*2c2f96dcSApple OSS Distributions 	array[index].reset(anObject, OSRetain);
331*2c2f96dcSApple OSS Distributions }
332*2c2f96dcSApple OSS Distributions 
333*2c2f96dcSApple OSS Distributions void
replaceObject(unsigned int index,OSSharedPtr<const OSMetaClassBase> const & anObject)334*2c2f96dcSApple OSS Distributions OSArray::replaceObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
335*2c2f96dcSApple OSS Distributions {
336*2c2f96dcSApple OSS Distributions 	return replaceObject(index, anObject.get());
337*2c2f96dcSApple OSS Distributions }
338*2c2f96dcSApple OSS Distributions 
339*2c2f96dcSApple OSS Distributions void
removeObject(unsigned int index)340*2c2f96dcSApple OSS Distributions OSArray::removeObject(unsigned int index)
341*2c2f96dcSApple OSS Distributions {
342*2c2f96dcSApple OSS Distributions 	unsigned int i;
343*2c2f96dcSApple OSS Distributions 	ArraySharedPtrType oldObject;
344*2c2f96dcSApple OSS Distributions 
345*2c2f96dcSApple OSS Distributions 	if (index >= count) {
346*2c2f96dcSApple OSS Distributions 		return;
347*2c2f96dcSApple OSS Distributions 	}
348*2c2f96dcSApple OSS Distributions 
349*2c2f96dcSApple OSS Distributions 	haveUpdated();
350*2c2f96dcSApple OSS Distributions 	oldObject = os::move(array[index]);
351*2c2f96dcSApple OSS Distributions 
352*2c2f96dcSApple OSS Distributions 	count--;
353*2c2f96dcSApple OSS Distributions 	for (i = index; i < count; i++) {
354*2c2f96dcSApple OSS Distributions 		array[i] = os::move(array[i + 1]);
355*2c2f96dcSApple OSS Distributions 	}
356*2c2f96dcSApple OSS Distributions }
357*2c2f96dcSApple OSS Distributions 
358*2c2f96dcSApple OSS Distributions bool
isEqualTo(const OSArray * anArray) const359*2c2f96dcSApple OSS Distributions OSArray::isEqualTo(const OSArray *anArray) const
360*2c2f96dcSApple OSS Distributions {
361*2c2f96dcSApple OSS Distributions 	unsigned int i;
362*2c2f96dcSApple OSS Distributions 
363*2c2f96dcSApple OSS Distributions 	if (this == anArray) {
364*2c2f96dcSApple OSS Distributions 		return true;
365*2c2f96dcSApple OSS Distributions 	}
366*2c2f96dcSApple OSS Distributions 
367*2c2f96dcSApple OSS Distributions 	if (count != anArray->getCount()) {
368*2c2f96dcSApple OSS Distributions 		return false;
369*2c2f96dcSApple OSS Distributions 	}
370*2c2f96dcSApple OSS Distributions 
371*2c2f96dcSApple OSS Distributions 	for (i = 0; i < count; i++) {
372*2c2f96dcSApple OSS Distributions 		if (!array[i]->isEqualTo(anArray->getObject(i))) {
373*2c2f96dcSApple OSS Distributions 			return false;
374*2c2f96dcSApple OSS Distributions 		}
375*2c2f96dcSApple OSS Distributions 	}
376*2c2f96dcSApple OSS Distributions 
377*2c2f96dcSApple OSS Distributions 	return true;
378*2c2f96dcSApple OSS Distributions }
379*2c2f96dcSApple OSS Distributions 
380*2c2f96dcSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObject) const381*2c2f96dcSApple OSS Distributions OSArray::isEqualTo(const OSMetaClassBase *anObject) const
382*2c2f96dcSApple OSS Distributions {
383*2c2f96dcSApple OSS Distributions 	OSArray *otherArray;
384*2c2f96dcSApple OSS Distributions 
385*2c2f96dcSApple OSS Distributions 	otherArray = OSDynamicCast(OSArray, anObject);
386*2c2f96dcSApple OSS Distributions 	if (otherArray) {
387*2c2f96dcSApple OSS Distributions 		return isEqualTo(otherArray);
388*2c2f96dcSApple OSS Distributions 	} else {
389*2c2f96dcSApple OSS Distributions 		return false;
390*2c2f96dcSApple OSS Distributions 	}
391*2c2f96dcSApple OSS Distributions }
392*2c2f96dcSApple OSS Distributions 
393*2c2f96dcSApple OSS Distributions OSObject *
getObject(unsigned int index) const394*2c2f96dcSApple OSS Distributions OSArray::getObject(unsigned int index) const
395*2c2f96dcSApple OSS Distributions {
396*2c2f96dcSApple OSS Distributions 	if (index >= count) {
397*2c2f96dcSApple OSS Distributions 		return NULL;
398*2c2f96dcSApple OSS Distributions 	} else {
399*2c2f96dcSApple OSS Distributions 		return static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[index].get()));
400*2c2f96dcSApple OSS Distributions 	}
401*2c2f96dcSApple OSS Distributions }
402*2c2f96dcSApple OSS Distributions 
403*2c2f96dcSApple OSS Distributions OSObject *
getLastObject() const404*2c2f96dcSApple OSS Distributions OSArray::getLastObject() const
405*2c2f96dcSApple OSS Distributions {
406*2c2f96dcSApple OSS Distributions 	if (count == 0) {
407*2c2f96dcSApple OSS Distributions 		return NULL;
408*2c2f96dcSApple OSS Distributions 	} else {
409*2c2f96dcSApple OSS Distributions 		return static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[count - 1].get()));
410*2c2f96dcSApple OSS Distributions 	}
411*2c2f96dcSApple OSS Distributions }
412*2c2f96dcSApple OSS Distributions 
413*2c2f96dcSApple OSS Distributions unsigned int
getNextIndexOfObject(const OSMetaClassBase * anObject,unsigned int index) const414*2c2f96dcSApple OSS Distributions OSArray::getNextIndexOfObject(const OSMetaClassBase * anObject,
415*2c2f96dcSApple OSS Distributions     unsigned int index) const
416*2c2f96dcSApple OSS Distributions {
417*2c2f96dcSApple OSS Distributions 	while ((index < count) && (array[index] != anObject)) {
418*2c2f96dcSApple OSS Distributions 		index++;
419*2c2f96dcSApple OSS Distributions 	}
420*2c2f96dcSApple OSS Distributions 	if (index >= count) {
421*2c2f96dcSApple OSS Distributions 		index = (unsigned int)-1;
422*2c2f96dcSApple OSS Distributions 	}
423*2c2f96dcSApple OSS Distributions 	return index;
424*2c2f96dcSApple OSS Distributions }
425*2c2f96dcSApple OSS Distributions 
426*2c2f96dcSApple OSS Distributions unsigned int
iteratorSize() const427*2c2f96dcSApple OSS Distributions OSArray::iteratorSize() const
428*2c2f96dcSApple OSS Distributions {
429*2c2f96dcSApple OSS Distributions 	return sizeof(unsigned int);
430*2c2f96dcSApple OSS Distributions }
431*2c2f96dcSApple OSS Distributions 
432*2c2f96dcSApple OSS Distributions bool
initIterator(void * inIterator) const433*2c2f96dcSApple OSS Distributions OSArray::initIterator(void *inIterator) const
434*2c2f96dcSApple OSS Distributions {
435*2c2f96dcSApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
436*2c2f96dcSApple OSS Distributions 
437*2c2f96dcSApple OSS Distributions 	*iteratorP = 0;
438*2c2f96dcSApple OSS Distributions 	return true;
439*2c2f96dcSApple OSS Distributions }
440*2c2f96dcSApple OSS Distributions 
441*2c2f96dcSApple OSS Distributions bool
getNextObjectForIterator(void * inIterator,OSObject ** ret) const442*2c2f96dcSApple OSS Distributions OSArray::getNextObjectForIterator(void *inIterator, OSObject **ret) const
443*2c2f96dcSApple OSS Distributions {
444*2c2f96dcSApple OSS Distributions 	unsigned int *iteratorP = (unsigned int *) inIterator;
445*2c2f96dcSApple OSS Distributions 	unsigned int index = (*iteratorP)++;
446*2c2f96dcSApple OSS Distributions 
447*2c2f96dcSApple OSS Distributions 	if (index < count) {
448*2c2f96dcSApple OSS Distributions 		*ret = static_cast<OSObject *>(const_cast<OSMetaClassBase *>(array[index].get()));
449*2c2f96dcSApple OSS Distributions 		return true;
450*2c2f96dcSApple OSS Distributions 	} else {
451*2c2f96dcSApple OSS Distributions 		*ret = NULL;
452*2c2f96dcSApple OSS Distributions 		return false;
453*2c2f96dcSApple OSS Distributions 	}
454*2c2f96dcSApple OSS Distributions }
455*2c2f96dcSApple OSS Distributions 
456*2c2f96dcSApple OSS Distributions bool
serialize(OSSerialize * s) const457*2c2f96dcSApple OSS Distributions OSArray::serialize(OSSerialize *s) const
458*2c2f96dcSApple OSS Distributions {
459*2c2f96dcSApple OSS Distributions 	if (s->previouslySerialized(this)) {
460*2c2f96dcSApple OSS Distributions 		return true;
461*2c2f96dcSApple OSS Distributions 	}
462*2c2f96dcSApple OSS Distributions 
463*2c2f96dcSApple OSS Distributions 	if (!s->addXMLStartTag(this, "array")) {
464*2c2f96dcSApple OSS Distributions 		return false;
465*2c2f96dcSApple OSS Distributions 	}
466*2c2f96dcSApple OSS Distributions 
467*2c2f96dcSApple OSS Distributions 	for (unsigned i = 0; i < count; i++) {
468*2c2f96dcSApple OSS Distributions 		if (array[i] == NULL || !array[i]->serialize(s)) {
469*2c2f96dcSApple OSS Distributions 			return false;
470*2c2f96dcSApple OSS Distributions 		}
471*2c2f96dcSApple OSS Distributions 	}
472*2c2f96dcSApple OSS Distributions 
473*2c2f96dcSApple OSS Distributions 	return s->addXMLEndTag("array");
474*2c2f96dcSApple OSS Distributions }
475*2c2f96dcSApple OSS Distributions 
476*2c2f96dcSApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)477*2c2f96dcSApple OSS Distributions OSArray::setOptions(unsigned options, unsigned mask, void *)
478*2c2f96dcSApple OSS Distributions {
479*2c2f96dcSApple OSS Distributions 	unsigned old = super::setOptions(options, mask);
480*2c2f96dcSApple OSS Distributions 	if ((old ^ options) & mask) {
481*2c2f96dcSApple OSS Distributions 		// Value changed need to recurse over all of the child collections
482*2c2f96dcSApple OSS Distributions 		for (unsigned i = 0; i < count; i++) {
483*2c2f96dcSApple OSS Distributions 			OSCollection *coll = OSDynamicCast(OSCollection, array[i].get());
484*2c2f96dcSApple OSS Distributions 			if (coll) {
485*2c2f96dcSApple OSS Distributions 				coll->setOptions(options, mask);
486*2c2f96dcSApple OSS Distributions 			}
487*2c2f96dcSApple OSS Distributions 		}
488*2c2f96dcSApple OSS Distributions 	}
489*2c2f96dcSApple OSS Distributions 
490*2c2f96dcSApple OSS Distributions 	return old;
491*2c2f96dcSApple OSS Distributions }
492*2c2f96dcSApple OSS Distributions 
493*2c2f96dcSApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)494*2c2f96dcSApple OSS Distributions OSArray::copyCollection(OSDictionary *cycleDict)
495*2c2f96dcSApple OSS Distributions {
496*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSDictionary> ourCycleDict;
497*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSCollection> ret;
498*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSArray> newArray;
499*2c2f96dcSApple OSS Distributions 
500*2c2f96dcSApple OSS Distributions 	if (!cycleDict) {
501*2c2f96dcSApple OSS Distributions 		ourCycleDict = OSDictionary::withCapacity(16);
502*2c2f96dcSApple OSS Distributions 		if (!ourCycleDict) {
503*2c2f96dcSApple OSS Distributions 			return nullptr;
504*2c2f96dcSApple OSS Distributions 		}
505*2c2f96dcSApple OSS Distributions 		cycleDict = ourCycleDict.get();
506*2c2f96dcSApple OSS Distributions 	}
507*2c2f96dcSApple OSS Distributions 
508*2c2f96dcSApple OSS Distributions 	do {
509*2c2f96dcSApple OSS Distributions 		// Check for a cycle
510*2c2f96dcSApple OSS Distributions 		ret = super::copyCollection(cycleDict);
511*2c2f96dcSApple OSS Distributions 		if (ret) {
512*2c2f96dcSApple OSS Distributions 			continue;
513*2c2f96dcSApple OSS Distributions 		}
514*2c2f96dcSApple OSS Distributions 
515*2c2f96dcSApple OSS Distributions 		newArray = OSArray::withArray(this);
516*2c2f96dcSApple OSS Distributions 		if (!newArray) {
517*2c2f96dcSApple OSS Distributions 			continue;
518*2c2f96dcSApple OSS Distributions 		}
519*2c2f96dcSApple OSS Distributions 
520*2c2f96dcSApple OSS Distributions 		// Insert object into cycle Dictionary
521*2c2f96dcSApple OSS Distributions 		cycleDict->setObject((const OSSymbol *) this, newArray.get());
522*2c2f96dcSApple OSS Distributions 
523*2c2f96dcSApple OSS Distributions 		for (unsigned int i = 0; i < count; i++) {
524*2c2f96dcSApple OSS Distributions 			OSCollection *coll =
525*2c2f96dcSApple OSS Distributions 			    OSDynamicCast(OSCollection, static_cast<OSObject *>(
526*2c2f96dcSApple OSS Distributions 				    const_cast<OSMetaClassBase *>(
527*2c2f96dcSApple OSS Distributions 					    newArray->array[i].get())));
528*2c2f96dcSApple OSS Distributions 
529*2c2f96dcSApple OSS Distributions 			if (coll) {
530*2c2f96dcSApple OSS Distributions 				OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
531*2c2f96dcSApple OSS Distributions 				if (!newColl) {
532*2c2f96dcSApple OSS Distributions 					return ret;
533*2c2f96dcSApple OSS Distributions 				}
534*2c2f96dcSApple OSS Distributions 
535*2c2f96dcSApple OSS Distributions 				newArray->replaceObject(i, newColl.get());
536*2c2f96dcSApple OSS Distributions 			}
537*2c2f96dcSApple OSS Distributions 		}
538*2c2f96dcSApple OSS Distributions 
539*2c2f96dcSApple OSS Distributions 		ret = os::move(newArray);
540*2c2f96dcSApple OSS Distributions 	} while (false);
541*2c2f96dcSApple OSS Distributions 
542*2c2f96dcSApple OSS Distributions 	return ret;
543*2c2f96dcSApple OSS Distributions }
544