]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOBufferMemoryDescriptor.h
xnu-792.2.4.tar.gz
[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 * 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 #ifndef _IOBUFFERMEMORYDESCRIPTOR_H
23 #define _IOBUFFERMEMORYDESCRIPTOR_H
24
25 #include <IOKit/IOMemoryDescriptor.h>
26
27 enum {
28 kIOMemoryPhysicallyContiguous = 0x00000010,
29 kIOMemoryPageable = 0x00000020,
30 kIOMemoryPurgeable = 0x00000040,
31 kIOMemorySharingTypeMask = 0x000f0000,
32 kIOMemoryUnshared = 0x00000000,
33 kIOMemoryKernelUserShared = 0x00010000
34 };
35
36 #define _IOBUFFERMEMORYDESCRIPTOR_INTASKWITHOPTIONS_ 1
37 /*!
38 @class IOBufferMemoryDescriptor
39 @abstract Provides a simple memory descriptor that allocates its own buffer memory.
40 */
41
42 class IOBufferMemoryDescriptor : public IOGeneralMemoryDescriptor
43 {
44 OSDeclareDefaultStructors(IOBufferMemoryDescriptor);
45
46 protected:
47 /*! @struct ExpansionData
48 @discussion This structure will be used to expand the capablilties of this class in the future.
49 */
50 struct ExpansionData {
51 vm_map_t map;
52 };
53
54 /*! @var reserved
55 Reserved for future use. (Internal use only) */
56 ExpansionData * reserved;
57
58 protected:
59 void * _buffer;
60 vm_size_t _capacity;
61 vm_offset_t _alignment;
62 IOOptionBits _options;
63 IOPhysicalAddress * _physAddrs;
64 unsigned _physSegCount;
65
66 private:
67 virtual bool initWithOptions(
68 IOOptionBits options,
69 vm_size_t capacity,
70 vm_offset_t alignment,
71 task_t inTask);
72
73 OSMetaClassDeclareReservedUsed(IOBufferMemoryDescriptor, 0);
74 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 1);
75 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 2);
76 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 3);
77 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 4);
78 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 5);
79 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 6);
80 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 7);
81 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 8);
82 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 9);
83 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 10);
84 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 11);
85 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 12);
86 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 13);
87 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 14);
88 OSMetaClassDeclareReservedUnused(IOBufferMemoryDescriptor, 15);
89
90 protected:
91 virtual void free();
92
93 virtual bool initWithAddress( void * address, /* not supported */
94 IOByteCount withLength,
95 IODirection withDirection );
96
97 virtual bool initWithAddress( vm_address_t address, /* not supported */
98 IOByteCount withLength,
99 IODirection withDirection,
100 task_t withTask );
101
102 virtual bool initWithPhysicalAddress(
103 IOPhysicalAddress address, /* not supported */
104 IOByteCount withLength,
105 IODirection withDirection );
106
107 virtual bool initWithPhysicalRanges(
108 IOPhysicalRange * ranges, /* not supported */
109 UInt32 withCount,
110 IODirection withDirection,
111 bool asReference = false );
112
113 virtual bool initWithRanges( IOVirtualRange * ranges, /* not supported */
114 UInt32 withCount,
115 IODirection withDirection,
116 task_t withTask,
117 bool asReference = false );
118
119 IOGeneralMemoryDescriptor::withAddress; /* not supported */
120 IOGeneralMemoryDescriptor::withPhysicalAddress; /* not supported */
121 IOGeneralMemoryDescriptor::withPhysicalRanges; /* not supported */
122 IOGeneralMemoryDescriptor::withRanges; /* not supported */
123 IOGeneralMemoryDescriptor::withSubRange; /* not supported */
124
125 public:
126
127 /*
128 * withOptions:
129 *
130 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
131 * hold capacity bytes. The descriptor's length is initially set to the
132 * capacity.
133 */
134 virtual bool initWithOptions( IOOptionBits options,
135 vm_size_t capacity,
136 vm_offset_t alignment);
137
138 static IOBufferMemoryDescriptor * withOptions( IOOptionBits options,
139 vm_size_t capacity,
140 vm_offset_t alignment = 1);
141
142 /*! @function inTaskWithOptions
143 @abstract Creates a memory buffer with memory descriptor for that buffer.
144 @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.
145 @param inTask The task the buffer will be allocated in.
146 @param options Options for the allocation:<br>
147 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>
148 kIOMemoryPageable - pass to request memory be non-wired - the default for kernel allocated memory is wired.<br>
149 kIOMemoryPurgeable - pass to request memory that may later have its purgeable state set with IOMemoryDescriptor::setPurgeable. Only supported for kIOMemoryPageable allocations.<br>
150 kIOMemoryKernelUserShared - pass to request memory that will be mapped into both the kernel and client applications.
151 @param capacity The number of bytes to allocate.
152 @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.
153 @result Returns an instance of class IOBufferMemoryDescriptor to be released by the caller, which will free the memory desriptor and associated buffer. */
154
155 static IOBufferMemoryDescriptor * inTaskWithOptions(
156 task_t inTask,
157 IOOptionBits options,
158 vm_size_t capacity,
159 vm_offset_t alignment = 1);
160
161 /*
162 * withCapacity:
163 *
164 * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
165 * hold capacity bytes. The descriptor's length is initially set to the
166 * capacity.
167 */
168 static IOBufferMemoryDescriptor * withCapacity(
169 vm_size_t capacity,
170 IODirection withDirection,
171 bool withContiguousMemory = false);
172 /*
173 * initWithBytes:
174 *
175 * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
176 * The descriptor's length and capacity are set to the input buffer's size.
177 */
178 virtual bool initWithBytes(const void * bytes,
179 vm_size_t withLength,
180 IODirection withDirection,
181 bool withContiguousMemory = false);
182
183 /*
184 * withBytes:
185 *
186 * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
187 * The descriptor's length and capacity are set to the input buffer's size.
188 */
189 static IOBufferMemoryDescriptor * withBytes(
190 const void * bytes,
191 vm_size_t withLength,
192 IODirection withDirection,
193 bool withContiguousMemory = false);
194
195 /*
196 * setLength:
197 *
198 * Change the buffer length of the memory descriptor. When a new buffer
199 * is created, the initial length of the buffer is set to be the same as
200 * the capacity. The length can be adjusted via setLength for a shorter
201 * transfer (there is no need to create more buffer descriptors when you
202 * can reuse an existing one, even for different transfer sizes). Note
203 * that the specified length must not exceed the capacity of the buffer.
204 */
205 virtual void setLength(vm_size_t length);
206
207 /*
208 * setDirection:
209 *
210 * Change the direction of the transfer. This method allows one to redirect
211 * the descriptor's transfer direction. This eliminates the need to destroy
212 * and create new buffers when different transfer directions are needed.
213 */
214 virtual void setDirection(IODirection direction);
215
216 /*
217 * getCapacity:
218 *
219 * Get the buffer capacity
220 */
221 virtual vm_size_t getCapacity() const;
222
223 /*
224 * getBytesNoCopy:
225 *
226 * Return the virtual address of the beginning of the buffer
227 */
228 virtual void *getBytesNoCopy();
229
230 /*
231 * getBytesNoCopy:
232 *
233 * Return the virtual address of an offset from the beginning of the buffer
234 */
235 virtual void *getBytesNoCopy(vm_size_t start, vm_size_t withLength);
236
237 /*
238 * appendBytes:
239 *
240 * Add some data to the end of the buffer. This method automatically
241 * maintains the memory descriptor buffer length. Note that appendBytes
242 * will not copy past the end of the memory descriptor's current capacity.
243 */
244 virtual bool appendBytes(const void *bytes, vm_size_t withLength);
245 };
246
247 #endif /* !_IOBUFFERMEMORYDESCRIPTOR_H */