Fixed some bugs and modifications to mark

This commit is contained in:
valtermiari 2023-03-15 18:03:08 +01:00
parent 02c9ae0ab4
commit 6840297c08
10 changed files with 114 additions and 24 deletions

View file

@ -6,6 +6,20 @@
#define X_LENGTH 1000;
#define Y_LENGTH 500;
/*
* Description:
* This class is designed to test the Garbage Collector with a mock game,
* that consists of several live objects in the form of players, that in
* turn consists partially of Point objects.
*
* Goal:
* to find out if all the objects are allocated successfully
* and to see if they are reachable from the stack, i.e. they can get marked.
*
* Result:
* all objects gets allocated, but only Game object gets marked.
*/
class Game {
private:
@ -22,12 +36,17 @@ public:
}
Player* create_player(string s, Point pos, Point size, Point dir) {
Player *p = static_cast<Player*>(GC::Heap::alloc(sizeof(Player)));
p->Player(s, pos, size, dir); // This will probably both be allocated on "our" heap as well as the heap for this program
return p;
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
define an alternative constructor, that's actually a method. Since our "alloc" does not call the constructor
of the object
*/
p->init(s, pos, size, dir);
return p;
}
std::vector<Player> create_players(int nr) {
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);
@ -43,7 +62,11 @@ int main() {
gc->check_init();
Game *game = static_cast<Game*>(gc->alloc(sizeof(Game)));
game->create_players(100);
game->create_players(2);
std::cout << "Player size: " << sizeof(Player) << std::endl;
std::cout << "Game size: " << sizeof(Game) << std::endl;
std::cout << "Point size: " << sizeof(Point) << std::endl;
gc->collect(GC::MARK);
gc->print_contents();

View file

@ -12,10 +12,12 @@ Node *create_chain(int depth) {
last_node->id = depth;
last_node->child = nullptr;
nodes.push_back(last_node);
for (int i = 0; i < depth; i++) {
for (size_t i = 0; i < depth; i++) {
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
node->id = depth-i;
node->child = nodes[i-1];
node->child = nodes[i];
//node->child = nodes.at(i-1);
std::cout << "Child of node: " << node << " is: " << node->child << std::endl;
nodes.push_back(node);
}
for (size_t i = 0; i < nodes.size(); i++) {
@ -84,11 +86,14 @@ int main() {
Node *root = static_cast<Node *>(gc->alloc(sizeof(Node)));
root = test_chain(3, false);
Node *root_child = root->child;
std::cout << "Adress of root:\t" << &root << std::endl;
std::cout << "Root points to:\t" << root << std::endl;
std::cout << "Root child:\t" << root->child << std::endl;
std::cout << "Root child:\t" << root_child << std::endl;
std::cout << "Root child, child:\t" << root_child->child << std::endl;
gc->collect(MARK);
gc->collect(GC::MARK);
gc->print_contents();
return 0;
}

View file

@ -35,4 +35,13 @@ public:
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) {
name = n;
position = pos;
size = s;
direction = dir;
}
};