DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.5.0" />

 MemoryAllocator

public static class MemoryAllocator
Represents interop layer between .NET memory pools and MemoryAllocator<T>.
using System.Buffers; using System.Runtime.CompilerServices; namespace DotNext.Buffers { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class MemoryAllocator { [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] private static MemoryOwner<T> Allocate<[System.Runtime.CompilerServices.Nullable(2)] T>(this ArrayPool<T> pool, int length) { return new MemoryOwner<T>(pool, length); } public static MemoryAllocator<T> ToAllocator<[System.Runtime.CompilerServices.Nullable(2)] T>(this ArrayPool<T> pool) { return pool.Allocate<T>; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] private static MemoryOwner<T> Allocate<[System.Runtime.CompilerServices.Nullable(2)] T>(this MemoryPool<T> pool, int length) { return new MemoryOwner<T>(pool, length); } public static MemoryAllocator<T> ToAllocator<[System.Runtime.CompilerServices.Nullable(2)] T>(this MemoryPool<T> pool) { return pool.Allocate<T>; } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static MemoryOwner<T> Invoke<[System.Runtime.CompilerServices.Nullable(2)] T>(this MemoryAllocator<T> allocator, int length, bool exactSize) { MemoryOwner<T> owner = allocator(length); if (!exactSize) MemoryOwner<T>.Expand(ref owner); return owner; } } }