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