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
30bb611c8fSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31bb611c8fSApple OSS Distributions
32c1dac77fSApple OSS Distributions #include <libkern/c++/OSArray.h>
33bb611c8fSApple OSS Distributions #include <libkern/c++/OSCollection.h>
34bb611c8fSApple OSS Distributions #include <libkern/c++/OSCollectionIterator.h>
35c1dac77fSApple OSS Distributions #include <libkern/c++/OSLib.h>
36bb611c8fSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
37c1dac77fSApple OSS Distributions
38c1dac77fSApple OSS Distributions #define super OSIterator
39c1dac77fSApple OSS Distributions
OSDefineMetaClassAndStructors(OSCollectionIterator,OSIterator)40c1dac77fSApple OSS Distributions OSDefineMetaClassAndStructors(OSCollectionIterator, OSIterator)
41c1dac77fSApple OSS Distributions
42a5e72196SApple OSS Distributions bool
43a5e72196SApple OSS Distributions OSCollectionIterator::initWithCollection(const OSCollection *inColl)
44c1dac77fSApple OSS Distributions {
45a5e72196SApple OSS Distributions if (!super::init() || !inColl) {
46c1dac77fSApple OSS Distributions return false;
47a5e72196SApple OSS Distributions }
48c1dac77fSApple OSS Distributions
49bb611c8fSApple OSS Distributions collection.reset(inColl, OSRetain);
50a5e72196SApple OSS Distributions collIterator = NULL;
51c1dac77fSApple OSS Distributions initialUpdateStamp = 0;
52c1dac77fSApple OSS Distributions valid = false;
53c1dac77fSApple OSS Distributions
540f3703acSApple OSS Distributions return true;
55c1dac77fSApple OSS Distributions }
56c1dac77fSApple OSS Distributions
57bb611c8fSApple OSS Distributions OSSharedPtr<OSCollectionIterator>
withCollection(const OSCollection * inColl)58c1dac77fSApple OSS Distributions OSCollectionIterator::withCollection(const OSCollection *inColl)
59c1dac77fSApple OSS Distributions {
60bb611c8fSApple OSS Distributions OSSharedPtr<OSCollectionIterator> me = OSMakeShared<OSCollectionIterator>();
61c1dac77fSApple OSS Distributions
62c1dac77fSApple OSS Distributions if (me && !me->initWithCollection(inColl)) {
63bb611c8fSApple OSS Distributions return nullptr;
64c1dac77fSApple OSS Distributions }
65c1dac77fSApple OSS Distributions
66c1dac77fSApple OSS Distributions return me;
67c1dac77fSApple OSS Distributions }
68c1dac77fSApple OSS Distributions
69a5e72196SApple OSS Distributions void
free()70a5e72196SApple OSS Distributions OSCollectionIterator::free()
71c1dac77fSApple OSS Distributions {
72*e6231be0SApple OSS Distributions freeIteratorStorage();
73c1dac77fSApple OSS Distributions
74bb611c8fSApple OSS Distributions collection.reset();
75c1dac77fSApple OSS Distributions
76c1dac77fSApple OSS Distributions super::free();
77c1dac77fSApple OSS Distributions }
78c1dac77fSApple OSS Distributions
79a5e72196SApple OSS Distributions void
reset()80a5e72196SApple OSS Distributions OSCollectionIterator::reset()
81c1dac77fSApple OSS Distributions {
82c1dac77fSApple OSS Distributions valid = false;
83*e6231be0SApple OSS Distributions bool initialized = initializeIteratorStorage();
84c1dac77fSApple OSS Distributions
85*e6231be0SApple OSS Distributions if (!initialized) {
86*e6231be0SApple OSS Distributions // reusing existing storage
87*e6231be0SApple OSS Distributions void * storage = getIteratorStorage();
88*e6231be0SApple OSS Distributions bzero(storage, collection->iteratorSize());
89c1dac77fSApple OSS Distributions
90*e6231be0SApple OSS Distributions if (!collection->initIterator(storage)) {
91c1dac77fSApple OSS Distributions return;
92a5e72196SApple OSS Distributions }
93c1dac77fSApple OSS Distributions
94c1dac77fSApple OSS Distributions initialUpdateStamp = collection->updateStamp;
95c1dac77fSApple OSS Distributions valid = true;
96c1dac77fSApple OSS Distributions }
97*e6231be0SApple OSS Distributions }
98c1dac77fSApple OSS Distributions
99a5e72196SApple OSS Distributions bool
isValid()100a5e72196SApple OSS Distributions OSCollectionIterator::isValid()
101c1dac77fSApple OSS Distributions {
102*e6231be0SApple OSS Distributions initializeIteratorStorage();
103*e6231be0SApple OSS Distributions
104*e6231be0SApple OSS Distributions if (!valid || collection->updateStamp != initialUpdateStamp) {
105c1dac77fSApple OSS Distributions return false;
106a5e72196SApple OSS Distributions }
107c1dac77fSApple OSS Distributions
108c1dac77fSApple OSS Distributions return true;
109c1dac77fSApple OSS Distributions }
110c1dac77fSApple OSS Distributions
111*e6231be0SApple OSS Distributions bool
initializeIteratorStorage()112*e6231be0SApple OSS Distributions OSCollectionIterator::initializeIteratorStorage()
113*e6231be0SApple OSS Distributions {
114*e6231be0SApple OSS Distributions void * result = NULL;
115*e6231be0SApple OSS Distributions bool initialized = false;
116*e6231be0SApple OSS Distributions
117*e6231be0SApple OSS Distributions #if __LP64__
118*e6231be0SApple OSS Distributions OSCollectionIteratorStorageType storageType = getStorageType();
119*e6231be0SApple OSS Distributions switch (storageType) {
120*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageUnallocated:
121*e6231be0SApple OSS Distributions if (collection->iteratorSize() > sizeof(inlineStorage) || isSubclassed()) {
122*e6231be0SApple OSS Distributions collIterator = (void *)kalloc_data(collection->iteratorSize(), Z_WAITOK);
123*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(collection->iteratorSize());
124*e6231be0SApple OSS Distributions if (!collection->initIterator(collIterator)) {
125*e6231be0SApple OSS Distributions kfree_data(collIterator, collection->iteratorSize());
126*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(-((size_t) collection->iteratorSize()));
127*e6231be0SApple OSS Distributions collIterator = NULL;
128*e6231be0SApple OSS Distributions initialized = false;
129*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
130*e6231be0SApple OSS Distributions } else {
131*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStoragePointer);
132*e6231be0SApple OSS Distributions result = collIterator;
133*e6231be0SApple OSS Distributions initialized = true;
134*e6231be0SApple OSS Distributions }
135*e6231be0SApple OSS Distributions } else {
136*e6231be0SApple OSS Distributions bzero(&inlineStorage[0], collection->iteratorSize());
137*e6231be0SApple OSS Distributions if (!collection->initIterator(&inlineStorage[0])) {
138*e6231be0SApple OSS Distributions bzero(&inlineStorage[0], collection->iteratorSize());
139*e6231be0SApple OSS Distributions initialized = false;
140*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
141*e6231be0SApple OSS Distributions } else {
142*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageInline);
143*e6231be0SApple OSS Distributions result = &inlineStorage[0];
144*e6231be0SApple OSS Distributions initialized = true;
145*e6231be0SApple OSS Distributions }
146*e6231be0SApple OSS Distributions }
147*e6231be0SApple OSS Distributions break;
148*e6231be0SApple OSS Distributions case OSCollectionIteratorStoragePointer:
149*e6231be0SApple OSS Distributions // already initialized
150*e6231be0SApple OSS Distributions initialized = false;
151*e6231be0SApple OSS Distributions break;
152*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageInline:
153*e6231be0SApple OSS Distributions // already initialized
154*e6231be0SApple OSS Distributions initialized = false;
155*e6231be0SApple OSS Distributions break;
156*e6231be0SApple OSS Distributions default:
157*e6231be0SApple OSS Distributions panic("unexpected storage type %u", storageType);
158*e6231be0SApple OSS Distributions }
159*e6231be0SApple OSS Distributions #else
160*e6231be0SApple OSS Distributions if (!collIterator) {
161*e6231be0SApple OSS Distributions collIterator = (void *)kalloc_data(collection->iteratorSize(), Z_WAITOK);
162*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(collection->iteratorSize());
163*e6231be0SApple OSS Distributions if (!collection->initIterator(collIterator)) {
164*e6231be0SApple OSS Distributions kfree_data(collIterator, collection->iteratorSize());
165*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(-((size_t) collection->iteratorSize()));
166*e6231be0SApple OSS Distributions collIterator = NULL;
167*e6231be0SApple OSS Distributions initialized = false;
168*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
169*e6231be0SApple OSS Distributions } else {
170*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStoragePointer);
171*e6231be0SApple OSS Distributions result = collIterator;
172*e6231be0SApple OSS Distributions initialized = true;
173*e6231be0SApple OSS Distributions }
174*e6231be0SApple OSS Distributions }
175*e6231be0SApple OSS Distributions #endif /* __LP64__ */
176*e6231be0SApple OSS Distributions
177*e6231be0SApple OSS Distributions if (initialized) {
178*e6231be0SApple OSS Distributions valid = true;
179*e6231be0SApple OSS Distributions initialUpdateStamp = collection->updateStamp;
180*e6231be0SApple OSS Distributions }
181*e6231be0SApple OSS Distributions
182*e6231be0SApple OSS Distributions return initialized;
183*e6231be0SApple OSS Distributions }
184*e6231be0SApple OSS Distributions
185*e6231be0SApple OSS Distributions void *
getIteratorStorage()186*e6231be0SApple OSS Distributions OSCollectionIterator::getIteratorStorage()
187*e6231be0SApple OSS Distributions {
188*e6231be0SApple OSS Distributions void * result = NULL;
189*e6231be0SApple OSS Distributions
190*e6231be0SApple OSS Distributions #if __LP64__
191*e6231be0SApple OSS Distributions OSCollectionIteratorStorageType storageType = getStorageType();
192*e6231be0SApple OSS Distributions
193*e6231be0SApple OSS Distributions switch (storageType) {
194*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageUnallocated:
195*e6231be0SApple OSS Distributions result = NULL;
196*e6231be0SApple OSS Distributions break;
197*e6231be0SApple OSS Distributions case OSCollectionIteratorStoragePointer:
198*e6231be0SApple OSS Distributions result = collIterator;
199*e6231be0SApple OSS Distributions break;
200*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageInline:
201*e6231be0SApple OSS Distributions result = &inlineStorage[0];
202*e6231be0SApple OSS Distributions break;
203*e6231be0SApple OSS Distributions default:
204*e6231be0SApple OSS Distributions panic("unexpected storage type %u", storageType);
205*e6231be0SApple OSS Distributions }
206*e6231be0SApple OSS Distributions #else
207*e6231be0SApple OSS Distributions OSCollectionIteratorStorageType storageType __assert_only = getStorageType();
208*e6231be0SApple OSS Distributions assert(storageType == OSCollectionIteratorStoragePointer || storageType == OSCollectionIteratorStorageUnallocated);
209*e6231be0SApple OSS Distributions result = collIterator;
210*e6231be0SApple OSS Distributions #endif /* __LP64__ */
211*e6231be0SApple OSS Distributions
212*e6231be0SApple OSS Distributions return result;
213*e6231be0SApple OSS Distributions }
214*e6231be0SApple OSS Distributions
215*e6231be0SApple OSS Distributions void
freeIteratorStorage()216*e6231be0SApple OSS Distributions OSCollectionIterator::freeIteratorStorage()
217*e6231be0SApple OSS Distributions {
218*e6231be0SApple OSS Distributions #if __LP64__
219*e6231be0SApple OSS Distributions OSCollectionIteratorStorageType storageType = getStorageType();
220*e6231be0SApple OSS Distributions
221*e6231be0SApple OSS Distributions switch (storageType) {
222*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageUnallocated:
223*e6231be0SApple OSS Distributions break;
224*e6231be0SApple OSS Distributions case OSCollectionIteratorStoragePointer:
225*e6231be0SApple OSS Distributions kfree_data(collIterator, collection->iteratorSize());
226*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(-((size_t) collection->iteratorSize()));
227*e6231be0SApple OSS Distributions collIterator = NULL;
228*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
229*e6231be0SApple OSS Distributions break;
230*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageInline:
231*e6231be0SApple OSS Distributions bzero(&inlineStorage[0], collection->iteratorSize());
232*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
233*e6231be0SApple OSS Distributions break;
234*e6231be0SApple OSS Distributions default:
235*e6231be0SApple OSS Distributions panic("unexpected storage type %u", storageType);
236*e6231be0SApple OSS Distributions }
237*e6231be0SApple OSS Distributions #else
238*e6231be0SApple OSS Distributions if (collIterator != NULL) {
239*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStoragePointer);
240*e6231be0SApple OSS Distributions kfree_data(collIterator, collection->iteratorSize());
241*e6231be0SApple OSS Distributions OSCONTAINER_ACCUMSIZE(-((size_t) collection->iteratorSize()));
242*e6231be0SApple OSS Distributions collIterator = NULL;
243*e6231be0SApple OSS Distributions setStorageType(OSCollectionIteratorStorageUnallocated);
244*e6231be0SApple OSS Distributions } else {
245*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStorageUnallocated);
246*e6231be0SApple OSS Distributions }
247*e6231be0SApple OSS Distributions #endif /* __LP64__ */
248*e6231be0SApple OSS Distributions }
249*e6231be0SApple OSS Distributions
250*e6231be0SApple OSS Distributions bool
isSubclassed()251*e6231be0SApple OSS Distributions OSCollectionIterator::isSubclassed()
252*e6231be0SApple OSS Distributions {
253*e6231be0SApple OSS Distributions return getMetaClass() != OSCollectionIterator::metaClass;
254*e6231be0SApple OSS Distributions }
255*e6231be0SApple OSS Distributions
256*e6231be0SApple OSS Distributions OSCollectionIteratorStorageType
getStorageType()257*e6231be0SApple OSS Distributions OSCollectionIterator::getStorageType()
258*e6231be0SApple OSS Distributions {
259*e6231be0SApple OSS Distributions #if __LP64__
260*e6231be0SApple OSS Distributions // Storage type is in the most significant 2 bits of collIterator
261*e6231be0SApple OSS Distributions return (OSCollectionIteratorStorageType)((uintptr_t)(collIterator) >> 62);
262*e6231be0SApple OSS Distributions #else
263*e6231be0SApple OSS Distributions if (collIterator != NULL) {
264*e6231be0SApple OSS Distributions return OSCollectionIteratorStoragePointer;
265*e6231be0SApple OSS Distributions } else {
266*e6231be0SApple OSS Distributions return OSCollectionIteratorStorageUnallocated;
267*e6231be0SApple OSS Distributions }
268*e6231be0SApple OSS Distributions #endif /* __LP64__ */
269*e6231be0SApple OSS Distributions }
270*e6231be0SApple OSS Distributions
271*e6231be0SApple OSS Distributions void
setStorageType(OSCollectionIteratorStorageType storageType)272*e6231be0SApple OSS Distributions OSCollectionIterator::setStorageType(OSCollectionIteratorStorageType storageType)
273*e6231be0SApple OSS Distributions {
274*e6231be0SApple OSS Distributions #if __LP64__
275*e6231be0SApple OSS Distributions switch (storageType) {
276*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageUnallocated:
277*e6231be0SApple OSS Distributions if (collIterator != NULL) {
278*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStorageInline);
279*e6231be0SApple OSS Distributions collIterator = NULL;
280*e6231be0SApple OSS Distributions }
281*e6231be0SApple OSS Distributions break;
282*e6231be0SApple OSS Distributions case OSCollectionIteratorStoragePointer:
283*e6231be0SApple OSS Distributions // Should already be set
284*e6231be0SApple OSS Distributions assert(collIterator != NULL);
285*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStoragePointer);
286*e6231be0SApple OSS Distributions break;
287*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageInline:
288*e6231be0SApple OSS Distributions // Set the two most sigificant bits of collIterator to 10b
289*e6231be0SApple OSS Distributions collIterator = (void *)(((uintptr_t)collIterator & ~0xC000000000000000) | ((uintptr_t)OSCollectionIteratorStorageInline << 62));
290*e6231be0SApple OSS Distributions break;
291*e6231be0SApple OSS Distributions default:
292*e6231be0SApple OSS Distributions panic("unexpected storage type %u", storageType);
293*e6231be0SApple OSS Distributions }
294*e6231be0SApple OSS Distributions #else
295*e6231be0SApple OSS Distributions switch (storageType) {
296*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageUnallocated:
297*e6231be0SApple OSS Distributions // Should already be set
298*e6231be0SApple OSS Distributions assert(collIterator == NULL);
299*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStorageUnallocated);
300*e6231be0SApple OSS Distributions break;
301*e6231be0SApple OSS Distributions case OSCollectionIteratorStoragePointer:
302*e6231be0SApple OSS Distributions // Should already be set
303*e6231be0SApple OSS Distributions assert(collIterator != NULL);
304*e6231be0SApple OSS Distributions assert(getStorageType() == OSCollectionIteratorStoragePointer);
305*e6231be0SApple OSS Distributions break;
306*e6231be0SApple OSS Distributions case OSCollectionIteratorStorageInline:
307*e6231be0SApple OSS Distributions panic("cannot use inline storage on LP32");
308*e6231be0SApple OSS Distributions break;
309*e6231be0SApple OSS Distributions default:
310*e6231be0SApple OSS Distributions panic("unexpected storage type %u", storageType);
311*e6231be0SApple OSS Distributions }
312*e6231be0SApple OSS Distributions #endif /* __LP64__ */
313*e6231be0SApple OSS Distributions }
314*e6231be0SApple OSS Distributions
315a5e72196SApple OSS Distributions OSObject *
getNextObject()316a5e72196SApple OSS Distributions OSCollectionIterator::getNextObject()
317c1dac77fSApple OSS Distributions {
318c1dac77fSApple OSS Distributions OSObject *retObj;
319c1dac77fSApple OSS Distributions bool retVal;
320*e6231be0SApple OSS Distributions void * storage;
321c1dac77fSApple OSS Distributions
322a5e72196SApple OSS Distributions if (!isValid()) {
323a5e72196SApple OSS Distributions return NULL;
324c1dac77fSApple OSS Distributions }
325c1dac77fSApple OSS Distributions
326*e6231be0SApple OSS Distributions storage = getIteratorStorage();
327*e6231be0SApple OSS Distributions assert(storage != NULL);
328*e6231be0SApple OSS Distributions
329*e6231be0SApple OSS Distributions retVal = collection->getNextObjectForIterator(storage, &retObj);
330a5e72196SApple OSS Distributions return (retVal)? retObj : NULL;
331a5e72196SApple OSS Distributions }
332