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