2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <IOKit/IODataQueue.h>
24 #include <IOKit/IODataQueueShared.h>
25 #include <IOKit/IOLib.h>
26 #include <IOKit/IOMemoryDescriptor.h>
36 #define super OSObject
38 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
40 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
42 IODataQueue
*dataQueue
= new IODataQueue
;
45 if (!dataQueue
->initWithCapacity(size
)) {
54 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
56 IODataQueue
*dataQueue
= new IODataQueue
;
59 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
68 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
74 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(round_page(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
), PAGE_SIZE
);
79 dataQueue
->queueSize
= size
;
86 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
88 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
91 void IODataQueue::free()
94 IOFreeAligned(dataQueue
, round_page(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
102 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
104 const UInt32 head
= dataQueue
->head
; // volatile
105 const UInt32 tail
= dataQueue
->tail
;
106 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
107 IODataQueueEntry
* entry
;
111 // Is there enough room at the end for the entry?
112 if ( (tail
+ entrySize
) <= dataQueue
->queueSize
)
114 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
116 entry
->size
= dataSize
;
117 memcpy(&entry
->data
, data
, dataSize
);
119 // The tail can be out of bound when the size of the new entry
120 // exactly matches the available space at the end of the queue.
121 // The tail can range from 0 to dataQueue->queueSize inclusive.
123 dataQueue
->tail
+= entrySize
;
125 else if ( head
> entrySize
) // Is there enough room at the beginning?
127 // Wrap around to the beginning, but do not allow the tail to catch
130 dataQueue
->queue
->size
= dataSize
;
132 // We need to make sure that there is enough room to set the size before
133 // doing this. The user client checks for this and will look for the size
134 // at the beginning if there isn't room for it at the end.
136 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
138 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
141 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
142 dataQueue
->tail
= entrySize
;
146 return false; // queue is full
151 // Do not allow the tail to catch up to the head when the queue is full.
152 // That's why the comparison uses a '>' rather than '>='.
154 if ( (head
- tail
) > entrySize
)
156 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
158 entry
->size
= dataSize
;
159 memcpy(&entry
->data
, data
, dataSize
);
160 dataQueue
->tail
+= entrySize
;
164 return false; // queue is full
168 // Send notification (via mach message) that data is available.
170 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
171 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
173 sendDataAvailableNotification();
179 void IODataQueue::setNotificationPort(mach_port_t port
)
181 static struct _notifyMsg init_msg
= { {
182 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
183 sizeof (struct _notifyMsg
),
190 if (notifyMsg
== 0) {
191 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
194 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
196 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
199 void IODataQueue::sendDataAvailableNotification()
202 mach_msg_header_t
* msgh
;
204 msgh
= (mach_msg_header_t
*)notifyMsg
;
206 kr
= mach_msg_send_from_kernel(msgh
, msgh
->msgh_size
);
208 case MACH_SEND_TIMED_OUT
: // Notification already sent
209 case MACH_MSG_SUCCESS
:
212 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
218 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
220 IOMemoryDescriptor
*descriptor
= 0;
222 if (dataQueue
!= 0) {
223 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);