Browse Source

MemoryAllocator.Default

af/UniformUnmanagedMemoryPoolMemoryAllocator-02-MemoryGuards
Anton Firszov 5 years ago
parent
commit
003e51e98f
  1. 2
      src/ImageSharp/Configuration.cs
  2. 21
      src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
  3. 32
      tests/ImageSharp.Tests/ConfigurationTests.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.Create();
public MemoryAllocator MemoryAllocator { get; set; } = MemoryAllocator.Default;
/// <summary>
/// Gets the maximum header size of all the formats.

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

@ -11,6 +11,27 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public abstract class MemoryAllocator
{
private static MemoryAllocator defaultMemoryAllocator = Create();
/// <summary>
/// Gets or sets the default global <see cref="MemoryAllocator"/> instance for the current process.
/// </summary>
/// <remarks>
/// Since <see cref="Configuration.Default"/> is lazy-initialized, setting the value of <see cref="Default"/>
/// will only override <see cref="Configuration.Default"/>'s <see cref="Configuration.MemoryAllocator"/>
/// before the first read of the <see cref="Configuration.Default"/> property.
/// After that, a manual assigment of <see cref="Configuration.Default"/> is necessary.
/// </remarks>
public static MemoryAllocator Default
{
get => defaultMemoryAllocator;
set
{
Guard.NotNull(value, nameof(Default));
defaultMemoryAllocator = value;
}
}
/// <summary>
/// Gets the length of the largest contiguous buffer that can be handled by this allocator instance in bytes.
/// </summary>

32
tests/ImageSharp.Tests/ConfigurationTests.cs

@ -3,10 +3,12 @@
using System;
using System.Linq;
using Microsoft.DotNet.RemoteExecutor;
using Moq;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Tests.Memory;
using Xunit;
// ReSharper disable InconsistentNaming
@ -146,5 +148,33 @@ namespace SixLabors.ImageSharp.Tests
Assert.Throws<ArgumentOutOfRangeException>(
() => config.StreamProcessingBufferSize = 0);
}
[Fact]
public void InheritsDefaultMemoryAllocatorInstance()
{
RemoteExecutor.Invoke(RunTest).Dispose();
static void RunTest()
{
MemoryAllocator allocator = new TestMemoryAllocator();
MemoryAllocator.Default = allocator;
var c1 = new Configuration();
var c2 = new Configuration(new MockConfigurationModule());
var c3 = Configuration.CreateDefaultInstance();
Assert.Same(allocator, Configuration.Default.MemoryAllocator);
Assert.Same(allocator, c1.MemoryAllocator);
Assert.Same(allocator, c2.MemoryAllocator);
Assert.Same(allocator, c3.MemoryAllocator);
}
}
private class MockConfigurationModule : IConfigurationModule
{
public void Configure(Configuration configuration)
{
}
}
}
}

Loading…
Cancel
Save