|
| 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 | +#if SPAN_RUNTIME_SUPPORT |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Buffers; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using Microsoft.Toolkit.HighPerformance.Extensions; |
| 12 | + |
| 13 | +namespace Microsoft.Toolkit.HighPerformance.Buffers.Internals |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// A custom <see cref="MemoryManager{T}"/> that can wrap arbitrary <see cref="object"/> instances. |
| 17 | + /// </summary> |
| 18 | + /// <typeparam name="T">The type of elements in the target memory area.</typeparam> |
| 19 | + internal sealed class RawObjectMemoryManager<T> : MemoryManager<T> |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// The target <see cref="object"/> instance. |
| 23 | + /// </summary> |
| 24 | + private readonly object instance; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The initial offset within <see cref="instance"/>. |
| 28 | + /// </summary> |
| 29 | + private readonly IntPtr offset; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The length of the target memory area. |
| 33 | + /// </summary> |
| 34 | + private readonly int length; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="RawObjectMemoryManager{T}"/> class. |
| 38 | + /// </summary> |
| 39 | + /// <param name="instance">The target <see cref="object"/> instance.</param> |
| 40 | + /// <param name="offset">The starting offset within <paramref name="instance"/>.</param> |
| 41 | + /// <param name="length">The usable length within <paramref name="instance"/>.</param> |
| 42 | + public RawObjectMemoryManager(object instance, IntPtr offset, int length) |
| 43 | + { |
| 44 | + this.instance = instance; |
| 45 | + this.offset = offset; |
| 46 | + this.length = length; |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + public override Span<T> GetSpan() |
| 51 | + { |
| 52 | + ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset); |
| 53 | + |
| 54 | + return MemoryMarshal.CreateSpan(ref r0, this.length); |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc/> |
| 58 | + public override unsafe MemoryHandle Pin(int elementIndex = 0) |
| 59 | + { |
| 60 | + if ((uint)elementIndex >= (uint)this.length) |
| 61 | + { |
| 62 | + ThrowArgumentOutOfRangeExceptionForInvalidElementIndex(); |
| 63 | + } |
| 64 | + |
| 65 | + // Allocating a pinned handle for the array with fail and throw an exception |
| 66 | + // if the array contains non blittable data. This is the expected behavior and |
| 67 | + // the same happens when trying to pin a Memory<T> instance obtained through |
| 68 | + // traditional means (eg. via the implicit T[] array conversion), if T is a |
| 69 | + // reference type or a type containing some references. |
| 70 | + GCHandle handle = GCHandle.Alloc(this.instance, GCHandleType.Pinned); |
| 71 | + ref T r0 = ref this.instance.DangerousGetObjectDataReferenceAt<T>(this.offset); |
| 72 | + ref T r1 = ref Unsafe.Add(ref r0, (nint)(uint)elementIndex); |
| 73 | + void* p = Unsafe.AsPointer(ref r1); |
| 74 | + |
| 75 | + return new MemoryHandle(p, handle); |
| 76 | + } |
| 77 | + |
| 78 | + /// <inheritdoc/> |
| 79 | + public override void Unpin() |
| 80 | + { |
| 81 | + } |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + protected override void Dispose(bool disposing) |
| 85 | + { |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Throws an <see cref="ArgumentOutOfRangeException"/> when the input index for <see cref="Pin"/> is not valid. |
| 90 | + /// </summary> |
| 91 | + private static void ThrowArgumentOutOfRangeExceptionForInvalidElementIndex() |
| 92 | + { |
| 93 | + throw new ArgumentOutOfRangeException("elementIndex", "The input element index was not in the valid range"); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#endif |
0 commit comments