using MemoryPools.Memory; using System; using System.Runtime.CompilerServices; /*https://github.com/sidristij/memory-pools*/ namespace MemoryPools { public static class Pool where T : class, new() { private static readonly DefaultObjectPool _freeObjectsQueue = new DefaultObjectPool(new DefaultPooledObjectPolicy()); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Get() { return _freeObjectsQueue.Get(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(T1 instance) where T1 : T { if (instance != null) _freeObjectsQueue.Return(instance); } } public static class Pool { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Get() where T : class, new() { return Pool.Get(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Return(T instance) where T : class, new() { Pool.Return(instance); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static CountdownMemoryOwner GetBuffer(int size) { return InternalArraysPool.Rent(size, false); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static CountdownMemoryOwner GetBufferFrom(ReadOnlySpan source) { return InternalArraysPool.RentFrom(source, false); } } }