Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit 891ad63

Browse files
authored
Merge pull request #585 from qmfrederik/features/winusb-writepipe-readonlymemory
WinUsb_WritePipeAsync: Use ReadOnlyMemory<byte> instead of Memory<byte>
2 parents bc93e82 + 218b45e commit 891ad63

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

src/WinUsb/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static PInvoke.WinUsb.WinUsb_ReadPipeAsync(PInvoke.WinUsb.SafeUsbHandle interfac
2323
static PInvoke.WinUsb.WinUsb_WritePipe(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte pipeID, System.IntPtr buffer, int bufferLength, out int lengthTransferred, System.IntPtr overlapped) -> bool
2424
static PInvoke.WinUsb.WinUsb_WritePipe(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte pipeID, System.ReadOnlySpan<byte> buffer, int bufferLength, out int lengthTransferred, System.Threading.NativeOverlapped? overlapped) -> bool
2525
static PInvoke.WinUsb.WinUsb_WritePipe(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte pipeID, byte[] buffer, int bufferLength, out int lengthTransferred, System.Threading.NativeOverlapped? overlapped) -> bool
26-
static PInvoke.WinUsb.WinUsb_WritePipeAsync(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte pipeID, System.Memory<byte> buffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<int>
26+
static PInvoke.WinUsb.WinUsb_WritePipeAsync(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte pipeID, System.ReadOnlyMemory<byte> buffer, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.ValueTask<int>
2727
static extern PInvoke.WinUsb.WinUsb_AbortPipe(PInvoke.WinUsb.SafeUsbHandle handle, byte pipeID) -> bool
2828
static extern PInvoke.WinUsb.WinUsb_Initialize(PInvoke.Kernel32.SafeObjectHandle deviceHandle, out PInvoke.WinUsb.SafeUsbHandle interfaceHandle) -> bool
2929
static extern PInvoke.WinUsb.WinUsb_QueryPipe(PInvoke.WinUsb.SafeUsbHandle interfaceHandle, byte alternateInterfaceNumber, byte pipeIndex, PInvoke.WinUsb.WINUSB_PIPE_INFORMATION* pipeInformation) -> bool

src/WinUsb/WinUsb+WinUsbOverlapped.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public static partial class WinUsb
1919
/// </summary>
2020
private class WinUsbOverlapped : Overlapped
2121
{
22-
private readonly Memory<byte> buffer;
2322
private readonly SafeUsbHandle handle;
2423
private readonly byte pipeID;
2524
private readonly CancellationToken cancellationToken;
@@ -41,18 +40,18 @@ private class WinUsbOverlapped : Overlapped
4140
/// <param name="pipeID">
4241
/// The ID of the pipe on which the I/O is being performed.
4342
/// </param>
44-
/// <param name="buffer">
45-
/// The buffer which is used by the I/O operation. This buffer will be pinned for the duration of
43+
/// <param name="bufferHandle">
44+
/// A handle to the buffer which is used by the I/O operation. This buffer will be pinned for the duration of
4645
/// the operation.
4746
/// </param>
4847
/// <param name="cancellationToken">
4948
/// A <see cref="CancellationToken"/> which can be used to cancel the overlapped I/O.
5049
/// </param>
51-
public WinUsbOverlapped(SafeUsbHandle handle, byte pipeID, Memory<byte> buffer, CancellationToken cancellationToken)
50+
public WinUsbOverlapped(SafeUsbHandle handle, byte pipeID, MemoryHandle bufferHandle, CancellationToken cancellationToken)
5251
{
5352
this.handle = handle ?? throw new ArgumentNullException(nameof(handle));
5453
this.pipeID = pipeID;
55-
this.buffer = buffer;
54+
this.BufferHandle = bufferHandle;
5655
this.cancellationToken = cancellationToken;
5756
}
5857

@@ -84,8 +83,6 @@ public WinUsbOverlapped(SafeUsbHandle handle, byte pipeID, Memory<byte> buffer,
8483
/// </returns>
8584
internal unsafe NativeOverlapped* Pack()
8685
{
87-
this.BufferHandle = this.buffer.Pin();
88-
8986
this.native = this.Pack(
9087
this.DeviceIOControlCompletionCallback,
9188
null);

src/WinUsb/WinUsb.Helpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static partial class WinUsb
3434
/// </returns>
3535
public static unsafe ValueTask<int> WinUsb_ReadPipeAsync(SafeUsbHandle interfaceHandle, byte pipeID, Memory<byte> buffer, CancellationToken cancellationToken)
3636
{
37-
var overlapped = new WinUsbOverlapped(interfaceHandle, pipeID, buffer, cancellationToken);
37+
var overlapped = new WinUsbOverlapped(interfaceHandle, pipeID, buffer.Pin(), cancellationToken);
3838
var nativeOverlapped = overlapped.Pack();
3939

4040
if (WinUsb_ReadPipe(
@@ -87,9 +87,9 @@ public static unsafe ValueTask<int> WinUsb_ReadPipeAsync(SafeUsbHandle interface
8787
/// <returns>
8888
/// A <see cref="ValueTask"/> which represents the asynchronous operation, and returns the number of bytes transferred.
8989
/// </returns>
90-
public static unsafe ValueTask<int> WinUsb_WritePipeAsync(SafeUsbHandle interfaceHandle, byte pipeID, Memory<byte> buffer, CancellationToken cancellationToken)
90+
public static unsafe ValueTask<int> WinUsb_WritePipeAsync(SafeUsbHandle interfaceHandle, byte pipeID, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
9191
{
92-
var overlapped = new WinUsbOverlapped(interfaceHandle, pipeID, buffer, cancellationToken);
92+
var overlapped = new WinUsbOverlapped(interfaceHandle, pipeID, buffer.Pin(), cancellationToken);
9393
var nativeOverlapped = overlapped.Pack();
9494

9595
if (WinUsb_WritePipe(

0 commit comments

Comments
 (0)