Hooked the GC back in B)

This commit is contained in:
Samuel Hammersberg 2023-05-05 18:50:05 +02:00
parent dead9eb75a
commit a388f480e5
24 changed files with 1404 additions and 227 deletions

View file

@ -5,40 +5,79 @@
#include <time.h>
#include <stdlib.h>
int main() {
using namespace std;
using TimeStamp = std::chrono::_V2::system_clock::time_point;
// void time_test()
// {
// using TimeStamp = std::chrono::_V2::system_clock::time_point;
list<char> l;
char c = 'a';
for (int i = 1; i <= 5; i++) {
l.push_back(c++);
}
// 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();
// auto iter = l.begin();
// auto stop = l.end();
while (iter != stop) {
cout << *iter << " ";
// 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++;
}
cout << endl;
iter = l.begin();
while (*iter != *stop) {
cout << *iter << " ";
iter++;
for (int i : list)
{
std::cout << i << " ";
}
cout << endl;
std::cout << 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;
int main() {
std::cout << "hello" << std::endl;
iter_test();
return 0;
}

View file

@ -0,0 +1,250 @@
#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;
}

View file

@ -8,6 +8,7 @@
void time_string(char *buffer);
void print_log_file(const std::string TESTS_PATH);
void readlink_test();
void null_test();
int main()
{
@ -17,7 +18,9 @@ int main()
// const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/";
// print_log_file(TESTS_PATH);
readlink_test();
// readlink_test();
null_test();
return 0;
}
@ -65,4 +68,10 @@ void readlink_test()
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;
}

View file

@ -11,7 +11,7 @@ struct Node {
};
Node *create_chain(int depth) {
cout << "entering create_chain";
cout << "entering create_chain" << endl;
std::vector<Node*> nodes;
if (depth > 0) {
Node *last_node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
@ -36,14 +36,14 @@ void create_array(size_t size) {
}
void detach_pointer(long **ptr) {
cout << "entering detach_pointer";
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";
cout << "entering test_chain" << endl;
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
Node *node_chain = create_chain(depth);
@ -80,14 +80,14 @@ int main() {
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(58000, false);
root2 = test_chain(58000, false);
root1 = test_chain(100000, false);
//root2 = test_chain(58000, false);
gc.collect(GC::COLLECT_ALL);
auto end = std::chrono::high_resolution_clock::now();

View file

@ -0,0 +1,20 @@
<?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>

116
src/GC/tests/linkedlist.cpp Normal file
View file

@ -0,0 +1,116 @@
#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 10
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();
GC::Heap::dispose();
return 0;
}

View file

@ -9,22 +9,8 @@ struct Obj {
};
int main() {
auto heap = GC::Heap::debug_the();
std::cout << "heap:\t" << heap << std::endl;
auto obj = static_cast<Obj *>(GC::Heap::alloc(sizeof(Obj)));
std::cout << "obj: \t" << obj << std::endl;
obj->a = 3;
obj->b = 4;
obj->c = 5;
std::cout << obj->a << ", " << obj->b << ", " << obj->c << std::endl;
heap->print_contents();
//delete heap;
return 0;
}

39
src/GC/tests/pointers.cpp Normal file
View file

@ -0,0 +1,39 @@
#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;
}

43
src/GC/tests/revrange.cpp Normal file
View file

@ -0,0 +1,43 @@
#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;
}

96
src/GC/tests/wrapper.c Normal file
View file

@ -0,0 +1,96 @@
#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;
}

View file

@ -0,0 +1,53 @@
#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;
}