2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc.
28 * sdouglas 05 Nov 99 - created.
31 #ifndef _IOKIT_IORANGEALLOCATOR_H
32 #define _IOKIT_IORANGEALLOCATOR_H
34 #include <libkern/c++/OSObject.h>
35 #include <IOKit/IOTypes.h>
37 typedef UInt32 IORangeScalar
;
39 /*! @class IORangeAllocator : public OSObject
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. */
43 class IORangeAllocator
: public OSObject
{
45 OSDeclareDefaultStructors(IORangeAllocator
)
50 UInt32 capacityIncrement
;
51 IORangeScalar defaultAlignmentMask
;
54 struct IORangeAllocatorElement
* elements
;
57 virtual bool allocElement( UInt32 index
);
59 virtual void deallocElement( UInt32 index
);
67 @abstract Standard initializer for IORangeAllocator.
68 @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.
69 @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.
70 @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.
71 @param capacity Sets the initial size of the free list in number of non-contiguous fragments. This value is also used for the capacityIncrement.
72 @param options Pass kLocking if the instance can be used by multiple threads.
73 @result Returns true if the instance is successfully initialize, false on failure. */
75 virtual bool init( IORangeScalar endOfRange
,
76 IORangeScalar defaultAlignment
,
78 IOOptionBits options
);
80 /*! @function withRange
81 @abstract Standard factory method for IORangeAllocator.
82 @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.
83 @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.
84 @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.
85 @param capacity Sets the initial size of the free list in number of non-contiguous fragments. This value is also used for the capacityIncrement.
86 @param options Pass kLocking if the instance can be used by multiple threads.
87 @result Returns the new IORangeAllocator instance, to be released by the caller, or zero on failure. */
89 static IORangeAllocator
* withRange( IORangeScalar endOfRange
,
90 IORangeScalar defaultAlignment
= 0, UInt32 capacity
= 0,
91 IOOptionBits options
= 0 );
94 virtual bool serialize(OSSerialize
*s
) const;
96 /*! @function getFragmentCount
97 @abstract Accessor to return the number of free fragments in the range.
98 @discussion This method returns a count of free fragments. Each fragment describes a non-contiguous free range - deallocations will merge contiguous fragments together.
99 @result The count of free fragments. */
101 virtual UInt32
getFragmentCount( void );
103 /*! @function getFragmentCapacity
104 @abstract Accessor to return the number of free fragments in the range.
105 @discussion This method returns the current capacity of the free fragment list.
106 @result The current capacity of free fragment list. */
108 virtual UInt32
getFragmentCapacity( void );
110 /*! @function setFragmentCapacityIncrement
111 @abstract Sets the count of fragments the free list will increase by when full.
112 @discussion This method sets the number of extra fragments the free list will expand to when full. It defaults to the initial capacity.
113 @param count The number of fragments to increment the capacity by when the free list is full. */
115 virtual void setFragmentCapacityIncrement( UInt32 count
);
117 /*! @function getFreeCount
118 @abstract Totals the sizes of the free fragments.
119 @discussion This method returns the total of the sizes of the fragments on the free list.
120 @result The total of the free fragments sizes. */
122 virtual IORangeScalar
getFreeCount( void );
124 /*! @function allocate
125 @abstract Allocate from the free list, at any offset.
126 @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.
127 @param size The size of the range requested.
128 @param result The beginning of the range allocated is returned here on success.
129 @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.
130 @result True if the allocation was successful, else false. */
132 virtual bool allocate( IORangeScalar size
,
133 IORangeScalar
* result
,
134 IORangeScalar alignment
= 0 );
136 /*! @function allocateRange
137 @abstract Allocate from the free list, at a set offset.
138 @discussion This method allocates a range from the free list, given a set offset passed in.
139 @param start The beginning of the range requested.
140 @param size The size of the range requested.
141 @result True if the allocation was successful, else false. */
143 virtual bool allocateRange( IORangeScalar start
,
144 IORangeScalar size
);
146 /*! @function deallocate
147 @abstract Deallocate a range to the free list.
148 @discussion This method deallocates a range to the free list, given a the start offset and length passed in.
149 @param start The beginning of the range requested.
150 @param size The size of the range requested. */
152 virtual void deallocate( IORangeScalar start
,
153 IORangeScalar size
);
156 #endif /* _IOKIT_IORANGEALLOCATOR_H */