Browse Source

remove outdated AllocationOptions.Contiguous

pull/1730/head
Anton Firszov 5 years ago
parent
commit
432c03a65d
  1. 8
      src/ImageSharp/Memory/Allocators/AllocationOptions.cs
  2. 10
      src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
  3. 18
      src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
  4. 2
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeKernelMap.cs
  5. 19
      tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
  6. 36
      tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs

8
src/ImageSharp/Memory/Allocators/AllocationOptions.cs

@ -19,12 +19,6 @@ namespace SixLabors.ImageSharp.Memory
/// <summary>
/// Indicates that the allocated buffer should be cleaned following allocation.
/// </summary>
Clean = 1,
/// <summary>
/// Affects only group allocations.
/// Indicates that the requested <see cref="MemoryGroup{T}"/> or <see cref="Buffer2D{T}"/> should be made of contiguous blocks up to <see cref="int.MaxValue"/>.
/// </summary>
Contiguous = 2
Clean = 1
}
}

10
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<T> poolGroup = options.Has(AllocationOptions.Contiguous) ?
null :
MemoryGroup<T>.Allocate(this.pool, totalLength, bufferAlignment, options);
return poolGroup ?? MemoryGroup<T>.Allocate(this.nonPoolAllocator, totalLength, bufferAlignment, options);
if (MemoryGroup<T>.TryAllocate(this.pool, totalLength, bufferAlignment, options, out MemoryGroup<T> poolGroup))
{
return poolGroup;
}
return MemoryGroup<T>.Allocate(this.nonPoolAllocator, totalLength, bufferAlignment, options);
}
public override void ReleaseRetainedResources()

18
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<T> Allocate(
public static bool TryAllocate(
UniformUnmanagedMemoryPool pool,
long totalLengthInElements,
int bufferAlignmentInElements,
AllocationOptions options = AllocationOptions.None)
AllocationOptions options,
out MemoryGroup<T> 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<T> Wrap(params Memory<T>[] source)

2
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<float>(this.MaxDiameter, bufferHeight, AllocationOptions.Clean | AllocationOptions.Contiguous);
this.data = memoryAllocator.Allocate2D<float>(this.MaxDiameter, bufferHeight, preferContiguosImageBuffers: true, AllocationOptions.Clean);
this.pinHandle = this.data.DangerousGetSingleMemory().Pin();
this.kernels = new ResizeKernel[destinationLength];
this.tempValues = new double[this.MaxDiameter];

19
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<S4>();
using MemoryGroup<S4> g = allocator.AllocateGroup<S4>(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()
{

36
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<T>.Allocate(pool, totalLength, bufferAlignment);
Assert.True(MemoryGroup<T>.TryAllocate(pool, totalLength, bufferAlignment, AllocationOptions.None, out MemoryGroup<T> g));
// Assert:
ValidateAllocateMemoryGroup(expectedNumberOfBuffers, expectedBufferSize, expectedSizeOfLastBuffer, g);
g.Dispose();
}
private static unsafe Span<byte> GetSpan(UniformUnmanagedMemoryPool pool, UnmanagedMemoryHandle h) =>
@ -116,38 +117,42 @@ namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
pool.Return(buffers);
using var g = MemoryGroup<byte>.Allocate(pool, 50, 10, options);
Assert.True(MemoryGroup<byte>.TryAllocate(pool, 50, 10, options, out MemoryGroup<byte> g));
Span<byte> expected = stackalloc byte[10];
expected.Fill((byte)(options == AllocationOptions.Clean ? 0 : 42));
foreach (Memory<byte> 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<S4>();
int requestElements = requestBytes / Unsafe.SizeOf<S4>();
using var g = MemoryGroup<S4>.Allocate(pool, requestElements, alignmentElements);
if (shouldReturnNull)
Assert.Equal(shouldSucceed, MemoryGroup<S4>.TryAllocate(pool, requestElements, alignmentElements, AllocationOptions.None, out MemoryGroup<S4> g));
if (shouldSucceed)
{
Assert.Null(g);
Assert.NotNull(g);
}
else
{
Assert.NotNull(g);
Assert.Null(g);
}
g?.Dispose();
}
internal static void ValidateAllocateMemoryGroup<T>(
@ -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<S4>();
using var g = MemoryGroup<S4>.Allocate(this.MemoryAllocator, length, 32, AllocationOptions.Contiguous);
Assert.Equal(length, g.BufferLength);
Assert.Equal(length, g.TotalLength);
Assert.Equal(1, g.Count);
}
}
}

Loading…
Cancel
Save