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