From 2472c4285b57215667c399f9ca122d0fa634da78 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 28 Oct 2021 21:25:44 +0200 Subject: [PATCH] MemoryAllocatorOptions -> MemoryAllocatorSettings, remove MinimumContiguousBlockSizeBytes --- src/ImageSharp/ImageFrame{TPixel}.cs | 2 +- .../Memory/Allocators/MemoryAllocator.cs | 8 +-- .../Allocators/MemoryAllocatorOptions.cs | 57 ------------------- .../Allocators/MemoryAllocatorSettings.cs | 31 ++++++++++ ...iformUnmanagedMemoryPoolMemoryAllocator.cs | 6 +- ...niformUnmanagedPoolMemoryAllocatorTests.cs | 22 ------- 6 files changed, 39 insertions(+), 87 deletions(-) delete mode 100644 src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs create mode 100644 src/ImageSharp/Memory/Allocators/MemoryAllocatorSettings.cs diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 949b8999a5..824fd0ec7a 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -217,7 +217,7 @@ namespace SixLabors.ImageSharp /// /// To ensure the memory is contiguous, should be initialized /// with a that enforces larger contiguous buffers. - /// See . + /// See . /// /// WARNING: Disposing or leaking the underlying image while still working with it's /// might lead to memory corruption. diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs index 6be5c50fa2..cc97d3b99f 100644 --- a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs +++ b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs @@ -22,15 +22,15 @@ namespace SixLabors.ImageSharp.Memory /// /// The . public static MemoryAllocator CreateDefault() => - new UniformUnmanagedMemoryPoolMemoryAllocator(null, null); + new UniformUnmanagedMemoryPoolMemoryAllocator(null); /// /// Creates the default using the provided options. /// - /// The . + /// The . /// The . - public static MemoryAllocator CreateDefault(MemoryAllocatorOptions options) => - new UniformUnmanagedMemoryPoolMemoryAllocator(options.MaximumPoolSizeMegabytes, options.MinimumContiguousBlockSizeBytes); + public static MemoryAllocator CreateDefault(MemoryAllocatorSettings settings) => + new UniformUnmanagedMemoryPoolMemoryAllocator(settings.MaximumPoolSizeMegabytes); /// /// Allocates an , holding a of length . diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs deleted file mode 100644 index acdf36c6a1..0000000000 --- a/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -namespace SixLabors.ImageSharp.Memory -{ - /// - /// Defines options for creating the default . - /// - public class MemoryAllocatorOptions - { - private int? maximumPoolSizeMegabytes; - private int? minimumContiguousBlockSizeBytes; - - /// - /// Gets or sets a value defining the maximum size of the 's internal memory pool - /// in Megabytes. means platform default. - /// - public int? MaximumPoolSizeMegabytes - { - get => this.maximumPoolSizeMegabytes; - set - { - if (value.HasValue) - { - Guard.MustBeGreaterThanOrEqualTo(value.Value, 0, nameof(this.MaximumPoolSizeMegabytes)); - } - - this.maximumPoolSizeMegabytes = value; - } - } - - /// - /// Gets or sets a value defining the minimum contiguous block size when allocating buffers for - /// , or . - /// means platform default. - /// - /// - /// Overriding this value is useful for interop scenarios - /// ensuring succeeds. - /// - public int? MinimumContiguousBlockSizeBytes - { - get => this.minimumContiguousBlockSizeBytes; - set - { - if (value.HasValue) - { - // It doesn't make sense to set this to small values in practice. - // Defining an arbitrary minimum of 65536. - Guard.MustBeGreaterThanOrEqualTo(value.Value, 65536, nameof(this.MaximumPoolSizeMegabytes)); - } - - this.minimumContiguousBlockSizeBytes = value; - } - } - } -} diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocatorSettings.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocatorSettings.cs new file mode 100644 index 0000000000..274e1739cb --- /dev/null +++ b/src/ImageSharp/Memory/Allocators/MemoryAllocatorSettings.cs @@ -0,0 +1,31 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +namespace SixLabors.ImageSharp.Memory +{ + /// + /// Defines options for creating the default . + /// + public class MemoryAllocatorSettings + { + private int? maximumPoolSizeMegabytes; + + /// + /// Gets or sets a value defining the maximum size of the 's internal memory pool + /// in Megabytes. means platform default. + /// + public int? MaximumPoolSizeMegabytes + { + get => this.maximumPoolSizeMegabytes; + set + { + if (value.HasValue) + { + Guard.MustBeGreaterThanOrEqualTo(value.Value, 0, nameof(this.MaximumPoolSizeMegabytes)); + } + + this.maximumPoolSizeMegabytes = value; + } + } + } +} diff --git a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs index 8d8e8e3df8..3aa2bbf83a 100644 --- a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs +++ b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs @@ -25,11 +25,11 @@ namespace SixLabors.ImageSharp.Memory private UniformUnmanagedMemoryPool pool; private readonly UnmanagedMemoryAllocator nonPoolAllocator; - public UniformUnmanagedMemoryPoolMemoryAllocator(int? maxPoolSizeMegabytes, int? minimumContiguousBlockBytes) + public UniformUnmanagedMemoryPoolMemoryAllocator(int? maxPoolSizeMegabytes) : this( - minimumContiguousBlockBytes.HasValue ? minimumContiguousBlockBytes.Value : DefaultContiguousPoolBlockSizeBytes, + DefaultContiguousPoolBlockSizeBytes, maxPoolSizeMegabytes.HasValue ? (long)maxPoolSizeMegabytes.Value * OneMegabyte : GetDefaultMaxPoolSizeBytes(), - minimumContiguousBlockBytes.HasValue ? Math.Max(minimumContiguousBlockBytes.Value, DefaultNonPoolBlockSizeBytes) : DefaultNonPoolBlockSizeBytes) + DefaultNonPoolBlockSizeBytes) { } diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs index 70eba2e6a3..9a98345b03 100644 --- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs +++ b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs @@ -145,28 +145,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators } } - [Theory] - [InlineData(false)] - [InlineData(true)] - public void MemoryAllocator_CreateDefault_WithOptions_CanForceContiguousAllocation(bool poolAllocation) - { - RemoteExecutor.Invoke(RunTest, poolAllocation.ToString()).Dispose(); - - static void RunTest(string poolAllocationStr) - { - int fortyEightMegabytes = 48 * (1 << 20); - var allocator = MemoryAllocator.CreateDefault(new MemoryAllocatorOptions() - { - MaximumPoolSizeMegabytes = bool.Parse(poolAllocationStr) ? 64 : 0, - MinimumContiguousBlockSizeBytes = fortyEightMegabytes - }); - - MemoryGroup g = allocator.AllocateGroup(fortyEightMegabytes, 1024); - Assert.Equal(1, g.Count); - Assert.Equal(fortyEightMegabytes, g.TotalLength); - } - } - [Theory] [InlineData(true)] [InlineData(false)]