-
Notifications
You must be signed in to change notification settings - Fork 0
/
pauselessgc.cpp
179 lines (159 loc) · 6.06 KB
/
pauselessgc.cpp
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
// pauselessgc.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <sstream>
#include <random>
#include "CollectableHash.h"
static int64_t identity_counter = 0;
class RandomCounted : public Collectable
{
public:
int points_at_me;
int64_t identity;
InstancePtr<RandomCounted> first;
InstancePtr<RandomCounted> second;
void set_first(RootPtr<RandomCounted> o2, RootPtr<RandomCounted> o) {
MEM_TEST();
assert(o2.var->owned);
assert(o.get() == o2.get());
if (nullptr != o.get()) ++o->points_at_me;
if (nullptr != first.get())--(first->points_at_me);
first = o;
}
void set_second(RootPtr<RandomCounted> o2, RootPtr<RandomCounted> o) {
MEM_TEST();
assert(o2.var->owned);
assert(o.get() == o2.get());
if (nullptr != o.get()) ++o->points_at_me;
if (nullptr != second.get())--second->points_at_me;
second = o;
}
RandomCounted(int i) :points_at_me(0),identity(i) {}
~RandomCounted()
{
// if (0 == points_at_me) std::cout << "Correct delete\n";
// else std::cout << "*** incorrect or cycle delete. Holds "<<points_at_me<<"\n";
}
int total_instance_vars() const {
MEM_TEST();
return 2; }
InstancePtrBase* index_into_instance_vars(int num) {
MEM_TEST();
switch (num) {
case 0: return &first;
case 1: return &second;
}
}
size_t my_size() const {
MEM_TEST();
return sizeof(*this); }
};
const int Testlen = 100000;
RootPtr<CollectableString> int_to_string(int a)
{
std::stringstream ss;
ss << a;
return cnew(CollectableString(ss.str().c_str()));
}
void mutator_thread()
{
if (!GC::CombinedThread)GC::init_thread();
thread_local RootPtr<RandomCounted>* bunch = new RootPtr<RandomCounted>[Testlen];
thread_local std::default_random_engine generator;
thread_local std::uniform_int_distribution<int> distribution(0, Testlen - 1);
//RootPtr<CollectableHashTable<CollectableString,RandomCounted> > hash = cnew2template(CollectableHashTable<CollectableString, RandomCounted>());
RootPtr<CollectableVector<RandomCounted> > vec= cnew (CollectableVector<RandomCounted>());
for (int k = 1; k <= 50; ++k) {
vec->clear();
for (int i = 0; i < Testlen; ++i)
{
GC::safe_point();
//RootPtr<CollectableString> index = int_to_string(i);
//hash->insert_or_assign(index, cnew(RandomCounted(i)));
int b = vec->size();
bunch[i] = cnew(RandomCounted(i));
//vec->push_front(hash[index]);
vec->push_back(bunch[i]);
//vec->insert(vec->end(), hash[index]);
//assert(t);
int v = vec->size();
assert(v == i + 1);
assert(vec[i].get() == bunch[i].get());
}
//distribution(generator);
for (int j = 0; j < 2; ++j) {
//GC::safe_point();
for (int i = 0; i < Testlen; ++i)
{
//RootPtr<CollectableString> ind = int_to_string(i);
GC::safe_point();
{
int j = distribution(generator);
//RootPtr<CollectableString> jind = int_to_string(j);
//assert(j >= 0);
//assert(j < Testlen);
//assert(!bunch[i]->deleted);
//assert(!bunch[j]->deleted);
//RootPtr<RandomCounted> jrc = hash[jind];
//hash[ind]->set_first(jrc, jrc);
bunch[i]->set_first(bunch[j], bunch[j]);
//assert(bunch[i].var->owned);
//assert(bunch[j].var->owned);
}
{
int j = distribution(generator);
//RootPtr<CollectableString> jind = int_to_string(j);
//RootPtr<RandomCounted> jrc = hash[jind];
//assert(j >= 0);
//assert(j < Testlen);
//assert(!bunch[i]->deleted);
//assert(!bunch[j]->deleted);
bunch[i]->set_second(bunch[j], bunch[j]);
//hash[ind]->set_second(jrc, jrc);
//assert(bunch[i].var->owned);
//assert(bunch[j].var->owned);
}
}
for (int i = 0; i < Testlen >> 1; ++i)
{
GC::safe_point();
RootPtr<CollectableString> index = int_to_string(Testlen - i - 1);
//hash->insert_or_assign(index, cnew(RandomCounted(Testlen - i - 1)));
bunch[i] = cnew(RandomCounted(Testlen-i-1));
//assert(bunch[i].var->owned);
//assert(!bunch[i]->deleted);
}
}
for (int i = 0; i < Testlen; ++i) {
int s = vec->size();
assert(s == Testlen-i);
//assert(vec->back()->identity == i);
RootPtr< RandomCounted> p;
bool t = vec->pop_back(p);
assert(t);
int s2 = vec->size();
int h = p->identity;
assert(h == Testlen-1-i);
}
int s = vec->size();
assert(s == 0);
}
}
int main()
{
std::cout << "Hello World!\n";
GC::init();
//auto m2 = std::thread(mutator_thread);
mutator_thread();
GC::exit_collect_thread();
//m2.join();
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file