1 /*
2  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <IOKit/pwr_mgt/IOPM.h>
29 #include <IOKit/pwr_mgt/IOPMPowerSourceList.h>
30 #include <IOKit/pwr_mgt/IOPMPowerSource.h>
31 
32 #define super OSObject
OSDefineMetaClassAndStructors(IOPMPowerSourceList,OSObject)33 OSDefineMetaClassAndStructors(IOPMPowerSourceList, OSObject)
34 
35 //******************************************************************************
36 // init
37 //
38 //******************************************************************************
39 void
40 IOPMPowerSourceList::initialize( void )
41 {
42 	firstItem = NULL;
43 	length = 0;
44 }
45 
46 //******************************************************************************
47 // addToList
48 //
49 //******************************************************************************
50 
51 IOReturn
addToList(IOPMPowerSource * newPowerSource)52 IOPMPowerSourceList::addToList(IOPMPowerSource *newPowerSource)
53 {
54 	IOPMPowerSource * nextPowerSource;
55 
56 	// Is new object already in the list?
57 	nextPowerSource = firstItem;
58 	while (nextPowerSource != NULL) {
59 		if (nextPowerSource == newPowerSource) {
60 			// yes, just return
61 			return IOPMNoErr;
62 		}
63 		nextPowerSource = nextInList(nextPowerSource);
64 	}
65 
66 	// add it to list
67 	newPowerSource->nextInList = firstItem;
68 	firstItem = newPowerSource;
69 	length++;
70 	return IOPMNoErr;
71 }
72 
73 
74 //******************************************************************************
75 // firstInList
76 //
77 //******************************************************************************
78 
79 IOPMPowerSource *
firstInList(void)80 IOPMPowerSourceList::firstInList( void )
81 {
82 	return firstItem;
83 }
84 
85 //******************************************************************************
86 // nextInList
87 //
88 //******************************************************************************
89 
90 IOPMPowerSource *
nextInList(IOPMPowerSource * currentItem)91 IOPMPowerSourceList::nextInList(IOPMPowerSource *currentItem)
92 {
93 	if (currentItem != NULL) {
94 		return currentItem->nextInList;
95 	}
96 	return NULL;
97 }
98 
99 //******************************************************************************
100 // numberOfItems
101 //
102 //******************************************************************************
103 
104 unsigned long
numberOfItems(void)105 IOPMPowerSourceList::numberOfItems( void )
106 {
107 	return length;
108 }
109 
110 //******************************************************************************
111 // removeFromList
112 //
113 // Find the item in the list, unlink it, and free it.
114 //******************************************************************************
115 
116 IOReturn
removeFromList(IOPMPowerSource * theItem)117 IOPMPowerSourceList::removeFromList( IOPMPowerSource * theItem )
118 {
119 	IOPMPowerSource * item = firstItem;
120 	IOPMPowerSource * temp;
121 
122 	if (NULL == item) {
123 		goto exit;
124 	}
125 
126 	if (item == theItem) {
127 		firstItem = item->nextInList;
128 		length--;
129 		item->release();
130 		return IOPMNoErr;
131 	}
132 	while (item->nextInList != NULL) {
133 		if (item->nextInList == theItem) {
134 			temp = item->nextInList;
135 			item->nextInList = temp->nextInList;
136 			length--;
137 			temp->release();
138 			return IOPMNoErr;
139 		}
140 		item = item->nextInList;
141 	}
142 
143 exit:
144 	return IOPMNoErr;
145 }
146 
147 
148 //******************************************************************************
149 // free
150 //
151 // Free all items in the list, and then free the list itself
152 //******************************************************************************
153 
154 void
free(void)155 IOPMPowerSourceList::free(void )
156 {
157 	IOPMPowerSource * 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