Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BufferPool.cs
Go to the documentation of this file.
1using System;
3
5
6public static class BufferPool
7{
8 private const int SMALL_BUFFER_SIZE = 32;
9
10 private const int MEDIUM_BUFFER_SIZE = 256;
11
12 private const int LARGE_BUFFER_SIZE = 16384;
13
14 private static object bufferLock = new object();
15
17
19
21
22 public static CachedBuffer Request(int size)
23 {
25 {
26 if (size <= 32)
27 {
28 if (SmallBufferQueue.Count == 0)
29 {
30 return new CachedBuffer(new byte[32]);
31 }
32 return SmallBufferQueue.Dequeue().Activate();
33 }
34 if (size <= 256)
35 {
36 if (MediumBufferQueue.Count == 0)
37 {
38 return new CachedBuffer(new byte[256]);
39 }
40 return MediumBufferQueue.Dequeue().Activate();
41 }
42 if (size <= 16384)
43 {
44 if (LargeBufferQueue.Count == 0)
45 {
46 return new CachedBuffer(new byte[16384]);
47 }
48 return LargeBufferQueue.Dequeue().Activate();
49 }
50 return new CachedBuffer(new byte[size]);
51 }
52 }
53
54 public static CachedBuffer Request(byte[] data, int offset, int size)
55 {
57 Buffer.BlockCopy(data, offset, cachedBuffer.Data, 0, size);
58 return cachedBuffer;
59 }
60
61 public static void Recycle(CachedBuffer buffer)
62 {
63 int length = buffer.Length;
65 {
66 if (length <= 32)
67 {
68 SmallBufferQueue.Enqueue(buffer);
69 }
70 else if (length <= 256)
71 {
73 }
74 else if (length <= 16384)
75 {
76 LargeBufferQueue.Enqueue(buffer);
77 }
78 }
79 }
80
81 public static void PrintBufferSizes()
82 {
84 {
85 Console.WriteLine("SmallBufferQueue.Count: " + SmallBufferQueue.Count);
86 Console.WriteLine("MediumBufferQueue.Count: " + MediumBufferQueue.Count);
87 Console.WriteLine("LargeBufferQueue.Count: " + LargeBufferQueue.Count);
89 }
90 }
91}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static void WriteLine()
Definition Console.cs:733
static CachedBuffer Request(int size)
Definition BufferPool.cs:22
static CachedBuffer Request(byte[] data, int offset, int size)
Definition BufferPool.cs:54
static Queue< CachedBuffer > LargeBufferQueue
Definition BufferPool.cs:20
static Queue< CachedBuffer > SmallBufferQueue
Definition BufferPool.cs:16
static void Recycle(CachedBuffer buffer)
Definition BufferPool.cs:61
static Queue< CachedBuffer > MediumBufferQueue
Definition BufferPool.cs:18