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