Browse Source

MemoryAllocator.Create & tests

af/UniformUnmanagedMemoryPoolMemoryAllocator-02-MemoryGuards
Anton Firszov 4 years ago
parent
commit
3740471d7b
  1. 2
      src/ImageSharp/Configuration.cs
  2. 4
      src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
  3. 2
      tests/ImageSharp.Tests/Formats/Tiff/Compression/PackBitsTiffCompressionTests.cs
  4. 2
      tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderHeaderTests.cs
  5. 37
      tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

2
src/ImageSharp/Configuration.cs

@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets or sets the <see cref="MemoryAllocator"/> that is currently in use.
/// </summary>
public MemoryAllocator MemoryAllocator { get; set; } = MemoryAllocator.CreateDefault();
public MemoryAllocator MemoryAllocator { get; set; } = MemoryAllocator.Create();
/// <summary>
/// Gets the maximum header size of all the formats.

4
src/ImageSharp/Memory/Allocators/MemoryAllocator.cs

@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Memory
/// Creates a default instance of a <see cref="MemoryAllocator"/> optimized for the executing platform.
/// </summary>
/// <returns>The <see cref="MemoryAllocator"/>.</returns>
public static MemoryAllocator CreateDefault() =>
public static MemoryAllocator Create() =>
new UniformUnmanagedMemoryPoolMemoryAllocator(null);
/// <summary>
@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
/// <param name="settings">The <see cref="MemoryAllocatorSettings"/>.</param>
/// <returns>The <see cref="MemoryAllocator"/>.</returns>
public static MemoryAllocator CreateDefault(MemoryAllocatorSettings settings) =>
public static MemoryAllocator Create(MemoryAllocatorSettings settings) =>
new UniformUnmanagedMemoryPoolMemoryAllocator(settings.MaximumPoolSizeMegabytes);
/// <summary>

2
tests/ImageSharp.Tests/Formats/Tiff/Compression/PackBitsTiffCompressionTests.cs

@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.Compression
using var stream = new BufferedReadStream(Configuration.Default, memoryStream);
byte[] buffer = new byte[expectedResult.Length];
using var decompressor = new PackBitsTiffCompression(MemoryAllocator.CreateDefault(), default, default);
using var decompressor = new PackBitsTiffCompression(MemoryAllocator.Create(), default, default);
decompressor.Decompress(stream, 0, (uint)inputData.Length, 1, buffer);
Assert.Equal(expectedResult, buffer);

2
tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderHeaderTests.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[Trait("Format", "Tiff")]
public class TiffEncoderHeaderTests
{
private static readonly MemoryAllocator MemoryAllocator = MemoryAllocator.CreateDefault();
private static readonly MemoryAllocator MemoryAllocator = MemoryAllocator.Create();
private static readonly Configuration Configuration = Configuration.Default;
private static readonly ITiffEncoderOptions Options = new TiffEncoder();

37
tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

@ -6,6 +6,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Memory.Internals;
@ -130,13 +131,13 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators
}
[Fact]
public void MemoryAllocator_CreateDefault_WithoutOptions_AllocatesDiscontiguousMemory()
public void MemoryAllocator_Create_WithoutSettings_AllocatesDiscontiguousMemory()
{
RemoteExecutor.Invoke(RunTest).Dispose();
static void RunTest()
{
var allocator = MemoryAllocator.CreateDefault();
var allocator = MemoryAllocator.Create();
long sixteenMegabytes = 16 * (1 << 20);
// Should allocate 4 times 4MB discontiguos blocks:
@ -145,6 +146,38 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators
}
}
[Fact]
public void MemoryAllocator_Create_LimitPoolSize()
{
RemoteExecutor.Invoke(RunTest).Dispose();
static void RunTest()
{
var allocator = MemoryAllocator.Create(new MemoryAllocatorSettings()
{
MaximumPoolSizeMegabytes = 8
});
MemoryGroup<byte> g0 = allocator.AllocateGroup<byte>(B(8), 1024);
MemoryGroup<byte> g1 = allocator.AllocateGroup<byte>(B(8), 1024);
ref byte r0 = ref MemoryMarshal.GetReference(g0[0].Span);
ref byte r1 = ref MemoryMarshal.GetReference(g1[0].Span);
g0.Dispose();
g1.Dispose();
MemoryGroup<byte> g2 = allocator.AllocateGroup<byte>(B(8), 1024);
MemoryGroup<byte> g3 = allocator.AllocateGroup<byte>(B(8), 1024);
ref byte r2 = ref MemoryMarshal.GetReference(g2[0].Span);
ref byte r3 = ref MemoryMarshal.GetReference(g3[0].Span);
Assert.True(Unsafe.AreSame(ref r0, ref r2));
Assert.False(Unsafe.AreSame(ref r1, ref r3));
}
static long B(int value) => value << 20;
}
[Theory]
[InlineData(true)]
[InlineData(false)]

Loading…
Cancel
Save