Skip to content

Commit 91c8704

Browse files
Merge branch 'master' into gazeCS
2 parents 6119b93 + b20befb commit 91c8704

24 files changed

+2142
-449
lines changed

Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs

Lines changed: 826 additions & 0 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit.HighPerformance/Extensions/IMemoryOwnerExtensions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Buffers;
67
using System.Diagnostics.Contracts;
78
using System.IO;
89
using System.Runtime.CompilerServices;
9-
using Microsoft.Toolkit.HighPerformance.Streams;
10+
using MemoryStream = Microsoft.Toolkit.HighPerformance.Streams.MemoryStream;
1011

1112
namespace Microsoft.Toolkit.HighPerformance.Extensions
1213
{
@@ -18,17 +19,18 @@ public static class IMemoryOwnerExtensions
1819
/// <summary>
1920
/// Returns a <see cref="Stream"/> wrapping the contents of the given <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.
2021
/// </summary>
21-
/// <param name="memory">The input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.</param>
22-
/// <returns>A <see cref="Stream"/> wrapping the data within <paramref name="memory"/>.</returns>
22+
/// <param name="memoryOwner">The input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.</param>
23+
/// <returns>A <see cref="Stream"/> wrapping the data within <paramref name="memoryOwner"/>.</returns>
2324
/// <remarks>
2425
/// The caller does not need to track the lifetime of the input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/>
2526
/// instance, as the returned <see cref="Stream"/> will take care of disposing that buffer when it is closed.
2627
/// </remarks>
28+
/// <exception cref="ArgumentException">Thrown when <paramref name="memoryOwner"/> has an invalid data store.</exception>
2729
[Pure]
2830
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
public static Stream AsStream(this IMemoryOwner<byte> memory)
31+
public static Stream AsStream(this IMemoryOwner<byte> memoryOwner)
3032
{
31-
return new IMemoryOwnerStream(memory);
33+
return MemoryStream.Create(memoryOwner);
3234
}
3335
}
3436
}

Microsoft.Toolkit.HighPerformance/Extensions/MemoryExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public static class MemoryExtensions
2626
/// In particular, the caller must ensure that the target buffer is not disposed as long
2727
/// as the returned <see cref="Stream"/> is in use, to avoid unexpected issues.
2828
/// </remarks>
29+
/// <exception cref="ArgumentException">Thrown when <paramref name="memory"/> has an invalid data store.</exception>
2930
[Pure]
3031
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3132
public static Stream AsStream(this Memory<byte> memory)
3233
{
33-
return new MemoryStream(memory);
34+
return MemoryStream.Create(memory, false);
3435
}
3536
}
3637
}

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlyMemoryExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System;
66
using System.Diagnostics.Contracts;
77
using System.IO;
8-
using System.Runtime.CompilerServices;
98
using MemoryStream = Microsoft.Toolkit.HighPerformance.Streams.MemoryStream;
109

1110
namespace Microsoft.Toolkit.HighPerformance.Extensions
@@ -26,11 +25,11 @@ public static class ReadOnlyMemoryExtensions
2625
/// In particular, the caller must ensure that the target buffer is not disposed as long
2726
/// as the returned <see cref="Stream"/> is in use, to avoid unexpected issues.
2827
/// </remarks>
28+
/// <exception cref="ArgumentException">Thrown when <paramref name="memory"/> has an invalid data store.</exception>
2929
[Pure]
30-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3130
public static Stream AsStream(this ReadOnlyMemory<byte> memory)
3231
{
33-
return new MemoryStream(memory);
32+
return MemoryStream.Create(memory, true);
3433
}
3534
}
3635
}

Microsoft.Toolkit.HighPerformance/Streams/IMemoryOwnerStream.cs renamed to Microsoft.Toolkit.HighPerformance/Streams/IMemoryOwnerStream{TSource}.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Buffers;
67
using System.IO;
78

@@ -10,29 +11,32 @@ namespace Microsoft.Toolkit.HighPerformance.Streams
1011
/// <summary>
1112
/// A <see cref="Stream"/> implementation wrapping an <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.
1213
/// </summary>
13-
internal sealed class IMemoryOwnerStream : MemoryStream
14+
/// <typeparam name="TSource">The type of source to use for the underlying data.</typeparam>
15+
internal sealed class IMemoryOwnerStream<TSource> : MemoryStream<TSource>
16+
where TSource : struct, ISpanOwner
1417
{
1518
/// <summary>
16-
/// The <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance currently in use.
19+
/// The <see cref="IDisposable"/> instance currently in use.
1720
/// </summary>
18-
private readonly IMemoryOwner<byte> memory;
21+
private readonly IDisposable disposable;
1922

2023
/// <summary>
21-
/// Initializes a new instance of the <see cref="IMemoryOwnerStream"/> class.
24+
/// Initializes a new instance of the <see cref="IMemoryOwnerStream{TSource}"/> class.
2225
/// </summary>
23-
/// <param name="memory">The input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance to use.</param>
24-
public IMemoryOwnerStream(IMemoryOwner<byte> memory)
25-
: base(memory.Memory)
26+
/// <param name="source">The input <typeparamref name="TSource"/> instance to use.</param>
27+
/// <param name="disposable">The <see cref="IDisposable"/> instance currently in use.</param>
28+
public IMemoryOwnerStream(TSource source, IDisposable disposable)
29+
: base(source, false)
2630
{
27-
this.memory = memory;
31+
this.disposable = disposable;
2832
}
2933

3034
/// <inheritdoc/>
3135
protected override void Dispose(bool disposing)
3236
{
3337
base.Dispose(disposing);
3438

35-
this.memory.Dispose();
39+
this.disposable.Dispose();
3640
}
3741
}
3842
}

Microsoft.Toolkit.HighPerformance/Streams/MemoryStream.ThrowExceptions.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -8,16 +8,41 @@
88
namespace Microsoft.Toolkit.HighPerformance.Streams
99
{
1010
/// <summary>
11-
/// A <see cref="Stream"/> implementation wrapping a <see cref="Memory{T}"/> or <see cref="ReadOnlyMemory{T}"/> instance.
11+
/// A factory class to produce <see cref="MemoryStream{TSource}"/> instances.
1212
/// </summary>
13-
internal partial class MemoryStream
13+
internal static partial class MemoryStream
1414
{
15+
/// <summary>
16+
/// Throws an <see cref="ArgumentException"/> when trying to write too many bytes to the target stream.
17+
/// </summary>
18+
public static void ThrowArgumentExceptionForEndOfStreamOnWrite()
19+
{
20+
throw new ArgumentException("The current stream can't contain the requested input data.");
21+
}
22+
23+
/// <summary>
24+
/// Throws a <see cref="NotSupportedException"/> when trying to set the length of the stream.
25+
/// </summary>
26+
public static void ThrowNotSupportedExceptionForSetLength()
27+
{
28+
throw new NotSupportedException("Setting the length is not supported for this stream.");
29+
}
30+
31+
/// <summary>
32+
/// Throws an <see cref="ArgumentException"/> when using an invalid seek mode.
33+
/// </summary>
34+
/// <returns>Nothing, as this method throws unconditionally.</returns>
35+
public static long ThrowArgumentExceptionForSeekOrigin()
36+
{
37+
throw new ArgumentException("The input seek mode is not valid.", "origin");
38+
}
39+
1540
/// <summary>
1641
/// Throws an <see cref="ArgumentOutOfRangeException"/> when setting the <see cref="Stream.Position"/> property.
1742
/// </summary>
1843
private static void ThrowArgumentOutOfRangeExceptionForPosition()
1944
{
20-
throw new ArgumentOutOfRangeException(nameof(Position), "The value for the property was not in the valid range.");
45+
throw new ArgumentOutOfRangeException(nameof(Stream.Position), "The value for the property was not in the valid range.");
2146
}
2247

2348
/// <summary>
@@ -60,37 +85,12 @@ private static void ThrowNotSupportedExceptionForCanWrite()
6085
throw new NotSupportedException("The current stream doesn't support writing.");
6186
}
6287

63-
/// <summary>
64-
/// Throws an <see cref="ArgumentException"/> when trying to write too many bytes to the target stream.
65-
/// </summary>
66-
private static void ThrowArgumentExceptionForEndOfStreamOnWrite()
67-
{
68-
throw new ArgumentException("The current stream can't contain the requested input data.");
69-
}
70-
71-
/// <summary>
72-
/// Throws a <see cref="NotSupportedException"/> when trying to set the length of the stream.
73-
/// </summary>
74-
private static void ThrowNotSupportedExceptionForSetLength()
75-
{
76-
throw new NotSupportedException("Setting the length is not supported for this stream.");
77-
}
78-
79-
/// <summary>
80-
/// Throws an <see cref="ArgumentException"/> when using an invalid seek mode.
81-
/// </summary>
82-
/// <returns>Nothing, as this method throws unconditionally.</returns>
83-
private static long ThrowArgumentExceptionForSeekOrigin()
84-
{
85-
throw new ArgumentException("The input seek mode is not valid.", "origin");
86-
}
87-
8888
/// <summary>
8989
/// Throws an <see cref="ObjectDisposedException"/> when using a disposed <see cref="Stream"/> instance.
9090
/// </summary>
9191
private static void ThrowObjectDisposedException()
9292
{
93-
throw new ObjectDisposedException(nameof(memory), "The current stream has already been disposed");
93+
throw new ObjectDisposedException("source", "The current stream has already been disposed");
9494
}
9595
}
9696
}

Microsoft.Toolkit.HighPerformance/Streams/MemoryStream.Validate.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -8,17 +8,17 @@
88
namespace Microsoft.Toolkit.HighPerformance.Streams
99
{
1010
/// <summary>
11-
/// A <see cref="Stream"/> implementation wrapping a <see cref="System.Memory{T}"/> or <see cref="System.ReadOnlyMemory{T}"/> instance.
11+
/// A factory class to produce <see cref="MemoryStream{TSource}"/> instances.
1212
/// </summary>
13-
internal partial class MemoryStream
13+
internal static partial class MemoryStream
1414
{
1515
/// <summary>
1616
/// Validates the <see cref="Stream.Position"/> argument.
1717
/// </summary>
1818
/// <param name="position">The new <see cref="Stream.Position"/> value being set.</param>
1919
/// <param name="length">The maximum length of the target <see cref="Stream"/>.</param>
2020
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21-
private static void ValidatePosition(long position, int length)
21+
public static void ValidatePosition(long position, int length)
2222
{
2323
if ((ulong)position >= (ulong)length)
2424
{
@@ -33,7 +33,7 @@ private static void ValidatePosition(long position, int length)
3333
/// <param name="offset">The offset within <paramref name="buffer"/>.</param>
3434
/// <param name="count">The number of elements to process within <paramref name="buffer"/>.</param>
3535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36-
private static void ValidateBuffer(byte[]? buffer, int offset, int count)
36+
public static void ValidateBuffer(byte[]? buffer, int offset, int count)
3737
{
3838
if (buffer is null)
3939
{
@@ -57,24 +57,24 @@ private static void ValidateBuffer(byte[]? buffer, int offset, int count)
5757
}
5858

5959
/// <summary>
60-
/// Validates the <see cref="CanWrite"/> property.
60+
/// Validates the <see cref="MemoryStream{TSource}.CanWrite"/> property.
6161
/// </summary>
6262
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
private void ValidateCanWrite()
63+
public static void ValidateCanWrite(bool canWrite)
6464
{
65-
if (!CanWrite)
65+
if (!canWrite)
6666
{
6767
ThrowNotSupportedExceptionForCanWrite();
6868
}
6969
}
7070

7171
/// <summary>
72-
/// Validates that the current instance hasn't been disposed.
72+
/// Validates that a given <see cref="Stream"/> instance hasn't been disposed.
7373
/// </summary>
7474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
75-
private void ValidateDisposed()
75+
public static void ValidateDisposed(bool disposed)
7676
{
77-
if (this.disposed)
77+
if (disposed)
7878
{
7979
ThrowObjectDisposedException();
8080
}

0 commit comments

Comments
 (0)