|
| 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.Runtime.CompilerServices; |
| 6 | + |
| 7 | +namespace System.Runtime.InteropServices |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Provides a collection of methods for interoperating with <see cref="Memory{T}"/>, <see cref="ReadOnlyMemory{T}"/>, |
| 11 | + /// <see cref="Span{T}"/>, and <see cref="ReadOnlySpan{T}"/>. |
| 12 | + /// </summary> |
| 13 | + public static partial class MemoryMarshal |
| 14 | + { |
| 15 | + /// <summary>Creates a <see cref="Memory{T}"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary> |
| 16 | + /// <param name="readOnlyMemory">The <see cref="ReadOnlyMemory{T}"/>.</param> |
| 17 | + /// <returns>A <see cref="Memory{T}"/> representing the same memory as the <see cref="ReadOnlyMemory{T}"/>, but writable.</returns> |
| 18 | + /// <remarks> |
| 19 | + /// <see cref="AsMemory{T}(ReadOnlyMemory{T})"/> must be used with extreme caution. <see cref="ReadOnlyMemory{T}"/> is used |
| 20 | + /// to represent immutable data and other memory that is not meant to be written to; <see cref="Memory{T}"/> instances created |
| 21 | + /// by <see cref="AsMemory{T}(ReadOnlyMemory{T})"/> should not be written to. The method exists to enable variables typed |
| 22 | + /// as <see cref="Memory{T}"/> but only used for reading to store a <see cref="ReadOnlyMemory{T}"/>. |
| 23 | + /// </remarks> |
| 24 | + public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> readOnlyMemory) => |
| 25 | + Unsafe.As<ReadOnlyMemory<T>, Memory<T>>(ref readOnlyMemory); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to the location where the 0th element |
| 29 | + /// would have been stored. Such a reference can be used for pinning but must never be dereferenced. |
| 30 | + /// </summary> |
| 31 | + public static ref T GetReference<T>(Span<T> span) |
| 32 | + { |
| 33 | + if (span.Pinnable == null) |
| 34 | + unsafe { return ref Unsafe.AsRef<T>(span.ByteOffset.ToPointer()); } |
| 35 | + else |
| 36 | + return ref Unsafe.AddByteOffset<T>(ref span.Pinnable.Data, span.ByteOffset); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Returns a reference to the 0th element of the ReadOnlySpan. If the Span is empty, returns a reference to the location where the 0th element |
| 41 | + /// would have been stored. Such a reference can be used for pinning but must never be dereferenced. |
| 42 | + /// </summary> |
| 43 | + public static ref T GetReference<T>(ReadOnlySpan<T> span) |
| 44 | + { |
| 45 | + if (span.Pinnable == null) |
| 46 | + unsafe { return ref Unsafe.AsRef<T>(span.ByteOffset.ToPointer()); } |
| 47 | + else |
| 48 | + return ref Unsafe.AddByteOffset<T>(ref span.Pinnable.Data, span.ByteOffset); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments