// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace SixLabors.Fonts { /// /// A custom that can wrap of instances /// and cast them to be for any arbitrary unmanaged value type. /// /// The value type to use when casting the wrapped instance. internal sealed class ByteMemoryManager : MemoryManager where T : unmanaged { /// /// The wrapped of instance. /// private readonly Memory memory; /// /// Initializes a new instance of the class. /// /// The of instance to wrap. public ByteMemoryManager(Memory memory) => this.memory = memory; /// protected override void Dispose(bool disposing) { } /// public override Span GetSpan() => MemoryMarshal.Cast(this.memory.Span); /// public override MemoryHandle Pin(int elementIndex = 0) // We need to adjust the offset into the wrapped byte segment, // as the input index refers to the target-cast memory of T. // We just have to shift this index by the byte size of T. => this.memory.Slice(elementIndex * Unsafe.SizeOf()).Pin(); /// public override void Unpin() { } } }