using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Helpers;
namespace ComputeSharp.D2D1.Shaders.Interop.Buffers;
///
/// A custom that wraps a pinned memory buffer.
///
internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager
{
///
/// A pointer to the buffer.
///
private readonly byte* buffer;
///
/// The length of the buffer.
///
private readonly int length;
///
/// Initializes a new instance of the class with the specified parameters.
///
/// A wrapping the input pinned buffer.
public PinnedBufferMemoryManager(ReadOnlySpan span)
{
this.buffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
this.length = span.Length;
}
///
public override Span GetSpan()
{
return new(this.buffer, this.length);
}
///
public override unsafe MemoryHandle Pin(int elementIndex = 0)
{
if ((uint)elementIndex >= this.length)
{
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(elementIndex));
}
return new(this.buffer + elementIndex);
}
///
public override void Unpin()
{
}
///
protected override void Dispose(bool disposing)
{
}
}