Game test!
Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
parent
24daa73a39
commit
02c9ae0ab4
10 changed files with 162 additions and 69 deletions
|
|
@ -12,6 +12,9 @@ DBGFLAGS = -g
|
||||||
advance:
|
advance:
|
||||||
$(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out
|
$(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out
|
||||||
|
|
||||||
|
file:
|
||||||
|
$(CC) $(WFLAGS) $(STDFLAGS) tests/file.cpp -o tests/file.out
|
||||||
|
|
||||||
heap:
|
heap:
|
||||||
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp
|
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp
|
||||||
|
|
||||||
|
|
@ -35,6 +38,10 @@ linker_vg:
|
||||||
make linker
|
make linker
|
||||||
valgrind $(VGFLAGS) tests/linker.out
|
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:
|
extern_lib:
|
||||||
rm -f lib/heap.o lib/libheap.so tests/extern_lib.out
|
rm -f lib/heap.o lib/libheap.so tests/extern_lib.out
|
||||||
$(CC) $(STDFLAGS) -c -fPIC -o lib/heap.o lib/heap.cpp
|
$(CC) $(STDFLAGS) -c -fPIC -o lib/heap.o lib/heap.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <time.h>
|
#include <ctime>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "chunk.hpp"
|
#include "chunk.hpp"
|
||||||
|
|
||||||
|
|
@ -26,7 +27,7 @@ namespace GC
|
||||||
// make const
|
// make const
|
||||||
GCEventType m_type;
|
GCEventType m_type;
|
||||||
std::time_t m_timestamp;
|
std::time_t m_timestamp;
|
||||||
Chunk *m_chunk;
|
Chunk *m_chunk = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GCEvent(GCEventType type)
|
GCEvent(GCEventType type)
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,21 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "chunk.hpp"
|
#include "chunk.hpp"
|
||||||
|
#include "profiler.hpp"
|
||||||
|
|
||||||
#define HEAP_SIZE 65536
|
#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
|
#define FREE_THRESH (uint)20
|
||||||
|
|
||||||
namespace GC
|
namespace GC
|
||||||
{
|
{
|
||||||
|
|
||||||
|
enum CollectOption {
|
||||||
|
MARK=0x1,
|
||||||
|
SWEEP=0x2,
|
||||||
|
FREE=0x4,
|
||||||
|
COLLECT_ALL=0x7
|
||||||
|
};
|
||||||
|
|
||||||
class Heap
|
class Heap
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -53,14 +55,10 @@ namespace GC
|
||||||
return *iter;
|
return *iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
void collect();
|
inline static bool getProfilerMode() {
|
||||||
void sweep(Heap *heap);
|
auto heap = Heap::the();
|
||||||
uintptr_t *try_recycle_chunks(size_t size);
|
return heap->m_profiler_enable;
|
||||||
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 Heap *m_instance = nullptr;
|
inline static Heap *m_instance = nullptr;
|
||||||
const char *m_heap;
|
const char *m_heap;
|
||||||
|
|
@ -72,6 +70,15 @@ namespace GC
|
||||||
std::vector<Chunk *> m_allocated_chunks;
|
std::vector<Chunk *> m_allocated_chunks;
|
||||||
std::vector<Chunk *> m_freed_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:
|
public:
|
||||||
/**
|
/**
|
||||||
* These are the only two functions which are exposed
|
* These are the only two functions which are exposed
|
||||||
|
|
@ -92,7 +99,7 @@ namespace GC
|
||||||
m_instance = new Heap();
|
m_instance = new Heap();
|
||||||
return m_instance;
|
return m_instance;
|
||||||
}
|
}
|
||||||
void collect(uint flags); // conditional collection
|
void collect(CollectOption flags); // conditional collection
|
||||||
void check_init(); // print dummy things
|
void check_init(); // print dummy things
|
||||||
void print_contents(); // print dummy things
|
void print_contents(); // print dummy things
|
||||||
void set_profiler(bool mode);
|
void set_profiler(bool mode);
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ namespace GC {
|
||||||
|
|
||||||
std::ofstream createFileStream();
|
std::ofstream createFileStream();
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void record(GCEventType type);
|
static void record(GCEventType type);
|
||||||
static void record(GCEventType type, Chunk *chunk);
|
static void record(GCEventType type, Chunk *chunk);
|
||||||
|
|
|
||||||
|
|
@ -175,6 +175,8 @@ namespace GC
|
||||||
*/
|
*/
|
||||||
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk *> &worklist)
|
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk *> &worklist)
|
||||||
{
|
{
|
||||||
|
if (getProfilerMode())
|
||||||
|
Profiler::record(MarkStart);
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
// To find adresses thats in the worklist
|
// To find adresses thats in the worklist
|
||||||
for (; start < end; start++)
|
for (; start < end; start++)
|
||||||
|
|
@ -202,6 +204,8 @@ namespace GC
|
||||||
|
|
||||||
if (!chunk->marked)
|
if (!chunk->marked)
|
||||||
{
|
{
|
||||||
|
if (getProfilerMode())
|
||||||
|
Profiler::record(ChunkMarked, chunk);
|
||||||
chunk->marked = true;
|
chunk->marked = true;
|
||||||
// Remove the marked chunk from the worklist
|
// Remove the marked chunk from the worklist
|
||||||
it = worklist.erase(it);
|
it = worklist.erase(it);
|
||||||
|
|
@ -247,6 +251,7 @@ namespace GC
|
||||||
{
|
{
|
||||||
// Add the unmarked chunks to freed chunks and remove from
|
// Add the unmarked chunks to freed chunks and remove from
|
||||||
// the list of allocated chunks
|
// the list of allocated chunks
|
||||||
|
Profiler::record(ChunkSwept, chunk);
|
||||||
heap->m_freed_chunks.push_back(chunk);
|
heap->m_freed_chunks.push_back(chunk);
|
||||||
iter = heap->m_allocated_chunks.erase(iter);
|
iter = heap->m_allocated_chunks.erase(iter);
|
||||||
}
|
}
|
||||||
|
|
@ -336,8 +341,10 @@ namespace GC
|
||||||
*
|
*
|
||||||
* @param flags Bitmap of flags
|
* @param flags Bitmap of flags
|
||||||
*/
|
*/
|
||||||
void Heap::collect(uint flags)
|
void Heap::collect(CollectOption flags)
|
||||||
{
|
{
|
||||||
|
if (getProfilerMode())
|
||||||
|
Profiler::record(CollectStart);
|
||||||
|
|
||||||
cout << "DEBUG COLLECT\nFLAGS: ";
|
cout << "DEBUG COLLECT\nFLAGS: ";
|
||||||
if (flags & MARK)
|
if (flags & MARK)
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,31 @@ namespace GC
|
||||||
auto end = profiler->m_events.end();
|
auto end = profiler->m_events.end();
|
||||||
|
|
||||||
std::ofstream fstr = profiler->createFileStream();
|
std::ofstream fstr = profiler->createFileStream();
|
||||||
|
char buffer[22];
|
||||||
|
std::tm *btm;
|
||||||
|
std::time_t tt;
|
||||||
|
Chunk *chunk;
|
||||||
|
|
||||||
while (start != end)
|
while (start != end)
|
||||||
{
|
{
|
||||||
auto event = *start++;
|
auto event = *start++;
|
||||||
|
|
||||||
fstr << "--------------------------------"
|
tt = event->getTimeStamp();
|
||||||
<< event->TypeToString()
|
btm = std::localtime(&tt);
|
||||||
<< event->getTimeStamp
|
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::time_t tt = std::time(NULL);
|
||||||
std::tm *ptm = std::localtime(&tt);
|
std::tm *ptm = std::localtime(&tt);
|
||||||
|
|
||||||
char buffer[32];
|
char buffer[32];
|
||||||
std::strftime(buffer, 32, "/profiler/log_%a_%H_%M_%S.txt", ptm);
|
std::strftime(buffer, 32, "/profiler/log_%a_%H_%M_%S.txt", ptm);
|
||||||
|
std::string filename(buffer);
|
||||||
|
|
||||||
std::ofstream fstr(buffer);
|
const std::string ABS_PATH = "/home/virre/dev/systemF/org/language/src/GC/";
|
||||||
|
std::string fullpath = ABS_PATH + filename;
|
||||||
|
|
||||||
|
std::ofstream fstr(fullpath);
|
||||||
return fstr;
|
return fstr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -6,43 +6,39 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// list<char> l;
|
using namespace std;
|
||||||
// char c = 'a';
|
using TimeStamp = std::chrono::_V2::system_clock::time_point;
|
||||||
// for (int i = 1; i <= 5; i++) {
|
|
||||||
// l.push_back(c++);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// auto iter = l.begin();
|
list<char> l;
|
||||||
// auto stop = l.end();
|
char c = 'a';
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
l.push_back(c++);
|
||||||
|
}
|
||||||
|
|
||||||
// while (iter != stop) {
|
auto iter = l.begin();
|
||||||
// cout << *iter << " ";
|
auto stop = l.end();
|
||||||
|
|
||||||
// iter++;
|
while (iter != stop) {
|
||||||
// }
|
cout << *iter << " ";
|
||||||
// cout << endl;
|
|
||||||
// iter = l.begin();
|
|
||||||
// while (*iter != *stop) {
|
|
||||||
// cout << *iter << " ";
|
|
||||||
// iter++;
|
|
||||||
// }
|
|
||||||
// cout << endl;
|
|
||||||
|
|
||||||
// cout << "rebased" << endl;
|
iter++;
|
||||||
// cout << "iter: " << *iter << "\nstop: " << *stop << endl;
|
}
|
||||||
|
cout << endl;
|
||||||
|
iter = l.begin();
|
||||||
|
while (*iter != *stop) {
|
||||||
|
cout << *iter << " ";
|
||||||
|
iter++;
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
// TimeStamp ts = std::chrono::system_clock::now();
|
cout << "rebased" << endl;
|
||||||
// std::time_t tt = std::chrono::system_clock::to_time_t(ts);
|
cout << "iter: " << *iter << "\nstop: " << *stop << endl;
|
||||||
// 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;
|
|
||||||
|
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
26
src/GC/tests/file.cpp
Normal file
26
src/GC/tests/file.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "player.hpp"
|
#include "player.hpp"
|
||||||
|
#include "heap.hpp"
|
||||||
|
|
||||||
#define X_LENGTH 1000;
|
#define X_LENGTH 1000;
|
||||||
#define Y_LENGTH 500;
|
#define Y_LENGTH 500;
|
||||||
|
|
@ -14,12 +15,38 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Game() {
|
Game() : dimensions(1000, 500) {}
|
||||||
dimensions = {X_LENGTH, Y_LENGTH};
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_player(Player& p) {
|
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 *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;
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,14 @@
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
class Point {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
int x, y;
|
||||||
|
Point(int _x, int _y) : x(_x), y(_y) {}
|
||||||
|
};
|
||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -13,12 +21,9 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Player(string n, Point pos, Point s, Point dir) {
|
Player(string n, Point pos, Point s, Point dir)
|
||||||
name = n;
|
: name(n), position(pos.x, pos.y), size(s.x, s.y), direction(dir.x, dir.y)
|
||||||
position = pos;
|
{}
|
||||||
size = s;
|
|
||||||
direction = dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
void move() {
|
void move() {
|
||||||
position.x += direction.x;
|
position.x += direction.x;
|
||||||
|
|
@ -31,7 +36,3 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Point {
|
|
||||||
int x, y;
|
|
||||||
};
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue