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