diff --git a/src/ImageSharp/Memory/Allocators/AllocationOptions.cs b/src/ImageSharp/Memory/Allocators/AllocationOptions.cs
index 72c785532b..ae856c978c 100644
--- a/src/ImageSharp/Memory/Allocators/AllocationOptions.cs
+++ b/src/ImageSharp/Memory/Allocators/AllocationOptions.cs
@@ -19,12 +19,6 @@ namespace SixLabors.ImageSharp.Memory
///
/// Indicates that the allocated buffer should be cleaned following allocation.
///
- Clean = 1,
-
- ///
- /// Affects only group allocations.
- /// Indicates that the requested or should be made of contiguous blocks up to .
- ///
- Contiguous = 2
+ Clean = 1
}
}
diff --git a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
index 2e664b4133..ecc30c97cd 100644
--- a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
+++ b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
@@ -144,10 +144,12 @@ namespace SixLabors.ImageSharp.Memory
}
// Attempt to rent the whole group from the pool, allocate a group of unmanaged buffers if the attempt fails:
- MemoryGroup poolGroup = options.Has(AllocationOptions.Contiguous) ?
- null :
- MemoryGroup.Allocate(this.pool, totalLength, bufferAlignment, options);
- return poolGroup ?? MemoryGroup.Allocate(this.nonPoolAllocator, totalLength, bufferAlignment, options);
+ if (MemoryGroup.TryAllocate(this.pool, totalLength, bufferAlignment, options, out MemoryGroup poolGroup))
+ {
+ return poolGroup;
+ }
+
+ return MemoryGroup.Allocate(this.nonPoolAllocator, totalLength, bufferAlignment, options);
}
public override void ReleaseRetainedResources()
diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
index da7419b4bb..5ff17f0c20 100644
--- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
+++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
@@ -85,9 +85,7 @@ namespace SixLabors.ImageSharp.Memory
int bufferAlignmentInElements,
AllocationOptions options = AllocationOptions.None)
{
- int bufferCapacityInBytes = options.Has(AllocationOptions.Contiguous) ?
- int.MaxValue :
- allocator.GetBufferCapacityInBytes();
+ int bufferCapacityInBytes = allocator.GetBufferCapacityInBytes();
Guard.NotNull(allocator, nameof(allocator));
Guard.MustBeGreaterThanOrEqualTo(totalLengthInElements, 0, nameof(totalLengthInElements));
Guard.MustBeGreaterThanOrEqualTo(bufferAlignmentInElements, 0, nameof(bufferAlignmentInElements));
@@ -151,11 +149,12 @@ namespace SixLabors.ImageSharp.Memory
return new Owned(buffers, length, length, true);
}
- public static MemoryGroup Allocate(
+ public static bool TryAllocate(
UniformUnmanagedMemoryPool pool,
long totalLengthInElements,
int bufferAlignmentInElements,
- AllocationOptions options = AllocationOptions.None)
+ AllocationOptions options,
+ out MemoryGroup memoryGroup)
{
Guard.NotNull(pool, nameof(pool));
Guard.MustBeGreaterThanOrEqualTo(totalLengthInElements, 0, nameof(totalLengthInElements));
@@ -165,7 +164,8 @@ namespace SixLabors.ImageSharp.Memory
if (bufferAlignmentInElements > blockCapacityInElements)
{
- return null;
+ memoryGroup = null;
+ return false;
}
if (totalLengthInElements == 0)
@@ -197,10 +197,12 @@ namespace SixLabors.ImageSharp.Memory
if (arrays == null)
{
// Pool is full
- return null;
+ memoryGroup = null;
+ return false;
}
- return new Owned(pool, arrays, bufferLength, totalLengthInElements, sizeOfLastBuffer, options);
+ memoryGroup = new Owned(pool, arrays, bufferLength, totalLengthInElements, sizeOfLastBuffer, options);
+ return true;
}
public static MemoryGroup Wrap(params Memory[] source)
diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs
index 4486c67750..c9dda5f6bc 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs
@@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
this.sourceLength = sourceLength;
this.DestinationLength = destinationLength;
this.MaxDiameter = (radius * 2) + 1;
- this.data = memoryAllocator.Allocate2D(this.MaxDiameter, bufferHeight, AllocationOptions.Clean | AllocationOptions.Contiguous);
+ this.data = memoryAllocator.Allocate2D(this.MaxDiameter, bufferHeight, preferContiguosImageBuffers: true, AllocationOptions.Clean);
this.pinHandle = this.data.DangerousGetSingleMemory().Pin();
this.kernels = new ResizeKernel[destinationLength];
this.tempValues = new double[this.MaxDiameter];
diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
index 4b4a04df88..e67831e197 100644
--- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
+++ b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
@@ -111,25 +111,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators
}
}
- [Theory]
- [InlineData(512)]
- [InlineData(2048)]
- [InlineData(8192)]
- [InlineData(65536)]
- public void AllocateGroup_OptionsContiguous_AllocatesContiguousBuffer(int lengthInBytes)
- {
- var allocator = new UniformUnmanagedMemoryPoolMemoryAllocator(
- 128,
- 1024,
- 2048,
- 4096);
- int length = lengthInBytes / Unsafe.SizeOf();
- using MemoryGroup g = allocator.AllocateGroup(length, 32, AllocationOptions.Contiguous);
- Assert.Equal(length, g.BufferLength);
- Assert.Equal(length, g.TotalLength);
- Assert.Equal(1, g.Count);
- }
-
[Fact]
public unsafe void Allocate_MemoryIsPinnableMultipleTimes()
{
diff --git a/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs b/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
index 0549309c1e..e6d07a191c 100644
--- a/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
+++ b/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
@@ -93,10 +93,11 @@ namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
var pool = new UniformUnmanagedMemoryPool(bufferCapacity, expectedNumberOfBuffers);
// Act:
- using var g = MemoryGroup.Allocate(pool, totalLength, bufferAlignment);
+ Assert.True(MemoryGroup.TryAllocate(pool, totalLength, bufferAlignment, AllocationOptions.None, out MemoryGroup g));
// Assert:
ValidateAllocateMemoryGroup(expectedNumberOfBuffers, expectedBufferSize, expectedSizeOfLastBuffer, g);
+ g.Dispose();
}
private static unsafe Span GetSpan(UniformUnmanagedMemoryPool pool, UnmanagedMemoryHandle h) =>
@@ -116,38 +117,42 @@ namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
pool.Return(buffers);
- using var g = MemoryGroup.Allocate(pool, 50, 10, options);
+ Assert.True(MemoryGroup.TryAllocate(pool, 50, 10, options, out MemoryGroup g));
Span expected = stackalloc byte[10];
expected.Fill((byte)(options == AllocationOptions.Clean ? 0 : 42));
foreach (Memory memory in g)
{
Assert.True(expected.SequenceEqual(memory.Span));
}
+
+ g.Dispose();
}
[Theory]
- [InlineData(64, 4, 60, 240, false)]
- [InlineData(64, 4, 60, 244, true)]
+ [InlineData(64, 4, 60, 240, true)]
+ [InlineData(64, 4, 60, 244, false)]
public void Allocate_FromPool_AroundLimit(
int bufferCapacityBytes,
int poolCapacity,
int alignmentBytes,
int requestBytes,
- bool shouldReturnNull)
+ bool shouldSucceed)
{
var pool = new UniformUnmanagedMemoryPool(bufferCapacityBytes, poolCapacity);
int alignmentElements = alignmentBytes / Unsafe.SizeOf();
int requestElements = requestBytes / Unsafe.SizeOf();
- using var g = MemoryGroup.Allocate(pool, requestElements, alignmentElements);
- if (shouldReturnNull)
+ Assert.Equal(shouldSucceed, MemoryGroup.TryAllocate(pool, requestElements, alignmentElements, AllocationOptions.None, out MemoryGroup g));
+ if (shouldSucceed)
{
- Assert.Null(g);
+ Assert.NotNull(g);
}
else
{
- Assert.NotNull(g);
+ Assert.Null(g);
}
+
+ g?.Dispose();
}
internal static void ValidateAllocateMemoryGroup(
@@ -218,19 +223,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
Assert.Equal(expectedBlockCount, this.MemoryAllocator.ReturnLog.Count);
Assert.True(bufferHashes.SetEquals(this.MemoryAllocator.ReturnLog.Select(l => l.HashCodeOfBuffer)));
}
-
- [Theory]
- [InlineData(128)]
- [InlineData(1024)]
- public void Allocate_OptionsContiguous_AllocatesContiguousBuffer(int lengthInBytes)
- {
- this.MemoryAllocator.BufferCapacityInBytes = 256;
- int length = lengthInBytes / Unsafe.SizeOf();
- using var g = MemoryGroup.Allocate(this.MemoryAllocator, length, 32, AllocationOptions.Contiguous);
- Assert.Equal(length, g.BufferLength);
- Assert.Equal(length, g.TotalLength);
- Assert.Equal(1, g.Count);
- }
}
}