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