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