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