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