mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
// Copyright (c) Six Labors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using SixLabors.ImageSharp.Diagnostics;
|
|
using Xunit;
|
|
|
|
namespace SixLabors.ImageSharp.Tests
|
|
{
|
|
public static class MemoryAllocatorValidator
|
|
{
|
|
public static IDisposable MonitorAllocations(int max = 0)
|
|
{
|
|
MemoryDiagnostics.Current = new();
|
|
return new TestMemoryAllocatorDisposable(max);
|
|
}
|
|
|
|
public static void ValidateAllocation(int max = 0)
|
|
{
|
|
var count = MemoryDiagnostics.TotalUndisposedAllocationCount;
|
|
var pass = count <= max;
|
|
Assert.True(pass, $"Expected a max of {max} undisposed buffers but found {count}");
|
|
|
|
if (count > 0)
|
|
{
|
|
Debug.WriteLine("We should have Zero undisposed memory allocations.");
|
|
}
|
|
|
|
MemoryDiagnostics.Current = null;
|
|
}
|
|
|
|
public struct TestMemoryAllocatorDisposable : IDisposable
|
|
{
|
|
private readonly int max;
|
|
|
|
public TestMemoryAllocatorDisposable(int max) => this.max = max;
|
|
|
|
public void Dispose()
|
|
=> ValidateAllocation(this.max);
|
|
}
|
|
}
|
|
}
|
|
|