forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferPool.cs
More file actions
85 lines (76 loc) · 2.7 KB
/
BufferPool.cs
File metadata and controls
85 lines (76 loc) · 2.7 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
using System;
using System.Diagnostics;
using System.Threading;
namespace ServiceStack.Redis
{
/// <summary>
/// Courtesy of @marcgravell
/// http://code.google.com/p/protobuf-net/source/browse/trunk/protobuf-net/BufferPool.cs
/// </summary>
internal class BufferPool
{
internal static void Flush()
{
for (int i = 0; i < pool.Length; i++)
{
Interlocked.Exchange(ref pool[i], null); // and drop the old value on the floor
}
}
private BufferPool() { }
const int PoolSize = 1000; //1.45MB
//internal const int BufferLength = 1450; //MTU size - some headers
private static readonly object[] pool = new object[PoolSize];
internal static byte[] GetBuffer(int bufferSize)
{
return bufferSize > RedisConfig.BufferPoolMaxSize
? new byte[bufferSize]
: GetBuffer();
}
internal static byte[] GetBuffer()
{
object tmp;
for (int i = 0; i < pool.Length; i++)
{
if ((tmp = Interlocked.Exchange(ref pool[i], null)) != null)
return (byte[])tmp;
}
return new byte[RedisConfig.BufferLength];
}
internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes)
{
Debug.Assert(buffer != null);
Debug.Assert(toFitAtLeastBytes > buffer.Length);
Debug.Assert(copyFromIndex >= 0);
Debug.Assert(copyBytes >= 0);
// try doubling, else match
int newLength = buffer.Length * 2;
if (newLength < toFitAtLeastBytes) newLength = toFitAtLeastBytes;
var newBuffer = new byte[newLength];
if (copyBytes > 0)
{
Buffer.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes);
}
if (buffer.Length == RedisConfig.BufferLength)
{
ReleaseBufferToPool(ref buffer);
}
buffer = newBuffer;
}
internal static void ReleaseBufferToPool(ref byte[] buffer)
{
if (buffer == null) return;
if (buffer.Length == RedisConfig.BufferLength)
{
for (int i = 0; i < pool.Length; i++)
{
if (Interlocked.CompareExchange(ref pool[i], buffer, null) == null)
{
break; // found a null; swapped it in
}
}
}
// if no space, just drop it on the floor
buffer = null;
}
}
}