| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftStackPool.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using SwiftCollections.Diagnostics; |
| | | 9 | | using SwiftCollections.Lazy; |
| | | 10 | | using System; |
| | | 11 | | using System.Threading; |
| | | 12 | | |
| | | 13 | | namespace SwiftCollections.Pool; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// A specialized pool for managing <see cref="SwiftStack{T}"/> instances, |
| | | 17 | | /// providing efficient reuse to minimize memory allocations and improve performance. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of elements in the stack.</typeparam> |
| | | 20 | | public sealed class SwiftStackPool<T> : SwiftCollectionPool<SwiftStack<T>, T>, IDisposable |
| | | 21 | | { |
| | | 22 | | #region Singleton Instance |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Shared instance of the stack pool, providing a globally accessible pool. |
| | | 26 | | /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal. |
| | | 27 | | /// </summary> |
| | 1 | 28 | | private readonly static SwiftLazyDisposable<SwiftStackPool<T>> _lazyInstance = |
| | 1 | 29 | | new(() => new SwiftStackPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the shared instance of the pool. |
| | | 33 | | /// </summary> |
| | 2 | 34 | | public static SwiftStackPool<T> Shared => _lazyInstance.Value; |
| | | 35 | | |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | #region Fields |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Tracks whether the pool has been disposed. |
| | | 42 | | /// </summary> |
| | | 43 | | private volatile bool _disposed; |
| | | 44 | | |
| | | 45 | | #endregion |
| | | 46 | | |
| | | 47 | | #region Methods |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Rents a stack instance from the pool. If the pool is empty, a new stack is created. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <returns>A <see cref="SwiftStack{T}"/> instance.</returns> |
| | | 53 | | public override SwiftStack<T> Rent() |
| | | 54 | | { |
| | 7 | 55 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>)); |
| | | 56 | | |
| | 6 | 57 | | return base.Rent(); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Releases a stack instance back to the pool for reuse. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="stack">The stack to release.</param> |
| | | 64 | | public override void Release(SwiftStack<T> stack) |
| | | 65 | | { |
| | 5 | 66 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>)); |
| | | 67 | | |
| | 5 | 68 | | if (stack == null) return; |
| | | 69 | | |
| | 3 | 70 | | base.Release(stack); |
| | 3 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Clears all pooled stacks from the pool. |
| | | 75 | | /// </summary> |
| | | 76 | | public override void Clear() |
| | | 77 | | { |
| | 8 | 78 | | if (_disposed) return; |
| | | 79 | | |
| | 6 | 80 | | base.Clear(); |
| | 6 | 81 | | } |
| | | 82 | | |
| | | 83 | | #endregion |
| | | 84 | | |
| | | 85 | | #region IDisposable Implementation |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Releases all resources used by the SwiftStackPool. |
| | | 89 | | /// </summary> |
| | | 90 | | public void Dispose() |
| | | 91 | | { |
| | 6 | 92 | | if (_disposed) |
| | 1 | 93 | | return; |
| | | 94 | | |
| | 5 | 95 | | Clear(); |
| | 5 | 96 | | base.Flush(); |
| | | 97 | | |
| | 5 | 98 | | _disposed = true; |
| | | 99 | | |
| | 5 | 100 | | GC.SuppressFinalize(this); |
| | 5 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Releases the resources used by the SwiftStackPool instance. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <remarks> |
| | | 107 | | /// This finalizer ensures that unmanaged resources are released if Dispose was not called explicitly. |
| | | 108 | | /// It is recommended to call Dispose to release resources deterministically. |
| | | 109 | | /// </remarks> |
| | 3 | 110 | | ~SwiftStackPool() => Dispose(); |
| | | 111 | | |
| | | 112 | | #endregion |
| | | 113 | | } |