2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IODataQueue.h>
30 #include <IOKit/IODataQueueShared.h>
31 #include <IOKit/IOLib.h>
32 #include <IOKit/IOMemoryDescriptor.h>
33 #include <libkern/OSAtomic.h>
43 #define super OSObject
45 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
47 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
49 IODataQueue
*dataQueue
= new IODataQueue
;
52 if (!dataQueue
->initWithCapacity(size
)) {
61 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
63 IODataQueue
*dataQueue
= new IODataQueue
;
66 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
75 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
77 vm_size_t allocSize
= 0;
83 if (size
> UINT32_MAX
- DATA_QUEUE_MEMORY_HEADER_SIZE
) {
87 allocSize
= round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
);
89 if (allocSize
< size
) {
93 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(allocSize
, PAGE_SIZE
);
98 dataQueue
->queueSize
= size
;
105 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
107 // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
108 // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
109 if ((entrySize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) ||
110 // check (numEntries + 1)
111 (numEntries
> UINT32_MAX
-1) ||
112 // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
113 (entrySize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
> UINT32_MAX
/(numEntries
+1))) {
117 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
120 void IODataQueue::free()
123 IOFreeAligned(dataQueue
, round_page(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
131 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
133 const UInt32 head
= dataQueue
->head
; // volatile
134 const UInt32 tail
= dataQueue
->tail
;
135 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
136 IODataQueueEntry
* entry
;
138 // Check for overflow of entrySize
139 if (dataSize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) {
142 // Check for underflow of (dataQueue->queueSize - tail)
143 if (dataQueue
->queueSize
< tail
) {
149 // Is there enough room at the end for the entry?
150 if ((entrySize
<= UINT32_MAX
- tail
) &&
151 ((tail
+ entrySize
) <= dataQueue
->queueSize
) )
153 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
155 entry
->size
= dataSize
;
156 memcpy(&entry
->data
, data
, dataSize
);
158 // The tail can be out of bound when the size of the new entry
159 // exactly matches the available space at the end of the queue.
160 // The tail can range from 0 to dataQueue->queueSize inclusive.
162 OSAddAtomic(entrySize
, (SInt32
*)&dataQueue
->tail
);
164 else if ( head
> entrySize
) // Is there enough room at the beginning?
166 // Wrap around to the beginning, but do not allow the tail to catch
169 dataQueue
->queue
->size
= dataSize
;
171 // We need to make sure that there is enough room to set the size before
172 // doing this. The user client checks for this and will look for the size
173 // at the beginning if there isn't room for it at the end.
175 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
177 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
180 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
181 OSCompareAndSwap(dataQueue
->tail
, entrySize
, &dataQueue
->tail
);
185 return false; // queue is full
190 // Do not allow the tail to catch up to the head when the queue is full.
191 // That's why the comparison uses a '>' rather than '>='.
193 if ( (head
- tail
) > entrySize
)
195 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
197 entry
->size
= dataSize
;
198 memcpy(&entry
->data
, data
, dataSize
);
199 OSAddAtomic(entrySize
, (SInt32
*)&dataQueue
->tail
);
203 return false; // queue is full
207 // Send notification (via mach message) that data is available.
209 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
210 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
212 sendDataAvailableNotification();
218 void IODataQueue::setNotificationPort(mach_port_t port
)
220 static struct _notifyMsg init_msg
= { {
221 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
222 sizeof (struct _notifyMsg
),
229 if (notifyMsg
== 0) {
230 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
233 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
235 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
238 void IODataQueue::sendDataAvailableNotification()
241 mach_msg_header_t
* msgh
;
243 msgh
= (mach_msg_header_t
*)notifyMsg
;
244 if (msgh
&& msgh
->msgh_remote_port
) {
245 kr
= mach_msg_send_from_kernel_with_options(msgh
, msgh
->msgh_size
, MACH_SEND_TIMEOUT
, MACH_MSG_TIMEOUT_NONE
);
247 case MACH_SEND_TIMED_OUT
: // Notification already sent
248 case MACH_MSG_SUCCESS
:
249 case MACH_SEND_NO_BUFFER
:
252 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
258 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
260 IOMemoryDescriptor
*descriptor
= 0;
262 if (dataQueue
!= 0) {
263 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);