Browse Source

Logging made optional in TestMemoryAllocator

af/UniformUnmanagedMemoryPoolMemoryAllocator-02-MemoryGuards
Anton Firszov 4 years ago
parent
commit
3c69a21e76
  1. 1
      tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
  2. 1
      tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs
  3. 18
      tests/ImageSharp.Tests/TestUtilities/TestMemoryAllocator.cs

1
tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs

@ -194,6 +194,7 @@ namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
public void MemoryAllocatorIsUtilizedCorrectly(AllocationOptions allocationOptions)
{
this.MemoryAllocator.BufferCapacityInBytes = 200;
this.MemoryAllocator.EnableLogging();
HashSet<int> bufferHashes;

1
tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs

@ -117,6 +117,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms
int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4;
var allocator = new TestMemoryAllocator();
allocator.EnableLogging();
configuration.MemoryAllocator = allocator;
configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes;

18
tests/ImageSharp.Tests/TestUtilities/TestMemoryAllocator.cs

@ -12,8 +12,8 @@ namespace SixLabors.ImageSharp.Tests.Memory
{
internal class TestMemoryAllocator : MemoryAllocator
{
private readonly List<AllocationRequest> allocationLog = new List<AllocationRequest>();
private readonly List<ReturnRequest> returnLog = new List<ReturnRequest>();
private List<AllocationRequest> allocationLog;
private List<ReturnRequest> returnLog;
public TestMemoryAllocator(byte dirtyValue = 42)
{
@ -27,12 +27,18 @@ namespace SixLabors.ImageSharp.Tests.Memory
public int BufferCapacityInBytes { get; set; } = int.MaxValue;
public IReadOnlyList<AllocationRequest> AllocationLog => this.allocationLog;
public IReadOnlyList<AllocationRequest> AllocationLog => this.allocationLog ?? throw new InvalidOperationException("Call TestMemoryAllocator.EnableLogging() first!");
public IReadOnlyList<ReturnRequest> ReturnLog => this.returnLog;
public IReadOnlyList<ReturnRequest> ReturnLog => this.returnLog ?? throw new InvalidOperationException("Call TestMemoryAllocator.EnableLogging() first!");
protected internal override int GetBufferCapacityInBytes() => this.BufferCapacityInBytes;
public void EnableLogging()
{
this.allocationLog = new List<AllocationRequest>();
this.returnLog = new List<ReturnRequest>();
}
public override IMemoryOwner<T> Allocate<T>(int length, AllocationOptions options = AllocationOptions.None)
{
T[] array = this.AllocateArray<T>(length, options);
@ -43,7 +49,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
where T : struct
{
var array = new T[length + 42];
this.allocationLog.Add(AllocationRequest.Create<T>(options, length, array));
this.allocationLog?.Add(AllocationRequest.Create<T>(options, length, array));
if (options == AllocationOptions.None)
{
@ -57,7 +63,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
private void Return<T>(BasicArrayBuffer<T> buffer)
where T : struct
{
this.returnLog.Add(new ReturnRequest(buffer.Array.GetHashCode()));
this.returnLog?.Add(new ReturnRequest(buffer.Array.GetHashCode()));
}
public struct AllocationRequest

Loading…
Cancel
Save