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