]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSCollectionIterator.cpp
b0e01b757e3f20aec515ab0814b2ff3a60cc2c5a
[apple/xnu.git] / libkern / c++ / OSCollectionIterator.cpp
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* IOArray.h created by rsulack on Thu 11-Sep-1997 */
31
32 #include <libkern/c++/OSCollectionIterator.h>
33 #include <libkern/c++/OSCollection.h>
34 #include <libkern/c++/OSArray.h>
35 #include <libkern/c++/OSLib.h>
36
37 #define super OSIterator
38
39 OSDefineMetaClassAndStructors(OSCollectionIterator, OSIterator)
40
41 #if OSALLOCDEBUG
42 extern "C" {
43 extern int debug_container_malloc_size;
44 };
45 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
46 #else
47 #define ACCUMSIZE(s)
48 #endif
49
50 bool OSCollectionIterator::initWithCollection(const OSCollection *inColl)
51 {
52 if ( !super::init() || !inColl)
53 return false;
54
55 inColl->retain();
56 collection = inColl;
57 collIterator = 0;
58 initialUpdateStamp = 0;
59 valid = false;
60
61 return this;
62 }
63
64 OSCollectionIterator *
65 OSCollectionIterator::withCollection(const OSCollection *inColl)
66 {
67
68 OSCollectionIterator *me = new OSCollectionIterator;
69
70 if (me && !me->initWithCollection(inColl)) {
71 me->release();
72 return 0;
73 }
74
75 return me;
76 }
77
78 void OSCollectionIterator::free()
79 {
80 if (collIterator) {
81 kfree((vm_offset_t)collIterator, collection->iteratorSize());
82 ACCUMSIZE(-(collection->iteratorSize()));
83 collIterator = 0;
84 }
85
86 if (collection) {
87 collection->release();
88 collection = 0;
89 }
90
91 super::free();
92 }
93
94 void OSCollectionIterator::reset()
95 {
96 valid = false;
97
98 if (!collIterator) {
99 collIterator = (void *)kalloc(collection->iteratorSize());
100 ACCUMSIZE(collection->iteratorSize());
101 if (!collIterator)
102 return;
103 }
104
105 if (!collection->initIterator(collIterator))
106 return;
107
108 initialUpdateStamp = collection->updateStamp;
109 valid = true;
110 }
111
112 bool OSCollectionIterator::isValid()
113 {
114 if (!collIterator) {
115 collIterator = (void *)kalloc(collection->iteratorSize());
116 ACCUMSIZE(collection->iteratorSize());
117 if (!collection->initIterator(collIterator))
118 return false;
119 initialUpdateStamp = collection->updateStamp;
120 valid = true;
121 }
122 else if (!valid || collection->updateStamp != initialUpdateStamp)
123 return false;
124
125 return true;
126 }
127
128 OSObject *OSCollectionIterator::getNextObject()
129 {
130 OSObject *retObj;
131 bool retVal;
132
133 if (!isValid())
134 return 0;
135
136 retVal = collection->getNextObjectForIterator(collIterator, &retObj);
137 return (retVal)? retObj : 0;
138 }
139