-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathObjectPool`1.cs
251 lines (199 loc) · 6.3 KB
/
ObjectPool`1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
namespace MonoGame.Extended.VideoPlayback;
/// <inheritdoc />
/// <summary>
/// An object pool implementation.
/// </summary>
/// <typeparam name="T">Type of objects in the pool.</typeparam>
internal sealed class ObjectPool<T> : DisposableBase
{
/// <summary>
/// Creates a new <see cref="ObjectPool{T}"/> instance.
/// </summary>
/// <param name="collectThreshold">Threshold when calling <see cref="Collect"/>.</param>
/// <param name="alloc">Allocation function.</param>
/// <param name="dealloc">Deallocation function.</param>
internal ObjectPool(int collectThreshold, Func<T> alloc, Action<T> dealloc)
: this(collectThreshold, alloc, dealloc, null)
{
}
/// <summary>
/// Creates a new <see cref="ObjectPool{T}"/> instance.
/// </summary>
/// <param name="collectThreshold">Threshold when calling <see cref="Collect"/>.</param>
/// <param name="alloc">Allocation function.</param>
/// <param name="dealloc">Deallocation function.</param>
/// <param name="reset">Object reset function.</param>
internal ObjectPool(int collectThreshold, Func<T> alloc, Action<T> dealloc, RefAction<T>? reset)
{
_objectsInUse = new HashSet<T>();
_freeObjects = new LinkedList<T>();
_collectThreshold = collectThreshold;
_alloc = alloc;
_dealloc = dealloc;
_reset = reset;
}
/// <summary>
/// Acquires an object. If there is no object available, it will call <see cref="Alloc"/> to create one.
/// </summary>
/// <returns>Acquired or allocated object.</returns>
internal T Acquire()
{
EnsureNotDisposed();
if (NumberOfObjectsInUse == Count)
{
return Alloc();
}
if (_freeObjects.Count == 0)
{
throw new ApplicationException("This should not happen");
}
// Move the object from free list to using list
var firstNode = _freeObjects.First;
_freeObjects.RemoveFirst();
var obj = firstNode!.Value;
_objectsInUse.Add(obj);
return obj;
}
/// <summary>
/// Allocates a new object and returns it.
/// </summary>
/// <returns>Allocated object.</returns>
internal T Alloc()
{
EnsureNotDisposed();
var alloc = _alloc;
Debug.Assert(alloc != null, nameof(alloc) + " != null");
var obj = alloc();
_objectsInUse.Add(obj);
return obj;
}
/// <summary>
/// Releases an object.
/// </summary>
/// <param name="obj">The object to release. If this value is <see langword="null"/>, the method does nothing.</param>
internal bool Release(T? obj)
{
return Release(obj, false);
}
/// <summary>
/// Releases and deallocates an object.
/// </summary>
/// <param name="obj">The object to destroy. If this value is <see langword="null"/>, the method does nothing.</param>
/// <returns><see langword="true"/> if the object is found in the pool and successfully destroyed, otherwise <see langword="false"/>.</returns>
internal bool Destroy(T? obj)
{
return Release(obj, true);
}
/// <summary>
/// Deallocates all unused objects.
/// If number of objects in this pool is smaller than the threshold set when creating the pool, this method does nothing.
/// </summary>
internal void Collect()
{
EnsureNotDisposed();
var dealloc = _dealloc;
Debug.Assert(dealloc != null, nameof(dealloc) + " != null");
if (NumberOfObjectsInUse <= _collectThreshold)
{
// No need to collect.
return;
}
if (_freeObjects.Count == 0)
{
// Nothing to collect.
return;
}
// Create a temporary storage and perform deallocation on it.
var freeObjects = new List<T>(_freeObjects);
_freeObjects.Clear();
var objects = freeObjects.ToArray();
foreach (var obj in objects)
{
dealloc(obj);
}
}
/// <summary>
/// Destroys all objects in this pool. Used for preparing restarting playback.
/// </summary>
internal void Reset()
{
var dealloc = _dealloc;
Debug.Assert(dealloc != null, nameof(dealloc) + " != null");
foreach (var obj in _objectsInUse)
{
dealloc(obj);
}
foreach (var obj in _freeObjects)
{
dealloc(obj);
}
_objectsInUse.Clear();
_freeObjects.Clear();
}
/// <summary>
/// Number of objects in this pool.
/// </summary>
internal int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
get => _objectsInUse.Count + _freeObjects.Count;
}
/// <summary>
/// Number of objects in use.
/// </summary>
internal int NumberOfObjectsInUse
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
get => _objectsInUse.Count;
}
protected override void Dispose(bool disposing)
{
Reset();
_alloc = null;
_dealloc = null;
_reset = null;
}
private bool Release(T? obj, bool deallocObject)
{
EnsureNotDisposed();
if (ReferenceEquals(obj, null))
{
return false;
}
if (!_objectsInUse.Contains(obj))
{
return false;
}
if (deallocObject)
{
// Remove the object from using list
_objectsInUse.Remove(obj);
var dealloc = _dealloc;
Debug.Assert(dealloc != null, nameof(dealloc) + " != null");
dealloc(obj);
}
else
{
// Move the object from using list to free list
_objectsInUse.Remove(obj);
_reset?.Invoke(ref obj);
_freeObjects.AddLast(obj);
}
return true;
}
[ItemNotNull]
private readonly HashSet<T> _objectsInUse;
[ItemNotNull]
private readonly LinkedList<T> _freeObjects;
private readonly int _collectThreshold;
private Func<T>? _alloc;
private Action<T>? _dealloc;
private RefAction<T>? _reset;
}