yeet
This commit is contained in:
parent
e2b3f36a64
commit
43ce0ecd71
39 changed files with 0 additions and 4330 deletions
|
|
@ -1,87 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#define HEAP_SIZE 65536 // Arbitrary for now, 2^16
|
||||
using namespace std;
|
||||
|
||||
/* A simple mark and sweep algorithm */
|
||||
|
||||
// Shouldn't be exposed. For now, it is
|
||||
struct ObjectHeader {
|
||||
size_t size = sizeof(this);
|
||||
bool marked = false;
|
||||
|
||||
};
|
||||
|
||||
struct Object : ObjectHeader {
|
||||
char name; // should be something like id, but for testing sake its char
|
||||
Object* child;
|
||||
// Object(char name_) {}
|
||||
Object(char name_, Object* child_) {
|
||||
name = name_;
|
||||
child = child_;
|
||||
}
|
||||
};
|
||||
|
||||
// Representing the heap as a simple struct for now
|
||||
struct Heap {
|
||||
Object heap_space[HEAP_SIZE];
|
||||
};
|
||||
|
||||
// For now it assumes that it is given root objects from the start, no root finding included
|
||||
class MarkSweep {
|
||||
public:
|
||||
void mark(Object* obj) {
|
||||
if (!markedBit(obj)) {
|
||||
markBit(obj);
|
||||
Object* ref = obj->child;
|
||||
if (ref != nullptr) {
|
||||
mark(ref);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sweep(vector<Object*> worklist) {
|
||||
for (Object* obj: worklist) {
|
||||
if (!markedBit(obj) && obj != nullptr) {
|
||||
delete obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool markedBit(Object* obj) {
|
||||
return obj->marked;
|
||||
}
|
||||
|
||||
void markBit(Object* obj) {
|
||||
obj->marked = true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main() {
|
||||
Object* b = new Object('B', nullptr);
|
||||
// b->name = 'B';
|
||||
// b->child = nullptr;
|
||||
Object* c = new Object('C', b);
|
||||
// c->name = 'C';
|
||||
// c->child = b; // c -> d
|
||||
Object* d = new Object('D', nullptr);
|
||||
// d->name = 'D';
|
||||
// d->child = nullptr;
|
||||
|
||||
//Heap* heap = new Heap{*c, *b, *d};
|
||||
vector<Object*> worklist = {c, b, d};
|
||||
MarkSweep* gc = new MarkSweep();
|
||||
|
||||
gc->mark(c);
|
||||
cout << "Expected 1, got: " << b->marked << '\n';
|
||||
cout << "Expected 1, got: " << c->marked << '\n';
|
||||
cout << "Expected 0, got: " << d->marked << '\n';
|
||||
|
||||
gc->sweep(worklist);
|
||||
cout << b->name << '\n';
|
||||
cout << c->name << '\n';
|
||||
cout << d->name << '\n'; // The object at d is now deleted (freed)
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// void time_test()
|
||||
// {
|
||||
// using TimeStamp = std::chrono::_V2::system_clock::time_point;
|
||||
|
||||
// std::list<char> l;
|
||||
// char c = 'a';
|
||||
// for (int i = 1; i <= 5; i++) {
|
||||
// l.push_back(c++);
|
||||
// }
|
||||
|
||||
// auto iter = l.begin();
|
||||
// auto stop = l.end();
|
||||
|
||||
// while (iter != stop) {
|
||||
// std::cout << *iter << " ";
|
||||
|
||||
// iter++;
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
// iter = l.begin();
|
||||
// while (*iter != *stop) {
|
||||
// std::cout << *iter << " ";
|
||||
// iter++;
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
|
||||
// std::cout << "rebased" << std::endl;
|
||||
// std::cout << "iter: " << *iter << "\nstop: " << *stop << 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;
|
||||
// }
|
||||
|
||||
void iter_test()
|
||||
{
|
||||
std::list<int> list;
|
||||
list.push_back(1);
|
||||
list.push_back(2);
|
||||
list.push_back(4);
|
||||
list.push_back(5);
|
||||
|
||||
auto iter = list.begin();
|
||||
|
||||
while (iter != list.end())
|
||||
{
|
||||
if (*iter == 4)
|
||||
{
|
||||
iter = list.erase(iter);
|
||||
std::cout << *iter << "\n";
|
||||
list.insert(iter, 3);
|
||||
// list.insert(iter, 3);
|
||||
// std::cout << "n: " << *(++iter) << "\n";
|
||||
// iter = list.erase(++iter);
|
||||
}
|
||||
iter++;
|
||||
}
|
||||
|
||||
for (int i : list)
|
||||
{
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
std::cout << "hello" << std::endl;
|
||||
|
||||
iter_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
struct Obj {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
};
|
||||
|
||||
int main() {
|
||||
GC::Heap::init();
|
||||
Obj *obj;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
obj = static_cast<Obj *>(GC::Heap::alloc(sizeof(Obj)));
|
||||
obj->a = i * i + 1;
|
||||
obj->b = i * i + 2;
|
||||
obj->c = i * i + 3;
|
||||
}
|
||||
|
||||
// heap->force_collect();
|
||||
auto heap = GC::Heap::debug_the();
|
||||
heap->collect(COLLECT_ALL);
|
||||
|
||||
std::cout << obj->a << ", " << obj->b << ", " << obj->c << std::endl;
|
||||
|
||||
//delete heap;
|
||||
GC::Heap::dispose();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,250 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
using GC::Chunk;
|
||||
|
||||
void alloc_test();
|
||||
void add_to_free_list(Chunk *chunk);
|
||||
void merge_free_list(Chunk *chunk, bool do_merge);
|
||||
void do_merge_list();
|
||||
void print_free_list();
|
||||
|
||||
std::list<Chunk *> m_free_list;
|
||||
|
||||
int main()
|
||||
{
|
||||
alloc_test();
|
||||
|
||||
// std::list<int> test;
|
||||
|
||||
// test.push_back(1);
|
||||
// test.push_back(2);
|
||||
// test.push_back(3);
|
||||
// test.push_back(4);
|
||||
// test.push_back(5);
|
||||
|
||||
// auto iter = test.begin();
|
||||
|
||||
// std::cout << "First? " << *(iter++) << "\n";
|
||||
// std::cout << "Second? " << *(iter--) << "\n";
|
||||
// std::cout << "First? " << *iter << std::endl;
|
||||
|
||||
// auto i = test.begin();
|
||||
// while (i != test.end())
|
||||
// {
|
||||
// std::cout << *i << " ";
|
||||
// ++i;
|
||||
// }
|
||||
|
||||
// if (i == test.end())
|
||||
// std::cout << "great success!";
|
||||
|
||||
// std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void alloc_test()
|
||||
{
|
||||
auto tmp = static_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
|
||||
auto c1 = new Chunk((size_t)(8), tmp);
|
||||
auto c2 = new Chunk((size_t)(4), c1->m_start + (size_t)(8));
|
||||
auto c3 = new Chunk((size_t)(16), c2->m_start + (size_t)(4));
|
||||
auto c4 = new Chunk((size_t)(4), c3->m_start + (size_t)(16));
|
||||
auto c5 = new Chunk((size_t)(32), c4->m_start + (size_t)(4));
|
||||
|
||||
// std::cout << "test: " << (uintptr_t *)(tmp + (size_t)(2)) << std::endl;
|
||||
|
||||
std::cout << "tmp: " << tmp << "\ntmp: " << (tmp + (size_t)(28)) << std::endl;
|
||||
|
||||
// add_to_free_list(c1);
|
||||
// add_to_free_list(c2);
|
||||
// add_to_free_list(c3);
|
||||
// add_to_free_list(c4);
|
||||
// add_to_free_list(c5);
|
||||
|
||||
merge_free_list(c1, false);
|
||||
merge_free_list(c2, false);
|
||||
merge_free_list(c3, false);
|
||||
merge_free_list(c4, false);
|
||||
merge_free_list(c5, false);
|
||||
|
||||
std::cout << "----- BEFORE MERGE ----------------------";
|
||||
// print_free_list();
|
||||
|
||||
do_merge_list();
|
||||
|
||||
std::cout << "----- AFTER MERGE -----------------------";
|
||||
// print_free_list();
|
||||
}
|
||||
|
||||
void add_to_free_list(Chunk *chunk)
|
||||
{
|
||||
Chunk *curr;
|
||||
auto iter = m_free_list.begin();
|
||||
uintptr_t *prev_start = nullptr;
|
||||
uintptr_t *prev_end = nullptr;
|
||||
|
||||
if (m_free_list.size() == 0)
|
||||
{
|
||||
m_free_list.push_back(chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
while (iter != m_free_list.end())
|
||||
{
|
||||
curr = *iter;
|
||||
|
||||
// If the curr chunk is aligned before param
|
||||
if (curr->m_start + curr->m_size == chunk->m_start)
|
||||
{
|
||||
Chunk *merged = new Chunk(
|
||||
curr->m_size + chunk->m_size,
|
||||
curr->m_start);
|
||||
iter = m_free_list.erase(iter);
|
||||
m_free_list.insert(iter, merged);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the curr chunk is aligned after param
|
||||
if (chunk->m_start + chunk->m_size == curr->m_start)
|
||||
{
|
||||
Chunk *merged = new Chunk(
|
||||
curr->m_size + chunk->m_size,
|
||||
chunk->m_start);
|
||||
iter = m_free_list.erase(iter);
|
||||
m_free_list.insert(iter, merged);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the first chunk starts after param
|
||||
if (prev_start == nullptr && curr->m_start > chunk->m_start)
|
||||
{
|
||||
m_free_list.insert(iter, chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
if (prev_end < chunk->m_start && (chunk->m_start + chunk->m_size) < curr->m_start)
|
||||
{
|
||||
m_free_list.insert(iter, chunk);
|
||||
return;
|
||||
}
|
||||
|
||||
prev_start = curr->m_start;
|
||||
prev_end = prev_start + curr->m_size;
|
||||
iter++;
|
||||
}
|
||||
|
||||
// This is only reachable if the chunk is at the end
|
||||
m_free_list.push_back(chunk);
|
||||
}
|
||||
|
||||
void merge_free_list(Chunk *chunk, bool do_merge)
|
||||
{
|
||||
auto i = m_free_list.begin();
|
||||
uintptr_t *prev_start = nullptr, *prev_end;
|
||||
bool chunk_inserted = false;
|
||||
|
||||
while (i != m_free_list.end())
|
||||
{
|
||||
|
||||
// if chunk is left-aligned
|
||||
if ((*i)->m_start + (*i)->m_size == chunk->m_start)
|
||||
{
|
||||
m_free_list.insert(++i, chunk);
|
||||
chunk_inserted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// if chunk is right-aligned
|
||||
if (chunk->m_start + chunk->m_size == (*i)->m_start)
|
||||
{
|
||||
m_free_list.insert(i, chunk);
|
||||
chunk_inserted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// is new first
|
||||
if (prev_start == nullptr && (*i)->m_start > chunk->m_start)
|
||||
{
|
||||
m_free_list.insert(i, chunk);
|
||||
chunk_inserted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// if between chunks
|
||||
if (prev_end < chunk->m_start && (chunk->m_start + chunk->m_size) < (*i)->m_start)
|
||||
{
|
||||
m_free_list.insert(i, chunk);
|
||||
chunk_inserted = true;
|
||||
break;
|
||||
}
|
||||
|
||||
prev_start = (*i)->m_start;
|
||||
prev_end = (*i)->m_start + (*i)->m_size;
|
||||
i++;
|
||||
}
|
||||
|
||||
// is new last
|
||||
if (!chunk_inserted && i == m_free_list.end())
|
||||
m_free_list.push_back(chunk);
|
||||
|
||||
if (do_merge)
|
||||
do_merge_list();
|
||||
}
|
||||
|
||||
void do_merge_list()
|
||||
{
|
||||
std::cout << "DO MERGE" << std::endl;
|
||||
auto i = m_free_list.begin();
|
||||
Chunk *prev = *(i++), *curr;
|
||||
print_free_list();
|
||||
|
||||
while (i != m_free_list.end())
|
||||
{
|
||||
curr = *i;
|
||||
|
||||
if ((prev->m_start + prev->m_size) == curr->m_start)
|
||||
{
|
||||
Chunk *merged = new Chunk(
|
||||
prev->m_size + curr->m_size,
|
||||
prev->m_start
|
||||
);
|
||||
|
||||
// replace current and previous with merged
|
||||
i = m_free_list.erase(i);
|
||||
i = m_free_list.erase(--i);
|
||||
m_free_list.insert(i, merged);
|
||||
|
||||
prev = merged;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev = curr;
|
||||
i++;
|
||||
}
|
||||
print_free_list();
|
||||
}
|
||||
print_free_list();
|
||||
}
|
||||
|
||||
void print_free_list()
|
||||
{
|
||||
std::cout << "free-list count: " << m_free_list.size() << "\n";
|
||||
|
||||
auto iter = m_free_list.begin();
|
||||
size_t cnt = 1;
|
||||
|
||||
while (iter != m_free_list.end())
|
||||
{
|
||||
std::cout << "C" << cnt << ":\n\tstart: " << (*iter)->m_start
|
||||
<< "\n\tsize: " << (*iter)->m_size << "\n";
|
||||
iter++;
|
||||
cnt++;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
// broken :(
|
||||
// [event_source(native)]
|
||||
class ESource {
|
||||
public:
|
||||
__event void TestEvent(int eValue);
|
||||
};
|
||||
|
||||
// [event_receiver(native)]
|
||||
class EReceiver {
|
||||
public:
|
||||
void Handler1(int eValue) {
|
||||
cout << "Handler1 with: " << eValue << endl;
|
||||
}
|
||||
|
||||
void Handler2(int eValue) {
|
||||
cout << "Handler2 with: " << eValue << endl;
|
||||
}
|
||||
|
||||
void hookEvent(ESource *eSource) {
|
||||
__hook(&ESource::TestEvent, eSource, &EReceiver::Handler1);
|
||||
__hook(&ESource::TestEvent, eSource, &EReceiver::Handler2);
|
||||
}
|
||||
|
||||
void unhookEvent(ESource *eSource) {
|
||||
__unhook(&ESource::TestEvent, eSource, &EReceiver::Handler1);
|
||||
__unhook(&ESource::TestEvent, eSource, &EReceiver::Handler2);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
ESource src;
|
||||
EReceiver rcv;
|
||||
|
||||
rcv.hookEvent(&src);
|
||||
__raise src.TestEvent(12);
|
||||
rcv.unhookEvent(&src);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
GC::Heap& singleton_test();
|
||||
void init_gc(GC::Heap& heap);
|
||||
void frame_test(GC::Heap& heap);
|
||||
|
||||
int main() {
|
||||
std::cout << "in main" << std::endl;
|
||||
GC::Heap &heap = singleton_test();
|
||||
|
||||
init_gc(heap);
|
||||
frame_test(heap);
|
||||
|
||||
heap.dispose();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This test is supposed to determine if the singleton pattern
|
||||
* implementation is working correctly. This test passes if the
|
||||
* first and second call prints the same memory address.
|
||||
*
|
||||
* Result: pass
|
||||
*
|
||||
* @return Pointer to the Heap singleton instance
|
||||
*/
|
||||
GC::Heap& singleton_test() {
|
||||
std::cout << "TESTING SINGLETON INSTANCES" << std::endl;
|
||||
std::cout << "===========================" << std::endl;
|
||||
std::cout << "Call 1:\t" << &GC::Heap::the() << std::endl; // First call which initializes the singleton instance
|
||||
GC::Heap &heap = GC::Heap::the(); // Second call which should return the initialized instance
|
||||
std::cout << "Call 2:\t" << &heap << std::endl;
|
||||
std::cout << "===========================" << std::endl;
|
||||
return heap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This test calls Heap::init() which saves the stack-frame
|
||||
* address from the calling function (this function).
|
||||
* Heap::init() is supposed to be called at the absolute
|
||||
* start of the program to save the address of the
|
||||
* topmost stack frame. This test doesn't do anything
|
||||
* but prepares for the next test(s).
|
||||
*
|
||||
* @param heap The Heap pointer to the singleton instance.
|
||||
*
|
||||
*/
|
||||
void init_gc(GC::Heap& heap){
|
||||
std::cout << "\n\n INITIALIZING THE HEAP" << std::endl;
|
||||
std::cout << "===========================" << std::endl;
|
||||
heap.init();
|
||||
heap.set_profiler(true);
|
||||
std::cout << "===========================" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tests the functionality of the intrinsic
|
||||
* function `__builtin_frame_address` which returns the
|
||||
* address of the corresponding level of stack frame.
|
||||
* When given a param of 0, it returns the current stack frame.
|
||||
* When given a param of 1, it returns the previous stack
|
||||
* frame, and so on.
|
||||
*
|
||||
* This test passes on two conditions:
|
||||
* 1) if the address of the current frame is smaller than
|
||||
* the address of the previous frame (assumed).
|
||||
* 2) if the previous frame has the same address as the one
|
||||
* saved in the Heap instance after running Heap::init().
|
||||
*
|
||||
* Result: pass
|
||||
*
|
||||
* @param heap The Heap instance
|
||||
*/
|
||||
void frame_test(GC::Heap& heap) {
|
||||
std::cout << "\n\n TESTING FRAME ADDRESSES" << std::endl;
|
||||
std::cout << "===========================" << std::endl;
|
||||
|
||||
#pragma clang diagnostic ignored "-Wframe-address" // clang++ directive to ignore warnings about __b_f_a
|
||||
auto curr_frame = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); // addr of curr stack frame
|
||||
std::cout << "Current stack frame:\t" << curr_frame << std::endl;
|
||||
#pragma clang diagnostic ignored "-Wframe-address"
|
||||
auto prev_frame = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1)); // addr of prev stack frame
|
||||
std::cout << "Previous stack frame:\t" << prev_frame << std::endl;
|
||||
|
||||
heap.check_init(); // prints the saved absolute top of the stack
|
||||
// auto alloced = heap->alloc(sizeof(unsigned long));
|
||||
|
||||
std::cout << "===========================" << std::endl;
|
||||
}
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
#include <ctime>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void time_string(char *buffer);
|
||||
void print_log_file(const std::string TESTS_PATH);
|
||||
void readlink_test();
|
||||
void null_test();
|
||||
|
||||
int main()
|
||||
{
|
||||
// char time_buffer[31];
|
||||
// time_string(time_buffer);
|
||||
|
||||
// const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/";
|
||||
// print_log_file(TESTS_PATH);
|
||||
|
||||
// readlink_test();
|
||||
|
||||
null_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void time_string(char *const buffer)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void print_log_file(const std::string TESTS_PATH)
|
||||
{
|
||||
std::string path = TESTS_PATH + "/testlog.txt";
|
||||
|
||||
std::ofstream testF(path);
|
||||
|
||||
testF << "hellow york";
|
||||
|
||||
testF.close();
|
||||
}
|
||||
|
||||
void readlink_test()
|
||||
{
|
||||
char buffer[1024];
|
||||
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer)-1);
|
||||
if (len == -1)
|
||||
{
|
||||
std::cout << "readlink error" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
buffer[len] = '\0';
|
||||
std::cout << "readlink:\n" << "'''" << buffer << "'''"; // << std::endl;
|
||||
|
||||
auto path = std::string(buffer);
|
||||
std::cout << path << "\nlen: " << path.size() << "\ncap:" << path.capacity();
|
||||
|
||||
size_t last_slash = path.find_last_of('/');
|
||||
std::string folder = path.substr(0, last_slash);
|
||||
|
||||
std::cout << "\n" << folder;
|
||||
|
||||
std::string log_path = folder + "/log_file_bla.txt";
|
||||
std::cout << "\n" << log_path << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void null_test() {
|
||||
int *p = nullptr;
|
||||
|
||||
std::cout << "P: " << nullptr << std::endl;
|
||||
}
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
#include <vector>
|
||||
|
||||
#include "player.hpp"
|
||||
#include "heap.hpp"
|
||||
|
||||
#define X_LENGTH 1000
|
||||
#define Y_LENGTH 500
|
||||
#define MAX_PLAYERS 100
|
||||
|
||||
/*
|
||||
* 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:
|
||||
|
||||
std::vector<Player*> *players;
|
||||
//std::vector<Player> *players;
|
||||
Point *dimensions;
|
||||
|
||||
public:
|
||||
|
||||
Game() {
|
||||
dimensions->x = X_LENGTH;
|
||||
dimensions->y = Y_LENGTH;
|
||||
}
|
||||
|
||||
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));
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
void create_players(int nr) {
|
||||
for (int i = 0; i < nr; i++) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
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->init();
|
||||
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();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,106 +0,0 @@
|
|||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
using std::cout, std::endl;
|
||||
|
||||
struct Node {
|
||||
int id;
|
||||
Node *child;
|
||||
};
|
||||
|
||||
Node *create_chain(int depth) {
|
||||
cout << "entering create_chain" << endl;
|
||||
std::vector<Node*> nodes;
|
||||
if (depth > 0) {
|
||||
Node *last_node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
|
||||
last_node->id = depth;
|
||||
last_node->child = nullptr;
|
||||
nodes.push_back(last_node);
|
||||
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];
|
||||
nodes.push_back(node);
|
||||
}
|
||||
cout << "\nexiting create_chain" << endl;
|
||||
return nodes[depth];
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void create_array(size_t size) {
|
||||
int *arr = static_cast<int *>(GC::Heap::alloc(sizeof(int) * size));
|
||||
}
|
||||
|
||||
void detach_pointer(long **ptr) {
|
||||
cout << "entering detach_pointer" << endl;
|
||||
long *dummy_ptr = nullptr;
|
||||
*ptr = dummy_ptr;
|
||||
cout << "\nexiting detach_pointer" << endl;
|
||||
}
|
||||
|
||||
Node *test_chain(int depth, bool detach) {
|
||||
cout << "entering test_chain" << endl;
|
||||
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
|
||||
Node *node_chain = create_chain(depth);
|
||||
if (detach)
|
||||
node_chain->child = nullptr;
|
||||
|
||||
cout << "\nexiting test_chain" << endl;
|
||||
return node_chain;
|
||||
}
|
||||
|
||||
void test_some_types() {
|
||||
cout << "entering test_some_types" << endl;
|
||||
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
std::cout << "Stack start from test_some_types:\t" << stack_start << std::endl;
|
||||
|
||||
long *l = static_cast<long *>(GC::Heap::alloc(sizeof(long)));
|
||||
std::cout << "l points to:\t\t" << l << std::endl;
|
||||
detach_pointer(&l);
|
||||
std::cout << "l points to:\t\t" << l << std::endl;
|
||||
|
||||
// Some more dummy values of different sizes, to test stack pointer alignment
|
||||
int *i = static_cast<int *>(GC::Heap::alloc(sizeof(int)));
|
||||
char *c = static_cast<char *>(GC::Heap::alloc(sizeof(int)));
|
||||
short *s = static_cast<short *>(GC::Heap::alloc(sizeof(short)));
|
||||
cout << "exiting test_some_types" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout << "entering main" << endl;
|
||||
using namespace std::literals;
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
//std::cout << "Value of start: " << start.time_since_epoch().count() << std::endl;
|
||||
GC::Heap::init();
|
||||
GC::Heap &gc = GC::Heap::the();
|
||||
gc.set_profiler(true);
|
||||
GC::Profiler::set_log_options(GC::FunctionCalls);
|
||||
gc.check_init();
|
||||
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
|
||||
Node *root1 = static_cast<Node *>(gc.alloc(sizeof(Node)));
|
||||
Node *root2 = static_cast<Node *>(gc.alloc(sizeof(Node)));
|
||||
root1 = test_chain(100000, false);
|
||||
//root2 = test_chain(58000, false);
|
||||
|
||||
gc.collect(GC::COLLECT_ALL);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
//std::cout << "Value of end: " << end.time_since_epoch().count() << std::endl;
|
||||
|
||||
gc.print_summary();
|
||||
gc.dispose();
|
||||
|
||||
std::cout
|
||||
<< "Execution time: "
|
||||
<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " ≈ "
|
||||
<< (end - start) / 1ms << "ms ≈ "
|
||||
<< (end - start) / 1s << "s.\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.h_test.out</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
|
|
@ -1,120 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
#define allocNode static_cast<Node *>(GC::Heap::alloc(sizeof(Node)))
|
||||
|
||||
using std::cout, std::endl;
|
||||
|
||||
struct Node // sizeof(Node) = 16
|
||||
{
|
||||
int value;
|
||||
Node *next {nullptr};
|
||||
};
|
||||
|
||||
Node *create_list(size_t length)
|
||||
{
|
||||
Node *head = allocNode;
|
||||
head->value = 0;
|
||||
|
||||
Node *prev = head;
|
||||
Node *next;
|
||||
|
||||
for (size_t i = 1; i < length; i++)
|
||||
{
|
||||
next = allocNode;
|
||||
next->value = i;
|
||||
prev->next = next;
|
||||
prev = next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
void print_list(Node* head)
|
||||
{
|
||||
cout << "\nPrinting list...\n";
|
||||
while (head != nullptr)
|
||||
{
|
||||
cout << head->value << " ";
|
||||
head = head->next;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void clear_list(Node *head)
|
||||
{
|
||||
while (head != nullptr)
|
||||
{
|
||||
Node *tmp = head->next;
|
||||
head->next = nullptr;
|
||||
head = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
#define LIST_SIZE 1000
|
||||
|
||||
void list_test1()
|
||||
{
|
||||
Node *list_1 = create_list(LIST_SIZE);
|
||||
// print_list(list_1);
|
||||
}
|
||||
|
||||
/*
|
||||
void list_test2()
|
||||
{
|
||||
Node *list_2 = create_list(LIST_SIZE);
|
||||
// print_list(list_2);
|
||||
}
|
||||
|
||||
void list_test3()
|
||||
{
|
||||
Node *list_3 = create_list(LIST_SIZE);
|
||||
// print_list(list_3);
|
||||
}
|
||||
|
||||
void list_test4()
|
||||
{
|
||||
Node *list_4 = create_list(LIST_SIZE);
|
||||
// print_list(list_4);
|
||||
}
|
||||
|
||||
void list_test5()
|
||||
{
|
||||
Node *list_5 = create_list(LIST_SIZE);
|
||||
// print_list(list_5);
|
||||
}
|
||||
|
||||
void list_test6()
|
||||
{
|
||||
Node *list_6 = create_list(LIST_SIZE);
|
||||
// print_list(list_6);
|
||||
}
|
||||
|
||||
void make_test() {
|
||||
list_test1();
|
||||
list_test2();
|
||||
list_test3();
|
||||
list_test4();
|
||||
list_test5();
|
||||
list_test6();
|
||||
}
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
GC::Heap::init();
|
||||
GC::Heap &heap = GC::Heap::the();
|
||||
heap.set_profiler(true);
|
||||
GC::Profiler::set_log_options(GC::FunctionCalls);
|
||||
//GC::Profiler::set_log_options(GC::AllOps);
|
||||
|
||||
// make_test();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
list_test1();
|
||||
|
||||
GC::Heap::dispose();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
struct Obj {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
class Point {
|
||||
|
||||
public:
|
||||
|
||||
int x, y;
|
||||
Point() {}
|
||||
Point(int _x, int _y) : x(_x), y(_y) {}
|
||||
};
|
||||
|
||||
class Player {
|
||||
|
||||
private:
|
||||
|
||||
string *name;
|
||||
Point *position;
|
||||
Point *size;
|
||||
Point *direction;
|
||||
|
||||
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)
|
||||
{} */
|
||||
|
||||
void move() {
|
||||
position->x += direction->x;
|
||||
position->y += direction->y;
|
||||
}
|
||||
|
||||
void set_speed(int dx, int 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) {
|
||||
name = n;
|
||||
position = pos;
|
||||
size = s;
|
||||
direction = dir;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
using std::cout, std::endl, std::hex;
|
||||
|
||||
struct Node {
|
||||
int value;
|
||||
Node *next {nullptr};
|
||||
};
|
||||
|
||||
void test(Node *n) {
|
||||
size_t n_size = 16;
|
||||
|
||||
auto c_start = reinterpret_cast<uintptr_t>(n);
|
||||
auto c_size = reinterpret_cast<uintptr_t>(n_size);
|
||||
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
|
||||
|
||||
cout << "Node *n:\t" << n << "\n";
|
||||
cout << "n_size: \t0x" << std::hex << n_size << "\n";
|
||||
cout << "c_start:\t0x" << std::hex << c_start << "\n";
|
||||
cout << "c_size: \t0x" << std::hex << c_size << "\n";
|
||||
cout << "c_end: \t0x" << std::hex << c_end << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
GC::Heap::init();
|
||||
GC::Heap &heap = GC::Heap::the();
|
||||
heap.set_profiler(true);
|
||||
heap.set_profiler_log_options(GC::FunctionCalls);
|
||||
|
||||
Node *n = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
|
||||
test(n);
|
||||
|
||||
GC::Heap::dispose();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
#define allocNode static_cast<Node *>(GC::Heap::alloc(sizeof(Node)))
|
||||
|
||||
using std::cout, std::endl;
|
||||
|
||||
struct Node {
|
||||
int value;
|
||||
Node *next {nullptr};
|
||||
};
|
||||
|
||||
void revRange(int n) {
|
||||
Node *next = nullptr;
|
||||
Node *prev = allocNode;
|
||||
while (n > 0) {
|
||||
next = allocNode;
|
||||
prev->next = next;
|
||||
prev->value = n--;
|
||||
prev = next;
|
||||
}
|
||||
}
|
||||
|
||||
void make_test() {
|
||||
int n = 10;
|
||||
while (n > 0)
|
||||
revRange(1000);
|
||||
}
|
||||
|
||||
int main() {
|
||||
GC::Heap::init();
|
||||
GC::Heap &heap = GC::Heap::the();
|
||||
heap.set_profiler(true);
|
||||
GC::Profiler::set_log_options(GC::FunctionCalls);
|
||||
|
||||
make_test();
|
||||
|
||||
GC::Heap::dispose();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
* Stack.cpp
|
||||
* - Tests stack scanning and stack pointers
|
||||
*
|
||||
* Goal: Find the values of the following variables
|
||||
* and their position on the stack
|
||||
* - unsigned long a
|
||||
* - unsigned long b
|
||||
* - unsigned long global_1
|
||||
* - unsigned long global_2
|
||||
*
|
||||
* Result: Passed
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
std::vector<uintptr_t *> iv;
|
||||
|
||||
void collect() {
|
||||
std::cout << "in collect" << std::endl;
|
||||
|
||||
uintptr_t *stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
|
||||
// denna orsakar segfault om man ger __b_f_a ett värde större än 2
|
||||
// uintptr_t *stack_end = reinterpret_cast<uintptr_t *>(__builtin_frame_address(100));
|
||||
|
||||
std::cout << "SP1:\t" << stack_start << "\nSP2:\t" << (stack_start - 1*sizeof(int)) << std::endl;
|
||||
std::cout << "SP-:\t" << --stack_start << std::endl;
|
||||
|
||||
const uintptr_t *stack_end = (stack_start + 30*sizeof(int));
|
||||
int vars_found = 0;
|
||||
|
||||
while (stack_start < stack_end) {
|
||||
|
||||
if (std::find(iv.begin(), iv.end(), stack_start) != iv.end()) {
|
||||
vars_found++;
|
||||
std::cout << "Found " << *(reinterpret_cast<unsigned long *>(stack_start)) << " at " << stack_start << std::endl;
|
||||
}
|
||||
|
||||
// std::cout << "SP address:\t\t" << stack_start << "\nSP value:\t\t" << *(reinterpret_cast<unsigned long *>(stack_start)) << std::endl;
|
||||
|
||||
stack_start++;
|
||||
}
|
||||
|
||||
if (vars_found == 0) {
|
||||
std::cout << "Found nothing" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int add(unsigned long a, unsigned long b) {
|
||||
iv.push_back(reinterpret_cast<uintptr_t *>(&a));
|
||||
iv.push_back(reinterpret_cast<uintptr_t *>(&b));
|
||||
std::cout << "'a':\t" << &a << "\n'b':\t" << &b << std::endl;
|
||||
collect();
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
unsigned long global_1 = 16;
|
||||
unsigned long global_2 = 32;
|
||||
|
||||
iv.push_back(&global_1);
|
||||
iv.push_back(&global_2);
|
||||
|
||||
std::cout << "'g1':\t" << &global_1 << "\n'g2':\t" << &global_2 << std::endl;
|
||||
|
||||
add(3,2);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
void dummy1();
|
||||
void dummy2();
|
||||
|
||||
int main() {
|
||||
|
||||
uintptr_t *prev1 = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
uintptr_t *prev2 = static_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||
|
||||
std::cout << "reinterpret:\t" << prev1 << "\nstatic:\t\t" << prev2 << std::endl;
|
||||
|
||||
std::cout << "Start:\t\t" << prev1 << std::endl;
|
||||
#pragma clang diagnostic ignored "-Wframe-address"
|
||||
uintptr_t *tmp = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1));
|
||||
std::cout << "Frame 1:\t" << tmp << "\t\tDiff:\t" << std::hex << "0x"<< tmp - prev1 << std::endl;
|
||||
prev1 = tmp;
|
||||
|
||||
#pragma clang diagnostic ignored "-Wframe-address"
|
||||
tmp = reinterpret_cast<uintptr_t *>(__builtin_frame_address(2));
|
||||
std::cout << "Frame 2:\t" << tmp << "\tDiff:\t" << std::hex << "0x" << tmp - prev1 << std::endl;
|
||||
prev1 = tmp;
|
||||
|
||||
// arg > 2 for __builtin_frame_address() results in segfault
|
||||
// #pragma clang diagnostic ignored "-Wframe-address"
|
||||
// tmp = reinterpret_cast<uintptr_t *>(__builtin_frame_address(3));
|
||||
// std::cout << "Frame 3:\t" << tmp << "\tDiff:\t" << std::hex << "0x" << prev1 - tmp << std::endl;
|
||||
|
||||
dummy1();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void dummy1() {
|
||||
std::cout << "D1 SFrame:\t" << __builtin_frame_address(0);
|
||||
#pragma clang diagnostic ignored "-Wframe-address"
|
||||
std::cout << "\t\tPrev:\t" << __builtin_frame_address(1) << std::endl;
|
||||
std::cout << "D1 RA:\t\t" << std::hex << __builtin_return_address(0) << std::endl;
|
||||
dummy2();
|
||||
}
|
||||
|
||||
void dummy2() {
|
||||
std::cout << "Frame:\t\t" << __builtin_frame_address(0);
|
||||
#pragma clang diagnostic ignored "-Wframe-address"
|
||||
std::cout << "\t\tPrev:\t" << __builtin_frame_address(1) << std::endl;
|
||||
void *ra = __builtin_return_address(0);
|
||||
std::cout << "D2 RA:\t\t" << std::hex << ra << std::endl;
|
||||
// gives same value as pure 'ra'
|
||||
// std::cout << "D2 ERA:\t\t" << std::hex << __builtin_extract_return_addr(ra) << std::endl;
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
#include <iostream>
|
||||
|
||||
#include "heap.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
int value;
|
||||
Node *left;
|
||||
Node *right;
|
||||
};
|
||||
|
||||
int getValue();
|
||||
Node *createNode();
|
||||
void insert();
|
||||
|
||||
int main() {
|
||||
GC::Heap::init();
|
||||
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getValue() {
|
||||
cout << "Enter a value to insert: ";
|
||||
int value;
|
||||
cin >> value;
|
||||
return value;
|
||||
}
|
||||
|
||||
Node *createNode() {
|
||||
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
|
||||
node->value = getValue();
|
||||
return node;
|
||||
}
|
||||
|
||||
void insert(Node *root) {
|
||||
Node *node = createNode();
|
||||
Node *curr = root;
|
||||
while (curr)
|
||||
}
|
||||
|
|
@ -1,96 +0,0 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cheap.h"
|
||||
|
||||
typedef struct object
|
||||
{
|
||||
int x, y, z;
|
||||
double velocity;
|
||||
} Object;
|
||||
|
||||
void test_init()
|
||||
{
|
||||
printf("----- IN TEST_INIT ----------------------------\n");
|
||||
|
||||
cheap_init();
|
||||
|
||||
printf("----- EXIT TEST_INIT --------------------------\n");
|
||||
}
|
||||
|
||||
/* Uncomment ONLY if run with DEBUG defined in cheap.h */
|
||||
|
||||
cheap_t *test_the()
|
||||
{
|
||||
printf("----- IN TEST_THE -----------------------------\n");
|
||||
|
||||
cheap_t *fst_heap = cheap_the();
|
||||
|
||||
printf("Heap 1:\t%p\n", fst_heap->obj);
|
||||
|
||||
cheap_t *snd_heap = cheap_the();
|
||||
|
||||
printf("Heap 2:\t%p\n", snd_heap->obj);
|
||||
|
||||
printf("----- EXIT TEST_THE ---------------------------\n");
|
||||
|
||||
free(snd_heap);
|
||||
return fst_heap;
|
||||
}
|
||||
|
||||
void test_profiler(cheap_t *heap)
|
||||
{
|
||||
printf("----- IN TEST_PROFILER ------------------------\n");
|
||||
|
||||
cheap_set_profiler(heap, false);
|
||||
cheap_set_profiler(heap, true);
|
||||
cheap_profiler_log_options(heap, FuncCallsOnly);
|
||||
|
||||
printf("----- EXIT TEST_PROFILER ----------------------\n");
|
||||
}
|
||||
|
||||
Object *test_alloc()
|
||||
{
|
||||
printf("----- IN TEST_ALLOC ---------------------------\n");
|
||||
|
||||
Object *o;
|
||||
o = (Object *)(cheap_alloc(sizeof(Object)));
|
||||
|
||||
o->x = 3;
|
||||
o->y = 4;
|
||||
o->z = 5;
|
||||
o->velocity = 1.0f;
|
||||
|
||||
printf("----- EXIT TEST_ALLOC -------------------------\n");
|
||||
return o;
|
||||
}
|
||||
|
||||
void test_dispose()
|
||||
{
|
||||
printf("----- IN TEST_DISPOSE -------------------------\n");
|
||||
|
||||
cheap_dispose();
|
||||
|
||||
printf("----- EXIT TEST_DISPOSE -----------------------\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_init();
|
||||
|
||||
/* Uncomment ONLY if run with DEBUG defined in cheap.h */
|
||||
cheap_t *heap = test_the();
|
||||
test_profiler(heap);
|
||||
|
||||
Object *o = test_alloc();
|
||||
printf("Object size: %lu\n", sizeof(Object));
|
||||
printf("Object:\n\tx: %d\n\ty: %d\n\tz: %d\n\tvel: %f\n", o->x, o->y, o->z, o->velocity);
|
||||
|
||||
test_dispose();
|
||||
|
||||
/* Sefault I don't understand, don't uncomment */
|
||||
// free(heap);
|
||||
// free(o);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cheap.h"
|
||||
|
||||
typedef struct node {
|
||||
int id;
|
||||
struct node *child;
|
||||
} Node;
|
||||
|
||||
// Global variables make the test less complex
|
||||
Node *HEAD = NULL;
|
||||
Node *CURRENT = NULL;
|
||||
|
||||
void insert_first(int node_id) {
|
||||
Node *new_head;
|
||||
new_head = (Node*)(cheap_alloc(sizeof(Node)));
|
||||
new_head->id = node_id;
|
||||
new_head->child = HEAD;
|
||||
|
||||
HEAD = new_head;
|
||||
}
|
||||
|
||||
// Creates a linked list of length depth. Global head "HEAD" is updated.
|
||||
Node *create_linked_list(int depth) {
|
||||
HEAD = (Node*)(cheap_alloc(sizeof(Node)));
|
||||
HEAD->id = 0;
|
||||
// Purposely omitting adding a child to "last_node", since its the last node
|
||||
for (int i = 1; i < depth - 1; i++) {
|
||||
insert_first(i);
|
||||
}
|
||||
return HEAD;
|
||||
}
|
||||
|
||||
void create_garbage(int amount) {
|
||||
for (int i = 0; i < amount; i++) {
|
||||
long *garbage = (long*)(cheap_alloc(sizeof(long)));
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
cheap_init();
|
||||
cheap_t *heap = cheap_the();
|
||||
cheap_set_profiler(heap, true);
|
||||
|
||||
// Every node in this list should be marked
|
||||
Node *head = create_linked_list(5);
|
||||
// Everything create here should be swept
|
||||
create_garbage(30);
|
||||
|
||||
cheap_dispose();
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue