2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <IOKit/IODataQueue.h>
25 #include <IOKit/IODataQueueShared.h>
26 #include <IOKit/IOLib.h>
27 #include <IOKit/IOMemoryDescriptor.h>
37 #define super OSObject
39 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
41 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
43 IODataQueue
*dataQueue
= new IODataQueue
;
46 if (!dataQueue
->initWithCapacity(size
)) {
55 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
57 IODataQueue
*dataQueue
= new IODataQueue
;
60 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
69 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
75 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(round_page_32(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
), PAGE_SIZE
);
80 dataQueue
->queueSize
= size
;
87 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
89 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
92 void IODataQueue::free()
95 IOFreeAligned(dataQueue
, round_page_32(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
103 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
105 const UInt32 head
= dataQueue
->head
; // volatile
106 const UInt32 tail
= dataQueue
->tail
;
107 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
108 IODataQueueEntry
* entry
;
112 // Is there enough room at the end for the entry?
113 if ( (tail
+ entrySize
) <= dataQueue
->queueSize
)
115 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
117 entry
->size
= dataSize
;
118 memcpy(&entry
->data
, data
, dataSize
);
120 // The tail can be out of bound when the size of the new entry
121 // exactly matches the available space at the end of the queue.
122 // The tail can range from 0 to dataQueue->queueSize inclusive.
124 dataQueue
->tail
+= entrySize
;
126 else if ( head
> entrySize
) // Is there enough room at the beginning?
128 // Wrap around to the beginning, but do not allow the tail to catch
131 dataQueue
->queue
->size
= dataSize
;
133 // We need to make sure that there is enough room to set the size before
134 // doing this. The user client checks for this and will look for the size
135 // at the beginning if there isn't room for it at the end.
137 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
139 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
142 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
143 dataQueue
->tail
= entrySize
;
147 return false; // queue is full
152 // Do not allow the tail to catch up to the head when the queue is full.
153 // That's why the comparison uses a '>' rather than '>='.
155 if ( (head
- tail
) > entrySize
)
157 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
159 entry
->size
= dataSize
;
160 memcpy(&entry
->data
, data
, dataSize
);
161 dataQueue
->tail
+= entrySize
;
165 return false; // queue is full
169 // Send notification (via mach message) that data is available.
171 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
172 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
174 sendDataAvailableNotification();
180 void IODataQueue::setNotificationPort(mach_port_t port
)
182 static struct _notifyMsg init_msg
= { {
183 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
184 sizeof (struct _notifyMsg
),
191 if (notifyMsg
== 0) {
192 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
195 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
197 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
200 void IODataQueue::sendDataAvailableNotification()
203 mach_msg_header_t
* msgh
;
205 msgh
= (mach_msg_header_t
*)notifyMsg
;
207 kr
= mach_msg_send_from_kernel(msgh
, msgh
->msgh_size
);
209 case MACH_SEND_TIMED_OUT
: // Notification already sent
210 case MACH_MSG_SUCCESS
:
213 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
219 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
221 IOMemoryDescriptor
*descriptor
= 0;
223 if (dataQueue
!= 0) {
224 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);