|
|
|
@ -3,6 +3,7 @@ |
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Buffers; |
|
|
|
using System.Diagnostics.Contracts; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
|
|
|
@ -13,12 +14,29 @@ namespace SixLabors.ImageSharp.Memory |
|
|
|
/// </summary>
|
|
|
|
internal static class MemoryOwnerExtensions |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Gets a <see cref="Span{T}"/> from an <see cref="IMemoryOwner{T}"/> instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer</param>
|
|
|
|
/// <returns>The <see cref="Span{T}"/></returns>
|
|
|
|
[Pure] |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static Span<T> GetSpan<T>(this IMemoryOwner<T> buffer) |
|
|
|
=> buffer.Memory.Span; |
|
|
|
{ |
|
|
|
return buffer.Memory.Span; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the length of an <see cref="IMemoryOwner{T}"/> internal buffer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer</param>
|
|
|
|
/// <returns>The length of the buffer</returns>
|
|
|
|
[Pure] |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static int Length<T>(this IMemoryOwner<T> buffer) |
|
|
|
=> buffer.GetSpan().Length; |
|
|
|
{ |
|
|
|
return buffer.Memory.Length; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a <see cref="Span{T}"/> to an offsetted position inside the buffer.
|
|
|
|
@ -26,6 +44,7 @@ namespace SixLabors.ImageSharp.Memory |
|
|
|
/// <param name="buffer">The buffer</param>
|
|
|
|
/// <param name="start">The start</param>
|
|
|
|
/// <returns>The <see cref="Span{T}"/></returns>
|
|
|
|
[Pure] |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start) |
|
|
|
{ |
|
|
|
@ -39,6 +58,7 @@ namespace SixLabors.ImageSharp.Memory |
|
|
|
/// <param name="start">The start</param>
|
|
|
|
/// <param name="length">The length of the slice</param>
|
|
|
|
/// <returns>The <see cref="Span{T}"/></returns>
|
|
|
|
[Pure] |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static Span<T> Slice<T>(this IMemoryOwner<T> buffer, int start, int length) |
|
|
|
{ |
|
|
|
@ -55,8 +75,17 @@ namespace SixLabors.ImageSharp.Memory |
|
|
|
buffer.GetSpan().Clear(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a reference to the first item in the internal buffer for an <see cref="IMemoryOwner{T}"/> instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The buffer</param>
|
|
|
|
/// <returns>A reference to the first item within the memory wrapped by <paramref name="buffer"/></returns>
|
|
|
|
[Pure] |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static ref T GetReference<T>(this IMemoryOwner<T> buffer) |
|
|
|
where T : struct => |
|
|
|
ref MemoryMarshal.GetReference(buffer.GetSpan()); |
|
|
|
where T : struct |
|
|
|
{ |
|
|
|
return ref MemoryMarshal.GetReference(buffer.GetSpan()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|