-
-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Memory | |
/// <typeparam name="T">The element type.</typeparam> | ||
internal abstract partial class MemoryGroup<T> : IMemoryGroup<T>, IDisposable | ||
where T : struct | ||
{ | ||
{ | ||
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (ubuntu-latest, net5.0, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (ubuntu-latest, netcoreapp3.1, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (ubuntu-latest, net6.0, 6.0.x, true, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, netcoreapp2.1, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, net6.0, 6.0.x, true, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, net5.0, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (macos-latest, netcoreapp3.1, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (macos-latest, net6.0, 6.0.x, true, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (macos-latest, net5.0, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, netcoreapp3.1, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, net472, -x64, false)
|
||
private static readonly int ElementSize = Unsafe.SizeOf<T>(); | ||
|
||
private MemoryGroupSpanCache memoryGroupSpanCache; | ||
|
@@ -43,7 +43,7 @@ private MemoryGroup(int bufferLength, long totalLength) | |
/// <inheritdoc /> | ||
public bool IsValid { get; private set; } = true; | ||
|
||
public MemoryGroupView<T> View { get; private set; } | ||
public MemoryGroupView<T> View { get; private set; } = null!; | ||
|
||
/// <inheritdoc /> | ||
public abstract Memory<T> this[int index] { get; } | ||
|
@@ -85,12 +85,14 @@ public static MemoryGroup<T> Allocate( | |
{ | ||
int bufferCapacityInBytes = allocator.GetBufferCapacityInBytes(); | ||
Guard.NotNull(allocator, nameof(allocator)); | ||
Guard.MustBeGreaterThanOrEqualTo(totalLengthInElements, 0, nameof(totalLengthInElements)); | ||
Guard.MustBeGreaterThanOrEqualTo(bufferAlignmentInElements, 0, nameof(bufferAlignmentInElements)); | ||
|
||
int blockCapacityInElements = bufferCapacityInBytes / ElementSize; | ||
if (totalLengthInElements < 0) | ||
{ | ||
throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of negative length={totalLengthInElements}."); | ||
} | ||
|
||
if (bufferAlignmentInElements > blockCapacityInElements) | ||
int blockCapacityInElements = bufferCapacityInBytes / ElementSize; | ||
if (bufferAlignmentInElements < 0 || bufferAlignmentInElements > blockCapacityInElements) | ||
{ | ||
throw new InvalidMemoryOperationException( | ||
$"The buffer capacity of the provided MemoryAllocator is insufficient for the requested buffer alignment: {bufferAlignmentInElements}."); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using SixLabors.ImageSharp.Memory; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
|
||
using Xunit; | ||
|
||
|
@@ -342,5 +343,27 @@ public void PublicMemoryGroup_IsMemoryGroupView() | |
Assert.False(mgBefore.IsValid); | ||
Assert.NotSame(mgBefore, buffer1.MemoryGroup); | ||
} | ||
|
||
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (ubuntu-latest, net5.0, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (ubuntu-latest, netcoreapp3.1, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (ubuntu-latest, net6.0, 6.0.x, true, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (windows-latest, netcoreapp2.1, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (windows-latest, net5.0, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (macos-latest, netcoreapp3.1, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (macos-latest, net6.0, 6.0.x, true, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (macos-latest, net5.0, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (windows-latest, netcoreapp3.1, -x64, false)
Check warning on line 346 in tests/ImageSharp.Tests/Memory/Buffer2DTests.cs GitHub Actions / Build (windows-latest, net472, -x64, false)
|
||
public static TheoryData<Size> InvalidLengths { get; set; } = new() | ||
{ | ||
{ new(-1, -1) }, | ||
{ new(32768, 32769) }, | ||
{ new(32769, 32768) } | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidLengths))] | ||
public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException(Size size) | ||
=> Assert.Throws<InvalidMemoryOperationException>(() => this.MemoryAllocator.Allocate2D<Rgba32>(size.Width, size.Height)); | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidLengths))] | ||
public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException_Size(Size size) | ||
=> Assert.Throws<InvalidMemoryOperationException>(() => this.MemoryAllocator.Allocate2D<Rgba32>(new Size(size))); | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidLengths))] | ||
public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException_OverAligned(Size size) | ||
=> Assert.Throws<InvalidMemoryOperationException>(() => this.MemoryAllocator.Allocate2DOveraligned<Rgba32>(size.Width, size.Height, 1)); | ||
} | ||
} |