xref: /xnu-8020.121.3/libkern/c++/OSCollection.cpp (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions /* IOArray.h created by rsulack on Thu 11-Sep-1997 */
29*fdd8201dSApple OSS Distributions 
30*fdd8201dSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*fdd8201dSApple OSS Distributions 
32*fdd8201dSApple OSS Distributions #include <libkern/OSDebug.h>
33*fdd8201dSApple OSS Distributions 
34*fdd8201dSApple OSS Distributions #include <libkern/c++/OSCollection.h>
35*fdd8201dSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
36*fdd8201dSApple OSS Distributions 
37*fdd8201dSApple OSS Distributions #include <IOKit/IOKitDebug.h>
38*fdd8201dSApple OSS Distributions 
39*fdd8201dSApple OSS Distributions #define super OSObject
40*fdd8201dSApple OSS Distributions 
41*fdd8201dSApple OSS Distributions OSDefineMetaClassAndAbstractStructors(OSCollection, OSObject)
42*fdd8201dSApple OSS Distributions 
43*fdd8201dSApple OSS Distributions 
44*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUsedX86(OSCollection, 0);
45*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUsedX86(OSCollection, 1);
46*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 2);
47*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 3);
48*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 4);
49*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 5);
50*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 6);
51*fdd8201dSApple OSS Distributions OSMetaClassDefineReservedUnused(OSCollection, 7);
52*fdd8201dSApple OSS Distributions 
53*fdd8201dSApple OSS Distributions bool
init()54*fdd8201dSApple OSS Distributions OSCollection::init()
55*fdd8201dSApple OSS Distributions {
56*fdd8201dSApple OSS Distributions 	if (!super::init()) {
57*fdd8201dSApple OSS Distributions 		return false;
58*fdd8201dSApple OSS Distributions 	}
59*fdd8201dSApple OSS Distributions 
60*fdd8201dSApple OSS Distributions 	updateStamp = 0;
61*fdd8201dSApple OSS Distributions 
62*fdd8201dSApple OSS Distributions 	return true;
63*fdd8201dSApple OSS Distributions }
64*fdd8201dSApple OSS Distributions 
65*fdd8201dSApple OSS Distributions void
haveUpdated()66*fdd8201dSApple OSS Distributions OSCollection::haveUpdated()
67*fdd8201dSApple OSS Distributions {
68*fdd8201dSApple OSS Distributions 	if (fOptions & kImmutable) {
69*fdd8201dSApple OSS Distributions 		if (!(gIOKitDebug & kOSRegistryModsMode)) {
70*fdd8201dSApple OSS Distributions 			panic("Trying to change a collection in the registry");
71*fdd8201dSApple OSS Distributions 		} else {
72*fdd8201dSApple OSS Distributions 			OSReportWithBacktrace("Trying to change a collection in the registry");
73*fdd8201dSApple OSS Distributions 		}
74*fdd8201dSApple OSS Distributions 	}
75*fdd8201dSApple OSS Distributions 	updateStamp++;
76*fdd8201dSApple OSS Distributions }
77*fdd8201dSApple OSS Distributions 
78*fdd8201dSApple OSS Distributions unsigned
setOptions(unsigned options,unsigned mask,void *)79*fdd8201dSApple OSS Distributions OSCollection::setOptions(unsigned options, unsigned mask, void *)
80*fdd8201dSApple OSS Distributions {
81*fdd8201dSApple OSS Distributions 	unsigned old = fOptions;
82*fdd8201dSApple OSS Distributions 
83*fdd8201dSApple OSS Distributions 	if (mask) {
84*fdd8201dSApple OSS Distributions 		fOptions = (old & ~mask) | (options & mask);
85*fdd8201dSApple OSS Distributions 	}
86*fdd8201dSApple OSS Distributions 
87*fdd8201dSApple OSS Distributions 	return old;
88*fdd8201dSApple OSS Distributions }
89*fdd8201dSApple OSS Distributions 
90*fdd8201dSApple OSS Distributions OSSharedPtr<OSCollection>
copyCollection(OSDictionary * cycleDict)91*fdd8201dSApple OSS Distributions OSCollection::copyCollection(OSDictionary *cycleDict)
92*fdd8201dSApple OSS Distributions {
93*fdd8201dSApple OSS Distributions 	if (cycleDict) {
94*fdd8201dSApple OSS Distributions 		OSObject *obj = cycleDict->getObject((const OSSymbol *) this);
95*fdd8201dSApple OSS Distributions 
96*fdd8201dSApple OSS Distributions 		return OSSharedPtr<OSCollection>(reinterpret_cast<OSCollection *>(obj), OSRetain);
97*fdd8201dSApple OSS Distributions 	} else {
98*fdd8201dSApple OSS Distributions 		// If we are here it means that there is a collection subclass that
99*fdd8201dSApple OSS Distributions 		// hasn't overridden the copyCollection method.  In which case just
100*fdd8201dSApple OSS Distributions 		// return a reference to ourselves.
101*fdd8201dSApple OSS Distributions 		// Hopefully this collection will not be inserted into the registry
102*fdd8201dSApple OSS Distributions 		return OSSharedPtr<OSCollection>(this, OSRetain);
103*fdd8201dSApple OSS Distributions 	}
104*fdd8201dSApple OSS Distributions }
105*fdd8201dSApple OSS Distributions 
106*fdd8201dSApple OSS Distributions bool
iterateObjects(void * refcon,bool (* callback)(void * refcon,OSObject * object))107*fdd8201dSApple OSS Distributions OSCollection::iterateObjects(void * refcon, bool (*callback)(void * refcon, OSObject * object))
108*fdd8201dSApple OSS Distributions {
109*fdd8201dSApple OSS Distributions 	uint64_t     iteratorStore[2];
110*fdd8201dSApple OSS Distributions 	unsigned int initialUpdateStamp;
111*fdd8201dSApple OSS Distributions 	bool         done;
112*fdd8201dSApple OSS Distributions 
113*fdd8201dSApple OSS Distributions 	assert(iteratorSize() < sizeof(iteratorStore));
114*fdd8201dSApple OSS Distributions 
115*fdd8201dSApple OSS Distributions 	if (!initIterator(&iteratorStore[0])) {
116*fdd8201dSApple OSS Distributions 		return false;
117*fdd8201dSApple OSS Distributions 	}
118*fdd8201dSApple OSS Distributions 
119*fdd8201dSApple OSS Distributions 	initialUpdateStamp = updateStamp;
120*fdd8201dSApple OSS Distributions 	done = false;
121*fdd8201dSApple OSS Distributions 	do{
122*fdd8201dSApple OSS Distributions 		OSObject * object;
123*fdd8201dSApple OSS Distributions 		if (!getNextObjectForIterator(&iteratorStore[0], &object)) {
124*fdd8201dSApple OSS Distributions 			break;
125*fdd8201dSApple OSS Distributions 		}
126*fdd8201dSApple OSS Distributions 		done = callback(refcon, object);
127*fdd8201dSApple OSS Distributions 	}while (!done && (initialUpdateStamp == updateStamp));
128*fdd8201dSApple OSS Distributions 
129*fdd8201dSApple OSS Distributions 	return initialUpdateStamp == updateStamp;
130*fdd8201dSApple OSS Distributions }
131*fdd8201dSApple OSS Distributions 
132*fdd8201dSApple OSS Distributions static bool
OSCollectionIterateObjectsBlock(void * refcon,OSObject * object)133*fdd8201dSApple OSS Distributions OSCollectionIterateObjectsBlock(void * refcon, OSObject * object)
134*fdd8201dSApple OSS Distributions {
135*fdd8201dSApple OSS Distributions 	bool (^block)(OSObject * object) = (typeof(block))refcon;
136*fdd8201dSApple OSS Distributions 	return block(object);
137*fdd8201dSApple OSS Distributions }
138*fdd8201dSApple OSS Distributions 
139*fdd8201dSApple OSS Distributions bool
140*fdd8201dSApple OSS Distributions OSCollection::iterateObjects(bool (^block)(OSObject * object))
141*fdd8201dSApple OSS Distributions {
142*fdd8201dSApple OSS Distributions 	return iterateObjects((void *) block, OSCollectionIterateObjectsBlock);
143*fdd8201dSApple OSS Distributions }
144