Testing works, stack-overflow fixed

This commit is contained in:
Victor Olin 2023-03-24 13:41:14 +01:00
parent 5e2f4d464d
commit 35b2aa4a2f
5 changed files with 34 additions and 20 deletions

View file

@ -21,7 +21,7 @@ heap:
h_test:
rm -f tests/h_test.out
# $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) tests/h_test.cpp lib/heap.cpp lib/profiler.cpp lib/event.cpp -o tests/h_test.out
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/h_test.out tests/h_test.cpp lib/gcoll.a
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -o tests/h_test.out tests/h_test.cpp lib/gcoll.a
h_test_vg: h_test
valgrind $(VGFLAGS) tests/h_test.out
@ -55,9 +55,9 @@ static_lib:
# remove old files
rm -f lib/event.o lib/profiler.o lib/heap.o lib/gcoll.a tests/extern_lib.out
# compile object files
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/event.o lib/event.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/profiler.o lib/profiler.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/heap.o lib/heap.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/event.o lib/event.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/profiler.o lib/profiler.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/heap.o lib/heap.cpp -fPIC
# create static library
ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o

View file

@ -55,7 +55,7 @@ namespace GC
std::vector<Chunk *> m_freed_chunks;
static bool profiler_enabled();
static Chunk *get_at(std::vector<Chunk *> &list, size_t n);
// static Chunk *get_at(std::vector<Chunk *> &list, size_t n);
void collect();
void sweep(Heap &heap);
Chunk *try_recycle_chunks(size_t size);

View file

@ -169,14 +169,14 @@ namespace GC
*
* @returns The pointer to the chunk at position n in list.
*/
Chunk *Heap::get_at(std::vector<Chunk *> &list, size_t n)
{
auto iter = list.begin();
if (!n)
return *iter;
std::advance(iter, n);
return *iter;
}
// Chunk *Heap::get_at(std::vector<Chunk *> &list, size_t n)
// {
// auto iter = list.begin();
// if (!n)
// return *iter;
// std::advance(iter, n);
// return *iter;
// }
/**
* Returns a bool whether the profiler is enabled

View file

@ -11,7 +11,7 @@
#include "event.hpp"
#include "profiler.hpp"
#define MAC_OS
// #define MAC_OS
namespace GC
{
@ -186,7 +186,7 @@ namespace GC
std::string folder = path.substr(0, last_slash);
#else
auto folder = std::string("/Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/tests");
return folder + "/logs";
#endif
return folder + "/logs";
}
}

View file

@ -1,11 +1,17 @@
#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";
std::vector<Node*> nodes;
if (depth > 0) {
Node *last_node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
@ -18,6 +24,7 @@ Node *create_chain(int depth) {
node->child = nodes[i];
nodes.push_back(node);
}
cout << "\nexiting create_chain" << endl;
return nodes[depth];
}
else
@ -29,21 +36,26 @@ void create_array(size_t size) {
}
void detach_pointer(long **ptr) {
cout << "entering detach_pointer";
long *dummy_ptr = nullptr;
*ptr = dummy_ptr;
cout << "\nexiting detach_pointer" << endl;
}
Node *test_chain(int depth, bool detach) {
cout << "entering test_chain";
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
Node *node_chain = create_chain(depth);
if (detach)
node_chain->child = nullptr;
return node_chain;
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;
@ -56,9 +68,11 @@ void test_some_types() {
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();
@ -70,9 +84,9 @@ int main() {
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(60000, false);
//root2 = test_chain(50000, true);
Node *root2 = static_cast<Node *>(gc.alloc(sizeof(Node)));
root1 = test_chain(58000, false);
root2 = test_chain(58000, false);
gc.collect(GC::COLLECT_ALL);
@ -80,7 +94,7 @@ int main() {
//std::cout << "Value of end: " << end.time_since_epoch().count() << std::endl;
gc.print_summary();
//gc.dispose();
gc.dispose();
std::cout
<< "Execution time: "