forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferPool.cs
More file actions
172 lines (145 loc) · 4.89 KB
/
BufferPool.cs
File metadata and controls
172 lines (145 loc) · 4.89 KB
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
using System;
namespace ServiceStack.Text.Pools
{
/// <summary>
/// Courtesy of @marcgravell
/// https://github.com/mgravell/protobuf-net/blob/master/src/protobuf-net/BufferPool.cs
/// </summary>
public sealed class BufferPool
{
public static void Flush()
{
lock (Pool)
{
for (var i = 0; i < Pool.Length; i++)
Pool[i] = null;
}
}
private BufferPool() { }
private const int POOL_SIZE = 20;
public const int BUFFER_LENGTH = 1450; //<= MTU - DJB
private static readonly CachedBuffer[] Pool = new CachedBuffer[POOL_SIZE];
public static byte[] GetBuffer()
{
return GetBuffer(BUFFER_LENGTH);
}
public static byte[] GetBuffer(int minSize)
{
byte[] cachedBuff = GetCachedBuffer(minSize);
return cachedBuff ?? new byte[minSize];
}
public static byte[] GetCachedBuffer(int minSize)
{
lock (Pool)
{
var bestIndex = -1;
byte[] bestMatch = null;
for (var i = 0; i < Pool.Length; i++)
{
var buffer = Pool[i];
if (buffer == null || buffer.Size < minSize)
{
continue;
}
if (bestMatch != null && bestMatch.Length < buffer.Size)
{
continue;
}
var tmp = buffer.Buffer;
if (tmp == null)
{
Pool[i] = null;
}
else
{
bestMatch = tmp;
bestIndex = i;
}
}
if (bestIndex >= 0)
{
Pool[bestIndex] = null;
}
return bestMatch;
}
}
/// <remarks>
/// https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element
/// </remarks>
private const int MaxByteArraySize = int.MaxValue - 56;
public 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 newLength = buffer.Length * 2;
if (newLength < 0)
{
newLength = MaxByteArraySize;
}
if (newLength < toFitAtLeastBytes) newLength = toFitAtLeastBytes;
if (copyBytes == 0)
{
ReleaseBufferToPool(ref buffer);
}
var newBuffer = GetCachedBuffer(toFitAtLeastBytes) ?? new byte[newLength];
if (copyBytes > 0)
{
Buffer.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
ReleaseBufferToPool(ref buffer);
}
buffer = newBuffer;
}
public static void ReleaseBufferToPool(ref byte[] buffer)
{
if (buffer == null) return;
lock (Pool)
{
var minIndex = 0;
var minSize = int.MaxValue;
for (var i = 0; i < Pool.Length; i++)
{
var tmp = Pool[i];
if (tmp == null || !tmp.IsAlive)
{
minIndex = 0;
break;
}
if (tmp.Size < minSize)
{
minIndex = i;
minSize = tmp.Size;
}
}
Pool[minIndex] = new CachedBuffer(buffer);
}
buffer = null;
}
private class CachedBuffer
{
private readonly WeakReference _reference;
public int Size { get; }
public bool IsAlive => _reference.IsAlive;
public byte[] Buffer => (byte[])_reference.Target;
public CachedBuffer(byte[] buffer)
{
Size = buffer.Length;
_reference = new WeakReference(buffer);
}
}
}
internal sealed class Helpers
{
private Helpers() { }
[System.Diagnostics.Conditional("DEBUG")]
internal static void DebugAssert(bool condition)
{
#if DEBUG
if (!condition && System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break();
System.Diagnostics.Debug.Assert(condition);
#endif
}
}
}