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