33
44using System . Collections . Concurrent ;
55using System . Diagnostics ;
6+ using System . Diagnostics . CodeAnalysis ;
67using System . Diagnostics . Metrics ;
78
89#nullable enable
@@ -30,11 +31,6 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
3031 /// </summary>
3132 public static int BlockSize => _blockSize ;
3233
33- /// <summary>
34- /// Optional callback to call when pool is being disposed.
35- /// </summary>
36- public Action < PinnedBlockMemoryPool > ? DisposeCallback { get ; set ; }
37-
3834 /// <summary>
3935 /// Thread-safe collection of blocks which are currently in the pool. A slab will pre-allocate all of the block tracking objects
4036 /// and add them to this collection. When memory is requested it is taken from here first, and when it is returned it is re-added.
@@ -57,6 +53,9 @@ internal sealed class PinnedBlockMemoryPool : MemoryPool<byte>, IThreadPoolWorkI
5753
5854 private readonly object _disposeSync = new object ( ) ;
5955
56+ private Action < object ? , PinnedBlockMemoryPool > ? _onPoolDisposed ;
57+ private object ? _onPoolDisposedState ;
58+
6059 /// <summary>
6160 /// This default value passed in to Rent to use the default value for the pool.
6261 /// </summary>
@@ -72,6 +71,15 @@ public PinnedBlockMemoryPool(IMeterFactory meterFactory)
7271 _metrics = new ( meterFactory ) ;
7372 }
7473
74+ /// <summary>
75+ /// Register a callback to call when the pool is being disposed.
76+ /// </summary>
77+ public void OnPoolDisposed ( Action < object ? , PinnedBlockMemoryPool > onPoolDisposed , object ? state = null )
78+ {
79+ _onPoolDisposed = onPoolDisposed ;
80+ _onPoolDisposedState = state ;
81+ }
82+
7583 public override IMemoryOwner < byte > Rent ( int size = AnySize )
7684 {
7785 if ( size > _blockSize )
@@ -214,7 +222,7 @@ protected override void Dispose(bool disposing)
214222 {
215223 _isDisposed = true ;
216224
217- DisposeCallback ? . Invoke ( this ) ;
225+ _onPoolDisposed ? . Invoke ( _onPoolDisposedState , this ) ;
218226
219227 if ( disposing )
220228 {
0 commit comments