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