]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOBufferMemoryDescriptor.h
b4bfa19c789b0fd8a5f0505c170a42ed931e2a74
[apple/xnu.git] / iokit / IOKit / IOBufferMemoryDescriptor.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #ifndef _IOBUFFERMEMORYDESCRIPTOR_H
24 #define _IOBUFFERMEMORYDESCRIPTOR_H
25
26 #include <IOKit/IOMemoryDescriptor.h>
27
28 enum {
29 kIOMemoryPhysicallyContiguous = 0x00000010,
30 kIOMemoryPageable = 0x00000020,
31 kIOMemoryPurgeable = 0x00000040,
32 kIOMemorySharingTypeMask = 0x000f0000,
33 kIOMemoryUnshared = 0x00000000,
34 kIOMemoryKernelUserShared = 0x00010000
35 };
36
37 #define _IOBUFFERMEMORYDESCRIPTOR_INTASKWITHOPTIONS_ 1
38 /*!
39 @class IOBufferMemoryDescriptor
40 @abstract Provides a simple memory descriptor that allocates its own buffer memory.
41 */
42
43 class IOBufferMemoryDescriptor : public IOGeneralMemoryDescriptor
44 {
45 OSDeclareDefaultStructors(IOBufferMemoryDescriptor);
46
47 protected:
48 /*! @struct ExpansionData
49 @discussion This structure will be used to expand the capablilties of this class in the future.
50 */
51 struct ExpansionData {
52 vm_map_t map;
53 };
54
55 /*! @var reserved
56 Reserved for future use. (Internal use only) */
57 ExpansionData * reserved;
58
59 protected:
60 void * _buffer;
61 vm_size_t _capacity;
62 vm_offset_t _alignment;
63 IOOptionBits _options;
64 IOPhysicalAddress * _physAddrs;
65 unsigned _physSegCount;
66
67 private:
68 virtual bool initWithOptions(
69 IOOptionBits options,
70 vm_size_t capacity,
71 vm_offset_t alignment,
72 task_t inTask);
73
74 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 0);
75 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 1);
76 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 2);
77 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 3);
78 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 4);
79 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 5);
80 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 6);
81 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 7);
82 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 8);
83 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 9);
84 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 10);
85 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 11);
86 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 12);
87 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 13);
88 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 14);
89 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 15);
90
91 protected:
92 virtual void free();
93
94 virtual bool initWithAddress( void * address, /* not supported */
95 IOByteCount withLength,
96 IODirection withDirection );
97
98 virtual bool initWithAddress( vm_address_t address, /* not supported */
99 IOByteCount withLength,
100 IODirection withDirection,
101 task_t withTask );
102
103 virtual bool initWithPhysicalAddress(
104 IOPhysicalAddress address, /* not supported */
105 IOByteCount withLength,
106 IODirection withDirection );
107
108 virtual bool initWithPhysicalRanges(
109 IOPhysicalRange * ranges, /* not supported */
110 UInt32 withCount,
111 IODirection withDirection,
112 bool asReference = false );
113
114 virtual bool initWithRanges( IOVirtualRange * ranges, /* not supported */
115 UInt32 withCount,
116 IODirection withDirection,
117 task_t withTask,
118 bool asReference = false );
119
120 IOGeneralMemoryDescriptor::withAddress; /* not supported */
121 IOGeneralMemoryDescriptor::withPhysicalAddress; /* not supported */
122 IOGeneralMemoryDescriptor::withPhysicalRanges; /* not supported */
123 IOGeneralMemoryDescriptor::withRanges; /* not supported */
124 IOGeneralMemoryDescriptor::withSubRange; /* not supported */
125
126 public:
127
128 /*
129 * withOptions:
130 *
131 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
132 * hold capacity bytes. The descriptor's length is initially set to the
133 * capacity.
134 */
135 virtual bool initWithOptions( IOOptionBits options,
136 vm_size_t capacity,
137 vm_offset_t alignment);
138
139 static IOBufferMemoryDescriptor * withOptions( IOOptionBits options,
140 vm_size_t capacity,
141 vm_offset_t alignment = 1);
142
143 /*! @function inTaskWithOptions
144 @abstract Creates a memory buffer with memory descriptor for that buffer.
145 @discussion Added in Mac OS X 10.2, this method allocates a memory buffer with a given size and alignment in the task's address space specified, and returns a memory descriptor instance representing the memory. It is recommended that memory allocated for I/O or sharing via mapping be created via IOBufferMemoryDescriptor. Options passed with the request specify the kind of memory to be allocated - pageablity and sharing are specified with option bits. This function may block and so should not be called from interrupt level or while a simple lock is held.
146 @param inTask The task the buffer will be allocated in.
147 @param options Options for the allocation:<br>
148 kIOMemoryPhysicallyContiguous - pass to request memory be physically contiguous. This option is heavily discouraged. The request may fail if memory is fragmented, may cause large amounts of paging activity, and may take a very long time to execute.<br>
149 kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
150 kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
151 kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.
152 @param capacity The number of bytes to allocate.
153 @param alignment The minimum required alignment of the buffer in bytes - 1 is the default for no required alignment. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
154 @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
155
156 static IOBufferMemoryDescriptor * inTaskWithOptions(
157 task_t inTask,
158 IOOptionBits options,
159 vm_size_t capacity,
160 vm_offset_t alignment = 1);
161
162 /*
163 * withCapacity:
164 *
165 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
166 * hold capacity bytes. The descriptor's length is initially set to the
167 * capacity.
168 */
169 static IOBufferMemoryDescriptor * withCapacity(
170 vm_size_t capacity,
171 IODirection withDirection,
172 bool withContiguousMemory = false);
173 /*
174 * initWithBytes:
175 *
176 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
177 * The descriptor's length and capacity are set to the input buffer's size.
178 */
179 virtual bool initWithBytes(const void * bytes,
180 vm_size_t withLength,
181 IODirection withDirection,
182 bool withContiguousMemory = false);
183
184 /*
185 * withBytes:
186 *
187 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
188 * The descriptor's length and capacity are set to the input buffer's size.
189 */
190 static IOBufferMemoryDescriptor * withBytes(
191 const void * bytes,
192 vm_size_t withLength,
193 IODirection withDirection,
194 bool withContiguousMemory = false);
195
196 /*
197 * setLength:
198 *
199 * Change the buffer length of the memory descriptor. When a new buffer
200 * is created, the initial length of the buffer is set to be the same as
201 * the capacity. The length can be adjusted via setLength for a shorter
202 * transfer (there is no need to create more buffer descriptors when you
203 * can reuse an existing one, even for different transfer sizes). Note
204 * that the specified length must not exceed the capacity of the buffer.
205 */
206 virtual void setLength(vm_size_t length);
207
208 /*
209 * setDirection:
210 *
211 * Change the direction of the transfer. This method allows one to redirect
212 * the descriptor's transfer direction. This eliminates the need to destroy
213 * and create new buffers when different transfer directions are needed.
214 */
215 virtual void setDirection(IODirection direction);
216
217 /*
218 * getCapacity:
219 *
220 * Get the buffer capacity
221 */
222 virtual vm_size_t getCapacity() const;
223
224 /*
225 * getBytesNoCopy:
226 *
227 * Return the virtual address of the beginning of the buffer
228 */
229 virtual void *getBytesNoCopy();
230
231 /*
232 * getBytesNoCopy:
233 *
234 * Return the virtual address of an offset from the beginning of the buffer
235 */
236 virtual void *getBytesNoCopy(vm_size_t start, vm_size_t withLength);
237
238 /*
239 * appendBytes:
240 *
241 * Add some data to the end of the buffer. This method automatically
242 * maintains the memory descriptor buffer length. Note that appendBytes
243 * will not copy past the end of the memory descriptor's current capacity.
244 */
245 virtual bool appendBytes(const void *bytes, vm_size_t withLength);
246 };
247
248 #endif /* !_IOBUFFERMEMORYDESCRIPTOR_H */