< Summary

Information
Class: SwiftCollections.Pool.SwiftStackPool<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftStackPool.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 113
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Shared()100%11100%
Rent()100%11100%
Release(...)100%22100%
Clear()100%22100%
Dispose()100%22100%
Finalize()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Pool/Default/SwiftStackPool.cs

#LineLine coverage
 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
 8using SwiftCollections.Diagnostics;
 9using SwiftCollections.Lazy;
 10using System;
 11using System.Threading;
 12
 13namespace 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>
 20public 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>
 128    private readonly static SwiftLazyDisposable<SwiftStackPool<T>> _lazyInstance =
 129        new(() => new SwiftStackPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 30
 31    /// <summary>
 32    /// Gets the shared instance of the pool.
 33    /// </summary>
 234    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    {
 755        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>));
 56
 657        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    {
 566        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>));
 67
 568        if (stack == null) return;
 69
 370        base.Release(stack);
 371    }
 72
 73    /// <summary>
 74    /// Clears all pooled stacks from the pool.
 75    /// </summary>
 76    public override void Clear()
 77    {
 878        if (_disposed) return;
 79
 680        base.Clear();
 681    }
 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    {
 692        if (_disposed)
 193            return;
 94
 595        Clear();
 596        base.Flush();
 97
 598        _disposed = true;
 99
 5100        GC.SuppressFinalize(this);
 5101    }
 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>
 3110    ~SwiftStackPool() => Dispose();
 111
 112    #endregion
 113}