-
-
Notifications
You must be signed in to change notification settings - Fork 846
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
|
||
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}."); | ||
|