From a22e5f685866614a027ff4a394e2af71d4b412c8 Mon Sep 17 00:00:00 2001 From: David Smith Date: Thu, 13 Feb 2025 22:09:42 +0000 Subject: [PATCH] Add a version of registerBuffers that isn't varargs --- Sources/System/IORing.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sources/System/IORing.swift b/Sources/System/IORing.swift index fb0c20a..b718991 100644 --- a/Sources/System/IORing.swift +++ b/Sources/System/IORing.swift @@ -496,16 +496,18 @@ public struct IORing: ~Copyable { try handleRegistrationResult(result) } - public mutating func registerFileSlots(count: Int) -> RegisteredResources { + public mutating func registerFileSlots(count: Int) -> RegisteredResources< + IORingFileSlot.Resource + > { precondition(_registeredFiles == nil) precondition(count < UInt32.max) - let files = [UInt32](repeating: UInt32.max, count: count) + let files = [UInt32](repeating: UInt32.max, count: count) let regResult = files.withUnsafeBufferPointer { bPtr in io_uring_register( self.ringDescriptor, IORING_REGISTER_FILES, - UnsafeMutableRawPointer(mutating:bPtr.baseAddress!), + UnsafeMutableRawPointer(mutating: bPtr.baseAddress!), UInt32(truncatingIfNeeded: count) ) } @@ -523,7 +525,9 @@ public struct IORing: ~Copyable { RegisteredResources(resources: _registeredFiles ?? []) } - public mutating func registerBuffers(_ buffers: UnsafeMutableRawBufferPointer...) -> RegisteredResources { + public mutating func registerBuffers(_ buffers: some Collection) + -> RegisteredResources + { precondition(buffers.count < UInt32.max) precondition(_registeredBuffers == nil) //TODO: check if io_uring has preconditions it needs for the buffers (e.g. alignment) @@ -542,6 +546,12 @@ public struct IORing: ~Copyable { return registeredBuffers } + public mutating func registerBuffers(_ buffers: UnsafeMutableRawBufferPointer...) + -> RegisteredResources + { + registerBuffers(buffers) + } + public struct RegisteredResources: RandomAccessCollection { let resources: [T] @@ -588,7 +598,8 @@ public struct IORing: ~Copyable { raw, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries) } _writeRequest( - last.makeRawRequest(), ring: &submissionRing, submissionQueueEntries: submissionQueueEntries) + last.makeRawRequest(), ring: &submissionRing, + submissionQueueEntries: submissionQueueEntries) } //@inlinable //TODO: make sure the array allocation gets optimized out...