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