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