|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Buffers; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using Microsoft.Toolkit.HighPerformance.Buffers.Internals.Interfaces; |
| 10 | +using Microsoft.Toolkit.HighPerformance.Extensions; |
| 11 | +using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers; |
| 12 | + |
| 13 | +namespace Microsoft.Toolkit.HighPerformance.Buffers.Internals |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// A custom <see cref="MemoryManager{T}"/> that casts data from a <typeparamref name="TFrom"/> array, to <typeparamref name="TTo"/> values. |
| 17 | + /// </summary> |
| 18 | + /// <typeparam name="TFrom">The source type of items to read.</typeparam> |
| 19 | + /// <typeparam name="TTo">The target type to cast the source items to.</typeparam> |
| 20 | + internal sealed class ArrayMemoryManager<TFrom, TTo> : MemoryManager<TTo>, IMemoryManager |
| 21 | + where TFrom : unmanaged |
| 22 | + where TTo : unmanaged |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// The source <typeparamref name="TFrom"/> array to read data from. |
| 26 | + /// </summary> |
| 27 | + private readonly TFrom[] array; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The starting offset within <see name="array"/>. |
| 31 | + /// </summary> |
| 32 | + private readonly int offset; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The original used length for <see name="array"/>. |
| 36 | + /// </summary> |
| 37 | + private readonly int length; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Initializes a new instance of the <see cref="ArrayMemoryManager{TFrom, TTo}"/> class. |
| 41 | + /// </summary> |
| 42 | + /// <param name="array">The source <typeparamref name="TFrom"/> array to read data from.</param> |
| 43 | + /// <param name="offset">The starting offset within <paramref name="array"/>.</param> |
| 44 | + /// <param name="length">The original used length for <paramref name="array"/>.</param> |
| 45 | + public ArrayMemoryManager(TFrom[] array, int offset, int length) |
| 46 | + { |
| 47 | + this.array = array; |
| 48 | + this.offset = offset; |
| 49 | + this.length = length; |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + public override Span<TTo> GetSpan() |
| 54 | + { |
| 55 | +#if SPAN_RUNTIME_SUPPORT |
| 56 | + ref TFrom r0 = ref this.array.DangerousGetReferenceAt(this.offset); |
| 57 | + ref TTo r1 = ref Unsafe.As<TFrom, TTo>(ref r0); |
| 58 | + int length = RuntimeHelpers.ConvertLength<TFrom, TTo>(this.length); |
| 59 | + |
| 60 | + return MemoryMarshal.CreateSpan(ref r1, length); |
| 61 | +#else |
| 62 | + Span<TFrom> span = this.array.AsSpan(this.offset, this.length); |
| 63 | + |
| 64 | + // We rely on MemoryMarshal.Cast here to deal with calculating the effective |
| 65 | + // size of the new span to return. This will also make the behavior consistent |
| 66 | + // for users that are both using this type as well as casting spans directly. |
| 67 | + return MemoryMarshal.Cast<TFrom, TTo>(span); |
| 68 | +#endif |
| 69 | + } |
| 70 | + |
| 71 | + /// <inheritdoc/> |
| 72 | + public override unsafe MemoryHandle Pin(int elementIndex = 0) |
| 73 | + { |
| 74 | + if ((uint)elementIndex >= (uint)(this.length * Unsafe.SizeOf<TFrom>() / Unsafe.SizeOf<TTo>())) |
| 75 | + { |
| 76 | + ThrowArgumentOutOfRangeExceptionForInvalidIndex(); |
| 77 | + } |
| 78 | + |
| 79 | + int |
| 80 | + bytePrefix = this.offset * Unsafe.SizeOf<TFrom>(), |
| 81 | + byteSuffix = elementIndex * Unsafe.SizeOf<TTo>(), |
| 82 | + byteOffset = bytePrefix + byteSuffix; |
| 83 | + |
| 84 | + GCHandle handle = GCHandle.Alloc(this.array, GCHandleType.Pinned); |
| 85 | + |
| 86 | + ref TFrom r0 = ref this.array.DangerousGetReference(); |
| 87 | + ref byte r1 = ref Unsafe.As<TFrom, byte>(ref r0); |
| 88 | + ref byte r2 = ref Unsafe.Add(ref r1, byteOffset); |
| 89 | + void* pi = Unsafe.AsPointer(ref r2); |
| 90 | + |
| 91 | + return new MemoryHandle(pi, handle); |
| 92 | + } |
| 93 | + |
| 94 | + /// <inheritdoc/> |
| 95 | + public override void Unpin() |
| 96 | + { |
| 97 | + } |
| 98 | + |
| 99 | + /// <inheritdoc/> |
| 100 | + protected override void Dispose(bool disposing) |
| 101 | + { |
| 102 | + } |
| 103 | + |
| 104 | + /// <inheritdoc/> |
| 105 | + public Memory<T> GetMemory<T>(int offset, int length) |
| 106 | + where T : unmanaged |
| 107 | + { |
| 108 | + // We need to calculate the right offset and length of the new Memory<T>. The local offset |
| 109 | + // is the original offset into the wrapped TFrom[] array, while the input offset is the one |
| 110 | + // with respect to TTo items in the Memory<TTo> instance that is currently being cast. |
| 111 | + int |
| 112 | + absoluteOffset = this.offset + RuntimeHelpers.ConvertLength<TTo, TFrom>(offset), |
| 113 | + absoluteLength = RuntimeHelpers.ConvertLength<TTo, TFrom>(length); |
| 114 | + |
| 115 | + // We have a special handling in cases where the user is circling back to the original type |
| 116 | + // of the wrapped array. In this case we can just return a memory wrapping that array directly, |
| 117 | + // with offset and length being adjusted, without the memory manager indirection. |
| 118 | + if (typeof(T) == typeof(TFrom)) |
| 119 | + { |
| 120 | + return (Memory<T>)(object)this.array.AsMemory(absoluteOffset, absoluteLength); |
| 121 | + } |
| 122 | + |
| 123 | + return new ArrayMemoryManager<TFrom, T>(this.array, absoluteOffset, absoluteLength).Memory; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Throws an <see cref="ArgumentOutOfRangeException"/> when the target index for <see cref="Pin"/> is invalid. |
| 128 | + /// </summary> |
| 129 | + private static void ThrowArgumentOutOfRangeExceptionForInvalidIndex() |
| 130 | + { |
| 131 | + throw new ArgumentOutOfRangeException("elementIndex", "The input index is not in the valid range"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments