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>
42 #define super OSObject
44 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
46 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
48 IODataQueue
*dataQueue
= new IODataQueue
;
51 if (!dataQueue
->initWithCapacity(size
)) {
60 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
62 IODataQueue
*dataQueue
= new IODataQueue
;
65 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
74 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
76 vm_size_t allocSize
= 0;
82 allocSize
= round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
);
84 if (allocSize
< size
) {
88 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(allocSize
, PAGE_SIZE
);
93 dataQueue
->queueSize
= size
;
100 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
102 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
105 void IODataQueue::free()
108 IOFreeAligned(dataQueue
, round_page(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
116 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
118 const UInt32 head
= dataQueue
->head
; // volatile
119 const UInt32 tail
= dataQueue
->tail
;
120 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
121 IODataQueueEntry
* entry
;
125 // Is there enough room at the end for the entry?
126 if ( (tail
+ entrySize
) <= dataQueue
->queueSize
)
128 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
130 entry
->size
= dataSize
;
131 memcpy(&entry
->data
, data
, dataSize
);
133 // The tail can be out of bound when the size of the new entry
134 // exactly matches the available space at the end of the queue.
135 // The tail can range from 0 to dataQueue->queueSize inclusive.
137 dataQueue
->tail
+= entrySize
;
139 else if ( head
> entrySize
) // Is there enough room at the beginning?
141 // Wrap around to the beginning, but do not allow the tail to catch
144 dataQueue
->queue
->size
= dataSize
;
146 // We need to make sure that there is enough room to set the size before
147 // doing this. The user client checks for this and will look for the size
148 // at the beginning if there isn't room for it at the end.
150 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
152 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
155 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
156 dataQueue
->tail
= entrySize
;
160 return false; // queue is full
165 // Do not allow the tail to catch up to the head when the queue is full.
166 // That's why the comparison uses a '>' rather than '>='.
168 if ( (head
- tail
) > entrySize
)
170 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
172 entry
->size
= dataSize
;
173 memcpy(&entry
->data
, data
, dataSize
);
174 dataQueue
->tail
+= entrySize
;
178 return false; // queue is full
182 // Send notification (via mach message) that data is available.
184 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
185 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
187 sendDataAvailableNotification();
193 void IODataQueue::setNotificationPort(mach_port_t port
)
195 static struct _notifyMsg init_msg
= { {
196 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
197 sizeof (struct _notifyMsg
),
204 if (notifyMsg
== 0) {
205 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
208 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
210 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
213 void IODataQueue::sendDataAvailableNotification()
216 mach_msg_header_t
* msgh
;
218 msgh
= (mach_msg_header_t
*)notifyMsg
;
219 if (msgh
&& msgh
->msgh_remote_port
) {
220 kr
= mach_msg_send_from_kernel_proper(msgh
, msgh
->msgh_size
);
222 case MACH_SEND_TIMED_OUT
: // Notification already sent
223 case MACH_MSG_SUCCESS
:
226 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
232 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
234 IOMemoryDescriptor
*descriptor
= 0;
236 if (dataQueue
!= 0) {
237 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);