You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// A pool for reusing objects of type <typeparamref name="T"/>.
10
+
/// </summary>
11
+
/// <typeparam name="T">The type to pool objects for.</typeparam>
12
+
/// <remarks>
13
+
/// This implementation keeps a cache of retained objects.
14
+
/// This means that if objects are returned when the pool has already reached "maximumRetained" objects they will be available to be Garbage Collected.
15
+
/// </remarks>
16
+
internalsealedclassObjectPool<T>
17
+
whereT:class
18
+
{
19
+
privatereadonlyFunc<T>createFunc;
20
+
privatereadonlyFunc<T,bool>returnFunc;
21
+
privatereadonlyintmaxCapacity;
22
+
privateintnumItems;
23
+
24
+
privatereadonlyConcurrentQueue<T>items=new();
25
+
privateT?fastItem;
26
+
27
+
/// <summary>
28
+
/// Initializes a new instance of the <see cref="ObjectPool{T}"/> class.
29
+
/// </summary>
30
+
/// <param name="policy">The pooling policy to use.</param>
31
+
publicObjectPool(IPooledObjectPolicy<T>policy)
32
+
:this(policy,Environment.ProcessorCount*2)
33
+
{
34
+
}
35
+
36
+
/// <summary>
37
+
/// Initializes a new instance of the <see cref="ObjectPool{T}"/> class.
38
+
/// </summary>
39
+
/// <param name="policy">The pooling policy to use.</param>
40
+
/// <param name="maximumRetained">The maximum number of objects to retain in the pool.</param>
// no room, clean up the count and drop the object on the floor
98
+
_=Interlocked.Decrement(refthis.numItems);
99
+
returnfalse;
100
+
}
101
+
102
+
returntrue;
103
+
}
104
+
}
105
+
106
+
/// <summary>
107
+
/// Represents a policy for managing pooled objects.
108
+
/// </summary>
109
+
/// <typeparam name="T">The type of object which is being pooled.</typeparam>
110
+
#pragma warning disable SA1201// Elements should appear in the correct order
111
+
internalinterfaceIPooledObjectPolicy<T>
112
+
#pragma warning restore SA1201// Elements should appear in the correct order
113
+
whereT:notnull
114
+
{
115
+
/// <summary>
116
+
/// Create a <typeparamref name="T"/>.
117
+
/// </summary>
118
+
/// <returns>The <typeparamref name="T"/> which was created.</returns>
119
+
publicTCreate();
120
+
121
+
/// <summary>
122
+
/// Runs some processing when an object was returned to the pool. Can be used to reset the state of an object and indicate if the object should be returned to the pool.
123
+
/// </summary>
124
+
/// <param name="obj">The object to return to the pool.</param>
125
+
/// <returns><see langword="true" /> if the object should be returned to the pool. <see langword="false" /> if it's not possible/desirable for the pool to keep the object.</returns>
0 commit comments