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