File tree Expand file tree Collapse file tree 3 files changed +16
-11
lines changed Expand file tree Collapse file tree 3 files changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -70,8 +70,6 @@ public void Free(object item)
70
70
return ;
71
71
}
72
72
73
- IPoolSource < T > elementSource = _arePooledItems ? PoolFactory . GetSource < T > ( ) : null ;
74
-
75
73
for ( int i = 0 ; i < items . Length ; i ++ )
76
74
{
77
75
object itm = items [ i ] ;
@@ -85,10 +83,6 @@ public void Free(object item)
85
83
{
86
84
obj . Source . Free ( obj ) ;
87
85
}
88
- else if ( elementSource != null )
89
- {
90
- elementSource . Free ( obj ) ;
91
- }
92
86
}
93
87
}
94
88
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public interface IPoolObject : IDisposable
7
7
/// <summary>
8
8
/// The source pool this item came from
9
9
/// </summary>
10
- IPoolSource Source { get ; }
10
+ IPoolSource Source { get ; set ; }
11
11
12
12
/// <summary>
13
13
/// Instruct the <see cref="IPoolSource"/> to ignore this object
Original file line number Diff line number Diff line change @@ -9,26 +9,37 @@ internal class ObjectPool<T> : IPoolSource<T> where T : class
9
9
10
10
private readonly Type _poolType ;
11
11
private readonly List < T > _pool ;
12
+ private readonly bool _isPooledInterface ;
12
13
13
14
public ObjectPool ( )
14
15
{
15
16
_poolType = typeof ( T ) ;
16
17
_pool = new List < T > ( ) ;
18
+ _isPooledInterface = typeof ( IPoolObject ) . IsAssignableFrom ( typeof ( T ) ) ;
17
19
}
18
20
19
21
public T Get ( )
20
22
{
23
+ T item ;
21
24
lock ( _pool )
22
25
{
23
26
if ( _pool . Count == 0 )
24
27
{
25
- return ( T ) Activator . CreateInstance ( typeof ( T ) ) ;
28
+ item = ( T ) Activator . CreateInstance ( typeof ( T ) ) ;
26
29
}
30
+ else
31
+ {
32
+ item = _pool [ 0 ] ;
33
+ _pool . RemoveAt ( 0 ) ;
34
+ }
35
+ }
27
36
28
- T item = _pool [ 0 ] ;
29
- _pool . RemoveAt ( 0 ) ;
30
- return item ;
37
+ if ( _isPooledInterface && item is IPoolObject pooled )
38
+ {
39
+ pooled . Source = this ;
31
40
}
41
+
42
+ return item ;
32
43
}
33
44
34
45
public void Free ( object item )
You can’t perform that action at this time.
0 commit comments