{"record":{"id":"5c842243dd8d5624","repo":"SixLabors/ImageSharp","slug":"the-buffer-capacity-of-the-provided-memoryallocator-is","errorCode":null,"errorMessage":"The buffer capacity of the provided MemoryAllocator is insufficient for the requested buffer alignment: {alignment}.","messagePattern":"The buffer capacity of the provided MemoryAllocator is insufficient for the requested buffer alignment: (.+?)\\.","errorType":"exception","errorClass":"InvalidMemoryOperationException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Memory/InvalidMemoryOperationException.cs","lineNumber":36,"sourceCode":"    public InvalidMemoryOperationException(string message)\n        : base(message)\n    {\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"InvalidMemoryOperationException\"/> class.\n    /// </summary>\n    public InvalidMemoryOperationException()\n    {\n    }\n\n    [DoesNotReturn]\n    internal static void ThrowNegativeAllocationException(long length) =>\n        throw new InvalidMemoryOperationException($\"Attempted to allocate a buffer of negative length={length}.\");\n\n    [DoesNotReturn]\n    internal static void ThrowInvalidAlignmentException(long alignment) =>\n        throw new InvalidMemoryOperationException(\n                $\"The buffer capacity of the provided MemoryAllocator is insufficient for the requested buffer alignment: {alignment}.\");\n\n    [DoesNotReturn]\n    internal static void ThrowAllocationOverLimitException(ulong length, long limit) =>\n            throw new InvalidMemoryOperationException($\"Attempted to allocate a buffer of length={length} that exceeded the limit {limit}.\");\n\n    [DoesNotReturn]\n    internal static void ThrowAccumulativeAllocationOverLimitException(long requestedLength, long totalLength, long limit) =>\n            throw new InvalidMemoryOperationException(\n                $\"Attempted to allocate a buffer of length={requestedLength} that would increase the accumulative allocation size to {totalLength}, exceeding the limit {limit}.\");\n}\n","sourceCodeStart":18,"sourceCodeEnd":48,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Memory/InvalidMemoryOperationException.cs#L18-L48","documentation":"Thrown when a caller requests an aligned buffer but the underlying MemoryAllocator cannot hand out blocks large enough to satisfy the requested alignment — i.e. the allocator's per-buffer capacity is smaller than the alignment. ImageSharp fails fast because aligned-beyond-capacity requests cannot be honored.","triggerScenarios":"Calling MemoryAllocator.Allocate<T>(length, Alignment alignment) (or Allocate2D overloads taking alignment) with an alignment value larger than the allocator's configured maximum buffer capacity (e.g. UniformUnmanagedPoolMemoryAllocator with small pool block sizes).","commonSituations":"Configuring a small memory pool (e.g. for low-memory environments) and then using SIMD/vectorized code paths that demand 32/64-byte alignment; supplying Alignment from configuration without checking the allocator's capacity.","solutions":["Increase the allocator's buffer capacity (pool block size) so it is >= the requested alignment when constructing the MemoryAllocator.","Lower the requested Alignment parameter to a value the allocator can satisfy (e.g. 16 or 32 bytes).","Use the default allocator (which has adequate capacity) instead of a custom tightly constrained one for aligned allocations."],"exampleFix":"// before\nvar allocator = new UniformUnmanagedPoolMemoryAllocator(blockSize: 16);\nvar buf = allocator.Allocate<byte>(1024, Alignment.Create(64)); // throws\n// after\nvar allocator = new UniformUnmanagedPoolMemoryAllocator(blockSize: 4096);\nvar buf = allocator.Allocate<byte>(1024, Alignment.Create(64));","handlingStrategy":"validation","validationCode":"if ((int)alignment > allocatorMaxBufferCapacity)\n    throw new ArgumentException($\"Alignment {alignment} exceeds allocator buffer capacity {allocatorMaxBufferCapacity}\");","typeGuard":null,"tryCatchPattern":"try { var buf = allocator.Allocate<T>(length, Alignment.Create(64)); }\ncatch (InvalidMemoryOperationException ex) when (ex.Message.Contains(\"alignment\"))\n{ var buf = allocator.Allocate<T>(length); /* unaligned fallback */ }","preventionTips":["Size the allocator's pool block capacity at or above the largest alignment you request.","Keep requested alignments at the SIMD-friendly minimum (16/32 bytes) rather than arbitrary large values.","Centralize allocator creation so alignment expectations and capacity stay in sync."],"tags":["memory","allocator","alignment","configuration"],"backgroundTag":"invalid-config-value","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}