From 8a234894dd7019570b882d0317c01856bb3a4152 Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Wed, 1 Jan 2025 22:32:12 +0000 Subject: [PATCH] distinguish between read and write count in writeReadFixed() --- Sources/IORing/IORing.swift | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Sources/IORing/IORing.swift b/Sources/IORing/IORing.swift index 7677843..ca7d795 100644 --- a/Sources/IORing/IORing.swift +++ b/Sources/IORing/IORing.swift @@ -850,34 +850,39 @@ public extension IORing { // happen func writeReadFixed( _ data: inout [UInt8], - count: Int? = nil, + writeCount: Int? = nil, + readCount: Int? = nil, offset: Int = -1, bufferIndex: UInt16, bufferOffset: Int = 0, fd: FileDescriptorRepresentable ) async throws { guard let fixedBuffers else { throw Errno.invalidArgument } - let count = count ?? fixedBuffers.count - guard count <= data.count else { throw Errno.outOfRange } + + let writeCount = writeCount ?? fixedBuffers.size + let readCount = readCount ?? fixedBuffers.size + + guard writeCount <= data.count, readCount <= data.count else { throw Errno.outOfRange } + try fixedBuffers.validate( - count: count, + count: max(readCount, writeCount), bufferIndex: bufferIndex, bufferOffset: bufferOffset ) let address = try fixedBuffers.unsafeMutableRawPointer( at: bufferIndex, - count: count, + count: writeCount, bufferOffset: bufferOffset ) data.withUnsafeBytes { bytes in - _ = memcpy(address, UnsafeRawPointer(bytes.baseAddress!), count) + _ = memcpy(address, UnsafeRawPointer(bytes.baseAddress!), writeCount) } let result = try await withSubmissionGroup { (group: SubmissionGroup) in let _: SingleshotSubmission = try await io_uring_op_write_fixed( fd: fd, - count: count, + count: writeCount, offset: offset, bufferIndex: bufferIndex, bufferOffset: 0, @@ -887,7 +892,7 @@ public extension IORing { let _: SingleshotSubmission = try await io_uring_op_read_fixed( fd: fd, - count: count, + count: readCount, offset: offset, bufferIndex: bufferIndex, bufferOffset: 0, @@ -900,7 +905,7 @@ public extension IORing { } data.withUnsafeMutableBytes { bytes in - _ = memcpy(UnsafeMutableRawPointer(bytes.baseAddress!), address, count) + _ = memcpy(UnsafeMutableRawPointer(bytes.baseAddress!), address, readCount) } }