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
|
using System;
namespace XUtliPoolLib
{
public class SmallBufferPool<T>
{
public T[] buffer;
public BufferBlock[] blocks;
private BlockInfo[] blockInitRef;
public int allocBlockCount = 0;
public void Init(BlockInfo[] blockInit, int tSize)
{
this.blockInitRef = blockInit;
int num = 0;
int num2 = 0;
int i = 0;
int num3 = blockInit.Length;
while (i < num3)
{
BlockInfo blockInfo = blockInit[i];
num += blockInfo.count;
num2 += blockInfo.size * blockInfo.count;
i++;
}
this.buffer = new T[num2];
this.blocks = new BufferBlock[num];
BufferPoolMgr.TotalCount += num2 * tSize + num * 17;
int num4 = 0;
int num5 = 0;
int j = 0;
int num6 = blockInit.Length;
while (j < num6)
{
BlockInfo blockInfo2 = blockInit[j];
int k = 0;
int count = blockInfo2.count;
while (k < count)
{
BufferBlock bufferBlock = this.blocks[num4];
bufferBlock.offset = num5;
bufferBlock.size = 0;
bufferBlock.capacity = blockInfo2.size;
bufferBlock.blockIndex = num4;
bufferBlock.inUse = false;
this.blocks[num4] = bufferBlock;
num5 += blockInfo2.size;
num4++;
k++;
}
j++;
}
}
private bool InnerGetBlock(ref BufferBlock block, int size, int initSize)
{
int num = 0;
int i = 0;
int num2 = this.blockInitRef.Length;
while (i < num2)
{
BlockInfo blockInfo = this.blockInitRef[i];
bool flag = blockInfo.size >= size;
if (flag)
{
int j = num;
int num3 = num + blockInfo.count;
while (j < num3)
{
BufferBlock bufferBlock = this.blocks[j];
bool flag2 = !bufferBlock.inUse;
if (flag2)
{
bufferBlock.size = ((initSize < bufferBlock.capacity) ? initSize : bufferBlock.capacity);
bufferBlock.inUse = true;
this.blocks[j] = bufferBlock;
block = bufferBlock;
this.allocBlockCount++;
return true;
}
j++;
}
}
else
{
num += blockInfo.count;
}
i++;
}
block.blockIndex = -1;
block.capacity = size;
block.size = ((initSize < size) ? initSize : size);
block.inUse = true;
return false;
}
private void InnerGetBlock(ref SmallBuffer<T> sb, int size, int initSize)
{
BufferBlock bufferBlock = default(BufferBlock);
bool flag = this.InnerGetBlock(ref bufferBlock, size, initSize);
if (flag)
{
sb.Init(bufferBlock, this);
sb.DeepClear();
}
else
{
sb.Init(bufferBlock, this, new T[bufferBlock.capacity]);
XSingleton<XDebug>.singleton.AddWarningLog2("not enough buff size:{0}", new object[]
{
bufferBlock.capacity
});
BufferPoolMgr.AllocSize += bufferBlock.capacity;
}
}
public void GetBlock(ref SmallBuffer<T> sb, int size, int initSize = 0)
{
bool isInit = sb.IsInit;
if (!isInit)
{
this.InnerGetBlock(ref sb, size, initSize);
}
}
public void ExpandBlock(ref SmallBuffer<T> sb, int size)
{
this.InnerGetBlock(ref sb, size, 0);
}
public void ReturnBlock(ref SmallBuffer<T> sb)
{
BufferBlock bufferBlock = sb.bufferBlock;
bool inUse = bufferBlock.inUse;
if (inUse)
{
bufferBlock.size = 0;
bufferBlock.inUse = false;
bool flag = bufferBlock.blockIndex >= 0;
if (flag)
{
this.blocks[bufferBlock.blockIndex] = bufferBlock;
this.allocBlockCount--;
}
sb.debugName = "";
sb.UnInit();
}
}
}
}
|