-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHeapAllocatorTests.cs
281 lines (232 loc) · 10.2 KB
/
HeapAllocatorTests.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Rendering;
namespace Unity.Rendering.Tests
{
public class HeapAllocatorTests
{
[Test]
public void BasicTests()
{
var allocator = new HeapAllocator(1000);
allocator.DebugValidateInternalState();
Assert.AreEqual(allocator.FreeSpace, 1000);
HeapBlock b10 = allocator.Allocate(10);
allocator.DebugValidateInternalState();
HeapBlock b100 = allocator.Allocate(100);
allocator.DebugValidateInternalState();
// Check that the allocations have sufficient size.
Assert.GreaterOrEqual(b10.Length, 10);
Assert.GreaterOrEqual(b100.Length, 100);
// Check that the amount of free space has decreased accordingly.
Assert.LessOrEqual(allocator.FreeSpace, 1000 - 100 - 10);
allocator.Release(b10);
allocator.DebugValidateInternalState();
allocator.Release(b100);
allocator.DebugValidateInternalState();
// Everything should now be freed.
Assert.AreEqual(allocator.FreeSpace, 1000);
allocator.Dispose();
}
[Test]
public void AllocateEntireHeap()
{
var allocator = new HeapAllocator(100);
// Check that it's possible to allocate the entire heap.
Assert.AreEqual(allocator.Allocate(100).Length, 100);
allocator.Dispose();
}
[Test]
public void Coalescing()
{
var allocator = new HeapAllocator(100);
allocator.DebugValidateInternalState();
// Try to allocate ten blocks. These should succeed, because the heap should not be fragmented yet.
var blocks10 = Enumerable.Range(0, 10).Select(x => allocator.Allocate(10)).ToArray();
allocator.DebugValidateInternalState();
Assert.IsTrue(blocks10.All(b => b.Length == 10));
Assert.IsTrue(allocator.Full);
// Release all of them.
foreach (var b in blocks10)
{
allocator.Release(b);
allocator.DebugValidateInternalState();
}
// Now try to allocate the entire heap. It should succeed, because everything has been freed.
Assert.AreEqual(allocator.Allocate(100).Length, 100);
allocator.DebugValidateInternalState();
allocator.Dispose();
}
[Test]
public void RandomStressTest()
{
const int HeapSize = 1_000_000;
const int NumBlocks = 1_000;
const int NumRounds = 20;
const int MaxAlloc = 10_000;
const int OperationsPerRound = 4_000;
int numAllocs = 0;
int numReleases = 0;
int numFailed = 0;
var rnd = new System.Random(293875);
var allocator = new HeapAllocator(HeapSize);
var blocks = Enumerable.Range(0, NumBlocks).Select(x => new HeapBlock()).ToArray();
var blockEnds = new SortedSet<ulong>();
// Stress test the allocator by doing a bunch of random allocs and deallocs and
// try to verify that allocator internal asserts don't fire, and free space behaves
// as expected.
for (int i = 0; i < NumRounds; ++i)
{
Assert.IsTrue(allocator.Empty);
// Perform random alloc/dealloc operations
for (int j = 0; j < OperationsPerRound; ++j)
{
ulong before = allocator.FreeSpace;
int b = rnd.Next(NumBlocks);
int size = 0;
if (blocks[b].Empty)
{
size = rnd.Next(1, MaxAlloc);
blocks[b] = allocator.Allocate((ulong)size);
allocator.DebugValidateInternalState();
if (blocks[b].Empty)
{
size = 0;
++numFailed;
}
else
{
size = (int)blocks[b].Length;
bool added = blockEnds.Add(blocks[b].end);
Assert.IsTrue(added);
}
++numAllocs;
}
else
{
size = -(int)blocks[b].Length;
allocator.Release(blocks[b]);
allocator.DebugValidateInternalState();
bool removed = blockEnds.Remove(blocks[b].end);
Assert.IsTrue(removed);
blocks[b] = new HeapBlock();
++numReleases;
}
ulong after = allocator.FreeSpace;
ulong highest = allocator.OnePastHighestUsedAddress;
Assert.AreEqual((long)after, (long)before - size);
if (blockEnds.Count > 0)
Assert.AreEqual(blockEnds.Max, highest);
else
Assert.AreEqual(0, highest);
}
for (int b = 0; b < NumBlocks; ++b)
{
if (!blocks[b].Empty)
{
allocator.Release(blocks[b]);
blocks[b] = new HeapBlock();
blockEnds.Clear();
}
}
Assert.IsTrue(allocator.Empty);
}
Debug.Log($"Allocs: {numAllocs}, Releases: {numReleases}, Failed: {numFailed}");
allocator.Dispose();
}
[Test]
public void AllocationsDontOverlap()
{
// Make sure that allocations given by the allocator are disjoint (i.e. don't alias).
const int HeapSize = 1_000_000;
const int NumBlocks = 1_000;
const int MaxAlloc = 10_000;
const int OperationsPerRound = 10_000;
var rnd = new System.Random(9283572);
var allocator = new HeapAllocator(HeapSize);
var blocks = Enumerable.Range(0, NumBlocks).Select(x => new HeapBlock()).ToArray();
var inUse = new ulong[HeapSize / 8 + 1];
Func<ulong, (ulong, int)> qword = (ulong i) => (i / 64, (int)(i % 64));
// Perform random alloc/dealloc operations
for (int i = 0; i < OperationsPerRound; ++i)
{
int b = rnd.Next(NumBlocks);
const ulong kAllOnes = ~0UL;
int size = 0;
if (blocks[b].Empty)
{
size = rnd.Next(1, MaxAlloc);
blocks[b] = allocator.Allocate((ulong)size);
// Mark the block as allocated, and check that it wasn't allocated.
// Do tests and sets entire qwords at a time so it's fast
var begin = qword(blocks[b].begin);
var end = qword(blocks[b].end);
if (begin.Item1 == end.Item1)
{
ulong qw = begin.Item1;
ulong mask = kAllOnes << begin.Item2;
mask &= ~(kAllOnes << end.Item2);
Assert.IsTrue((inUse[qw] & mask) == 0, "Elements were already allocated");
inUse[qw] |= mask;
}
else
{
ulong qw = begin.Item1;
ulong mask = kAllOnes << begin.Item2;
Assert.IsTrue((inUse[qw] & mask) == 0, "Elements were already allocated");
inUse[qw] |= mask;
for (qw = begin.Item1 + 1; qw < end.Item1; ++qw)
{
mask = kAllOnes;
Assert.IsTrue((inUse[qw] & mask) == 0, "Elements were already allocated");
inUse[qw] |= mask;
}
qw = end.Item1;
mask = ~(kAllOnes << end.Item2);
Assert.IsTrue((inUse[qw] & mask) == 0, "Elements were already allocated");
inUse[qw] |= mask;
}
}
else
{
allocator.Release(blocks[b]);
// Mark the block as not allocated, and check that it was allocated.
var begin = qword(blocks[b].begin);
var end = qword(blocks[b].end);
if (begin.Item1 == end.Item1)
{
ulong qw = begin.Item1;
ulong mask = kAllOnes << begin.Item2;
mask &= ~(kAllOnes << end.Item2);
Assert.IsTrue((inUse[qw] & mask) == mask, "Elements were not allocated");
inUse[qw] &= ~mask;
}
else
{
ulong qw = begin.Item1;
ulong mask = kAllOnes << begin.Item2;
Assert.IsTrue((inUse[qw] & mask) == mask, "Elements were not allocated");
inUse[qw] &= ~mask;
for (qw = begin.Item1 + 1; qw < end.Item1; ++qw)
{
mask = kAllOnes;
Assert.IsTrue((inUse[qw] & mask) == mask, "Elements were not allocated");
inUse[qw] &= ~mask;
}
qw = end.Item1;
mask = ~(kAllOnes << end.Item2);
Assert.IsTrue((inUse[qw] & mask) == mask, "Elements were not allocated");
inUse[qw] &= ~mask;
}
blocks[b] = new HeapBlock();
}
}
allocator.Dispose();
}
}
}