using System; using System.Runtime.CompilerServices; /*https://github.com/sidristij/memory-pools*/ namespace MemoryPools.Memory.Pooling { /// /// This pool returns NEW instance if pool is empty and old if non-empty. /// When got, user can return instance back to pool. If not returned, GC will collect it /// This means, that if you want to detect 'leaks', you should: /// 1) add [CallerFilePath], [CallerLineNumber] to your Init() method parameters /// 2) make finalizer in your type and log saved fileName and lineNumber from (1). /// /// /// MyType Init(int arg0, string arg1 /// #if DEBUG /// , [CallerFilePath] string fileName = default, [CallerLineNumber] int lineNumber = default /// #endif /// ) /// { /// #if DEBUG /// _fileName = fileName; /// _lineNumber = lineNumber; /// #endif /// } /// #if DEBUG /// ~MyType() /// { /// Console.WriteLine($" - {_fileName}:{_lineNumber}"); /// } /// #endif /// public sealed class BucketsBasedCrossThreadsMemoryPool { private BucketsBasedCrossThreadsArrayPool _pool; [ThreadStatic] private static BucketsBasedCrossThreadsMemoryPool _mempool; internal BucketsBasedCrossThreadsArrayPool _arraysPool => _pool ??= new BucketsBasedCrossThreadsArrayPool(); public static BucketsBasedCrossThreadsMemoryPool Shared => _mempool ??= new BucketsBasedCrossThreadsMemoryPool(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public CountdownMemoryOwner Rent(int minBufferSize = -1) { return Pool>.Get().Init(_arraysPool.Rent(minBufferSize), minBufferSize); } } }