-
-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
V2 - Limit all memory allocations in the MemoryAllocator layer
- 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, net6.0, 6.0.x, true, -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 (windows-latest, net472, -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 (windows-latest, netcoreapp3.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, netcoreapp2.1, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, net472, -x86, 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 (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 (ubuntu-latest, net5.0, -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 (ubuntu-latest, netcoreapp3.1, -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, net5.0, -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, net472, -x64, false)
Check warning on line 23 in src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs GitHub Actions / Build (windows-latest, net472, -x86, 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 (windows-latest, netcoreapp3.1, -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, net5.0, -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}."); | ||
|