blob: 36aabc3a06ebce5230d5dac6330b0e0b70cc07df (
plain)
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
|
using System;
using System.Threading;
namespace ProtoBuf
{
internal sealed class BufferPool
{
private const int PoolSize = 20;
internal const int BufferLength = 1024;
private static readonly object[] pool = new object[20];
internal static void Flush()
{
for (int i = 0; i < BufferPool.pool.Length; i++)
{
Interlocked.Exchange(ref BufferPool.pool[i], null);
}
}
private BufferPool()
{
}
internal static byte[] GetBuffer()
{
for (int i = 0; i < BufferPool.pool.Length; i++)
{
object obj;
bool flag = (obj = Interlocked.Exchange(ref BufferPool.pool[i], null)) != null;
if (flag)
{
return (byte[])obj;
}
}
return new byte[1024];
}
internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
{
Helpers.DebugAssert(buffer != null);
Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length);
Helpers.DebugAssert(copyFromIndex >= 0);
Helpers.DebugAssert(copyBytes >= 0);
int num = buffer.Length * 2;
bool flag = num < toFitAtLeastBytes;
if (flag)
{
num = toFitAtLeastBytes;
}
byte[] array = new byte[num];
bool flag2 = copyBytes > 0;
if (flag2)
{
Helpers.BlockCopy(buffer, copyFromIndex, array, 0, copyBytes);
}
bool flag3 = buffer.Length == 1024;
if (flag3)
{
BufferPool.ReleaseBufferToPool(ref buffer);
}
buffer = array;
}
internal static void ReleaseBufferToPool(ref byte[] buffer)
{
bool flag = buffer == null;
if (!flag)
{
bool flag2 = buffer.Length == 1024;
if (flag2)
{
for (int i = 0; i < BufferPool.pool.Length; i++)
{
bool flag3 = Interlocked.CompareExchange(ref BufferPool.pool[i], buffer, null) == null;
if (flag3)
{
break;
}
}
}
buffer = null;
}
}
}
}
|