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