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