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
)
80 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
), PAGE_SIZE
);
85 dataQueue
->queueSize
= size
;
92 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
94 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
97 void IODataQueue::free()
100 IOFreeAligned(dataQueue
, round_page(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
108 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
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
;
117 // Is there enough room at the end for the entry?
118 if ( (tail
+ entrySize
) <= dataQueue
->queueSize
)
120 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
122 entry
->size
= dataSize
;
123 memcpy(&entry
->data
, data
, dataSize
);
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.
129 dataQueue
->tail
+= entrySize
;
131 else if ( head
> entrySize
) // Is there enough room at the beginning?
133 // Wrap around to the beginning, but do not allow the tail to catch
136 dataQueue
->queue
->size
= dataSize
;
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.
142 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
144 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
147 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
148 dataQueue
->tail
= entrySize
;
152 return false; // queue is full
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 '>='.
160 if ( (head
- tail
) > entrySize
)
162 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
164 entry
->size
= dataSize
;
165 memcpy(&entry
->data
, data
, dataSize
);
166 dataQueue
->tail
+= entrySize
;
170 return false; // queue is full
174 // Send notification (via mach message) that data is available.
176 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
177 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
179 sendDataAvailableNotification();
185 void IODataQueue::setNotificationPort(mach_port_t port
)
187 static struct _notifyMsg init_msg
= { {
188 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
189 sizeof (struct _notifyMsg
),
196 if (notifyMsg
== 0) {
197 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
200 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
202 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
205 void IODataQueue::sendDataAvailableNotification()
208 mach_msg_header_t
* msgh
;
210 msgh
= (mach_msg_header_t
*)notifyMsg
;
211 if (msgh
&& msgh
->msgh_remote_port
) {
212 kr
= mach_msg_send_from_kernel_proper(msgh
, msgh
->msgh_size
);
214 case MACH_SEND_TIMED_OUT
: // Notification already sent
215 case MACH_MSG_SUCCESS
:
218 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
224 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
226 IOMemoryDescriptor
*descriptor
= 0;
228 if (dataQueue
!= 0) {
229 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);