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 #define DISABLE_DATAQUEUE_WARNING
31 #include <IOKit/IODataQueue.h>
33 #undef DISABLE_DATAQUEUE_WARNING
35 #include <IOKit/IODataQueueShared.h>
36 #include <IOKit/IOLib.h>
37 #include <IOKit/IOMemoryDescriptor.h>
38 #include <libkern/OSAtomic.h>
48 #define super OSObject
50 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
52 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
54 IODataQueue
*dataQueue
= new IODataQueue
;
57 if (!dataQueue
->initWithCapacity(size
)) {
66 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
68 IODataQueue
*dataQueue
= new IODataQueue
;
71 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
80 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
82 vm_size_t allocSize
= 0;
88 if (size
> UINT32_MAX
- DATA_QUEUE_MEMORY_HEADER_SIZE
) {
92 allocSize
= round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
);
94 if (allocSize
< size
) {
98 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(allocSize
, PAGE_SIZE
);
103 dataQueue
->queueSize
= size
;
108 notifyMsg
= IOMalloc(sizeof(mach_msg_header_t
));
112 bzero(notifyMsg
, sizeof(mach_msg_header_t
));
117 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
119 // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
120 // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
121 if ((entrySize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) ||
122 // check (numEntries + 1)
123 (numEntries
> UINT32_MAX
-1) ||
124 // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
125 (entrySize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
> UINT32_MAX
/(numEntries
+1))) {
129 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
132 void IODataQueue::free()
135 IOFreeAligned(dataQueue
, round_page(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
139 IOFree(notifyMsg
, sizeof(mach_msg_header_t
));
149 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
151 const UInt32 head
= dataQueue
->head
; // volatile
152 const UInt32 tail
= dataQueue
->tail
;
153 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
154 IODataQueueEntry
* entry
;
156 // Check for overflow of entrySize
157 if (dataSize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) {
160 // Check for underflow of (dataQueue->queueSize - tail)
161 if (dataQueue
->queueSize
< tail
) {
167 // Is there enough room at the end for the entry?
168 if ((entrySize
<= UINT32_MAX
- tail
) &&
169 ((tail
+ entrySize
) <= dataQueue
->queueSize
) )
171 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
173 entry
->size
= dataSize
;
174 memcpy(&entry
->data
, data
, dataSize
);
176 // The tail can be out of bound when the size of the new entry
177 // exactly matches the available space at the end of the queue.
178 // The tail can range from 0 to dataQueue->queueSize inclusive.
180 OSAddAtomic(entrySize
, (SInt32
*)&dataQueue
->tail
);
182 else if ( head
> entrySize
) // Is there enough room at the beginning?
184 // Wrap around to the beginning, but do not allow the tail to catch
187 dataQueue
->queue
->size
= dataSize
;
189 // We need to make sure that there is enough room to set the size before
190 // doing this. The user client checks for this and will look for the size
191 // at the beginning if there isn't room for it at the end.
193 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
195 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
198 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
199 OSCompareAndSwap(dataQueue
->tail
, entrySize
, &dataQueue
->tail
);
203 return false; // queue is full
208 // Do not allow the tail to catch up to the head when the queue is full.
209 // That's why the comparison uses a '>' rather than '>='.
211 if ( (head
- tail
) > entrySize
)
213 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
215 entry
->size
= dataSize
;
216 memcpy(&entry
->data
, data
, dataSize
);
217 OSAddAtomic(entrySize
, (SInt32
*)&dataQueue
->tail
);
221 return false; // queue is full
225 // Send notification (via mach message) that data is available.
227 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
228 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
230 sendDataAvailableNotification();
236 void IODataQueue::setNotificationPort(mach_port_t port
)
238 mach_msg_header_t
* msgh
= (mach_msg_header_t
*) notifyMsg
;
241 bzero(msgh
, sizeof(mach_msg_header_t
));
242 msgh
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
243 msgh
->msgh_size
= sizeof(mach_msg_header_t
);
244 msgh
->msgh_remote_port
= port
;
248 void IODataQueue::sendDataAvailableNotification()
251 mach_msg_header_t
* msgh
;
253 msgh
= (mach_msg_header_t
*) notifyMsg
;
254 if (msgh
&& msgh
->msgh_remote_port
) {
255 kr
= mach_msg_send_from_kernel_with_options(msgh
, msgh
->msgh_size
, MACH_SEND_TIMEOUT
, MACH_MSG_TIMEOUT_NONE
);
257 case MACH_SEND_TIMED_OUT
: // Notification already sent
258 case MACH_MSG_SUCCESS
:
259 case MACH_SEND_NO_BUFFER
:
262 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
268 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
270 IOMemoryDescriptor
*descriptor
= 0;
272 if (dataQueue
!= 0) {
273 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);