forked from yck1509/NativePool
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
59 lines (51 loc) · 1.89 KB
/
Copy pathProgram.cs
File metadata and controls
59 lines (51 loc) · 1.89 KB
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
usingSystem;
usingSystem.Numerics;
usingUnsafe;
namespaceNativeHeap{
classTestObject:NativeObject{
BigIntegerbigValue;
Guidguid;
publicvoidInitialize(intnum){
bigValue=newBigInteger(num);
guid=Guid.NewGuid();
}
publicoverridestringToString()=>$"{bigValue}: {guid}";
}
classProgram{
staticvoidPrintInfo<T>(NativePool<T>pool)whereT:NativeObject{
Console.WriteLine($"full: {pool.IsFull}; numObjs: {pool.NumObjects}");
}
staticvoidMain(string[]args){
constintNumObjs=10,Iterations=100;
using(varpool=newNativePool<TestObject>(2,2)){
varobjs=newTestObject[NumObjs];
varrand=newRandom();
for(inti=0;i<Iterations;i++){
intindex=rand.Next(NumObjs);
varobj=objs[index];
if(obj==null){
objs[index]=(TestObject)pool.New();
GC.Collect();
objs[index].Initialize(i);
GC.Collect();
}elseif(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(varobjinpool){
Console.WriteLine(obj);
obj.Free();
PrintInfo(pool);
}
}
}
}
}