using System; using System.Buffers; using System.Runtime.InteropServices; using SixLabors.Memory; namespace SixLabors.ImageSharp.Tests.Memory { internal class TestMemoryAllocator : MemoryAllocator { public TestMemoryAllocator(byte dirtyValue = 42) { this.DirtyValue = dirtyValue; } /// /// The value to initilazie the result buffer with, with non-clean options () /// public byte DirtyValue { get; } internal override IMemoryOwner Allocate(int length, AllocationOptions options = AllocationOptions.None) { T[] array = this.AllocateArray(length, options); return new BasicArrayBuffer(array, length); } internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, AllocationOptions options = AllocationOptions.None) { byte[] array = this.AllocateArray(length, options); return new ManagedByteBuffer(array); } private T[] AllocateArray(int length, AllocationOptions options) where T : struct { var array = new T[length + 42]; if (options == AllocationOptions.None) { Span data = MemoryMarshal.Cast(array.AsSpan()); data.Fill(this.DirtyValue); } return array; } private class ManagedByteBuffer : BasicArrayBuffer, IManagedByteBuffer { public ManagedByteBuffer(byte[] array) : base(array) { } } } }