2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <IOKit/IODataQueue.h>
32 #include <IOKit/IODataQueueShared.h>
33 #include <IOKit/IOLib.h>
34 #include <IOKit/IOMemoryDescriptor.h>
44 #define super OSObject
46 OSDefineMetaClassAndStructors(IODataQueue
, OSObject
)
48 IODataQueue
*IODataQueue::withCapacity(UInt32 size
)
50 IODataQueue
*dataQueue
= new IODataQueue
;
53 if (!dataQueue
->initWithCapacity(size
)) {
62 IODataQueue
*IODataQueue::withEntries(UInt32 numEntries
, UInt32 entrySize
)
64 IODataQueue
*dataQueue
= new IODataQueue
;
67 if (!dataQueue
->initWithEntries(numEntries
, entrySize
)) {
76 Boolean
IODataQueue::initWithCapacity(UInt32 size
)
82 dataQueue
= (IODataQueueMemory
*)IOMallocAligned(round_page_32(size
+ DATA_QUEUE_MEMORY_HEADER_SIZE
), PAGE_SIZE
);
87 dataQueue
->queueSize
= size
;
94 Boolean
IODataQueue::initWithEntries(UInt32 numEntries
, UInt32 entrySize
)
96 return (initWithCapacity((numEntries
+ 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE
+ entrySize
)));
99 void IODataQueue::free()
102 IOFreeAligned(dataQueue
, round_page_32(dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
));
110 Boolean
IODataQueue::enqueue(void * data
, UInt32 dataSize
)
112 const UInt32 head
= dataQueue
->head
; // volatile
113 const UInt32 tail
= dataQueue
->tail
;
114 const UInt32 entrySize
= dataSize
+ DATA_QUEUE_ENTRY_HEADER_SIZE
;
115 IODataQueueEntry
* entry
;
119 // Is there enough room at the end for the entry?
120 if ( (tail
+ entrySize
) <= dataQueue
->queueSize
)
122 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
124 entry
->size
= dataSize
;
125 memcpy(&entry
->data
, data
, dataSize
);
127 // The tail can be out of bound when the size of the new entry
128 // exactly matches the available space at the end of the queue.
129 // The tail can range from 0 to dataQueue->queueSize inclusive.
131 dataQueue
->tail
+= entrySize
;
133 else if ( head
> entrySize
) // Is there enough room at the beginning?
135 // Wrap around to the beginning, but do not allow the tail to catch
138 dataQueue
->queue
->size
= dataSize
;
140 // We need to make sure that there is enough room to set the size before
141 // doing this. The user client checks for this and will look for the size
142 // at the beginning if there isn't room for it at the end.
144 if ( ( dataQueue
->queueSize
- tail
) >= DATA_QUEUE_ENTRY_HEADER_SIZE
)
146 ((IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
))->size
= dataSize
;
149 memcpy(&dataQueue
->queue
->data
, data
, dataSize
);
150 dataQueue
->tail
= entrySize
;
154 return false; // queue is full
159 // Do not allow the tail to catch up to the head when the queue is full.
160 // That's why the comparison uses a '>' rather than '>='.
162 if ( (head
- tail
) > entrySize
)
164 entry
= (IODataQueueEntry
*)((UInt8
*)dataQueue
->queue
+ tail
);
166 entry
->size
= dataSize
;
167 memcpy(&entry
->data
, data
, dataSize
);
168 dataQueue
->tail
+= entrySize
;
172 return false; // queue is full
176 // Send notification (via mach message) that data is available.
178 if ( ( head
== tail
) /* queue was empty prior to enqueue() */
179 || ( dataQueue
->head
== tail
) ) /* queue was emptied during enqueue() */
181 sendDataAvailableNotification();
187 void IODataQueue::setNotificationPort(mach_port_t port
)
189 static struct _notifyMsg init_msg
= { {
190 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0),
191 sizeof (struct _notifyMsg
),
198 if (notifyMsg
== 0) {
199 notifyMsg
= IOMalloc(sizeof(struct _notifyMsg
));
202 *((struct _notifyMsg
*)notifyMsg
) = init_msg
;
204 ((struct _notifyMsg
*)notifyMsg
)->h
.msgh_remote_port
= port
;
207 void IODataQueue::sendDataAvailableNotification()
210 mach_msg_header_t
* msgh
;
212 msgh
= (mach_msg_header_t
*)notifyMsg
;
214 kr
= mach_msg_send_from_kernel(msgh
, msgh
->msgh_size
);
216 case MACH_SEND_TIMED_OUT
: // Notification already sent
217 case MACH_MSG_SUCCESS
:
220 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr
);
226 IOMemoryDescriptor
*IODataQueue::getMemoryDescriptor()
228 IOMemoryDescriptor
*descriptor
= 0;
230 if (dataQueue
!= 0) {
231 descriptor
= IOMemoryDescriptor::withAddress(dataQueue
, dataQueue
->queueSize
+ DATA_QUEUE_MEMORY_HEADER_SIZE
, kIODirectionOutIn
);