summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/MemoryPool.cs
blob: a5aefc1a204e523b0fe9ddec31ff2a51f53ff197 (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;

namespace XUtliPoolLib
{
	public class MemoryPool<T>
	{
		private short m_blockSize = 0;

		private int m_chunkSize = 0;

		private MemoryPool<T>.MemoryChunk m_root = null;

		private MemoryPool<T>.MemoryChunk m_last = null;

		private MemoryPool<T>.MemoryChunk m_lastFreeChunk = null;

		private MemoryPool<T>.MemoryBlock m_freeMemBlock = null;

		private MemoryPool<T>.AllocMemoryBlock m_freeAllocBlock = null;

		public class MemoryChunk
		{
			public T[] mem = null;

			public MemoryPool<T>.MemoryChunk next = null;

			public uint blockBit = 0u;
		}

		public class MemoryBlock
		{
			public MemoryPool<T>.MemoryChunk chunk = null;

			public short start = -1;

			public MemoryPool<T>.MemoryBlock next = null;
		}

		public class AllocMemoryBlock
		{
			public MemoryPool<T>.MemoryBlock[] mem = new MemoryPool<T>.MemoryBlock[32];

			public MemoryPool<T>.AllocMemoryBlock next = null;
		}

		private void Expand()
		{
			MemoryPool<T>.MemoryChunk memoryChunk = new MemoryPool<T>.MemoryChunk();
			memoryChunk.mem = new T[this.m_chunkSize];
			bool flag = this.m_root == null;
			if (flag)
			{
				bool flag2 = this.m_last != null;
				if (flag2)
				{
					XSingleton<XDebug>.singleton.AddErrorLog("root null but last not null!", null, null, null, null, null);
				}
				this.m_root = memoryChunk;
				this.m_last = this.m_root;
			}
			else
			{
				this.m_last.next = memoryChunk;
				this.m_last = memoryChunk;
			}
		}

		private void IndexLastFreeChunk()
		{
			bool flag = this.m_lastFreeChunk == null;
			if (flag)
			{
				this.Expand();
				this.m_lastFreeChunk = this.m_last;
			}
		}

		private MemoryPool<T>.MemoryBlock GetMemoryBlock()
		{
			bool flag = this.m_freeMemBlock == null;
			MemoryPool<T>.MemoryBlock result;
			if (flag)
			{
				result = new MemoryPool<T>.MemoryBlock();
			}
			else
			{
				MemoryPool<T>.MemoryBlock freeMemBlock = this.m_freeMemBlock;
				this.m_freeMemBlock = this.m_freeMemBlock.next;
				result = freeMemBlock;
			}
			return result;
		}

		public void Init(short blockSize, int initChunkCount)
		{
			this.m_blockSize = blockSize;
			this.m_chunkSize = (int)(this.m_blockSize * 32);
			for (int i = 0; i < initChunkCount; i++)
			{
				this.Expand();
			}
			this.m_lastFreeChunk = this.m_root;
		}

		public MemoryPool<T>.AllocMemoryBlock Alloc(int blockCount)
		{
			bool flag = this.m_freeAllocBlock == null;
			MemoryPool<T>.AllocMemoryBlock allocMemoryBlock;
			if (flag)
			{
				allocMemoryBlock = new MemoryPool<T>.AllocMemoryBlock();
			}
			else
			{
				allocMemoryBlock = this.m_freeAllocBlock;
				this.m_freeAllocBlock = this.m_freeAllocBlock.next;
				allocMemoryBlock.next = null;
			}
			this.IndexLastFreeChunk();
			int num = 0;
			do
			{
				uint num2 = 1u;
				short num3 = 0;
				while ((int)num3 < this.m_chunkSize && num < blockCount)
				{
					bool flag2 = (this.m_lastFreeChunk.blockBit & num2) == 0u;
					if (flag2)
					{
						MemoryPool<T>.MemoryBlock memoryBlock = this.GetMemoryBlock();
						memoryBlock.chunk = this.m_lastFreeChunk;
						memoryBlock.start = num3;
						memoryBlock.next = null;
						allocMemoryBlock.mem[num] = memoryBlock;
						this.m_lastFreeChunk.blockBit |= num2;
						num++;
					}
					num2 <<= 1;
					num3 += this.m_blockSize;
				}
				bool flag3 = num2 == uint.MaxValue;
				if (flag3)
				{
					this.m_lastFreeChunk = this.m_lastFreeChunk.next;
				}
				this.IndexLastFreeChunk();
			}
			while (num < blockCount);
			return allocMemoryBlock;
		}

		public void Dealloc(MemoryPool<T>.AllocMemoryBlock amb)
		{
			for (int i = 0; i < amb.mem.Length; i++)
			{
				MemoryPool<T>.MemoryBlock memoryBlock = amb.mem[i];
				bool flag = memoryBlock != null;
				if (flag)
				{
					MemoryPool<T>.MemoryChunk chunk = memoryBlock.chunk;
					bool flag2 = chunk != null;
					if (flag2)
					{
						uint num = 1u << (int)(memoryBlock.start / this.m_blockSize);
						chunk.blockBit &= ~num;
					}
					amb.mem[i] = null;
					bool flag3 = this.m_freeMemBlock == null;
					if (flag3)
					{
						this.m_freeMemBlock = memoryBlock;
					}
					else
					{
						memoryBlock.next = this.m_freeMemBlock;
						this.m_freeMemBlock = memoryBlock;
					}
				}
				bool flag4 = this.m_freeAllocBlock == null;
				if (flag4)
				{
					this.m_freeAllocBlock = amb;
				}
				else
				{
					amb.next = this.m_freeAllocBlock;
					this.m_freeAllocBlock = amb;
				}
			}
		}
	}
}