1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* IOArray.h created by rsulack on Thu 11-Sep-1997 */
29 
30 #define IOKIT_ENABLE_SHARED_PTR
31 
32 #include <libkern/c++/OSArray.h>
33 #include <libkern/c++/OSCollection.h>
34 #include <libkern/c++/OSCollectionIterator.h>
35 #include <libkern/c++/OSLib.h>
36 #include <libkern/c++/OSSharedPtr.h>
37 
38 #define super OSIterator
39 
40 OSDefineMetaClassAndStructors(OSCollectionIterator, OSIterator)
41 
42 bool
43 OSCollectionIterator::initWithCollection(const OSCollection *inColl)
44 {
45 	if (!super::init() || !inColl) {
46 		return false;
47 	}
48 
49 	collection.reset(inColl, OSRetain);
50 	collIterator = NULL;
51 	initialUpdateStamp = 0;
52 	valid = false;
53 
54 	return true;
55 }
56 
57 OSSharedPtr<OSCollectionIterator>
58 OSCollectionIterator::withCollection(const OSCollection *inColl)
59 {
60 	OSSharedPtr<OSCollectionIterator> me = OSMakeShared<OSCollectionIterator>();
61 
62 	if (me && !me->initWithCollection(inColl)) {
63 		return nullptr;
64 	}
65 
66 	return me;
67 }
68 
69 void
70 OSCollectionIterator::free()
71 {
72 	if (collIterator) {
73 		kfree(collIterator, collection->iteratorSize());
74 		OSCONTAINER_ACCUMSIZE(-((size_t) collection->iteratorSize()));
75 		collIterator = NULL;
76 	}
77 
78 	collection.reset();
79 
80 	super::free();
81 }
82 
83 void
84 OSCollectionIterator::reset()
85 {
86 	valid = false;
87 
88 	if (!collIterator) {
89 		collIterator = (void *)kalloc_container(collection->iteratorSize());
90 		OSCONTAINER_ACCUMSIZE(collection->iteratorSize());
91 		if (!collIterator) {
92 			return;
93 		}
94 	}
95 
96 	if (!collection->initIterator(collIterator)) {
97 		return;
98 	}
99 
100 	initialUpdateStamp = collection->updateStamp;
101 	valid = true;
102 }
103 
104 bool
105 OSCollectionIterator::isValid()
106 {
107 	if (!collIterator) {
108 		collIterator = (void *)kalloc_container(collection->iteratorSize());
109 		OSCONTAINER_ACCUMSIZE(collection->iteratorSize());
110 		if (!collection->initIterator(collIterator)) {
111 			return false;
112 		}
113 		initialUpdateStamp = collection->updateStamp;
114 		valid = true;
115 	} else if (!valid || collection->updateStamp != initialUpdateStamp) {
116 		return false;
117 	}
118 
119 	return true;
120 }
121 
122 OSObject *
123 OSCollectionIterator::getNextObject()
124 {
125 	OSObject *retObj;
126 	bool retVal;
127 
128 	if (!isValid()) {
129 		return NULL;
130 	}
131 
132 	retVal = collection->getNextObjectForIterator(collIterator, &retObj);
133 	return (retVal)? retObj : NULL;
134 }
135