Skip to content

Commit 49e504d

Browse files
committed
Added T[] and MemoryManager<T> owner types
1 parent 1b08bd6 commit 49e504d

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
using Microsoft.Toolkit.HighPerformance.Extensions;
5+
6+
namespace Microsoft.Toolkit.HighPerformance.Streams
7+
{
8+
/// <summary>
9+
/// An <see cref="ISpanOwner"/> implementation wrapping an array.
10+
/// </summary>
11+
internal readonly struct ArrayOwner : ISpanOwner
12+
{
13+
/// <summary>
14+
/// The wrapped <see cref="byte"/> array.
15+
/// </summary>
16+
private readonly byte[] array;
17+
18+
/// <summary>
19+
/// The starting offset within <see cref="array"/>.
20+
/// </summary>
21+
private readonly int offset;
22+
23+
/// <summary>
24+
/// The usable length within <see cref="array"/>.
25+
/// </summary>
26+
private readonly int length;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ArrayOwner"/> struct.
30+
/// </summary>
31+
/// <param name="array">The wrapped <see cref="byte"/> array.</param>
32+
/// <param name="offset">The starting offset within <paramref name="array"/>.</param>
33+
/// <param name="length">The usable length within <paramref name="array"/>.</param>
34+
public ArrayOwner(byte[] array, int offset, int length)
35+
{
36+
this.array = array;
37+
this.offset = offset;
38+
this.length = length;
39+
}
40+
41+
/// <inheritdoc/>
42+
public int Length
43+
{
44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
45+
get => this.length;
46+
}
47+
48+
/// <inheritdoc/>
49+
public Span<byte> Span
50+
{
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
get
53+
{
54+
#if SPAN_RUNTIME_SUPPORT
55+
ref byte r0 = ref this.array.DangerousGetReferenceAt(this.offset);
56+
57+
return MemoryMarshal.CreateSpan(ref r0, this.length);
58+
#else
59+
return this.array.AsSpan(this.offset, this.length);
60+
#endif
61+
}
62+
}
63+
}
64+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Microsoft.Toolkit.HighPerformance.Streams
4+
{
5+
internal interface ISpanOwner
6+
{
7+
int Length { get; }
8+
9+
Span<byte> Span { get; }
10+
}
11+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using Microsoft.Toolkit.HighPerformance.Extensions;
6+
7+
namespace Microsoft.Toolkit.HighPerformance.Streams
8+
{
9+
/// <summary>
10+
/// An <see cref="ISpanOwner"/> implementation wrapping a <see cref="MemoryManager{T}"/> of <see cref="byte"/> instance.
11+
/// </summary>
12+
internal readonly struct MemoryManagerOwner : ISpanOwner
13+
{
14+
/// <summary>
15+
/// The wrapped <see cref="MemoryManager{T}"/> instance.
16+
/// </summary>
17+
private readonly MemoryManager<byte> memoryManager;
18+
19+
/// <summary>
20+
/// The starting offset within <see cref="memoryManager"/>.
21+
/// </summary>
22+
private readonly int offset;
23+
24+
/// <summary>
25+
/// The usable length within <see cref="memoryManager"/>.
26+
/// </summary>
27+
private readonly int length;
28+
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="MemoryManagerOwner"/> struct.
31+
/// </summary>
32+
/// <param name="memoryManager">The wrapped <see cref="MemoryManager{T}"/> instance.</param>
33+
/// <param name="offset">The starting offset within <paramref name="memoryManager"/>.</param>
34+
/// <param name="length">The usable length within <paramref name="memoryManager"/>.</param>
35+
public MemoryManagerOwner(MemoryManager<byte> memoryManager, int offset, int length)
36+
{
37+
this.memoryManager = memoryManager;
38+
this.offset = offset;
39+
this.length = length;
40+
}
41+
42+
/// <inheritdoc/>
43+
public int Length
44+
{
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46+
get => this.length;
47+
}
48+
49+
/// <inheritdoc/>
50+
public Span<byte> Span
51+
{
52+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53+
get
54+
{
55+
#if SPAN_RUNTIME_SUPPORT
56+
ref byte r0 = ref this.memoryManager.GetSpan().DangerousGetReferenceAt(this.offset);
57+
58+
return MemoryMarshal.CreateSpan(ref r0, this.length);
59+
#else
60+
return this.memoryManager.GetSpan();
61+
#endif
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)