]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IODataQueue.cpp
xnu-1504.3.12.tar.gz
[apple/xnu.git] / iokit / Kernel / IODataQueue.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29#include <IOKit/IODataQueue.h>
30#include <IOKit/IODataQueueShared.h>
31#include <IOKit/IOLib.h>
32#include <IOKit/IOMemoryDescriptor.h>
33
34#ifdef enqueue
35#undef enqueue
36#endif
37
38#ifdef dequeue
39#undef dequeue
40#endif
41
42#define super OSObject
43
44OSDefineMetaClassAndStructors(IODataQueue, OSObject)
45
46IODataQueue *IODataQueue::withCapacity(UInt32 size)
47{
48 IODataQueue *dataQueue = new IODataQueue;
49
50 if (dataQueue) {
51 if (!dataQueue->initWithCapacity(size)) {
52 dataQueue->release();
53 dataQueue = 0;
54 }
55 }
56
57 return dataQueue;
58}
59
60IODataQueue *IODataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
61{
62 IODataQueue *dataQueue = new IODataQueue;
63
64 if (dataQueue) {
65 if (!dataQueue->initWithEntries(numEntries, entrySize)) {
66 dataQueue->release();
67 dataQueue = 0;
68 }
69 }
70
71 return dataQueue;
72}
73
74Boolean IODataQueue::initWithCapacity(UInt32 size)
75{
76 if (!super::init()) {
77 return false;
78 }
79
b0d623f7 80 dataQueue = (IODataQueueMemory *)IOMallocAligned(round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE), PAGE_SIZE);
1c79356b
A
81 if (dataQueue == 0) {
82 return false;
83 }
84
2d21ac55
A
85 dataQueue->queueSize = size;
86 dataQueue->head = 0;
87 dataQueue->tail = 0;
1c79356b
A
88
89 return true;
90}
91
92Boolean IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize)
93{
94 return (initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize)));
95}
96
97void IODataQueue::free()
98{
99 if (dataQueue) {
b0d623f7 100 IOFreeAligned(dataQueue, round_page(dataQueue->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE));
1c79356b
A
101 }
102
103 super::free();
104
105 return;
106}
107
108Boolean IODataQueue::enqueue(void * data, UInt32 dataSize)
109{
110 const UInt32 head = dataQueue->head; // volatile
111 const UInt32 tail = dataQueue->tail;
112 const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
113 IODataQueueEntry * entry;
9bccf70c 114
1c79356b
A
115 if ( tail >= head )
116 {
9bccf70c
A
117 // Is there enough room at the end for the entry?
118 if ( (tail + entrySize) <= dataQueue->queueSize )
1c79356b
A
119 {
120 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
121
122 entry->size = dataSize;
123 memcpy(&entry->data, data, dataSize);
9bccf70c
A
124
125 // The tail can be out of bound when the size of the new entry
126 // exactly matches the available space at the end of the queue.
127 // The tail can range from 0 to dataQueue->queueSize inclusive.
128
1c79356b
A
129 dataQueue->tail += entrySize;
130 }
9bccf70c 131 else if ( head > entrySize ) // Is there enough room at the beginning?
1c79356b
A
132 {
133 // Wrap around to the beginning, but do not allow the tail to catch
134 // up to the head.
135
136 dataQueue->queue->size = dataSize;
9bccf70c
A
137
138 // We need to make sure that there is enough room to set the size before
139 // doing this. The user client checks for this and will look for the size
140 // at the beginning if there isn't room for it at the end.
141
142 if ( ( dataQueue->queueSize - tail ) >= DATA_QUEUE_ENTRY_HEADER_SIZE )
143 {
144 ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
145 }
146
1c79356b
A
147 memcpy(&dataQueue->queue->data, data, dataSize);
148 dataQueue->tail = entrySize;
149 }
150 else
151 {
152 return false; // queue is full
153 }
154 }
155 else
156 {
157 // Do not allow the tail to catch up to the head when the queue is full.
158 // That's why the comparison uses a '>' rather than '>='.
159
160 if ( (head - tail) > entrySize )
161 {
162 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
163
164 entry->size = dataSize;
165 memcpy(&entry->data, data, dataSize);
166 dataQueue->tail += entrySize;
167 }
168 else
169 {
170 return false; // queue is full
171 }
172 }
173
174 // Send notification (via mach message) that data is available.
175
176 if ( ( head == tail ) /* queue was empty prior to enqueue() */
177 || ( dataQueue->head == tail ) ) /* queue was emptied during enqueue() */
178 {
179 sendDataAvailableNotification();
180 }
181
182 return true;
183}
184
185void IODataQueue::setNotificationPort(mach_port_t port)
186{
187 static struct _notifyMsg init_msg = { {
188 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0),
189 sizeof (struct _notifyMsg),
190 MACH_PORT_NULL,
191 MACH_PORT_NULL,
192 0,
193 0
194 } };
195
196 if (notifyMsg == 0) {
197 notifyMsg = IOMalloc(sizeof(struct _notifyMsg));
198 }
199
200 *((struct _notifyMsg *)notifyMsg) = init_msg;
201
202 ((struct _notifyMsg *)notifyMsg)->h.msgh_remote_port = port;
203}
204
205void IODataQueue::sendDataAvailableNotification()
206{
207 kern_return_t kr;
208 mach_msg_header_t * msgh;
209
210 msgh = (mach_msg_header_t *)notifyMsg;
2d21ac55 211 if (msgh && msgh->msgh_remote_port) {
b0d623f7 212 kr = mach_msg_send_from_kernel_proper(msgh, msgh->msgh_size);
1c79356b
A
213 switch(kr) {
214 case MACH_SEND_TIMED_OUT: // Notification already sent
215 case MACH_MSG_SUCCESS:
216 break;
217 default:
218 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr);
219 break;
220 }
221 }
222}
223
224IOMemoryDescriptor *IODataQueue::getMemoryDescriptor()
225{
226 IOMemoryDescriptor *descriptor = 0;
227
228 if (dataQueue != 0) {
229 descriptor = IOMemoryDescriptor::withAddress(dataQueue, dataQueue->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE, kIODirectionOutIn);
230 }
231
232 return descriptor;
233}
234