Game test!

Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
Victor Olin 2023-03-10 11:08:15 +01:00
parent 24daa73a39
commit 02c9ae0ab4
10 changed files with 162 additions and 69 deletions

View file

@ -12,6 +12,9 @@ DBGFLAGS = -g
advance:
$(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out
file:
$(CC) $(WFLAGS) $(STDFLAGS) tests/file.cpp -o tests/file.out
heap:
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp
@ -35,6 +38,10 @@ linker_vg:
make linker
valgrind $(VGFLAGS) tests/linker.out
game:
rm -f tests/game.out
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) tests/game.cpp lib/heap.cpp -o tests/game.out
extern_lib:
rm -f lib/heap.o lib/libheap.so tests/extern_lib.out
$(CC) $(STDFLAGS) -c -fPIC -o lib/heap.o lib/heap.cpp

View file

@ -1,8 +1,9 @@
#pragma once
#include <time.h>
#include <ctime>
#include <iostream>
#include <list>
#include <time.h>
#include "chunk.hpp"
@ -26,7 +27,7 @@ namespace GC
// make const
GCEventType m_type;
std::time_t m_timestamp;
Chunk *m_chunk;
Chunk *m_chunk = nullptr;
public:
GCEvent(GCEventType type)

View file

@ -7,19 +7,21 @@
#include <vector>
#include "chunk.hpp"
#include "profiler.hpp"
#define HEAP_SIZE 65536
#define MARK (uint)0x1
#define SWEEP (uint)0x2
#define FREE (uint)0x4
#define COLLECT_ALL (uint)0x7
#define FREE_THRESH (uint)20
namespace GC
{
enum CollectOption {
MARK=0x1,
SWEEP=0x2,
FREE=0x4,
COLLECT_ALL=0x7
};
class Heap
{
@ -53,14 +55,10 @@ namespace GC
return *iter;
}
void collect();
void sweep(Heap *heap);
uintptr_t *try_recycle_chunks(size_t size);
void free(Heap *heap);
void free_overlap(Heap *heap);
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> &worklist);
void print_line(Chunk *chunk);
void print_worklist(std::vector<Chunk *> &list);
inline static bool getProfilerMode() {
auto heap = Heap::the();
return heap->m_profiler_enable;
}
inline static Heap *m_instance = nullptr;
const char *m_heap;
@ -72,6 +70,15 @@ namespace GC
std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks;
void collect();
void sweep(Heap *heap);
uintptr_t *try_recycle_chunks(size_t size);
void free(Heap *heap);
void free_overlap(Heap *heap);
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> &worklist);
void print_line(Chunk *chunk);
void print_worklist(std::vector<Chunk *> &list);
public:
/**
* These are the only two functions which are exposed
@ -92,7 +99,7 @@ namespace GC
m_instance = new Heap();
return m_instance;
}
void collect(uint flags); // conditional collection
void collect(CollectOption flags); // conditional collection
void check_init(); // print dummy things
void print_contents(); // print dummy things
void set_profiler(bool mode);

View file

@ -24,6 +24,7 @@ namespace GC {
std::vector<GCEvent *> m_events;
std::ofstream createFileStream();
public:
static void record(GCEventType type);

View file

@ -175,6 +175,8 @@ namespace GC
*/
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk *> &worklist)
{
if (getProfilerMode())
Profiler::record(MarkStart);
int counter = 0;
// To find adresses thats in the worklist
for (; start < end; start++)
@ -202,6 +204,8 @@ namespace GC
if (!chunk->marked)
{
if (getProfilerMode())
Profiler::record(ChunkMarked, chunk);
chunk->marked = true;
// Remove the marked chunk from the worklist
it = worklist.erase(it);
@ -247,6 +251,7 @@ namespace GC
{
// Add the unmarked chunks to freed chunks and remove from
// the list of allocated chunks
Profiler::record(ChunkSwept, chunk);
heap->m_freed_chunks.push_back(chunk);
iter = heap->m_allocated_chunks.erase(iter);
}
@ -336,8 +341,10 @@ namespace GC
*
* @param flags Bitmap of flags
*/
void Heap::collect(uint flags)
void Heap::collect(CollectOption flags)
{
if (getProfilerMode())
Profiler::record(CollectStart);
cout << "DEBUG COLLECT\nFLAGS: ";
if (flags & MARK)

View file

@ -33,14 +33,31 @@ namespace GC
auto end = profiler->m_events.end();
std::ofstream fstr = profiler->createFileStream();
char buffer[22];
std::tm *btm;
std::time_t tt;
Chunk *chunk;
while (start != end)
{
auto event = *start++;
fstr << "--------------------------------"
<< event->TypeToString()
<< event->getTimeStamp
tt = event->getTimeStamp();
btm = std::localtime(&tt);
std::strftime(buffer, 22, "%a %T", btm);
fstr << "--------------------------------\n"
<< buffer
<< "\nEvent:\t" << event->TypeToString();
chunk = event->getChunk();
if (chunk) {
fstr << "\nChunk:\t" << chunk->start
<< "\n Size: " << chunk->size
<< "\n Mark: " << chunk->marked;
}
fstr << "--------------------------------\n" << std::endl;
}
}
@ -48,11 +65,14 @@ namespace GC
{
std::time_t tt = std::time(NULL);
std::tm *ptm = std::localtime(&tt);
char buffer[32];
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/";
std::string fullpath = ABS_PATH + filename;
std::ofstream fstr(buffer);
std::ofstream fstr(fullpath);
return fstr;
}
}

View file

@ -6,43 +6,39 @@
#include <stdlib.h>
int main() {
// list<char> l;
// char c = 'a';
// for (int i = 1; i <= 5; i++) {
// l.push_back(c++);
// }
using namespace std;
using TimeStamp = std::chrono::_V2::system_clock::time_point;
// auto iter = l.begin();
// auto stop = l.end();
list<char> l;
char c = 'a';
for (int i = 1; i <= 5; i++) {
l.push_back(c++);
}
// while (iter != stop) {
// cout << *iter << " ";
auto iter = l.begin();
auto stop = l.end();
while (iter != stop) {
cout << *iter << " ";
// iter++;
// }
// cout << endl;
// iter = l.begin();
// while (*iter != *stop) {
// cout << *iter << " ";
// iter++;
// }
// cout << endl;
iter++;
}
cout << endl;
iter = l.begin();
while (*iter != *stop) {
cout << *iter << " ";
iter++;
}
cout << endl;
// cout << "rebased" << endl;
// cout << "iter: " << *iter << "\nstop: " << *stop << endl;
// TimeStamp ts = std::chrono::system_clock::now();
// std::time_t tt = std::chrono::system_clock::to_time_t(ts);
// std::string tstr = std::ctime(&tt);
// tstr.resize(tstr.size()-1);
// std::cout << tstr << std::endl;
char buffer[31];
std::time_t tt = std::time(NULL);
std::tm *ptm = std::localtime(&tt);
std::strftime(buffer, 31, "/profiler/log_%a_%H_%M_%S.txt", ptm);
std::cout << buffer << std::endl;
cout << "rebased" << endl;
cout << "iter: " << *iter << "\nstop: " << *stop << endl;
TimeStamp ts = std::chrono::system_clock::now();
std::time_t tt = std::chrono::system_clock::to_time_t(ts);
std::string tstr = std::ctime(&tt);
tstr.resize(tstr.size()-1);
std::cout << tstr << std::endl;
return 0;
}

26
src/GC/tests/file.cpp Normal file
View file

@ -0,0 +1,26 @@
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
#include <time.h>
int main()
{
char buffer[31];
std::time_t tt = std::time(NULL);
std::tm *ptm = std::localtime(&tt);
std::strftime(buffer, 31, "/logs/log_%a_%H_%M_%S.txt", ptm);
std::cout << buffer << std::endl;
const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/";
std::string path = TESTS_PATH + "/testlog.txt";
std::ofstream testF(path);
testF << "hellow yorld";
testF.close();
return 0;
}

View file

@ -1,6 +1,7 @@
#include <vector>
#include "player.hpp"
#include "heap.hpp"
#define X_LENGTH 1000;
#define Y_LENGTH 500;
@ -14,12 +15,38 @@ private:
public:
Game() {
dimensions = {X_LENGTH, Y_LENGTH};
}
Game() : dimensions(1000, 500) {}
void add_player(Player& p) {
players.push_back(p);
}
};
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;
}
std::vector<Player> 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);
}
}
};
int main() {
GC::Heap::init();
GC::Heap *gc = GC::Heap::debug_the();
gc->check_init();
Game *game = static_cast<Game*>(gc->alloc(sizeof(Game)));
game->create_players(100);
gc->collect(GC::MARK);
gc->print_contents();
return 0;
}

View file

@ -2,6 +2,14 @@
using std::string;
class Point {
public:
int x, y;
Point(int _x, int _y) : x(_x), y(_y) {}
};
class Player {
private:
@ -13,12 +21,9 @@ private:
public:
Player(string n, Point pos, Point s, Point dir) {
name = n;
position = pos;
size = s;
direction = 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;
@ -31,7 +36,3 @@ public:
}
};
struct Point {
int x, y;
};