< 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: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 97
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
 1using System;
 2using System.Threading;
 3
 4namespace SwiftCollections.Pool;
 5
 6/// <summary>
 7/// A specialized pool for managing <see cref="SwiftStack{T}"/> instances,
 8/// providing efficient reuse to minimize memory allocations and improve performance.
 9/// </summary>
 10/// <typeparam name="T">The type of elements in the stack.</typeparam>
 11public sealed class SwiftStackPool<T> : SwiftCollectionPool<SwiftStack<T>, T>, IDisposable
 12{
 13    #region Singleton Instance
 14
 15    /// <summary>
 16    /// Shared instance of the stack pool, providing a globally accessible pool.
 17    /// Uses <see cref="SwiftLazyDisposable{T}"/> to ensure lazy initialization and proper disposal.
 18    /// </summary>
 119    private readonly static SwiftLazyDisposable<SwiftStackPool<T>> _lazyInstance =
 220        new(() => new SwiftStackPool<T>(), LazyThreadSafetyMode.ExecutionAndPublication);
 21
 22    /// <summary>
 23    /// Gets the shared instance of the pool.
 24    /// </summary>
 225    public static SwiftStackPool<T> Shared => _lazyInstance.Value;
 26
 27    #endregion
 28
 29    #region Fields
 30
 31    /// <summary>
 32    /// Tracks whether the pool has been disposed.
 33    /// </summary>
 34    private volatile bool _disposed;
 35
 36    #endregion
 37
 38    #region Methods
 39
 40    /// <summary>
 41    /// Rents a stack instance from the pool. If the pool is empty, a new stack is created.
 42    /// </summary>
 43    /// <returns>A <see cref="SwiftStack{T}"/> instance.</returns>
 44    public override SwiftStack<T> Rent()
 745    {
 746        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>));
 47
 648        return base.Rent();
 649    }
 50
 51    /// <summary>
 52    /// Releases a stack instance back to the pool for reuse.
 53    /// </summary>
 54    /// <param name="stack">The stack to release.</param>
 55    public override void Release(SwiftStack<T> stack)
 356    {
 357        SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(SwiftStackPool<T>));
 58
 359        if (stack == null) return;
 60
 361        base.Release(stack);
 362    }
 63
 64    /// <summary>
 65    /// Clears all pooled stacks from the pool.
 66    /// </summary>
 67    public override void Clear()
 568    {
 569        if (_disposed) return;
 70
 571        base.Clear();
 572    }
 73
 74    #endregion
 75
 76    #region IDisposable Implementation
 77
 78    /// <summary>
 79    /// Releases all resources used by the SwiftStackPool.
 80    /// </summary>
 81    public void Dispose()
 582    {
 583        if (_disposed)
 184            return;
 85
 486        Clear();
 487        base.Flush();
 88
 489        _disposed = true;
 90
 491        GC.SuppressFinalize(this);
 592    }
 93
 394    ~SwiftStackPool() => Dispose();
 95
 96    #endregion
 97}