-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
59 lines (51 loc) · 1.89 KB
/
Program.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
using System;
using System.Numerics;
using Unsafe;
namespace NativeHeap {
class TestObject : NativeObject {
BigInteger bigValue;
Guid guid;
public void Initialize(int num) {
bigValue = new BigInteger(num);
guid = Guid.NewGuid();
}
public override string ToString() => $"{bigValue}: {guid}";
}
class Program {
static void PrintInfo<T>(NativePool<T> pool) where T : NativeObject {
Console.WriteLine($"full: {pool.IsFull}; numObjs: {pool.NumObjects}");
}
static void Main(string[] args) {
const int NumObjs = 10, Iterations = 100;
using (var pool = new NativePool<TestObject>(2, 2)) {
var objs = new TestObject[NumObjs];
var rand = new Random();
for (int i = 0; i < Iterations; i++) {
int index = rand.Next(NumObjs);
var obj = objs[index];
if (obj == null) {
objs[index] = (TestObject)pool.New();
GC.Collect();
objs[index].Initialize(i);
GC.Collect();
} else if (rand.Next() % 2 == 0) {
objs[index].Free();
GC.Collect();
objs[index] = null;
GC.Collect();
}
Console.WriteLine($"iteration {i}: objs[{index}] = {objs[index]?.ToString() ?? "null"}");
PrintInfo(pool);
Console.WriteLine();
GC.Collect();
}
Console.WriteLine("-----");
foreach (var obj in pool) {
Console.WriteLine(obj);
obj.Free();
PrintInfo(pool);
}
}
}
}
}