1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*c54f35caSApple OSS Distributions
31*c54f35caSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
32*c54f35caSApple OSS Distributions #include <libkern/c++/OSLib.h>
33*c54f35caSApple OSS Distributions #include <libkern/c++/OSOrderedSet.h>
34*c54f35caSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
35*c54f35caSApple OSS Distributions #include <os/cpp_util.h>
36*c54f35caSApple OSS Distributions
37*c54f35caSApple OSS Distributions #define super OSCollection
38*c54f35caSApple OSS Distributions
39*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors(OSOrderedSet, OSCollection)
40*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 0);
41*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 1);
42*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 2);
43*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 3);
44*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 4);
45*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 5);
46*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 6);
47*c54f35caSApple OSS Distributions OSMetaClassDefineReservedUnused(OSOrderedSet, 7);
48*c54f35caSApple OSS Distributions
49*c54f35caSApple OSS Distributions
50*c54f35caSApple OSS Distributions struct _Element {
51*c54f35caSApple OSS Distributions OSTaggedPtr<const OSMetaClassBase> obj;
52*c54f35caSApple OSS Distributions };
53*c54f35caSApple OSS Distributions
54*c54f35caSApple OSS Distributions #define EXT_CAST(obj) \
55*c54f35caSApple OSS Distributions reinterpret_cast<OSObject *>(const_cast<OSMetaClassBase *>(obj))
56*c54f35caSApple OSS Distributions
57*c54f35caSApple OSS Distributions bool
58*c54f35caSApple OSS Distributions OSOrderedSet::
initWithCapacity(unsigned int inCapacity,OSOrderFunction inOrdering,void * inOrderingRef)59*c54f35caSApple OSS Distributions initWithCapacity(unsigned int inCapacity,
60*c54f35caSApple OSS Distributions OSOrderFunction inOrdering, void *inOrderingRef)
61*c54f35caSApple OSS Distributions {
62*c54f35caSApple OSS Distributions if (!super::init()) {
63*c54f35caSApple OSS Distributions return false;
64*c54f35caSApple OSS Distributions }
65*c54f35caSApple OSS Distributions
66*c54f35caSApple OSS Distributions if (inCapacity > (UINT_MAX / sizeof(_Element))) {
67*c54f35caSApple OSS Distributions return false;
68*c54f35caSApple OSS Distributions }
69*c54f35caSApple OSS Distributions
70*c54f35caSApple OSS Distributions array = kallocp_type_container(_Element, &inCapacity, Z_WAITOK_ZERO);
71*c54f35caSApple OSS Distributions if (!array) {
72*c54f35caSApple OSS Distributions return false;
73*c54f35caSApple OSS Distributions }
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions count = 0;
76*c54f35caSApple OSS Distributions capacity = inCapacity;
77*c54f35caSApple OSS Distributions capacityIncrement = (inCapacity)? inCapacity : 16;
78*c54f35caSApple OSS Distributions ordering = inOrdering;
79*c54f35caSApple OSS Distributions orderingRef = inOrderingRef;
80*c54f35caSApple OSS Distributions
81*c54f35caSApple OSS Distributions OSCONTAINER_ACCUMSIZE(sizeof(_Element) * inCapacity);
82*c54f35caSApple OSS Distributions
83*c54f35caSApple OSS Distributions return true;
84*c54f35caSApple OSS Distributions }
85*c54f35caSApple OSS Distributions
86*c54f35caSApple OSS Distributions OSSharedPtr<OSOrderedSet>
87*c54f35caSApple OSS Distributions OSOrderedSet::
withCapacity(unsigned int capacity,OSOrderFunction ordering,void * orderingRef)88*c54f35caSApple OSS Distributions withCapacity(unsigned int capacity,
89*c54f35caSApple OSS Distributions OSOrderFunction ordering, void * orderingRef)
90*c54f35caSApple OSS Distributions {
91*c54f35caSApple OSS Distributions auto me = OSMakeShared<OSOrderedSet>();
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions if (me && !me->initWithCapacity(capacity, ordering, orderingRef)) {
94*c54f35caSApple OSS Distributions return nullptr;
95*c54f35caSApple OSS Distributions }
96*c54f35caSApple OSS Distributions
97*c54f35caSApple OSS Distributions return me;
98*c54f35caSApple OSS Distributions }
99*c54f35caSApple OSS Distributions
100*c54f35caSApple OSS Distributions static SInt32
OSOrderedSetBlockToFunc(const OSMetaClassBase * obj1,const OSMetaClassBase * obj2,void * context)101*c54f35caSApple OSS Distributions OSOrderedSetBlockToFunc(const OSMetaClassBase * obj1,
102*c54f35caSApple OSS Distributions const OSMetaClassBase * obj2,
103*c54f35caSApple OSS Distributions void * context)
104*c54f35caSApple OSS Distributions {
105*c54f35caSApple OSS Distributions OSOrderedSet::OSOrderBlock ordering = (typeof(ordering))context;
106*c54f35caSApple OSS Distributions
107*c54f35caSApple OSS Distributions return ordering(obj1, obj2);
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions
110*c54f35caSApple OSS Distributions
111*c54f35caSApple OSS Distributions OSSharedPtr<OSOrderedSet>
withCapacity(unsigned int capacity,OSOrderBlock ordering)112*c54f35caSApple OSS Distributions OSOrderedSet::withCapacity(unsigned int capacity, OSOrderBlock ordering)
113*c54f35caSApple OSS Distributions {
114*c54f35caSApple OSS Distributions auto me = OSMakeShared<OSOrderedSet>();
115*c54f35caSApple OSS Distributions
116*c54f35caSApple OSS Distributions if (me && !me->initWithCapacity(capacity, &OSOrderedSetBlockToFunc, ordering)) {
117*c54f35caSApple OSS Distributions return nullptr;
118*c54f35caSApple OSS Distributions }
119*c54f35caSApple OSS Distributions
120*c54f35caSApple OSS Distributions return me;
121*c54f35caSApple OSS Distributions }
122*c54f35caSApple OSS Distributions
123*c54f35caSApple OSS Distributions void
free()124*c54f35caSApple OSS Distributions OSOrderedSet::free()
125*c54f35caSApple OSS Distributions {
126*c54f35caSApple OSS Distributions (void) super::setOptions(0, kImmutable);
127*c54f35caSApple OSS Distributions flushCollection();
128*c54f35caSApple OSS Distributions
129*c54f35caSApple OSS Distributions if (array) {
130*c54f35caSApple OSS Distributions kfree_type(_Element, capacity, array);
131*c54f35caSApple OSS Distributions OSCONTAINER_ACCUMSIZE( -(sizeof(_Element) * capacity));
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions
134*c54f35caSApple OSS Distributions super::free();
135*c54f35caSApple OSS Distributions }
136*c54f35caSApple OSS Distributions
137*c54f35caSApple OSS Distributions unsigned int
getCount() const138*c54f35caSApple OSS Distributions OSOrderedSet::getCount() const
139*c54f35caSApple OSS Distributions {
140*c54f35caSApple OSS Distributions return count;
141*c54f35caSApple OSS Distributions }
142*c54f35caSApple OSS Distributions unsigned int
getCapacity() const143*c54f35caSApple OSS Distributions OSOrderedSet::getCapacity() const
144*c54f35caSApple OSS Distributions {
145*c54f35caSApple OSS Distributions return capacity;
146*c54f35caSApple OSS Distributions }
147*c54f35caSApple OSS Distributions unsigned int
getCapacityIncrement() const148*c54f35caSApple OSS Distributions OSOrderedSet::getCapacityIncrement() const
149*c54f35caSApple OSS Distributions {
150*c54f35caSApple OSS Distributions return capacityIncrement;
151*c54f35caSApple OSS Distributions }
152*c54f35caSApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)153*c54f35caSApple OSS Distributions OSOrderedSet::setCapacityIncrement(unsigned int increment)
154*c54f35caSApple OSS Distributions {
155*c54f35caSApple OSS Distributions capacityIncrement = (increment)? increment : 16;
156*c54f35caSApple OSS Distributions return capacityIncrement;
157*c54f35caSApple OSS Distributions }
158*c54f35caSApple OSS Distributions
159*c54f35caSApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)160*c54f35caSApple OSS Distributions OSOrderedSet::ensureCapacity(unsigned int newCapacity)
161*c54f35caSApple OSS Distributions {
162*c54f35caSApple OSS Distributions _Element *newArray;
163*c54f35caSApple OSS Distributions unsigned int finalCapacity;
164*c54f35caSApple OSS Distributions
165*c54f35caSApple OSS Distributions if (newCapacity <= capacity) {
166*c54f35caSApple OSS Distributions return capacity;
167*c54f35caSApple OSS Distributions }
168*c54f35caSApple OSS Distributions
169*c54f35caSApple OSS Distributions // round up
170*c54f35caSApple OSS Distributions finalCapacity = (((newCapacity - 1) / capacityIncrement) + 1)
171*c54f35caSApple OSS Distributions * capacityIncrement;
172*c54f35caSApple OSS Distributions if (finalCapacity < newCapacity) {
173*c54f35caSApple OSS Distributions return capacity;
174*c54f35caSApple OSS Distributions }
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions newArray = kreallocp_type_container(_Element, array,
177*c54f35caSApple OSS Distributions capacity, &finalCapacity, Z_WAITOK_ZERO);
178*c54f35caSApple OSS Distributions if (newArray) {
179*c54f35caSApple OSS Distributions OSCONTAINER_ACCUMSIZE(sizeof(_Element) * (finalCapacity - capacity));
180*c54f35caSApple OSS Distributions array = newArray;
181*c54f35caSApple OSS Distributions capacity = finalCapacity;
182*c54f35caSApple OSS Distributions }
183*c54f35caSApple OSS Distributions
184*c54f35caSApple OSS Distributions return capacity;
185*c54f35caSApple OSS Distributions }
186*c54f35caSApple OSS Distributions
187*c54f35caSApple OSS Distributions void
flushCollection()188*c54f35caSApple OSS Distributions OSOrderedSet::flushCollection()
189*c54f35caSApple OSS Distributions {
190*c54f35caSApple OSS Distributions unsigned int i;
191*c54f35caSApple OSS Distributions
192*c54f35caSApple OSS Distributions haveUpdated();
193*c54f35caSApple OSS Distributions
194*c54f35caSApple OSS Distributions for (i = 0; i < count; i++) {
195*c54f35caSApple OSS Distributions array[i].obj.reset();
196*c54f35caSApple OSS Distributions }
197*c54f35caSApple OSS Distributions
198*c54f35caSApple OSS Distributions count = 0;
199*c54f35caSApple OSS Distributions }
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions /* internal */
202*c54f35caSApple OSS Distributions bool
setObject(unsigned int index,const OSMetaClassBase * anObject)203*c54f35caSApple OSS Distributions OSOrderedSet::setObject(unsigned int index, const OSMetaClassBase *anObject)
204*c54f35caSApple OSS Distributions {
205*c54f35caSApple OSS Distributions unsigned int i;
206*c54f35caSApple OSS Distributions unsigned int newCount = count + 1;
207*c54f35caSApple OSS Distributions
208*c54f35caSApple OSS Distributions if ((index > count) || !anObject) {
209*c54f35caSApple OSS Distributions return false;
210*c54f35caSApple OSS Distributions }
211*c54f35caSApple OSS Distributions
212*c54f35caSApple OSS Distributions if (containsObject(anObject)) {
213*c54f35caSApple OSS Distributions return false;
214*c54f35caSApple OSS Distributions }
215*c54f35caSApple OSS Distributions
216*c54f35caSApple OSS Distributions // do we need more space?
217*c54f35caSApple OSS Distributions if (newCount > capacity && newCount > ensureCapacity(newCount)) {
218*c54f35caSApple OSS Distributions return false;
219*c54f35caSApple OSS Distributions }
220*c54f35caSApple OSS Distributions
221*c54f35caSApple OSS Distributions haveUpdated();
222*c54f35caSApple OSS Distributions if (index != count) {
223*c54f35caSApple OSS Distributions for (i = count; i > index; i--) {
224*c54f35caSApple OSS Distributions array[i] = os::move(array[i - 1]);
225*c54f35caSApple OSS Distributions }
226*c54f35caSApple OSS Distributions }
227*c54f35caSApple OSS Distributions array[index].obj.reset(anObject, OSRetain);
228*c54f35caSApple OSS Distributions count++;
229*c54f35caSApple OSS Distributions
230*c54f35caSApple OSS Distributions return true;
231*c54f35caSApple OSS Distributions }
232*c54f35caSApple OSS Distributions
233*c54f35caSApple OSS Distributions bool
setObject(unsigned int index,OSSharedPtr<const OSMetaClassBase> const & anObject)234*c54f35caSApple OSS Distributions OSOrderedSet::setObject(unsigned int index, OSSharedPtr<const OSMetaClassBase> const& anObject)
235*c54f35caSApple OSS Distributions {
236*c54f35caSApple OSS Distributions return setObject(index, anObject.get());
237*c54f35caSApple OSS Distributions }
238*c54f35caSApple OSS Distributions
239*c54f35caSApple OSS Distributions bool
setFirstObject(const OSMetaClassBase * anObject)240*c54f35caSApple OSS Distributions OSOrderedSet::setFirstObject(const OSMetaClassBase *anObject)
241*c54f35caSApple OSS Distributions {
242*c54f35caSApple OSS Distributions return setObject(0, anObject);
243*c54f35caSApple OSS Distributions }
244*c54f35caSApple OSS Distributions
245*c54f35caSApple OSS Distributions bool
setFirstObject(OSSharedPtr<const OSMetaClassBase> const & anObject)246*c54f35caSApple OSS Distributions OSOrderedSet::setFirstObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
247*c54f35caSApple OSS Distributions {
248*c54f35caSApple OSS Distributions return setFirstObject(anObject.get());
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions
251*c54f35caSApple OSS Distributions bool
setLastObject(const OSMetaClassBase * anObject)252*c54f35caSApple OSS Distributions OSOrderedSet::setLastObject(const OSMetaClassBase *anObject)
253*c54f35caSApple OSS Distributions {
254*c54f35caSApple OSS Distributions return setObject( count, anObject);
255*c54f35caSApple OSS Distributions }
256*c54f35caSApple OSS Distributions
257*c54f35caSApple OSS Distributions bool
setLastObject(OSSharedPtr<const OSMetaClassBase> const & anObject)258*c54f35caSApple OSS Distributions OSOrderedSet::setLastObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
259*c54f35caSApple OSS Distributions {
260*c54f35caSApple OSS Distributions return setLastObject(anObject.get());
261*c54f35caSApple OSS Distributions }
262*c54f35caSApple OSS Distributions
263*c54f35caSApple OSS Distributions
264*c54f35caSApple OSS Distributions #define ORDER(obj1, obj2) \
265*c54f35caSApple OSS Distributions (ordering ? ((*ordering)( (const OSObject *) obj1, (const OSObject *) obj2, orderingRef)) : 0)
266*c54f35caSApple OSS Distributions
267*c54f35caSApple OSS Distributions bool
setObject(const OSMetaClassBase * anObject)268*c54f35caSApple OSS Distributions OSOrderedSet::setObject(const OSMetaClassBase *anObject )
269*c54f35caSApple OSS Distributions {
270*c54f35caSApple OSS Distributions unsigned int i;
271*c54f35caSApple OSS Distributions
272*c54f35caSApple OSS Distributions // queue it behind those with same priority
273*c54f35caSApple OSS Distributions for (i = 0;
274*c54f35caSApple OSS Distributions (i < count) && (ORDER(array[i].obj.get(), anObject) >= 0);
275*c54f35caSApple OSS Distributions i++) {
276*c54f35caSApple OSS Distributions }
277*c54f35caSApple OSS Distributions
278*c54f35caSApple OSS Distributions return setObject(i, anObject);
279*c54f35caSApple OSS Distributions }
280*c54f35caSApple OSS Distributions
281*c54f35caSApple OSS Distributions bool
setObject(OSSharedPtr<const OSMetaClassBase> const & anObject)282*c54f35caSApple OSS Distributions OSOrderedSet::setObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
283*c54f35caSApple OSS Distributions {
284*c54f35caSApple OSS Distributions return setObject(anObject.get());
285*c54f35caSApple OSS Distributions }
286*c54f35caSApple OSS Distributions
287*c54f35caSApple OSS Distributions void
removeObject(const OSMetaClassBase * anObject)288*c54f35caSApple OSS Distributions OSOrderedSet::removeObject(const OSMetaClassBase *anObject)
289*c54f35caSApple OSS Distributions {
290*c54f35caSApple OSS Distributions bool deleted = false;
291*c54f35caSApple OSS Distributions unsigned int i;
292*c54f35caSApple OSS Distributions
293*c54f35caSApple OSS Distributions for (i = 0; i < count; i++) {
294*c54f35caSApple OSS Distributions if (deleted) {
295*c54f35caSApple OSS Distributions array[i - 1] = os::move(array[i]);
296*c54f35caSApple OSS Distributions } else if (array[i].obj == anObject) {
297*c54f35caSApple OSS Distributions deleted = true;
298*c54f35caSApple OSS Distributions haveUpdated(); // Pity we can't flush the log
299*c54f35caSApple OSS Distributions array[i].obj.reset();
300*c54f35caSApple OSS Distributions }
301*c54f35caSApple OSS Distributions }
302*c54f35caSApple OSS Distributions
303*c54f35caSApple OSS Distributions if (deleted) {
304*c54f35caSApple OSS Distributions count--;
305*c54f35caSApple OSS Distributions }
306*c54f35caSApple OSS Distributions }
307*c54f35caSApple OSS Distributions
308*c54f35caSApple OSS Distributions void
removeObject(OSSharedPtr<const OSMetaClassBase> const & anObject)309*c54f35caSApple OSS Distributions OSOrderedSet::removeObject(OSSharedPtr<const OSMetaClassBase> const& anObject)
310*c54f35caSApple OSS Distributions {
311*c54f35caSApple OSS Distributions return removeObject(anObject.get());
312*c54f35caSApple OSS Distributions }
313*c54f35caSApple OSS Distributions
314*c54f35caSApple OSS Distributions bool
containsObject(const OSMetaClassBase * anObject) const315*c54f35caSApple OSS Distributions OSOrderedSet::containsObject(const OSMetaClassBase *anObject) const
316*c54f35caSApple OSS Distributions {
317*c54f35caSApple OSS Distributions return anObject && member(anObject);
318*c54f35caSApple OSS Distributions }
319*c54f35caSApple OSS Distributions
320*c54f35caSApple OSS Distributions bool
member(const OSMetaClassBase * anObject) const321*c54f35caSApple OSS Distributions OSOrderedSet::member(const OSMetaClassBase *anObject) const
322*c54f35caSApple OSS Distributions {
323*c54f35caSApple OSS Distributions unsigned int i;
324*c54f35caSApple OSS Distributions
325*c54f35caSApple OSS Distributions for (i = 0;
326*c54f35caSApple OSS Distributions (i < count) && (array[i].obj != anObject);
327*c54f35caSApple OSS Distributions i++) {
328*c54f35caSApple OSS Distributions }
329*c54f35caSApple OSS Distributions
330*c54f35caSApple OSS Distributions return i < count;
331*c54f35caSApple OSS Distributions }
332*c54f35caSApple OSS Distributions
333*c54f35caSApple OSS Distributions /* internal */
334*c54f35caSApple OSS Distributions OSObject *
getObject(unsigned int index) const335*c54f35caSApple OSS Distributions OSOrderedSet::getObject( unsigned int index ) const
336*c54f35caSApple OSS Distributions {
337*c54f35caSApple OSS Distributions if (index >= count) {
338*c54f35caSApple OSS Distributions return NULL;
339*c54f35caSApple OSS Distributions }
340*c54f35caSApple OSS Distributions
341*c54f35caSApple OSS Distributions return const_cast<OSObject *>((const OSObject *) array[index].obj.get());
342*c54f35caSApple OSS Distributions }
343*c54f35caSApple OSS Distributions
344*c54f35caSApple OSS Distributions OSObject *
getFirstObject() const345*c54f35caSApple OSS Distributions OSOrderedSet::getFirstObject() const
346*c54f35caSApple OSS Distributions {
347*c54f35caSApple OSS Distributions if (count) {
348*c54f35caSApple OSS Distributions return const_cast<OSObject *>((const OSObject *) array[0].obj.get());
349*c54f35caSApple OSS Distributions } else {
350*c54f35caSApple OSS Distributions return NULL;
351*c54f35caSApple OSS Distributions }
352*c54f35caSApple OSS Distributions }
353*c54f35caSApple OSS Distributions
354*c54f35caSApple OSS Distributions OSObject *
getLastObject() const355*c54f35caSApple OSS Distributions OSOrderedSet::getLastObject() const
356*c54f35caSApple OSS Distributions {
357*c54f35caSApple OSS Distributions if (count) {
358*c54f35caSApple OSS Distributions return const_cast<OSObject *>((const OSObject *) array[count - 1].obj.get());
359*c54f35caSApple OSS Distributions } else {
360*c54f35caSApple OSS Distributions return NULL;
361*c54f35caSApple OSS Distributions }
362*c54f35caSApple OSS Distributions }
363*c54f35caSApple OSS Distributions
364*c54f35caSApple OSS Distributions SInt32
orderObject(const OSMetaClassBase * anObject)365*c54f35caSApple OSS Distributions OSOrderedSet::orderObject( const OSMetaClassBase * anObject )
366*c54f35caSApple OSS Distributions {
367*c54f35caSApple OSS Distributions return ORDER( anObject, NULL );
368*c54f35caSApple OSS Distributions }
369*c54f35caSApple OSS Distributions
370*c54f35caSApple OSS Distributions void *
getOrderingRef()371*c54f35caSApple OSS Distributions OSOrderedSet::getOrderingRef()
372*c54f35caSApple OSS Distributions {
373*c54f35caSApple OSS Distributions return orderingRef;
374*c54f35caSApple OSS Distributions }
375*c54f35caSApple OSS Distributions
376*c54f35caSApple OSS Distributions bool
isEqualTo(const OSOrderedSet * anOrderedSet) const377*c54f35caSApple OSS Distributions OSOrderedSet::isEqualTo(const OSOrderedSet *anOrderedSet) const
378*c54f35caSApple OSS Distributions {
379*c54f35caSApple OSS Distributions unsigned int i;
380*c54f35caSApple OSS Distributions
381*c54f35caSApple OSS Distributions if (this == anOrderedSet) {
382*c54f35caSApple OSS Distributions return true;
383*c54f35caSApple OSS Distributions }
384*c54f35caSApple OSS Distributions
385*c54f35caSApple OSS Distributions if (count != anOrderedSet->getCount()) {
386*c54f35caSApple OSS Distributions return false;
387*c54f35caSApple OSS Distributions }
388*c54f35caSApple OSS Distributions
389*c54f35caSApple OSS Distributions for (i = 0; i < count; i++) {
390*c54f35caSApple OSS Distributions if (!array[i].obj->isEqualTo(anOrderedSet->getObject(i))) {
391*c54f35caSApple OSS Distributions return false;
392*c54f35caSApple OSS Distributions }
393*c54f35caSApple OSS Distributions }
394*c54f35caSApple OSS Distributions
395*c54f35caSApple OSS Distributions return true;
396*c54f35caSApple OSS Distributions }
397*c54f35caSApple OSS Distributions
398*c54f35caSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * anObject) const399*c54f35caSApple OSS Distributions OSOrderedSet::isEqualTo(const OSMetaClassBase *anObject) const
400*c54f35caSApple OSS Distributions {
401*c54f35caSApple OSS Distributions OSOrderedSet *oSet;
402*c54f35caSApple OSS Distributions
403*c54f35caSApple OSS Distributions oSet = OSDynamicCast(OSOrderedSet, anObject);
404*c54f35caSApple OSS Distributions if (oSet) {
405*c54f35caSApple OSS Distributions return isEqualTo(oSet);
406*c54f35caSApple OSS Distributions } else {
407*c54f35caSApple OSS Distributions return false;
408*c54f35caSApple OSS Distributions }
409*c54f35caSApple OSS Distributions }
410*c54f35caSApple OSS Distributions
411*c54f35caSApple OSS Distributions unsigned int
iteratorSize() const412*c54f35caSApple OSS Distributions OSOrderedSet::iteratorSize() const
413*c54f35caSApple OSS Distributions {
414*c54f35caSApple OSS Distributions return sizeof(unsigned int);
415*c54f35caSApple OSS Distributions }
416*c54f35caSApple OSS Distributions
417*c54f35caSApple OSS Distributions bool
initIterator(void * inIterator) const418*c54f35caSApple OSS Distributions OSOrderedSet::initIterator(void *inIterator) const
419*c54f35caSApple OSS Distributions {
420*c54f35caSApple OSS Distributions unsigned int *iteratorP = (unsigned int *) inIterator;
421*c54f35caSApple OSS Distributions
422*c54f35caSApple OSS Distributions *iteratorP = 0;
423*c54f35caSApple OSS Distributions return true;
424*c54f35caSApple OSS Distributions }
425*c54f35caSApple OSS Distributions
426*c54f35caSApple OSS Distributions bool
427*c54f35caSApple OSS Distributions OSOrderedSet::
getNextObjectForIterator(void * inIterator,OSObject ** ret) const428*c54f35caSApple OSS Distributions getNextObjectForIterator(void *inIterator, OSObject **ret) const
429*c54f35caSApple OSS Distributions {
430*c54f35caSApple OSS Distributions unsigned int *iteratorP = (unsigned int *) inIterator;
431*c54f35caSApple OSS Distributions unsigned int index = (*iteratorP)++;
432*c54f35caSApple OSS Distributions
433*c54f35caSApple OSS Distributions if (index < count) {
434*c54f35caSApple OSS Distributions *ret = const_cast<OSObject *>((const OSObject *) array[index].obj.get());
435*c54f35caSApple OSS Distributions } else {
436*c54f35caSApple OSS Distributions *ret = NULL;
437*c54f35caSApple OSS Distributions }
438*c54f35caSApple OSS Distributions
439*c54f35caSApple OSS Distributions return *ret != NULL;
440*c54f35caSApple OSS Distributions }
441*c54f35caSApple OSS Distributions
442*c54f35caSApple OSS Distributions
443*c54f35caSApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)444*c54f35caSApple OSS Distributions OSOrderedSet::setOptions(unsigned options, unsigned mask, void *)
445*c54f35caSApple OSS Distributions {
446*c54f35caSApple OSS Distributions unsigned old = super::setOptions(options, mask);
447*c54f35caSApple OSS Distributions if ((old ^ options) & mask) {
448*c54f35caSApple OSS Distributions // Value changed need to recurse over all of the child collections
449*c54f35caSApple OSS Distributions for (unsigned i = 0; i < count; i++) {
450*c54f35caSApple OSS Distributions OSCollection *coll = OSDynamicCast(OSCollection, array[i].obj.get());
451*c54f35caSApple OSS Distributions if (coll) {
452*c54f35caSApple OSS Distributions coll->setOptions(options, mask);
453*c54f35caSApple OSS Distributions }
454*c54f35caSApple OSS Distributions }
455*c54f35caSApple OSS Distributions }
456*c54f35caSApple OSS Distributions
457*c54f35caSApple OSS Distributions return old;
458*c54f35caSApple OSS Distributions }
459*c54f35caSApple OSS Distributions
460*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)461*c54f35caSApple OSS Distributions OSOrderedSet::copyCollection(OSDictionary *cycleDict)
462*c54f35caSApple OSS Distributions {
463*c54f35caSApple OSS Distributions OSSharedPtr<OSDictionary> ourCycleDict;
464*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection> ret;
465*c54f35caSApple OSS Distributions OSSharedPtr<OSOrderedSet> newSet;
466*c54f35caSApple OSS Distributions
467*c54f35caSApple OSS Distributions if (!cycleDict) {
468*c54f35caSApple OSS Distributions ourCycleDict = OSDictionary::withCapacity(16);
469*c54f35caSApple OSS Distributions if (!ourCycleDict) {
470*c54f35caSApple OSS Distributions return nullptr;
471*c54f35caSApple OSS Distributions }
472*c54f35caSApple OSS Distributions cycleDict = ourCycleDict.get();
473*c54f35caSApple OSS Distributions }
474*c54f35caSApple OSS Distributions
475*c54f35caSApple OSS Distributions do {
476*c54f35caSApple OSS Distributions // Check for a cycle
477*c54f35caSApple OSS Distributions ret = super::copyCollection(cycleDict);
478*c54f35caSApple OSS Distributions if (ret) {
479*c54f35caSApple OSS Distributions continue;
480*c54f35caSApple OSS Distributions }
481*c54f35caSApple OSS Distributions
482*c54f35caSApple OSS Distributions // Duplicate the set with no contents
483*c54f35caSApple OSS Distributions newSet = OSOrderedSet::withCapacity(capacity, ordering, orderingRef);
484*c54f35caSApple OSS Distributions if (!newSet) {
485*c54f35caSApple OSS Distributions continue;
486*c54f35caSApple OSS Distributions }
487*c54f35caSApple OSS Distributions
488*c54f35caSApple OSS Distributions // Insert object into cycle Dictionary
489*c54f35caSApple OSS Distributions cycleDict->setObject((const OSSymbol *) this, newSet.get());
490*c54f35caSApple OSS Distributions
491*c54f35caSApple OSS Distributions newSet->capacityIncrement = capacityIncrement;
492*c54f35caSApple OSS Distributions
493*c54f35caSApple OSS Distributions // Now copy over the contents to the new duplicate
494*c54f35caSApple OSS Distributions for (unsigned int i = 0; i < count; i++) {
495*c54f35caSApple OSS Distributions OSObject *obj = EXT_CAST(array[i].obj.get());
496*c54f35caSApple OSS Distributions OSCollection *coll = OSDynamicCast(OSCollection, obj);
497*c54f35caSApple OSS Distributions if (coll) {
498*c54f35caSApple OSS Distributions OSSharedPtr<OSCollection> newColl = coll->copyCollection(cycleDict);
499*c54f35caSApple OSS Distributions if (newColl) {
500*c54f35caSApple OSS Distributions obj = newColl.get(); // Rely on cycleDict ref for a bit
501*c54f35caSApple OSS Distributions } else {
502*c54f35caSApple OSS Distributions return ret;
503*c54f35caSApple OSS Distributions }
504*c54f35caSApple OSS Distributions }
505*c54f35caSApple OSS Distributions
506*c54f35caSApple OSS Distributions newSet->setLastObject(obj);
507*c54f35caSApple OSS Distributions }
508*c54f35caSApple OSS Distributions
509*c54f35caSApple OSS Distributions ret = os::move(newSet);
510*c54f35caSApple OSS Distributions } while (false);
511*c54f35caSApple OSS Distributions
512*c54f35caSApple OSS Distributions return ret;
513*c54f35caSApple OSS Distributions }
514