diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 3d9552e42..818fb8193 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp /// /// Gets or sets the that is currently in use. /// - public MemoryAllocator MemoryAllocator { get; set; } = MemoryAllocator.CreateDefault(); + public MemoryAllocator MemoryAllocator { get; set; } = MemoryAllocator.Create(); /// /// Gets the maximum header size of all the formats. diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs index cc97d3b99..ec0771a48 100644 --- a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs +++ b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Memory /// Creates a default instance of a optimized for the executing platform. /// /// The . - public static MemoryAllocator CreateDefault() => + public static MemoryAllocator Create() => new UniformUnmanagedMemoryPoolMemoryAllocator(null); /// @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Memory /// /// The . /// The . - public static MemoryAllocator CreateDefault(MemoryAllocatorSettings settings) => + public static MemoryAllocator Create(MemoryAllocatorSettings settings) => new UniformUnmanagedMemoryPoolMemoryAllocator(settings.MaximumPoolSizeMegabytes); /// diff --git a/tests/ImageSharp.Tests/Formats/Tiff/Compression/PackBitsTiffCompressionTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/Compression/PackBitsTiffCompressionTests.cs index 0f9e468bd..bbca2610e 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/Compression/PackBitsTiffCompressionTests.cs +++ b/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); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderHeaderTests.cs index 613cf3211..b68670f1f 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderHeaderTests.cs +++ b/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(); diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs index 9a98345b0..f9376e27c 100644 --- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs +++ b/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 g0 = allocator.AllocateGroup(B(8), 1024); + MemoryGroup g1 = allocator.AllocateGroup(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 g2 = allocator.AllocateGroup(B(8), 1024); + MemoryGroup g3 = allocator.AllocateGroup(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)]