Skip to content

Commit effb9ab

Browse files
author
Oren (electricessence)
committed
Updated with improved nullable support.
1 parent 7480129 commit effb9ab

19 files changed

+129
-110
lines changed

source/.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[*.cs]
2+
3+
# CA1707: Identifiers should not contain underscores
4+
dotnet_diagnostic.CA1707.severity = silent
5+
6+
# CA1303: Do not pass literals as localized parameters
7+
dotnet_diagnostic.CA1303.severity = silent
8+
9+
# CA1051: Do not declare visible instance fields
10+
dotnet_diagnostic.CA1051.severity = silent

source/Array/InterlockedArrayObjectPool.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class InterlockedArrayObjectPool<T> : ObjectPoolBase<T>
1414
where T : class
1515
{
1616

17-
public InterlockedArrayObjectPool(Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY)
17+
public InterlockedArrayObjectPool(Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY)
1818
: base(factory, recycler, disposer, capacity)
1919
{
2020
Pool = new ReferenceContainer<T>[capacity - 1];
@@ -33,6 +33,7 @@ public InterlockedArrayObjectPool(Func<T> factory, int capacity = DEFAULT_CAPACI
3333
public override int Count
3434
=> Pool.Count(e => e.Value != null) + PocketCount;
3535

36+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Should never be null.")]
3637
protected virtual bool Store(ReferenceContainer<T>[] p, T item, int index)
3738
=> p[index].TrySave(item);
3839

@@ -43,7 +44,7 @@ protected override bool Receive(T item)
4344

4445
for (var i = 0; i < len; i++)
4546
{
46-
if (Store(elements, item, i))
47+
if (Store(elements!, item, i))
4748
{
4849
var m = MaxStored;
4950
if (i >= m) Interlocked.CompareExchange(ref MaxStored, m + MaxStoredIncrement, m);
@@ -55,7 +56,7 @@ protected override bool Receive(T item)
5556
return false;
5657
}
5758

58-
protected override T TryRelease()
59+
protected override T? TryRelease()
5960
{
6061
// We missed getting the first item or it wasn't there.
6162
var elements = Pool;
@@ -74,7 +75,7 @@ protected override T TryRelease()
7475
protected override void OnDispose()
7576
{
7677
base.OnDispose();
77-
Pool = null;
78+
Pool = null!;
7879
MaxStored = 0;
7980
}
8081

source/Array/OptimisticArrayObjectPool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class OptimisticArrayObjectPool<T> : InterlockedArrayObjectPool<T>
1212
where T : class
1313
{
1414

15-
public OptimisticArrayObjectPool(Func<T> factory, Action<T> recycler, int capacity = DEFAULT_CAPACITY)
15+
public OptimisticArrayObjectPool(Func<T> factory, Action<T>? recycler, int capacity = DEFAULT_CAPACITY)
1616
: base(factory, recycler, null /* disposer not applicable here */, capacity)
1717
{ }
1818

@@ -25,6 +25,7 @@ protected override bool SaveToPocket(T item)
2525
=> Pocket.SetIfNull(item);
2626

2727
// As suggested by Roslyn's implementation, don't worry about interlocking here. It's okay if a few get loose.
28+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Should never be null.")]
2829
protected override bool Store(ReferenceContainer<T>[] p, T item, int index)
2930
=> p[index].SetIfNull(item);
3031
}

source/Collection/CollectionWrapperObjectPool.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class CollectionWrapperObjectPool<T, TCollection> : TrimmableGenericColle
88
where T : class
99
where TCollection : class, ICollection<T>
1010
{
11-
public CollectionWrapperObjectPool(TCollection pool, Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY, bool countTrackingEnabled = true)
11+
public CollectionWrapperObjectPool(TCollection pool, Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY, bool countTrackingEnabled = true)
1212
: base(pool, factory, recycler, disposer, capacity, countTrackingEnabled)
1313
{
1414
}
@@ -35,9 +35,9 @@ protected override bool Receive(T item)
3535
return false;
3636
}
3737

38-
protected override T TryRelease()
38+
protected override T? TryRelease()
3939
{
40-
retry:
40+
retry:
4141

4242
var p = Pool;
4343
var item = p?.FirstOrDefault();
@@ -47,7 +47,7 @@ protected override T TryRelease()
4747
* This implementation is in place for reference more than practice. Sub classes should override. */
4848

4949
bool wasRemoved;
50-
lock (p) wasRemoved = p.Remove(item);
50+
lock (p!) wasRemoved = p.Remove(item);
5151
if (!wasRemoved) goto retry;
5252

5353
return item;

source/Collection/ConcurrentQueueObjectPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class ConcurrentQueueObjectPool<T> : TrimmableCollectionObjectPool
77
where T : class
88
{
99

10-
public ConcurrentQueueObjectPool(Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY)
10+
public ConcurrentQueueObjectPool(Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY)
1111
: base(new ConcurrentQueue<T>(), factory, recycler, disposer, capacity)
1212
{
1313
}
@@ -31,7 +31,7 @@ protected override bool Receive(T item)
3131
return true;
3232
}
3333

34-
protected override T TryRelease()
34+
protected override T? TryRelease()
3535
{
3636
var p = Pool;
3737
if (p == null) return null;

source/Collection/ConcurrentStackObjectPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class ConcurrentStackObjectPool<T> : TrimmableCollectionObjectPool
77
where T : class
88
{
99

10-
public ConcurrentStackObjectPool(Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY)
10+
public ConcurrentStackObjectPool(Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY)
1111
: base(new ConcurrentStack<T>(), factory, recycler, disposer, capacity)
1212
{
1313
}
@@ -26,7 +26,7 @@ protected override bool Receive(T item)
2626
return true;
2727
}
2828

29-
protected override T TryRelease()
29+
protected override T? TryRelease()
3030
{
3131
var p = Pool;
3232
if (p == null) return null;

source/Collection/QueueObjectPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class QueueObjectPool<T> : TrimmableCollectionObjectPoolBase<T, Qu
77
where T : class
88
{
99

10-
public QueueObjectPool(Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY)
10+
public QueueObjectPool(Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY)
1111
: base(
1212
new Queue<T>(Math.Min(DEFAULT_CAPACITY, capacity)) /* Very very slight speed improvment when capacity is initially set. */,
1313
factory, recycler, disposer, capacity, false)
@@ -34,7 +34,7 @@ protected override bool Receive(T item)
3434
return false;
3535
}
3636

37-
protected override T TryRelease()
37+
protected override T? TryRelease()
3838
{
3939
var p = Pool;
4040
if (p != null && p.Count != 0)

source/Collection/StackObjectPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class StackObjectPool<T> : TrimmableCollectionObjectPoolBase<T, St
77
where T : class
88
{
99

10-
public StackObjectPool(Func<T> factory, Action<T> recycler, Action<T> disposer, int capacity = DEFAULT_CAPACITY)
10+
public StackObjectPool(Func<T> factory, Action<T>? recycler, Action<T>? disposer, int capacity = DEFAULT_CAPACITY)
1111
: base(
1212
new Stack<T>(Math.Min(DEFAULT_CAPACITY, capacity)) /* Very very slight speed improvment when capacity is set. */,
1313
factory, recycler, disposer, capacity, false)
@@ -34,7 +34,7 @@ protected override bool Receive(T item)
3434
return false;
3535
}
3636

37-
protected override T TryRelease()
37+
protected override T? TryRelease()
3838
{
3939
var p = Pool;
4040
if (p != null && p.Count != 0)

source/GlobalSuppressions.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

source/IObjectPool.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
4+
using System.Diagnostics.Contracts;
45

56
namespace Open.Disposable
67
{
@@ -30,13 +31,17 @@ public interface IObjectPool<T> : IDisposable
3031
/// </summary>
3132
/// <param name="item">The item to return if available. Will be null if none avaialable.</param>
3233
/// <returns>True if an item is provided.</returns>
33-
bool TryTake(out T item);
34+
#if NETSTANDARD2_1
35+
bool TryTake([NotNullWhen(true)] out T? item);
36+
#else
37+
bool TryTake(out T? item);
38+
#endif
3439

3540
/// <summary>
3641
/// If the pool has an item currently avaialable, removes it from the pool and returns it.
3742
/// </summary>
3843
/// <returns>The item to return if available. Will be null if none avaialable.</returns>
39-
T TryTake();
44+
T? TryTake();
4045

4146

4247
/// <summary>
@@ -64,6 +69,9 @@ public static class ObjectPoolExtensions
6469
public static void Give<T>(this IObjectPool<T> target, IEnumerable<T> items)
6570
where T : class
6671
{
72+
if (target is null) throw new ArgumentNullException(nameof(target));
73+
Contract.EndContractBlock();
74+
6775
if (items == null) return;
6876
foreach (var i in items)
6977
target.Give(i);
@@ -80,6 +88,9 @@ public static void Give<T>(this IObjectPool<T> target, IEnumerable<T> items)
8088
public static void Give<T>(this IObjectPool<T> target, T item1, T item2, params T[] items)
8189
where T : class
8290
{
91+
if (target is null) throw new ArgumentNullException(nameof(target));
92+
Contract.EndContractBlock();
93+
8394
target.Give(item1);
8495
target.Give(item2);
8596
target.Give(items);

0 commit comments

Comments
 (0)