Fixed bug in sweep and updated tests

This commit is contained in:
valtermiari 2023-03-17 11:45:32 +01:00
parent ce714db3f1
commit 78ccade17d
6 changed files with 56 additions and 32 deletions

View file

@ -18,6 +18,7 @@ namespace GC
enum CollectOption {
MARK=0x1,
SWEEP=0x2,
MARK_SWEEP = 0x3,
FREE=0x4,
COLLECT_ALL=0x7
};
@ -103,6 +104,7 @@ namespace GC
void collect(CollectOption flags); // conditional collection
void check_init(); // print dummy things
void print_contents(); // print dummy things
void print_allocated_chunks(Heap *heap); // print the contents in m_allocated_chunks
void set_profiler(bool mode);
};
}

View file

@ -185,7 +185,7 @@ namespace GC
auto it = worklist.begin();
auto stop = worklist.end();
// for (auto it = worklist.begin(); it != worklist.end();) {
while (it != stop)
while (it != stop) // while (it != stop)
{
Chunk *chunk = *it;
@ -193,10 +193,10 @@ namespace GC
auto c_size = reinterpret_cast<uintptr_t>(chunk->size);
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
cout << "Value of Start:\t\t" << start << endl;
/* cout << "Value of Start:\t\t" << start << endl;
cout << "Start points to:\t" << hex << *start << endl;
cout << "Chunk start:\t\t" << hex << c_start << endl;
cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl;
cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl; */
// Check if the stack pointer aligns with the chunk
if (c_start <= *start && *start < c_end)
@ -212,6 +212,7 @@ namespace GC
it = worklist.erase(it);
// Recursively call mark, to see if the reachable chunk further points to another chunk
mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist);
//mark_step(c_start, c_end, worklist);
}
else
@ -283,10 +284,11 @@ namespace GC
*/
void Heap::sweep(Heap *heap)
{
cout << "--- sweep() was called ---" << endl;
auto iter = heap->m_allocated_chunks.begin();
auto stop = heap->m_allocated_chunks.end();
// for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
while (iter != stop)
// This cannot "iter != stop", results in seg fault, since the end gets updated, I think.
while (iter != heap->m_allocated_chunks.end())
{
Chunk *chunk = *iter;
@ -318,12 +320,14 @@ namespace GC
*/
void Heap::free(Heap *heap)
{
cout << "--- free() was called ---" << endl;
if (heap->m_freed_chunks.size() > FREE_THRESH)
{
while (heap->m_freed_chunks.size())
{
auto chunk = heap->m_freed_chunks.back();
heap->m_freed_chunks.pop_back();
cout << "Freed chunk was deleted" << endl;
delete chunk;
}
}
@ -392,6 +396,8 @@ namespace GC
*/
void Heap::collect(CollectOption flags)
{
set_profiler(true);
if (getProfilerMode())
Profiler::record(CollectStart);
@ -504,4 +510,11 @@ namespace GC
auto heap = Heap::the();
heap->m_profiler_enable = mode;
}
void Heap::print_allocated_chunks(Heap *heap) {
cout << "--- Allocated Chunks ---\n" << endl;
for (auto chunk : heap->m_allocated_chunks) {
print_line(chunk);
}
}
}

View file

@ -32,7 +32,7 @@ namespace GC
auto start = profiler->m_events.begin();
auto end = profiler->m_events.end();
std::ofstream fstr = profiler->createFileStream(); // this is now found
std::ofstream fstr = profiler->createFileStream();
char buffer[22];
std::tm *btm;
std::time_t tt;
@ -42,15 +42,15 @@ namespace GC
{
auto event = *start++;
tt = event->getTimeStamp(); // this is found
tt = event->getTimeStamp();
btm = std::localtime(&tt);
std::strftime(buffer, 22, "%a %T", btm);
fstr << "--------------------------------\n"
<< buffer
<< "\nEvent:\t" << event->TypeToString(); // this is not found
<< "\nEvent:\t" << event->TypeToString();
chunk = event->getChunk(); // this is found
chunk = event->getChunk();
if (chunk) {
fstr << "\nChunk:\t" << chunk->start
@ -69,7 +69,8 @@ namespace GC
std::strftime(buffer, 32, "/profiler/log_%a_%H_%M_%S.txt", ptm);
std::string filename(buffer);
const std::string ABS_PATH = "/home/virre/dev/systemF/org/language/src/GC/";
//const std::string ABS_PATH = "/home/virre/dev/systemF/org/language/src/GC/";
const std::string ABS_PATH = "/Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC";
std::string fullpath = ABS_PATH + filename;
std::ofstream fstr(fullpath);

View file

@ -26,8 +26,8 @@ class Game {
private:
//std::vector<Player*> *players;
std::vector<Player> *players;
std::vector<Player*> *players;
//std::vector<Player> *players;
Point *dimensions;
public:
@ -38,18 +38,18 @@ public:
}
void init() {
//players = static_cast<std::vector<Player*>*>(GC::Heap::alloc(sizeof(Player*) * MAX_PLAYERS));
players = static_cast<std::vector<Player>*>(GC::Heap::alloc(sizeof(Player) * MAX_PLAYERS));
players = static_cast<std::vector<Player*>*>(GC::Heap::alloc(sizeof(Player*) * MAX_PLAYERS));
//players = static_cast<std::vector<Player>*>(GC::Heap::alloc(sizeof(Player) * MAX_PLAYERS));
dimensions = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
dimensions->x = X_LENGTH;
dimensions->y = Y_LENGTH;
}
void add_player(Player *p) {
players->push_back(*p);
players->push_back(p);
}
Player* create_player(string s, Point pos, Point size, Point dir) {
Player* create_player(string *s, Point *pos, Point *size, Point *dir) {
Player *p = static_cast<Player*>(GC::Heap::alloc(sizeof(Player)));
/*
Cannot allocate by new, since it the allocates outside of "out" heap. That also lead so us having to
@ -62,7 +62,13 @@ public:
void create_players(int nr) {
for (int i = 0; i < nr; i++) {
Player *p = create_player(std::to_string(i), Point(i, i), Point(2, 2), Point(0, 0));
std::string *str = static_cast<std::string*>(GC::Heap::alloc(sizeof(std::string)));
Point *pos = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Point *size = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Point *dir = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Player *p = create_player(str, pos, size, dir);
add_player(p);
}
}

View file

@ -78,11 +78,12 @@ int main() {
// long *l = static_cast<long *>(gc->alloc(sizeof(long))); // 0x6-0xd | 0x
// This is allocated outside of the scope of the GC (if gc->init() isn't called), thus garbage
/* long *longs[21];
/* long *longs;
std::cout << "Pointer to ints:\t" << longs << std::endl;
for (int i = 0; i < 21; i++) {
longs[i] = static_cast<long *>(gc->alloc(sizeof(long)));
} */
longs = static_cast<long *>(gc->alloc(sizeof(long)));
longs++;
} */
Node *root = static_cast<Node *>(gc->alloc(sizeof(Node)));
root = test_chain(3, false);
@ -93,7 +94,7 @@ int main() {
std::cout << "Root child, child:\t" << root_child->child << std::endl;
gc->collect(GC::MARK);
gc->collect(GC::COLLECT_ALL);
gc->print_contents();
return 0;
}

View file

@ -15,36 +15,37 @@ class Player {
private:
string name;
Point position;
Point size;
Point direction;
string *name;
Point *position;
Point *size;
Point *direction;
public:
Player() {}
Player(string n, Point pos, Point s, Point dir)
/* Player(string n, Point pos, Point s, Point dir)
: name(n), position(pos.x, pos.y), size(s.x, s.y), direction(dir.x, dir.y)
{}
{} */
void move() {
position.x += direction.x;
position.y += direction.y;
position->x += direction->x;
position->y += direction->y;
}
void set_speed(int dx, int dy) {
direction.x = dx;
direction.y = dy;
direction->x = dx;
direction->y = dy;
}
// This is probably neccessary to initialize an object with our GC
// Since allocation and construction cannot be done at the same time
void init(string n, Point pos, Point s, Point dir) {
void init(string *n, Point *pos, Point *s, Point *dir) {
name = n;
position = pos;
size = s;
direction = dir;
}
};