Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectPool.cs
Go to the documentation of this file.
1
using
System.Threading
;
2
3
namespace
System.Reflection.Internal
;
4
5
internal
sealed
class
ObjectPool
<T> where T : class
6
{
7
private
struct
Element
8
{
9
internal
T
Value
;
10
}
11
12
private
readonly
Element
[]
_items
;
13
14
private
readonly Func<T>
_factory
;
15
16
internal
ObjectPool
(Func<T> factory)
17
: this(factory,
Environment
.ProcessorCount * 2)
18
{
19
}
20
21
internal
ObjectPool
(Func<T> factory,
int
size)
22
{
23
_factory
= factory;
24
_items
=
new
Element
[size];
25
}
26
27
private
T
CreateInstance
()
28
{
29
return
_factory
();
30
}
31
32
internal
T
Allocate
()
33
{
34
Element
[] items =
_items
;
35
int
num = 0;
36
T val;
37
while
(
true
)
38
{
39
if
(num < items.Length)
40
{
41
val = items[num].
Value
;
42
if
(val !=
null
&& val ==
Interlocked
.
CompareExchange
(ref items[num].
Value
,
null
, val))
43
{
44
break
;
45
}
46
num++;
47
continue
;
48
}
49
val =
CreateInstance
();
50
break
;
51
}
52
return
val;
53
}
54
55
internal
void
Free
(T
obj
)
56
{
57
Element
[] items =
_items
;
58
for
(
int
i = 0; i < items.Length; i++)
59
{
60
if
(items[i].Value ==
null
)
61
{
62
items[i].
Value
=
obj
;
63
break
;
64
}
65
}
66
}
67
}
System.Environment
Definition
Environment.cs:15
System.Reflection.Internal.ObjectPool.ObjectPool
ObjectPool(Func< T > factory)
Definition
ObjectPool.cs:16
System.Reflection.Internal.ObjectPool.Allocate
T Allocate()
Definition
ObjectPool.cs:32
System.Reflection.Internal.ObjectPool._factory
readonly Func< T > _factory
Definition
ObjectPool.cs:14
System.Reflection.Internal.ObjectPool._items
readonly Element[] _items
Definition
ObjectPool.cs:12
System.Reflection.Internal.ObjectPool.ObjectPool
ObjectPool(Func< T > factory, int size)
Definition
ObjectPool.cs:21
System.Reflection.Internal.ObjectPool.CreateInstance
T CreateInstance()
Definition
ObjectPool.cs:27
System.Reflection.Internal.ObjectPool.Free
void Free(T obj)
Definition
ObjectPool.cs:55
System.Reflection.Internal.ObjectPool
Definition
ObjectPool.cs:6
System.Threading.Interlocked.CompareExchange
static int CompareExchange(ref int location1, int value, int comparand)
System.Threading.Interlocked
Definition
Interlocked.cs:9
System.Reflection.Internal
Definition
AbstractMemoryBlock.cs:4
System.Threading
Definition
TaskToApm.cs:3
System.ExceptionArgument.obj
@ obj
System.Reflection.Internal.ObjectPool.Element.Value
T Value
Definition
ObjectPool.cs:9
System.Reflection.Internal.ObjectPool.Element
Definition
ObjectPool.cs:8
source
System.Reflection.Metadata
System.Reflection.Internal
ObjectPool.cs
Generated by
1.10.0