diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index 14cc237..10129d8 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -368,6 +368,8 @@ extension IORing.Request { .init(core: .write(file: file, buffer: buffer, offset: offset, context: context)) } + @available(*, deprecated, message: "Use the overload accepting UnsafeRawBufferPointer instead.") + @_disfavoredOverload @inlinable public static func write( _ buffer: UnsafeMutableRawBufferPointer, into file: IORing.RegisteredFile, @@ -378,6 +380,8 @@ extension IORing.Request { file: file, buffer: buffer, offset: offset, context: context)) } + @available(*, deprecated, message: "Use the overload accepting UnsafeRawBufferPointer instead.") + @_disfavoredOverload @inlinable public static func write( _ buffer: UnsafeMutableRawBufferPointer, into file: FileDescriptor, @@ -389,6 +393,60 @@ extension IORing.Request { ) } + /// Creates a request to write an unregistered buffer to a registered file. + /// + /// The request does not copy the buffer's contents. Keep its storage alive + /// and unchanged until the operation completes. The write only reads from + /// the buffer; it does not modify its contents. + /// + /// - Parameters: + /// - buffer: The bytes to write. + /// - file: The registered file to write to. + /// - offset: The byte offset in the file at which to begin writing. + /// Ignored for non-seekable files. Defaults to `0`. + /// - context: An application-specific value passed through to the + /// completion event. Defaults to `0`. + /// - Returns: An I/O ring write request. A successful completion's result + /// is the number of bytes written, which may be less than the buffer's count. + @inlinable public static func write( + _ buffer: UnsafeRawBufferPointer, + into file: IORing.RegisteredFile, + at offset: UInt64 = 0, + context: UInt64 = 0 + ) -> IORing.Request { + // The shared request encoder uses a mutable pointer, but writes only read the buffer. + .init(core: .writeUnregisteredSlot( + file: file, buffer: UnsafeMutableRawBufferPointer(mutating: buffer), offset: offset, context: context + )) + } + + /// Creates a request to write an unregistered buffer to a file descriptor. + /// + /// The request does not copy the buffer's contents. Keep its storage alive + /// and unchanged until the operation completes. The write only reads from + /// the buffer; it does not modify its contents. + /// + /// - Parameters: + /// - buffer: The bytes to write. + /// - file: The file descriptor to write to. + /// - offset: The byte offset in the file at which to begin writing. + /// Ignored for non-seekable files. Defaults to `0`. + /// - context: An application-specific value passed through to the + /// completion event. Defaults to `0`. + /// - Returns: An I/O ring write request. A successful completion's result + /// is the number of bytes written, which may be less than the buffer's count. + @inlinable public static func write( + _ buffer: UnsafeRawBufferPointer, + into file: FileDescriptor, + at offset: UInt64 = 0, + context: UInt64 = 0 + ) -> IORing.Request { + // The shared request encoder uses a mutable pointer, but writes only read the buffer. + .init(core: .writeUnregistered( + file: file, buffer: UnsafeMutableRawBufferPointer(mutating: buffer), offset: offset, context: context + )) + } + @inlinable public static func close( _ file: FileDescriptor, context: UInt64 = 0 diff --git a/Tests/SystemTests/IORequestTests.swift b/Tests/SystemTests/IORequestTests.swift index 773a90c..c7f28bb 100644 --- a/Tests/SystemTests/IORequestTests.swift +++ b/Tests/SystemTests/IORequestTests.swift @@ -2,6 +2,7 @@ #if os(Linux) import XCTest +import CSystem #if SYSTEM_PACKAGE @testable import SystemPackage @@ -21,6 +22,76 @@ func requestBytes(_ request: consuming RawIORequest) -> [UInt8] { // that were generated with liburing or manually written out, // which are known to work correctly. final class IORequestTests: XCTestCase { + func testWriteUnregisteredImmutableBuffer() { + let bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeBytes { buffer in + let request = IORing.Request.write( + buffer, into: FileDescriptor(rawValue: 7), at: 17, context: 42 + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.opcode, 23) + XCTAssertEqual(request.rawValue.fd, 7) + XCTAssertEqual(request.rawValue.flags, 0) + XCTAssertEqual(request.rawValue.addr, UInt64(UInt(bitPattern: buffer.baseAddress))) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(request.rawValue.off, 17) + XCTAssertEqual(request.rawValue.user_data, 42) + } + } + + func testWriteUnregisteredImmutableBufferToRegisteredFile() { + let bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeBytes { buffer in + let file = IORing.RegisteredFile(resource: 7, index: 3) + let request = IORing.Request.write( + buffer, into: file, at: 17, context: 42 + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.opcode, 23) + XCTAssertEqual(request.rawValue.fd, 3) + XCTAssertEqual(request.rawValue.flags, 1) + XCTAssertEqual(request.rawValue.addr, UInt64(UInt(bitPattern: buffer.baseAddress))) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(request.rawValue.off, 17) + XCTAssertEqual(request.rawValue.user_data, 42) + } + } + + func testInferredRebasedWriteBuffer() { + var bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeMutableBytes { buffer in + let written = 1 + let file = FileDescriptor(rawValue: 7) + let request = IORing.Request.write( + .init(rebasing: buffer[written...]), into: file, at: UInt64(written) + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + let registered = IORing.Request.write( + .init(rebasing: buffer[written...]), + into: IORing.RegisteredFile(resource: 7, index: 3), at: UInt64(written) + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.len, 2) + XCTAssertEqual(request.rawValue.off, 1) + XCTAssertEqual(registered.rawValue.len, 2) + XCTAssertEqual(registered.rawValue.off, 1) + } + } + + func testMutableWriteFunctionReferences() { + let write: (UnsafeMutableRawBufferPointer, FileDescriptor, UInt64, UInt64) -> IORing.Request = + IORing.Request.write + let writeRegistered: (UnsafeMutableRawBufferPointer, IORing.RegisteredFile, UInt64, UInt64) -> IORing.Request = + IORing.Request.write + var bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeMutableBytes { buffer in + let request = write(buffer, FileDescriptor(rawValue: 7), 17, 42) + .makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + let registered = writeRegistered(buffer, IORing.RegisteredFile(resource: 7, index: 3), 17, 42) + .makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.fd, 7) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(registered.rawValue.fd, 3) + XCTAssertEqual(registered.rawValue.flags, 1) + } + } + func testNop() { let req = IORing.Request.nop().makeRawRequest( pathBuffers: PendingPathBuffers(reservedCapacity: 0)) diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index a13b786..6a51bba 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -126,6 +126,30 @@ final class IORingTests: XCTestCase { return ring } + func testWriteImmutableUnregisteredBuffer() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + let bytes: [UInt8] = [1, 2, 3] + try bytes.withUnsafeBytes { buffer in + var ring = try IORing(queueDepth: 1) + let (readFD, writeFD) = try FileDescriptor.pipe() + defer { + try? readFD.close() + try? writeFD.close() + } + XCTAssertTrue(try ring.submit(linkedRequests: .write(buffer, into: writeFD, context: 42))) + let completion = try ring.blockingConsumeCompletion(timeout: .seconds(1)) + XCTAssertEqual(completion.context, 42) + guard completion.result == 3 else { + XCTFail("Expected three bytes written, got \(completion.result)") + return + } + var received = [UInt8](repeating: 0, count: 3) + let count = try received.withUnsafeMutableBytes { try readFD.read(into: $0) } + XCTAssertEqual(count, 3) + XCTAssertEqual(received, bytes) + } + } + func testUndersizedSubmissionQueue() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring: IORing = try IORing(queueDepth: 1)