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