| 1 | /* |
| 2 | * Copyright (c) 1998-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 | #include <IOKit/pwr_mgt/IOPM.h> |
| 23 | #include <IOKit/pwr_mgt/IOPMinformeeList.h> |
| 24 | #include <IOKit/pwr_mgt/IOPMinformee.h> |
| 25 | |
| 26 | #define super OSObject |
| 27 | OSDefineMetaClassAndStructors(IOPMinformeeList,OSObject) |
| 28 | |
| 29 | //********************************************************************************* |
| 30 | // init |
| 31 | // |
| 32 | //********************************************************************************* |
| 33 | void IOPMinformeeList::initialize ( void ) |
| 34 | { |
| 35 | firstItem = NULL; |
| 36 | length = 0; |
| 37 | } |
| 38 | |
| 39 | //********************************************************************************* |
| 40 | // addToList |
| 41 | // |
| 42 | //********************************************************************************* |
| 43 | |
| 44 | IOReturn IOPMinformeeList::addToList ( IOPMinformee * newInformee ) |
| 45 | { |
| 46 | IOPMinformee * nextInformee; |
| 47 | nextInformee = firstItem; // Is new object already in the list? |
| 48 | while ( nextInformee != NULL ) { |
| 49 | if ( nextInformee->whatObject == newInformee->whatObject ) { |
| 50 | return IOPMNoErr; // yes, just return |
| 51 | } |
| 52 | nextInformee = nextInList(nextInformee); |
| 53 | } |
| 54 | newInformee->nextInList = firstItem; // add it to list |
| 55 | firstItem = newInformee; |
| 56 | length += 1; |
| 57 | return IOPMNoErr; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | //********************************************************************************* |
| 62 | // firstInList |
| 63 | // |
| 64 | //********************************************************************************* |
| 65 | |
| 66 | IOPMinformee * IOPMinformeeList::firstInList ( void ) |
| 67 | { |
| 68 | return firstItem; |
| 69 | } |
| 70 | |
| 71 | //********************************************************************************* |
| 72 | // nextInList |
| 73 | // |
| 74 | //********************************************************************************* |
| 75 | |
| 76 | IOPMinformee * IOPMinformeeList::nextInList ( IOPMinformee * currentItem ) |
| 77 | { |
| 78 | if ( currentItem != NULL ) { |
| 79 | return (currentItem->nextInList); |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | //********************************************************************************* |
| 85 | // numberOfItems |
| 86 | // |
| 87 | //********************************************************************************* |
| 88 | |
| 89 | unsigned long IOPMinformeeList::numberOfItems ( void ) |
| 90 | { |
| 91 | return length; |
| 92 | } |
| 93 | |
| 94 | //********************************************************************************* |
| 95 | // findItem |
| 96 | // |
| 97 | // Look through the list for the one which points to the object identified |
| 98 | // by the parameter. Return a pointer to the list item or NULL. |
| 99 | //********************************************************************************* |
| 100 | |
| 101 | IOPMinformee * IOPMinformeeList::findItem ( IOService * driverOrChild ) |
| 102 | { |
| 103 | IOPMinformee * nextObject; |
| 104 | |
| 105 | nextObject = firstInList(); |
| 106 | while ( nextObject != NULL ) { |
| 107 | if ( nextObject->whatObject == driverOrChild ) { |
| 108 | return nextObject; |
| 109 | } |
| 110 | nextObject = nextInList(nextObject); |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | //********************************************************************************* |
| 117 | // removeFromList |
| 118 | // |
| 119 | // Find the item in the list, unlink it, and free it. |
| 120 | //********************************************************************************* |
| 121 | |
| 122 | IOReturn IOPMinformeeList::removeFromList ( IOService * theItem ) |
| 123 | { |
| 124 | IOPMinformee * item = firstItem; |
| 125 | IOPMinformee * temp; |
| 126 | |
| 127 | if ( item != NULL ) { |
| 128 | if ( item->whatObject == theItem ) { |
| 129 | firstItem = item->nextInList; |
| 130 | length--; |
| 131 | item->release(); |
| 132 | return IOPMNoErr; |
| 133 | } |
| 134 | while ( item->nextInList != NULL ) { |
| 135 | if ( item->nextInList->whatObject == theItem ) { |
| 136 | temp = item->nextInList; |
| 137 | item->nextInList = temp->nextInList; |
| 138 | length--; |
| 139 | temp->release(); |
| 140 | return IOPMNoErr; |
| 141 | } |
| 142 | item = item->nextInList; |
| 143 | } |
| 144 | } |
| 145 | return IOPMNoErr; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | //********************************************************************************* |
| 150 | // free |
| 151 | // |
| 152 | // Free all items in the list, and then free the list itself |
| 153 | //********************************************************************************* |
| 154 | |
| 155 | void IOPMinformeeList::free (void ) |
| 156 | { |
| 157 | IOPMinformee * next = firstItem; |
| 158 | |
| 159 | while ( next != NULL ) { |
| 160 | firstItem = next->nextInList; |
| 161 | length--; |
| 162 | next->release(); |
| 163 | next = firstItem; |
| 164 | } |
| 165 | super::free(); |
| 166 | } |
| 167 | |