From ce714db3f1b28626c44add4cfa1c05b74ac312b2 Mon Sep 17 00:00:00 2001 From: valtermiari Date: Thu, 16 Mar 2023 17:06:44 +0100 Subject: [PATCH] Some improvments to Game test --- src/GC/include/heap.hpp | 2 +- src/GC/lib/heap.cpp | 66 ++++++++++++++++++++++++----------------- src/GC/tests/game.cpp | 32 ++++++++++++++------ src/GC/tests/player.hpp | 3 ++ 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 9f5ba41..3f03587 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -78,7 +78,7 @@ namespace GC void mark(uintptr_t *start, const uintptr_t *end, std::vector &worklist); void print_line(Chunk *chunk); void print_worklist(std::vector &list); - void mark_step(uintptr_t memory_location, std::vector &worklist); + void mark_step(uintptr_t start, uintptr_t end, std::vector &worklist); public: /** diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index 617eeab..50883cd 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -7,7 +7,8 @@ #include #include -#include "../include/heap.hpp" +//#include "../include/heap.hpp" +#include using std::cout, std::endl, std::vector, std::hex, std::dec; @@ -175,11 +176,11 @@ namespace GC */ void Heap::mark(uintptr_t *start, const uintptr_t *end, vector &worklist) { - cout << "--- Marked was called ---" << endl; + cout << "--- mark() was called ---\n" << endl; if (getProfilerMode()) Profiler::record(MarkStart); // To find adresses thats in the worklist - for (; start < end; start++) + for (; start <= end; start++) { auto it = worklist.begin(); auto stop = worklist.end(); @@ -192,6 +193,7 @@ namespace GC auto c_size = reinterpret_cast(chunk->size); auto c_end = reinterpret_cast(c_start + c_size); + 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; @@ -209,12 +211,8 @@ namespace GC // Remove the marked chunk from the worklist it = worklist.erase(it); // Recursively call mark, to see if the reachable chunk further points to another chunk - // Previously -> - // mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist); - // Testing this, it should move to the next adress and end shouldn't be of concern - // since it is a static size of pointers - // New -> mark((uintptr_t *) c_end, (uintptr_t *) (c_end + sizeof(uintptr_t)), worklist); - mark_step(c_end, worklist); + mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist); + //mark_step(c_start, c_end, worklist); } else { @@ -232,34 +230,48 @@ namespace GC // Testing a strategy where if a pointer on the stack is pointing to a chunk, nested chunks, // that are not located on the stack frame, will possibly be adjecent to the found chunk, // allowing for a different, more efficient strategy, that doesn't have to scan the stack frame - void Heap::mark_step(uintptr_t memory_location, vector &worklist) { + void Heap::mark_step(uintptr_t start, uintptr_t end, vector &worklist) { - auto it = worklist.begin(); - auto end = worklist.end(); + // Should loop through the chunk size, such that if the object holds a pointer, + // wherever that pointer is located in the object, that pointer, to another chunk, + // gets detected - while (it != end) { + cout << "--- mark_step() was called ---\n" << endl; + for (; start <= end; start += sizeof(uintptr_t)) { - Chunk *chunk = *it; - auto c_start = reinterpret_cast(chunk->start); - auto c_size = reinterpret_cast(chunk->size); - auto c_end = reinterpret_cast(c_start + c_size); + auto it = worklist.begin(); + auto end = worklist.end(); + + while (it != end) { - if (c_start <= memory_location && memory_location < c_end) { + Chunk *chunk = *it; + auto c_start = reinterpret_cast(chunk->start); + auto c_size = reinterpret_cast(chunk->size); + auto c_end = reinterpret_cast(c_start + c_size); - if (!chunk->marked) { - // Mark the chunk and erase it from the worklist - chunk->marked = true; - it = worklist.erase(it); - // Update the memory location we want to look at - memory_location = c_end; + cout << "Value of Start:\t\t" << start << endl; + cout << "Chunk start:\t\t" << hex << c_start << endl; + cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl; + + if (c_start <= start && start < c_end) { + + if (!chunk->marked) { + // Mark the chunk and erase it from the worklist + chunk->marked = true; + it = worklist.erase(it); + cout << "Marked this chunk ^\n" << endl; + // Update the memory location we want to look at + //memory_location = c_end; + mark_step(c_start, c_end, worklist); + } + else { + it++; + } } else { it++; } } - else { - it++; - } } } diff --git a/src/GC/tests/game.cpp b/src/GC/tests/game.cpp index b4c1cc5..6a1bca8 100644 --- a/src/GC/tests/game.cpp +++ b/src/GC/tests/game.cpp @@ -3,8 +3,9 @@ #include "player.hpp" #include "heap.hpp" -#define X_LENGTH 1000; -#define Y_LENGTH 500; +#define X_LENGTH 1000 +#define Y_LENGTH 500 +#define MAX_PLAYERS 100 /* * Description: @@ -20,19 +21,32 @@ * all objects gets allocated, but only Game object gets marked. */ + class Game { private: - std::vector players; - Point dimensions; + //std::vector *players; + std::vector *players; + Point *dimensions; public: - Game() : dimensions(1000, 500) {} + Game() { + dimensions->x = X_LENGTH; + dimensions->y = Y_LENGTH; + } - void add_player(Player& p) { - players.push_back(p); + void init() { + //players = static_cast*>(GC::Heap::alloc(sizeof(Player*) * MAX_PLAYERS)); + players = static_cast*>(GC::Heap::alloc(sizeof(Player) * MAX_PLAYERS)); + dimensions = static_cast(GC::Heap::alloc(sizeof(Point))); + dimensions->x = X_LENGTH; + dimensions->y = Y_LENGTH; + } + + void add_player(Player *p) { + players->push_back(*p); } Player* create_player(string s, Point pos, Point size, Point dir) { @@ -49,19 +63,19 @@ 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)); - add_player(*p); + add_player(p); } } }; - int main() { GC::Heap::init(); GC::Heap *gc = GC::Heap::debug_the(); gc->check_init(); Game *game = static_cast(gc->alloc(sizeof(Game))); + game->init(); game->create_players(2); std::cout << "Player size: " << sizeof(Player) << std::endl; diff --git a/src/GC/tests/player.hpp b/src/GC/tests/player.hpp index 6a554e8..d9251e1 100644 --- a/src/GC/tests/player.hpp +++ b/src/GC/tests/player.hpp @@ -7,6 +7,7 @@ class Point { public: int x, y; + Point() {} Point(int _x, int _y) : x(_x), y(_y) {} }; @@ -21,6 +22,8 @@ private: public: + Player() {} + 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) {}