Loading...
iokit/Kernel/IOSharedDataQueue.cpp xnu-6153.41.3 xnu-8792.61.2
--- xnu/xnu-6153.41.3/iokit/Kernel/IOSharedDataQueue.cpp
+++ xnu/xnu-8792.61.2/iokit/Kernel/IOSharedDataQueue.cpp
@@ -26,10 +26,13 @@
  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
+#define IOKIT_ENABLE_SHARED_PTR
+
 #include <IOKit/IOSharedDataQueue.h>
 #include <IOKit/IODataQueueShared.h>
 #include <IOKit/IOLib.h>
 #include <IOKit/IOMemoryDescriptor.h>
+#include <libkern/c++/OSSharedPtr.h>
 
 #ifdef enqueue
 #undef enqueue
@@ -43,29 +46,28 @@
 
 OSDefineMetaClassAndStructors(IOSharedDataQueue, IODataQueue)
 
-IOSharedDataQueue *IOSharedDataQueue::withCapacity(UInt32 size)
-{
-	IOSharedDataQueue *dataQueue = new IOSharedDataQueue;
+OSSharedPtr<IOSharedDataQueue>
+IOSharedDataQueue::withCapacity(UInt32 size)
+{
+	OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
 
 	if (dataQueue) {
 		if (!dataQueue->initWithCapacity(size)) {
-			dataQueue->release();
-			dataQueue = NULL;
+			return nullptr;
 		}
 	}
 
 	return dataQueue;
 }
 
-IOSharedDataQueue *
+OSSharedPtr<IOSharedDataQueue>
 IOSharedDataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
 {
-	IOSharedDataQueue *dataQueue = new IOSharedDataQueue;
+	OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
 
 	if (dataQueue) {
 		if (!dataQueue->initWithEntries(numEntries, entrySize)) {
-			dataQueue->release();
-			dataQueue = NULL;
+			return nullptr;
 		}
 	}
 
@@ -77,12 +79,13 @@
 {
 	IODataQueueAppendix *   appendix;
 	vm_size_t               allocSize;
+	kern_return_t           kr;
 
 	if (!super::init()) {
 		return false;
 	}
 
-	_reserved = (ExpansionData *)IOMalloc(sizeof(struct ExpansionData));
+	_reserved = IOMallocType(ExpansionData);
 	if (!_reserved) {
 		return false;
 	}
@@ -97,11 +100,11 @@
 		return false;
 	}
 
-	dataQueue = (IODataQueueMemory *)IOMallocAligned(allocSize, PAGE_SIZE);
-	if (dataQueue == NULL) {
-		return false;
-	}
-	bzero(dataQueue, allocSize);
+	kr = kmem_alloc(kernel_map, (vm_offset_t *)&dataQueue, allocSize,
+	    (kma_flags_t)(KMA_DATA | KMA_ZERO), IOMemoryTag(kernel_map));
+	if (kr != KERN_SUCCESS) {
+		return false;
+	}
 
 	dataQueue->queueSize    = size;
 //  dataQueue->head         = 0;
@@ -115,7 +118,7 @@
 	appendix->version   = 0;
 
 	if (!notifyMsg) {
-		notifyMsg = IOMalloc(sizeof(mach_msg_header_t));
+		notifyMsg = IOMallocType(mach_msg_header_t);
 		if (!notifyMsg) {
 			return false;
 		}
@@ -131,26 +134,27 @@
 IOSharedDataQueue::free()
 {
 	if (dataQueue) {
-		IOFreeAligned(dataQueue, round_page(getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE));
+		kmem_free(kernel_map, (vm_offset_t)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));
+			IOFreeType(notifyMsg, mach_msg_header_t);
 			notifyMsg = NULL;
 		}
 	}
 
 	if (_reserved) {
-		IOFree(_reserved, sizeof(struct ExpansionData));
+		IOFreeType(_reserved, ExpansionData);
 		_reserved = NULL;
 	}
 
 	super::free();
 }
 
-IOMemoryDescriptor *
+OSSharedPtr<IOMemoryDescriptor>
 IOSharedDataQueue::getMemoryDescriptor()
 {
-	IOMemoryDescriptor *descriptor = NULL;
+	OSSharedPtr<IOMemoryDescriptor> descriptor;
 
 	if (dataQueue != NULL) {
 		descriptor = IOMemoryDescriptor::withAddress(dataQueue, getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE, kIODirectionOutIn);
@@ -182,7 +186,7 @@
 		UInt32              headOffset   = dataQueue->head;
 		UInt32              queueSize    = getQueueSize();
 
-		if (headOffset >= queueSize) {
+		if (headOffset > queueSize) {
 			return NULL;
 		}