// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using MemoryStream = CommunityToolkit.HighPerformance.Streams.MemoryStream; namespace CommunityToolkit.HighPerformance; /// /// Helpers for working with the type. /// public static class MemoryExtensions { #if NETSTANDARD2_1_OR_GREATER /// /// Returns a instance wrapping the underlying data for the given instance. /// /// The type of items in the input instance. /// The input instance. /// The height of the resulting 2D area. /// The width of each row in the resulting 2D area. /// The resulting instance. /// /// Thrown when one of the input parameters is out of range. /// /// /// Thrown when the requested area is outside of bounds for . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Memory2D AsMemory2D(this Memory memory, int height, int width) { return new(memory, height, width); } /// /// Returns a instance wrapping the underlying data for the given instance. /// /// The type of items in the input instance. /// The input instance. /// The initial offset within . /// The height of the resulting 2D area. /// The width of each row in the resulting 2D area. /// The pitch in the resulting 2D area. /// The resulting instance. /// /// Thrown when one of the input parameters is out of range. /// /// /// Thrown when the requested area is outside of bounds for . /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Memory2D AsMemory2D(this Memory memory, int offset, int height, int width, int pitch) { return new(memory, offset, height, width, pitch); } #endif /// /// Casts a of one primitive type to of bytes. /// /// The type if items in the source . /// The source , of type . /// A of bytes. /// /// Thrown if the property of the new would exceed . /// /// Thrown when the data store of is not supported. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Memory AsBytes(this Memory memory) where T : unmanaged { return MemoryMarshal.AsMemory(((ReadOnlyMemory)memory).Cast()); } /// /// Casts a of one primitive type to another primitive type . /// /// The type of items in the source . /// The type of items in the destination . /// The source , of type . /// A of type /// Thrown when the data store of is not supported. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Memory Cast(this Memory memory) where TFrom : unmanaged where TTo : unmanaged { return MemoryMarshal.AsMemory(((ReadOnlyMemory)memory).Cast()); } /// /// Returns a wrapping the contents of the given of instance. /// /// The input of instance. /// A wrapping the data within . /// /// Since this method only receives a instance, which does not track /// the lifetime of its underlying buffer, it is responsibility of the caller to manage that. /// In particular, the caller must ensure that the target buffer is not disposed as long /// as the returned is in use, to avoid unexpected issues. /// /// Thrown when has an invalid data store. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Stream AsStream(this Memory memory) { return MemoryStream.Create(memory, false); } }