xref: /xnu-8796.121.2/iokit/Kernel/IORangeAllocator.cpp (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions /*
29*c54f35caSApple OSS Distributions  * Copyright (c) 1999 Apple Computer, Inc.
30*c54f35caSApple OSS Distributions  *
31*c54f35caSApple OSS Distributions  *
32*c54f35caSApple OSS Distributions  * HISTORY
33*c54f35caSApple OSS Distributions  *
34*c54f35caSApple OSS Distributions  * sdouglas 05 Nov 99 - created.
35*c54f35caSApple OSS Distributions  */
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions #include <libkern/c++/OSArray.h>
38*c54f35caSApple OSS Distributions #include <libkern/c++/OSNumber.h>
39*c54f35caSApple OSS Distributions #include <IOKit/IORangeAllocator.h>
40*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h>
41*c54f35caSApple OSS Distributions #include <IOKit/IOLocks.h>
42*c54f35caSApple OSS Distributions #include <IOKit/assert.h>
43*c54f35caSApple OSS Distributions 
44*c54f35caSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions #undef super
47*c54f35caSApple OSS Distributions #define super OSObject
48*c54f35caSApple OSS Distributions 
49*c54f35caSApple OSS Distributions OSDefineMetaClassAndStructors( IORangeAllocator, OSObject )
50*c54f35caSApple OSS Distributions 
51*c54f35caSApple OSS Distributions struct IORangeAllocatorElement {
52*c54f35caSApple OSS Distributions 	// closed range
53*c54f35caSApple OSS Distributions 	IORangeScalar       start;
54*c54f35caSApple OSS Distributions 	IORangeScalar       end;
55*c54f35caSApple OSS Distributions };
56*c54f35caSApple OSS Distributions 
57*c54f35caSApple OSS Distributions LCK_GRP_DECLARE(range_allocator_grp, "range_allocator_grp");
58*c54f35caSApple OSS Distributions LCK_MTX_DECLARE(gIORangeAllocatorLock, &range_allocator_grp);
59*c54f35caSApple OSS Distributions 
60*c54f35caSApple OSS Distributions #define LOCK()          \
61*c54f35caSApple OSS Distributions 	if( options & kLocking)	lck_mtx_lock( &gIORangeAllocatorLock )
62*c54f35caSApple OSS Distributions #define UNLOCK()        \
63*c54f35caSApple OSS Distributions 	if( options & kLocking)	lck_mtx_unlock( &gIORangeAllocatorLock )
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66*c54f35caSApple OSS Distributions 
67*c54f35caSApple OSS Distributions bool
init(IORangeScalar endOfRange,IORangeScalar _defaultAlignment,UInt32 _capacity,IOOptionBits _options)68*c54f35caSApple OSS Distributions IORangeAllocator::init( IORangeScalar endOfRange,
69*c54f35caSApple OSS Distributions     IORangeScalar _defaultAlignment,
70*c54f35caSApple OSS Distributions     UInt32 _capacity,
71*c54f35caSApple OSS Distributions     IOOptionBits _options )
72*c54f35caSApple OSS Distributions {
73*c54f35caSApple OSS Distributions 	if (!super::init()) {
74*c54f35caSApple OSS Distributions 		return false;
75*c54f35caSApple OSS Distributions 	}
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions 	if (!_capacity) {
78*c54f35caSApple OSS Distributions 		_capacity = 1;
79*c54f35caSApple OSS Distributions 	}
80*c54f35caSApple OSS Distributions 	if (!_defaultAlignment) {
81*c54f35caSApple OSS Distributions 		_defaultAlignment = 1;
82*c54f35caSApple OSS Distributions 	}
83*c54f35caSApple OSS Distributions 	capacity            = 0;
84*c54f35caSApple OSS Distributions 	capacityIncrement   = _capacity;
85*c54f35caSApple OSS Distributions 	numElements         = 0;
86*c54f35caSApple OSS Distributions 	elements            = NULL;
87*c54f35caSApple OSS Distributions 	defaultAlignmentMask = _defaultAlignment - 1;
88*c54f35caSApple OSS Distributions 	options             = _options;
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions 	if (endOfRange) {
91*c54f35caSApple OSS Distributions 		deallocate( 0, endOfRange + 1 );
92*c54f35caSApple OSS Distributions 	}
93*c54f35caSApple OSS Distributions 
94*c54f35caSApple OSS Distributions 	return true;
95*c54f35caSApple OSS Distributions }
96*c54f35caSApple OSS Distributions 
97*c54f35caSApple OSS Distributions IORangeAllocator *
withRange(IORangeScalar endOfRange,IORangeScalar defaultAlignment,UInt32 capacity,IOOptionBits options)98*c54f35caSApple OSS Distributions IORangeAllocator::withRange(
99*c54f35caSApple OSS Distributions 	IORangeScalar endOfRange,
100*c54f35caSApple OSS Distributions 	IORangeScalar defaultAlignment,
101*c54f35caSApple OSS Distributions 	UInt32 capacity,
102*c54f35caSApple OSS Distributions 	IOOptionBits options )
103*c54f35caSApple OSS Distributions {
104*c54f35caSApple OSS Distributions 	IORangeAllocator * thingy;
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions 	thingy = new IORangeAllocator;
107*c54f35caSApple OSS Distributions 	if (thingy && !thingy->init( endOfRange, defaultAlignment,
108*c54f35caSApple OSS Distributions 	    capacity, options )) {
109*c54f35caSApple OSS Distributions 		thingy->release();
110*c54f35caSApple OSS Distributions 		thingy = NULL;
111*c54f35caSApple OSS Distributions 	}
112*c54f35caSApple OSS Distributions 
113*c54f35caSApple OSS Distributions 	return thingy;
114*c54f35caSApple OSS Distributions }
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions void
free()117*c54f35caSApple OSS Distributions IORangeAllocator::free()
118*c54f35caSApple OSS Distributions {
119*c54f35caSApple OSS Distributions 	if (elements) {
120*c54f35caSApple OSS Distributions 		IODeleteData( elements, IORangeAllocatorElement, capacity );
121*c54f35caSApple OSS Distributions 	}
122*c54f35caSApple OSS Distributions 
123*c54f35caSApple OSS Distributions 	super::free();
124*c54f35caSApple OSS Distributions }
125*c54f35caSApple OSS Distributions 
126*c54f35caSApple OSS Distributions UInt32
getFragmentCount(void)127*c54f35caSApple OSS Distributions IORangeAllocator::getFragmentCount( void )
128*c54f35caSApple OSS Distributions {
129*c54f35caSApple OSS Distributions 	return numElements;
130*c54f35caSApple OSS Distributions }
131*c54f35caSApple OSS Distributions 
132*c54f35caSApple OSS Distributions UInt32
getFragmentCapacity(void)133*c54f35caSApple OSS Distributions IORangeAllocator::getFragmentCapacity( void )
134*c54f35caSApple OSS Distributions {
135*c54f35caSApple OSS Distributions 	return capacity;
136*c54f35caSApple OSS Distributions }
137*c54f35caSApple OSS Distributions 
138*c54f35caSApple OSS Distributions void
setFragmentCapacityIncrement(UInt32 count)139*c54f35caSApple OSS Distributions IORangeAllocator::setFragmentCapacityIncrement( UInt32 count )
140*c54f35caSApple OSS Distributions {
141*c54f35caSApple OSS Distributions 	capacityIncrement = count;
142*c54f35caSApple OSS Distributions }
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions // allocate element at index
146*c54f35caSApple OSS Distributions bool
allocElement(UInt32 index)147*c54f35caSApple OSS Distributions IORangeAllocator::allocElement( UInt32 index )
148*c54f35caSApple OSS Distributions {
149*c54f35caSApple OSS Distributions 	UInt32                      newCapacity;
150*c54f35caSApple OSS Distributions 	IORangeAllocatorElement *   newElements;
151*c54f35caSApple OSS Distributions 
152*c54f35caSApple OSS Distributions 	if (((numElements == capacity) && capacityIncrement)
153*c54f35caSApple OSS Distributions 	    || (!elements)) {
154*c54f35caSApple OSS Distributions 		if (os_add_overflow(capacity, capacityIncrement, &newCapacity)) {
155*c54f35caSApple OSS Distributions 			return false;
156*c54f35caSApple OSS Distributions 		}
157*c54f35caSApple OSS Distributions 		newElements = IONewData( IORangeAllocatorElement, newCapacity );
158*c54f35caSApple OSS Distributions 		if (!newElements) {
159*c54f35caSApple OSS Distributions 			return false;
160*c54f35caSApple OSS Distributions 		}
161*c54f35caSApple OSS Distributions 
162*c54f35caSApple OSS Distributions 		if (elements) {
163*c54f35caSApple OSS Distributions 			bcopy( elements,
164*c54f35caSApple OSS Distributions 			    newElements,
165*c54f35caSApple OSS Distributions 			    index * sizeof(IORangeAllocatorElement));
166*c54f35caSApple OSS Distributions 			bcopy( elements + index,
167*c54f35caSApple OSS Distributions 			    newElements + index + 1,
168*c54f35caSApple OSS Distributions 			    (numElements - index) * sizeof(IORangeAllocatorElement));
169*c54f35caSApple OSS Distributions 
170*c54f35caSApple OSS Distributions 			IODeleteData( elements, IORangeAllocatorElement, capacity );
171*c54f35caSApple OSS Distributions 		}
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions 		elements = newElements;
174*c54f35caSApple OSS Distributions 		capacity = newCapacity;
175*c54f35caSApple OSS Distributions 	} else {
176*c54f35caSApple OSS Distributions 		bcopy( elements + index,
177*c54f35caSApple OSS Distributions 		    elements + index + 1,
178*c54f35caSApple OSS Distributions 		    (numElements - index) * sizeof(IORangeAllocatorElement));
179*c54f35caSApple OSS Distributions 	}
180*c54f35caSApple OSS Distributions 	numElements++;
181*c54f35caSApple OSS Distributions 
182*c54f35caSApple OSS Distributions 	return true;
183*c54f35caSApple OSS Distributions }
184*c54f35caSApple OSS Distributions 
185*c54f35caSApple OSS Distributions // destroy element at index
186*c54f35caSApple OSS Distributions void
deallocElement(UInt32 index)187*c54f35caSApple OSS Distributions IORangeAllocator::deallocElement( UInt32 index )
188*c54f35caSApple OSS Distributions {
189*c54f35caSApple OSS Distributions 	numElements--;
190*c54f35caSApple OSS Distributions 	bcopy( elements + index + 1,
191*c54f35caSApple OSS Distributions 	    elements + index,
192*c54f35caSApple OSS Distributions 	    (numElements - index) * sizeof(IORangeAllocatorElement));
193*c54f35caSApple OSS Distributions }
194*c54f35caSApple OSS Distributions 
195*c54f35caSApple OSS Distributions bool
allocate(IORangeScalar size,IORangeScalar * result,IORangeScalar alignment)196*c54f35caSApple OSS Distributions IORangeAllocator::allocate( IORangeScalar size,
197*c54f35caSApple OSS Distributions     IORangeScalar * result,
198*c54f35caSApple OSS Distributions     IORangeScalar alignment )
199*c54f35caSApple OSS Distributions {
200*c54f35caSApple OSS Distributions 	IORangeScalar       data, dataEnd;
201*c54f35caSApple OSS Distributions 	IORangeScalar       thisStart, thisEnd;
202*c54f35caSApple OSS Distributions 	UInt32              index;
203*c54f35caSApple OSS Distributions 	bool                ok = false;
204*c54f35caSApple OSS Distributions 
205*c54f35caSApple OSS Distributions 	if (!size || !result) {
206*c54f35caSApple OSS Distributions 		return false;
207*c54f35caSApple OSS Distributions 	}
208*c54f35caSApple OSS Distributions 
209*c54f35caSApple OSS Distributions 	if (0 == alignment) {
210*c54f35caSApple OSS Distributions 		alignment = defaultAlignmentMask;
211*c54f35caSApple OSS Distributions 	} else {
212*c54f35caSApple OSS Distributions 		alignment--;
213*c54f35caSApple OSS Distributions 	}
214*c54f35caSApple OSS Distributions 
215*c54f35caSApple OSS Distributions 	size = (size + defaultAlignmentMask) & ~defaultAlignmentMask;
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions 	LOCK();
218*c54f35caSApple OSS Distributions 
219*c54f35caSApple OSS Distributions 	for (index = 0; index < numElements; index++) {
220*c54f35caSApple OSS Distributions 		thisStart = elements[index].start;
221*c54f35caSApple OSS Distributions 		thisEnd = elements[index].end;
222*c54f35caSApple OSS Distributions 		data = (thisStart + alignment) & ~alignment;
223*c54f35caSApple OSS Distributions 		dataEnd = (data + size - 1);
224*c54f35caSApple OSS Distributions 
225*c54f35caSApple OSS Distributions 		ok = (dataEnd <= thisEnd);
226*c54f35caSApple OSS Distributions 		if (ok) {
227*c54f35caSApple OSS Distributions 			if (data != thisStart) {
228*c54f35caSApple OSS Distributions 				if (dataEnd != thisEnd) {
229*c54f35caSApple OSS Distributions 					if (allocElement( index + 1 )) {
230*c54f35caSApple OSS Distributions 						elements[index++].end = data - 1;
231*c54f35caSApple OSS Distributions 						elements[index].start = dataEnd + 1;
232*c54f35caSApple OSS Distributions 						elements[index].end = thisEnd;
233*c54f35caSApple OSS Distributions 					} else {
234*c54f35caSApple OSS Distributions 						ok = false;
235*c54f35caSApple OSS Distributions 					}
236*c54f35caSApple OSS Distributions 				} else {
237*c54f35caSApple OSS Distributions 					elements[index].end = data - 1;
238*c54f35caSApple OSS Distributions 				}
239*c54f35caSApple OSS Distributions 			} else {
240*c54f35caSApple OSS Distributions 				if (dataEnd != thisEnd) {
241*c54f35caSApple OSS Distributions 					elements[index].start = dataEnd + 1;
242*c54f35caSApple OSS Distributions 				} else {
243*c54f35caSApple OSS Distributions 					deallocElement( index );
244*c54f35caSApple OSS Distributions 				}
245*c54f35caSApple OSS Distributions 			}
246*c54f35caSApple OSS Distributions 			if (ok) {
247*c54f35caSApple OSS Distributions 				*result = data;
248*c54f35caSApple OSS Distributions 			}
249*c54f35caSApple OSS Distributions 			break;
250*c54f35caSApple OSS Distributions 		}
251*c54f35caSApple OSS Distributions 	}
252*c54f35caSApple OSS Distributions 
253*c54f35caSApple OSS Distributions 	UNLOCK();
254*c54f35caSApple OSS Distributions 
255*c54f35caSApple OSS Distributions 	return ok;
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions 
258*c54f35caSApple OSS Distributions bool
allocateRange(IORangeScalar data,IORangeScalar size)259*c54f35caSApple OSS Distributions IORangeAllocator::allocateRange( IORangeScalar data,
260*c54f35caSApple OSS Distributions     IORangeScalar size )
261*c54f35caSApple OSS Distributions {
262*c54f35caSApple OSS Distributions 	IORangeScalar       thisStart, thisEnd;
263*c54f35caSApple OSS Distributions 	IORangeScalar       dataEnd;
264*c54f35caSApple OSS Distributions 	UInt32              index;
265*c54f35caSApple OSS Distributions 	bool                found = false;
266*c54f35caSApple OSS Distributions 
267*c54f35caSApple OSS Distributions 	if (!size) {
268*c54f35caSApple OSS Distributions 		return 0;
269*c54f35caSApple OSS Distributions 	}
270*c54f35caSApple OSS Distributions 
271*c54f35caSApple OSS Distributions 	size = (size + defaultAlignmentMask) & ~defaultAlignmentMask;
272*c54f35caSApple OSS Distributions 	dataEnd = data + size - 1;
273*c54f35caSApple OSS Distributions 
274*c54f35caSApple OSS Distributions 	LOCK();
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions 	for (index = 0;
277*c54f35caSApple OSS Distributions 	    (!found) && (index < numElements);
278*c54f35caSApple OSS Distributions 	    index++) {
279*c54f35caSApple OSS Distributions 		thisStart = elements[index].start;
280*c54f35caSApple OSS Distributions 		thisEnd = elements[index].end;
281*c54f35caSApple OSS Distributions 
282*c54f35caSApple OSS Distributions 		if (thisStart > data) {
283*c54f35caSApple OSS Distributions 			break;
284*c54f35caSApple OSS Distributions 		}
285*c54f35caSApple OSS Distributions 		found = (dataEnd <= thisEnd);
286*c54f35caSApple OSS Distributions 
287*c54f35caSApple OSS Distributions 		if (found) {
288*c54f35caSApple OSS Distributions 			if (data != thisStart) {
289*c54f35caSApple OSS Distributions 				if (dataEnd != thisEnd) {
290*c54f35caSApple OSS Distributions 					found = allocElement( index + 1 );
291*c54f35caSApple OSS Distributions 					if (found) {
292*c54f35caSApple OSS Distributions 						elements[index++].end = data - 1;
293*c54f35caSApple OSS Distributions 						elements[index].start = dataEnd + 1;
294*c54f35caSApple OSS Distributions 						elements[index].end = thisEnd;
295*c54f35caSApple OSS Distributions 					}
296*c54f35caSApple OSS Distributions 				} else {
297*c54f35caSApple OSS Distributions 					elements[index].end = data - 1;
298*c54f35caSApple OSS Distributions 				}
299*c54f35caSApple OSS Distributions 			} else if (dataEnd != thisEnd) {
300*c54f35caSApple OSS Distributions 				elements[index].start = dataEnd + 1;
301*c54f35caSApple OSS Distributions 			} else {
302*c54f35caSApple OSS Distributions 				deallocElement( index );
303*c54f35caSApple OSS Distributions 			}
304*c54f35caSApple OSS Distributions 		}
305*c54f35caSApple OSS Distributions 	}
306*c54f35caSApple OSS Distributions 
307*c54f35caSApple OSS Distributions 	UNLOCK();
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions 	return found;
310*c54f35caSApple OSS Distributions }
311*c54f35caSApple OSS Distributions 
312*c54f35caSApple OSS Distributions void
deallocate(IORangeScalar data,IORangeScalar size)313*c54f35caSApple OSS Distributions IORangeAllocator::deallocate( IORangeScalar data,
314*c54f35caSApple OSS Distributions     IORangeScalar size )
315*c54f35caSApple OSS Distributions {
316*c54f35caSApple OSS Distributions 	IORangeScalar       dataEnd;
317*c54f35caSApple OSS Distributions 	UInt32              index;
318*c54f35caSApple OSS Distributions 	bool                headContig = false;
319*c54f35caSApple OSS Distributions 	bool                tailContig = false;
320*c54f35caSApple OSS Distributions 
321*c54f35caSApple OSS Distributions 	size = (size + defaultAlignmentMask) & ~defaultAlignmentMask;
322*c54f35caSApple OSS Distributions 	dataEnd = data + size - 1;
323*c54f35caSApple OSS Distributions 
324*c54f35caSApple OSS Distributions 	LOCK();
325*c54f35caSApple OSS Distributions 
326*c54f35caSApple OSS Distributions 	for (index = 0; index < numElements; index++) {
327*c54f35caSApple OSS Distributions 		if (elements[index].start < data) {
328*c54f35caSApple OSS Distributions 			headContig = (data <= (elements[index].end + 1));
329*c54f35caSApple OSS Distributions 			continue;
330*c54f35caSApple OSS Distributions 		}
331*c54f35caSApple OSS Distributions 		tailContig = ((data + size) >= elements[index].start);
332*c54f35caSApple OSS Distributions 		break;
333*c54f35caSApple OSS Distributions 	}
334*c54f35caSApple OSS Distributions 
335*c54f35caSApple OSS Distributions 	if (headContig) {
336*c54f35caSApple OSS Distributions 		if (tailContig) {
337*c54f35caSApple OSS Distributions 			elements[index - 1].end = elements[index].end;
338*c54f35caSApple OSS Distributions 			deallocElement( index );
339*c54f35caSApple OSS Distributions 		} else /*safe*/ if (dataEnd > elements[index - 1].end) {
340*c54f35caSApple OSS Distributions 			elements[index - 1].end = dataEnd;
341*c54f35caSApple OSS Distributions 		}
342*c54f35caSApple OSS Distributions 	} else if (tailContig) {
343*c54f35caSApple OSS Distributions 		if (data < elements[index].start) { /*safe*/
344*c54f35caSApple OSS Distributions 			elements[index].start = data;
345*c54f35caSApple OSS Distributions 		}
346*c54f35caSApple OSS Distributions 	} else if (allocElement( index)) {
347*c54f35caSApple OSS Distributions 		elements[index].start = data;
348*c54f35caSApple OSS Distributions 		elements[index].end = dataEnd;
349*c54f35caSApple OSS Distributions 	}
350*c54f35caSApple OSS Distributions 
351*c54f35caSApple OSS Distributions 	UNLOCK();
352*c54f35caSApple OSS Distributions }
353*c54f35caSApple OSS Distributions 
354*c54f35caSApple OSS Distributions bool
serialize(OSSerialize * s) const355*c54f35caSApple OSS Distributions IORangeAllocator::serialize(OSSerialize *s) const
356*c54f35caSApple OSS Distributions {
357*c54f35caSApple OSS Distributions 	OSArray *   array = OSArray::withCapacity( numElements * 2 );
358*c54f35caSApple OSS Distributions 	OSNumber *  num;
359*c54f35caSApple OSS Distributions 	UInt32      index;
360*c54f35caSApple OSS Distributions 	bool        ret;
361*c54f35caSApple OSS Distributions 
362*c54f35caSApple OSS Distributions 	if (!array) {
363*c54f35caSApple OSS Distributions 		return false;
364*c54f35caSApple OSS Distributions 	}
365*c54f35caSApple OSS Distributions 
366*c54f35caSApple OSS Distributions 	LOCK();
367*c54f35caSApple OSS Distributions 
368*c54f35caSApple OSS Distributions 	for (index = 0; index < numElements; index++) {
369*c54f35caSApple OSS Distributions 		if ((num = OSNumber::withNumber( elements[index].start,
370*c54f35caSApple OSS Distributions 		    8 * sizeof(IORangeScalar)))) {
371*c54f35caSApple OSS Distributions 			array->setObject(num);
372*c54f35caSApple OSS Distributions 			num->release();
373*c54f35caSApple OSS Distributions 		}
374*c54f35caSApple OSS Distributions 		if ((num = OSNumber::withNumber( elements[index].end,
375*c54f35caSApple OSS Distributions 		    8 * sizeof(IORangeScalar)))) {
376*c54f35caSApple OSS Distributions 			array->setObject(num);
377*c54f35caSApple OSS Distributions 			num->release();
378*c54f35caSApple OSS Distributions 		}
379*c54f35caSApple OSS Distributions 	}
380*c54f35caSApple OSS Distributions 
381*c54f35caSApple OSS Distributions 	UNLOCK();
382*c54f35caSApple OSS Distributions 
383*c54f35caSApple OSS Distributions 	ret = array->serialize(s);
384*c54f35caSApple OSS Distributions 	array->release();
385*c54f35caSApple OSS Distributions 
386*c54f35caSApple OSS Distributions 	return ret;
387*c54f35caSApple OSS Distributions }
388*c54f35caSApple OSS Distributions 
389*c54f35caSApple OSS Distributions IORangeScalar
getFreeCount(void)390*c54f35caSApple OSS Distributions IORangeAllocator::getFreeCount( void )
391*c54f35caSApple OSS Distributions {
392*c54f35caSApple OSS Distributions 	UInt32              index;
393*c54f35caSApple OSS Distributions 	IORangeScalar       sum = 0;
394*c54f35caSApple OSS Distributions 
395*c54f35caSApple OSS Distributions 	for (index = 0; index < numElements; index++) {
396*c54f35caSApple OSS Distributions 		sum += elements[index].end - elements[index].start + 1;
397*c54f35caSApple OSS Distributions 	}
398*c54f35caSApple OSS Distributions 
399*c54f35caSApple OSS Distributions 	return sum;
400*c54f35caSApple OSS Distributions }
401