IOSharedDataQueue.cpp [plain text]
#include <IOKit/IOSharedDataQueue.h>
#include <IOKit/IODataQueueShared.h>
#include <IOKit/IOLib.h>
#include <IOKit/IOMemoryDescriptor.h>
#ifdef enqueue
#undef enqueue
#endif
#ifdef dequeue
#undef dequeue
#endif
#define super IODataQueue
OSDefineMetaClassAndStructors(IOSharedDataQueue, IODataQueue)
IOSharedDataQueue *IOSharedDataQueue::withCapacity(UInt32 size)
{
IOSharedDataQueue *dataQueue = new IOSharedDataQueue;
if (dataQueue) {
if (!dataQueue->initWithCapacity(size)) {
dataQueue->release();
dataQueue = NULL;
}
}
return dataQueue;
}
IOSharedDataQueue *
IOSharedDataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
{
IOSharedDataQueue *dataQueue = new IOSharedDataQueue;
if (dataQueue) {
if (!dataQueue->initWithEntries(numEntries, entrySize)) {
dataQueue->release();
dataQueue = NULL;
}
}
return dataQueue;
}
Boolean
IOSharedDataQueue::initWithCapacity(UInt32 size)
{
IODataQueueAppendix * appendix;
vm_size_t allocSize;
if (!super::init()) {
return false;
}
_reserved = (ExpansionData *)IOMalloc(sizeof(struct ExpansionData));
if (!_reserved) {
return false;
}
if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE - DATA_QUEUE_MEMORY_APPENDIX_SIZE) {
return false;
}
allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE);
if (allocSize < size) {
return false;
}
dataQueue = (IODataQueueMemory *)IOMallocAligned(allocSize, PAGE_SIZE);
if (dataQueue == NULL) {
return false;
}
bzero(dataQueue, allocSize);
dataQueue->queueSize = size;
if (!setQueueSize(size)) {
return false;
}
appendix = (IODataQueueAppendix *)((UInt8 *)dataQueue + size + DATA_QUEUE_MEMORY_HEADER_SIZE);
appendix->version = 0;
if (!notifyMsg) {
notifyMsg = IOMalloc(sizeof(mach_msg_header_t));
if (!notifyMsg) {
return false;
}
}
bzero(notifyMsg, sizeof(mach_msg_header_t));
setNotificationPort(MACH_PORT_NULL);
return true;
}
void
IOSharedDataQueue::free()
{
if (dataQueue) {
IOFreeAligned(dataQueue, round_page(getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE));
dataQueue = NULL;
if (notifyMsg) {
IOFree(notifyMsg, sizeof(mach_msg_header_t));
notifyMsg = NULL;
}
}
if (_reserved) {
IOFree(_reserved, sizeof(struct ExpansionData));
_reserved = NULL;
}
super::free();
}
IOMemoryDescriptor *
IOSharedDataQueue::getMemoryDescriptor()
{
IOMemoryDescriptor *descriptor = NULL;
if (dataQueue != NULL) {
descriptor = IOMemoryDescriptor::withAddress(dataQueue, getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE, kIODirectionOutIn);
}
return descriptor;
}
IODataQueueEntry *
IOSharedDataQueue::peek()
{
IODataQueueEntry *entry = NULL;
UInt32 headOffset;
UInt32 tailOffset;
if (!dataQueue) {
return NULL;
}
headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
if (headOffset != tailOffset) {
volatile IODataQueueEntry * head = NULL;
UInt32 headSize = 0;
UInt32 headOffset = dataQueue->head;
UInt32 queueSize = getQueueSize();
if (headOffset >= queueSize) {
return NULL;
}
head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
headSize = head->size;
if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
(headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
(headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
(headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
entry = dataQueue->queue;
} else {
entry = (IODataQueueEntry *)head;
}
}
return entry;
}
Boolean
IOSharedDataQueue::enqueue(void * data, UInt32 dataSize)
{
UInt32 head;
UInt32 tail;
UInt32 newTail;
const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
IODataQueueEntry * entry;
tail = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_RELAXED);
head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_ACQUIRE);
if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
return false;
}
if (getQueueSize() < tail || getQueueSize() < head) {
return false;
}
if (tail >= head) {
if ((entrySize <= UINT32_MAX - tail) &&
((tail + entrySize) <= getQueueSize())) {
entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
entry->size = dataSize;
__nochk_memcpy(&entry->data, data, dataSize);
newTail = tail + entrySize;
} else if (head > entrySize) {
dataQueue->queue->size = dataSize;
if ((getQueueSize() - tail) >= DATA_QUEUE_ENTRY_HEADER_SIZE) {
((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
}
__nochk_memcpy(&dataQueue->queue->data, data, dataSize);
newTail = entrySize;
} else {
return false; }
} else {
if ((head - tail) > entrySize) {
entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
entry->size = dataSize;
__nochk_memcpy(&entry->data, data, dataSize);
newTail = tail + entrySize;
} else {
return false; }
}
__c11_atomic_store((_Atomic UInt32 *)&dataQueue->tail, newTail, __ATOMIC_RELEASE);
if (tail != head) {
__c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
}
if (tail == head) {
sendDataAvailableNotification();
}
return true;
}
Boolean
IOSharedDataQueue::dequeue(void *data, UInt32 *dataSize)
{
Boolean retVal = TRUE;
volatile IODataQueueEntry * entry = NULL;
UInt32 entrySize = 0;
UInt32 headOffset = 0;
UInt32 tailOffset = 0;
UInt32 newHeadOffset = 0;
if (!dataQueue || (data && !dataSize)) {
return false;
}
headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
if (headOffset != tailOffset) {
volatile IODataQueueEntry * head = NULL;
UInt32 headSize = 0;
UInt32 queueSize = getQueueSize();
if (headOffset > queueSize) {
return false;
}
head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
headSize = head->size;
if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
(headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
(headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
(headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
entry = dataQueue->queue;
entrySize = entry->size;
if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
return false;
}
newHeadOffset = entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
} else {
entry = head;
entrySize = entry->size;
if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headOffset) ||
(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE + headOffset > queueSize)) {
return false;
}
newHeadOffset = headOffset + entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
}
} else {
return false;
}
if (data) {
if (entrySize > *dataSize) {
return false;
}
__nochk_memcpy(data, (void *)entry->data, entrySize);
*dataSize = entrySize;
}
__c11_atomic_store((_Atomic UInt32 *)&dataQueue->head, newHeadOffset, __ATOMIC_RELEASE);
if (newHeadOffset == tailOffset) {
__c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
}
return retVal;
}
UInt32
IOSharedDataQueue::getQueueSize()
{
if (!_reserved) {
return 0;
}
return _reserved->queueSize;
}
Boolean
IOSharedDataQueue::setQueueSize(UInt32 size)
{
if (!_reserved) {
return false;
}
_reserved->queueSize = size;
return true;
}
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 0);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 1);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 2);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 3);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 4);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 5);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 6);
OSMetaClassDefineReservedUnused(IOSharedDataQueue, 7);