using System; using System.Collections.Generic; namespace VContainer.Internal { internal static class ListPool { const int DefaultCapacity = 32; private static readonly Stack> _pool = new Stack>(4); /// /// BufferScope supports releasing a buffer with using clause. /// internal readonly struct BufferScope : IDisposable { private readonly List _buffer; public BufferScope(List buffer) { _buffer = buffer; } public void Dispose() { Release(_buffer); } } /// /// Get a buffer from the pool. /// /// internal static List Get() { lock (_pool) { if (_pool.Count == 0) { return new List(DefaultCapacity); } return _pool.Pop(); } } /// /// Get a buffer from the pool. Returning a disposable struct to support recycling via using clause. /// /// /// internal static BufferScope Get(out List buffer) { buffer = Get(); return new BufferScope(buffer); } /// /// Declare a buffer won't be used anymore and put it back to the pool. /// /// internal static void Release(List buffer) { buffer.Clear(); lock (_pool) { _pool.Push(buffer); } } } }