xref: /xnu-8020.121.3/iokit/IOKit/IORangeAllocator.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 1998-2019 Apple Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions /*
29*fdd8201dSApple OSS Distributions  * Copyright (c) 1999 Apple Computer, Inc.
30*fdd8201dSApple OSS Distributions  *
31*fdd8201dSApple OSS Distributions  *
32*fdd8201dSApple OSS Distributions  * HISTORY
33*fdd8201dSApple OSS Distributions  *
34*fdd8201dSApple OSS Distributions  * sdouglas 05 Nov 99 - created.
35*fdd8201dSApple OSS Distributions  */
36*fdd8201dSApple OSS Distributions 
37*fdd8201dSApple OSS Distributions #ifndef _IOKIT_IORANGEALLOCATOR_H
38*fdd8201dSApple OSS Distributions #define _IOKIT_IORANGEALLOCATOR_H
39*fdd8201dSApple OSS Distributions 
40*fdd8201dSApple OSS Distributions #include <libkern/c++/OSPtr.h>
41*fdd8201dSApple OSS Distributions #include <libkern/c++/OSObject.h>
42*fdd8201dSApple OSS Distributions #include <IOKit/IOTypes.h>
43*fdd8201dSApple OSS Distributions 
44*fdd8201dSApple OSS Distributions typedef IOByteCount IORangeScalar;
45*fdd8201dSApple OSS Distributions 
46*fdd8201dSApple OSS Distributions /*! @class IORangeAllocator
47*fdd8201dSApple OSS Distributions  *   @abstract A utility class to manage allocations from a range.
48*fdd8201dSApple OSS Distributions  *   @discussion The IORangeAllocator class provides functions for allocating ranges, at a fixed or any offset, and freeing them back to a free list. It is useful for describing ranges of memory or address space without requiring storage in the memory - information describing the free elements is kept elsewhere. Ranges are described by a start offset and a size. IORangeAllocator is optionally protected against multithreaded access.
49*fdd8201dSApple OSS Distributions  */
50*fdd8201dSApple OSS Distributions 
51*fdd8201dSApple OSS Distributions class IORangeAllocator : public OSObject {
52*fdd8201dSApple OSS Distributions 	OSDeclareDefaultStructors(IORangeAllocator);
53*fdd8201dSApple OSS Distributions 
54*fdd8201dSApple OSS Distributions protected:
55*fdd8201dSApple OSS Distributions 	UInt32              numElements;
56*fdd8201dSApple OSS Distributions 	UInt32              capacity;
57*fdd8201dSApple OSS Distributions 	UInt32              capacityIncrement;
58*fdd8201dSApple OSS Distributions 	IORangeScalar       defaultAlignmentMask;
59*fdd8201dSApple OSS Distributions 	IOOptionBits        options;
60*fdd8201dSApple OSS Distributions 
61*fdd8201dSApple OSS Distributions 	struct IORangeAllocatorElement *    elements;
62*fdd8201dSApple OSS Distributions 
63*fdd8201dSApple OSS Distributions private:
64*fdd8201dSApple OSS Distributions 	virtual bool allocElement( UInt32 index );
65*fdd8201dSApple OSS Distributions 
66*fdd8201dSApple OSS Distributions 	virtual void deallocElement( UInt32 index );
67*fdd8201dSApple OSS Distributions 
68*fdd8201dSApple OSS Distributions public:
69*fdd8201dSApple OSS Distributions 	enum {
70*fdd8201dSApple OSS Distributions 		kLocking        = 0x00000001
71*fdd8201dSApple OSS Distributions 	};
72*fdd8201dSApple OSS Distributions 
73*fdd8201dSApple OSS Distributions /*! @function init
74*fdd8201dSApple OSS Distributions  *   @abstract Standard initializer for IORangeAllocator.
75*fdd8201dSApple OSS Distributions  *   @discussion This method initializes an IORangeAllocator and optionally sets the free list to contain one fragment, from zero to an endOfRange parameter. The capacity in terms of free fragments and locking options are set for the instance.
76*fdd8201dSApple OSS Distributions  *   @param endOfRange If the free list is to contain an initial fragment, set endOfRange to the last offset in the range, ie. size - 1, to create a free fragment for the range zero to endOfRange inclusive. If zero is passed, the free list will be initialized empty, and can be populated with calls to the deallocate method.
77*fdd8201dSApple OSS Distributions  *   @param defaultAlignment If this parameter is non-zero it specifies a required alignment for all allocations, for example pass 256 to align allocations on 256 byte boundaries. Zero or one specify unaligned allocations.
78*fdd8201dSApple OSS Distributions  *   @param capacity Sets the initial size of the free list in number of noncontiguous fragments. This value is also used for the capacityIncrement.
79*fdd8201dSApple OSS Distributions  *   @param options Pass kLocking if the instance can be used by multiple threads.
80*fdd8201dSApple OSS Distributions  *   @result Returns true if the instance is successfully initialized, false on failure. */
81*fdd8201dSApple OSS Distributions 
82*fdd8201dSApple OSS Distributions 	virtual bool init( IORangeScalar endOfRange,
83*fdd8201dSApple OSS Distributions 	    IORangeScalar defaultAlignment,
84*fdd8201dSApple OSS Distributions 	    UInt32 capacity,
85*fdd8201dSApple OSS Distributions 	    IOOptionBits options );
86*fdd8201dSApple OSS Distributions 
87*fdd8201dSApple OSS Distributions /*! @function withRange
88*fdd8201dSApple OSS Distributions  *   @abstract Standard factory method for IORangeAllocator.
89*fdd8201dSApple OSS Distributions  *   @discussion This method allocates and initializes an IORangeAllocator and optionally sets the free list to contain one fragment, from zero to an endOfRange parameter. The capacity in terms of free fragments and locking options are set for the instance.
90*fdd8201dSApple OSS Distributions  *   @param endOfRange If the free list is to contain an initial fragment, set endOfRange to the last offset in the range, ie. size - 1, to create a free fragment for the range zero to endOfRange inclusive. If zero is passed the free list will be initialized empty, and can be populated with calls to the deallocate method.
91*fdd8201dSApple OSS Distributions  *   @param defaultAlignment If this parameter is non-zero it specifies a required alignment for all allocations, for example pass 256 to align allocations on 256 byte boundaries. Zero or one specify unaligned allocations.
92*fdd8201dSApple OSS Distributions  *   @param capacity Sets the initial size of the free list in number of non-contiguous fragments. This value is also used for the capacityIncrement.
93*fdd8201dSApple OSS Distributions  *   @param options Pass kLocking if the instance can be used by multiple threads.
94*fdd8201dSApple OSS Distributions  *   @result Returns the new IORangeAllocator instance, to be released by the caller, or zero on failure. */
95*fdd8201dSApple OSS Distributions 
96*fdd8201dSApple OSS Distributions 	static OSPtr<IORangeAllocator>  withRange( IORangeScalar endOfRange,
97*fdd8201dSApple OSS Distributions 	    IORangeScalar defaultAlignment = 0,
98*fdd8201dSApple OSS Distributions 	    UInt32 capacity = 0,
99*fdd8201dSApple OSS Distributions 	    IOOptionBits options = 0 );
100*fdd8201dSApple OSS Distributions 
101*fdd8201dSApple OSS Distributions 	virtual void free() APPLE_KEXT_OVERRIDE;
102*fdd8201dSApple OSS Distributions 	virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE;
103*fdd8201dSApple OSS Distributions 
104*fdd8201dSApple OSS Distributions /*! @function getFragmentCount
105*fdd8201dSApple OSS Distributions  *   @abstract Accessor to return the number of free fragments in the range.
106*fdd8201dSApple OSS Distributions  *   @discussion This method returns a count of free fragments. Each fragment describes a non-contiguous free range - deallocations will merge contiguous fragments together.
107*fdd8201dSApple OSS Distributions  *   @result Returns the count of free fragments.
108*fdd8201dSApple OSS Distributions  */
109*fdd8201dSApple OSS Distributions 
110*fdd8201dSApple OSS Distributions 	virtual UInt32 getFragmentCount( void );
111*fdd8201dSApple OSS Distributions 
112*fdd8201dSApple OSS Distributions /*! @function getFragmentCapacity
113*fdd8201dSApple OSS Distributions  *   @abstract Accessor to return the number of free fragments in the range.
114*fdd8201dSApple OSS Distributions  *   @discussion This method returns the current capacity of the free fragment list.
115*fdd8201dSApple OSS Distributions  *   @result Returns the current capacity of free fragment list.
116*fdd8201dSApple OSS Distributions  */
117*fdd8201dSApple OSS Distributions 
118*fdd8201dSApple OSS Distributions 	virtual UInt32 getFragmentCapacity( void );
119*fdd8201dSApple OSS Distributions 
120*fdd8201dSApple OSS Distributions /*! @function setFragmentCapacityIncrement
121*fdd8201dSApple OSS Distributions  *   @abstract Sets the count of fragments the free list will increase by when full.
122*fdd8201dSApple OSS Distributions  *   @discussion This method sets the number of extra fragments the free list will expand to when full. It defaults to the initial capacity.
123*fdd8201dSApple OSS Distributions  *   @param count The number of fragments to increment the capacity by when the free list is full.
124*fdd8201dSApple OSS Distributions  */
125*fdd8201dSApple OSS Distributions 
126*fdd8201dSApple OSS Distributions 	virtual void setFragmentCapacityIncrement( UInt32 count );
127*fdd8201dSApple OSS Distributions 
128*fdd8201dSApple OSS Distributions /*! @function getFreeCount
129*fdd8201dSApple OSS Distributions  *   @abstract Totals the sizes of the free fragments.
130*fdd8201dSApple OSS Distributions  *   @discussion This method returns the total of the sizes of the fragments on the free list.
131*fdd8201dSApple OSS Distributions  *   @result Returns the total of the free fragments sizes.
132*fdd8201dSApple OSS Distributions  */
133*fdd8201dSApple OSS Distributions 
134*fdd8201dSApple OSS Distributions 	virtual IORangeScalar getFreeCount( void );
135*fdd8201dSApple OSS Distributions 
136*fdd8201dSApple OSS Distributions /*! @function allocate
137*fdd8201dSApple OSS Distributions  *   @abstract Allocates from the free list, at any offset.
138*fdd8201dSApple OSS Distributions  *   @discussion This method allocates a range from the free list. The alignment will default to the alignment set when the allocator was created or may be set here.
139*fdd8201dSApple OSS Distributions  *   @param size The size of the range requested.
140*fdd8201dSApple OSS Distributions  *   @param result The beginning of the range allocated is returned here on success.
141*fdd8201dSApple OSS Distributions  *   @param alignment If zero is passed, default to the allocators alignment, otherwise pass an alignment required for the allocation, for example 4096 to page align.
142*fdd8201dSApple OSS Distributions  *   @result Returns true if the allocation was successful, else false.
143*fdd8201dSApple OSS Distributions  */
144*fdd8201dSApple OSS Distributions 
145*fdd8201dSApple OSS Distributions 	virtual bool allocate( IORangeScalar size,
146*fdd8201dSApple OSS Distributions 	    IORangeScalar * result,
147*fdd8201dSApple OSS Distributions 	    IORangeScalar alignment = 0 );
148*fdd8201dSApple OSS Distributions 
149*fdd8201dSApple OSS Distributions /*! @function allocateRange
150*fdd8201dSApple OSS Distributions  *   @abstract Allocates from the free list, at a set offset.
151*fdd8201dSApple OSS Distributions  *   @discussion This method allocates a range from the free list, given a set offset passed in.
152*fdd8201dSApple OSS Distributions  *   @param start The beginning of the range requested.
153*fdd8201dSApple OSS Distributions  *   @param size The size of the range requested.
154*fdd8201dSApple OSS Distributions  *   @result Returns true if the allocation was successful, else false.
155*fdd8201dSApple OSS Distributions  */
156*fdd8201dSApple OSS Distributions 
157*fdd8201dSApple OSS Distributions 	virtual bool allocateRange( IORangeScalar start,
158*fdd8201dSApple OSS Distributions 	    IORangeScalar size );
159*fdd8201dSApple OSS Distributions 
160*fdd8201dSApple OSS Distributions /*! @function deallocate
161*fdd8201dSApple OSS Distributions  *   @abstract Deallocates a range to the free list.
162*fdd8201dSApple OSS Distributions  *   @discussion This method deallocates a range to the free list, given a the start offset and length passed in.
163*fdd8201dSApple OSS Distributions  *   @param start The beginning of the range requested.
164*fdd8201dSApple OSS Distributions  *   @param size Returns the size of the range requested.
165*fdd8201dSApple OSS Distributions  */
166*fdd8201dSApple OSS Distributions 
167*fdd8201dSApple OSS Distributions 	virtual void deallocate( IORangeScalar start,
168*fdd8201dSApple OSS Distributions 	    IORangeScalar size );
169*fdd8201dSApple OSS Distributions };
170*fdd8201dSApple OSS Distributions 
171*fdd8201dSApple OSS Distributions #endif /* _IOKIT_IORANGEALLOCATOR_H */
172