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>
40 struct IODataQueueInternal
42 mach_msg_header_t msg
;
54 #define super OSObject
56 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
58 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
60 IODataQueue
*dataQueue
= new IODataQueue
;
63 if (!dataQueue
->initWithCapacity(size
)) {
72 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
74 IODataQueue
*dataQueue
= new IODataQueue
;
77 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
86 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
88 vm_size_t allocSize
= 0;
94 if (size
> UINT32_MAX
- DATA_QUEUE_MEMORY_HEADER_SIZE
) {
98 allocSize
= round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
);
100 if (allocSize
< size
) {
105 notifyMsg
= IONew(IODataQueueInternal
, 1);
109 bzero(notifyMsg
, sizeof(IODataQueueInternal
));
110 ((IODataQueueInternal
*)notifyMsg
)->queueSize
= size
;
112 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(allocSize
, PAGE_SIZE
);
113 if (dataQueue
== 0) {
116 bzero(dataQueue
, allocSize
);
118 dataQueue
->queueSize
= size
;
119 // dataQueue->head = 0;
120 // dataQueue->tail = 0;
125 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
127 // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
128 // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
129 if ((entrySize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) ||
130 // check (numEntries + 1)
131 (numEntries
> UINT32_MAX
-1) ||
132 // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
133 (entrySize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
> UINT32_MAX
/(numEntries
+1))) {
137 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
140 void IODataQueue::free()
144 IOFreeAligned(dataQueue
, round_page(((IODataQueueInternal
*)notifyMsg
)->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
148 IODelete(notifyMsg
, IODataQueueInternal
, 1);
157 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
162 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
164 IODataQueueEntry
* entry
;
166 // Check for overflow of entrySize
167 if (dataSize
> UINT32_MAX
- DATA_QUEUE_ENTRY_HEADER_SIZE
) {
171 // Force a single read of head and tail
172 head
= __c11_atomic_load((_Atomic UInt32
*)&dataQueue
->head
, __ATOMIC_RELAXED
);
173 tail
= __c11_atomic_load((_Atomic UInt32
*)&dataQueue
->tail
, __ATOMIC_RELAXED
);
175 // Check for underflow of (dataQueue->queueSize - tail)
176 queueSize
= ((IODataQueueInternal
*) notifyMsg
)->queueSize
;
177 if ((queueSize
< tail
) || (queueSize
< head
)) {
183 // Is there enough room at the end for the entry?
184 if ((entrySize
<= UINT32_MAX
- tail
) &&
185 ((tail
+ entrySize
) <= queueSize
) )
187 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
189 entry
->size
= dataSize
;
190 memcpy(&entry
->data
, data
, dataSize
);
192 // The tail can be out of bound when the size of the new entry
193 // exactly matches the available space at the end of the queue.
194 // The tail can range from 0 to dataQueue->queueSize inclusive.
196 newTail
= tail
+ entrySize
;
198 else if ( head
> entrySize
) // Is there enough room at the beginning?
200 // Wrap around to the beginning, but do not allow the tail to catch
203 dataQueue
->queue
->size
= dataSize
;
205 // We need to make sure that there is enough room to set the size before
206 // doing this. The user client checks for this and will look for the size
207 // at the beginning if there isn't room for it at the end.
209 if ( ( queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
211 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
214 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
219 return false; // queue is full
224 // Do not allow the tail to catch up to the head when the queue is full.
225 // That's why the comparison uses a '>' rather than '>='.
227 if ( (head
- tail
) > entrySize
)
229 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
231 entry
->size
= dataSize
;
232 memcpy(&entry
->data
, data
, dataSize
);
233 newTail
= tail
+ entrySize
;
237 return false; // queue is full
241 // Store tail with a release memory barrier
242 __c11_atomic_store((_Atomic UInt32
*)&dataQueue
->tail
, newTail
, __ATOMIC_RELEASE
);
244 // Send notification (via mach message) that data is available.
246 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
247 || ( tail
== __c11_atomic_load((_Atomic UInt32
*)&dataQueue
->head
, __ATOMIC_RELAXED
) ) ) /* queue was emptied during enqueue() */
249 sendDataAvailableNotification();
255 void IODataQueue::setNotificationPort(mach_port_t port
)
257 mach_msg_header_t
* msgh
;
259 msgh
= &((IODataQueueInternal
*) notifyMsg
)->msg
;
260 bzero(msgh
, sizeof(mach_msg_header_t
));
261 msgh
->msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
262 msgh
->msgh_size
= sizeof(mach_msg_header_t
);
263 msgh
->msgh_remote_port
= port
;
266 void IODataQueue::sendDataAvailableNotification()
269 mach_msg_header_t
* msgh
;
271 msgh
= &((IODataQueueInternal
*) notifyMsg
)->msg
;
272 if (msgh
->msgh_remote_port
) {
273 kr
= mach_msg_send_from_kernel_with_options(msgh
, msgh
->msgh_size
, MACH_SEND_TIMEOUT
, MACH_MSG_TIMEOUT_NONE
);
275 case MACH_SEND_TIMED_OUT
: // Notification already sent
276 case MACH_MSG_SUCCESS
:
277 case MACH_SEND_NO_BUFFER
:
280 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
286 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
288 IOMemoryDescriptor
*descriptor
= 0;
291 queueSize
= ((IODataQueueInternal
*) notifyMsg
)->queueSize
;
292 if (dataQueue
!= 0) {
293 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);