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: h_test:
rm -f tests/h_test.out 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) $(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 h_test_vg: h_test
valgrind $(VGFLAGS) tests/h_test.out valgrind $(VGFLAGS) tests/h_test.out
@ -55,9 +55,9 @@ static_lib:
# remove old files # remove old files
rm -f lib/event.o lib/profiler.o lib/heap.o lib/gcoll.a tests/extern_lib.out rm -f lib/event.o lib/profiler.o lib/heap.o lib/gcoll.a tests/extern_lib.out
# compile object files # compile object files
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/event.o lib/event.cpp -fPIC $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -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) -O3 -g -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/heap.o lib/heap.cpp -fPIC
# create static library # create static library
ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o 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; std::vector<Chunk *> m_freed_chunks;
static bool profiler_enabled(); 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 collect();
void sweep(Heap &heap); void sweep(Heap &heap);
Chunk *try_recycle_chunks(size_t size); 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. * @returns The pointer to the chunk at position n in list.
*/ */
Chunk *Heap::get_at(std::vector<Chunk *> &list, size_t n) // Chunk *Heap::get_at(std::vector<Chunk *> &list, size_t n)
{ // {
auto iter = list.begin(); // auto iter = list.begin();
if (!n) // if (!n)
return *iter; // return *iter;
std::advance(iter, n); // std::advance(iter, n);
return *iter; // return *iter;
} // }
/** /**
* Returns a bool whether the profiler is enabled * Returns a bool whether the profiler is enabled

View file

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

View file

@ -1,11 +1,17 @@
#include <chrono>
#include <iostream>
#include "heap.hpp" #include "heap.hpp"
using std::cout, std::endl;
struct Node { struct Node {
int id; int id;
Node *child; Node *child;
}; };
Node *create_chain(int depth) { Node *create_chain(int depth) {
cout << "entering create_chain";
std::vector<Node*> nodes; std::vector<Node*> nodes;
if (depth > 0) { if (depth > 0) {
Node *last_node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node))); Node *last_node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
@ -18,6 +24,7 @@ Node *create_chain(int depth) {
node->child = nodes[i]; node->child = nodes[i];
nodes.push_back(node); nodes.push_back(node);
} }
cout << "\nexiting create_chain" << endl;
return nodes[depth]; return nodes[depth];
} }
else else
@ -29,21 +36,26 @@ void create_array(size_t size) {
} }
void detach_pointer(long **ptr) { void detach_pointer(long **ptr) {
cout << "entering detach_pointer";
long *dummy_ptr = nullptr; long *dummy_ptr = nullptr;
*ptr = dummy_ptr; *ptr = dummy_ptr;
cout << "\nexiting detach_pointer" << endl;
} }
Node *test_chain(int depth, bool detach) { Node *test_chain(int depth, bool detach) {
cout << "entering test_chain";
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
Node *node_chain = create_chain(depth); Node *node_chain = create_chain(depth);
if (detach) if (detach)
node_chain->child = nullptr; node_chain->child = nullptr;
return node_chain;
cout << "\nexiting test_chain" << endl;
return node_chain;
} }
void test_some_types() { void test_some_types() {
cout << "entering test_some_types" << endl;
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
std::cout << "Stack start from test_some_types:\t" << stack_start << std::endl; 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))); int *i = static_cast<int *>(GC::Heap::alloc(sizeof(int)));
char *c = static_cast<char *>(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))); short *s = static_cast<short *>(GC::Heap::alloc(sizeof(short)));
cout << "exiting test_some_types" << endl;
} }
int main() { int main() {
cout << "entering main" << endl;
using namespace std::literals; using namespace std::literals;
auto start = std::chrono::high_resolution_clock::now(); 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)); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
Node *root1 = static_cast<Node *>(gc.alloc(sizeof(Node))); Node *root1 = static_cast<Node *>(gc.alloc(sizeof(Node)));
//Node *root2 = static_cast<Node *>(gc.alloc(sizeof(Node))); Node *root2 = static_cast<Node *>(gc.alloc(sizeof(Node)));
root1 = test_chain(60000, false); root1 = test_chain(58000, false);
//root2 = test_chain(50000, true); root2 = test_chain(58000, false);
gc.collect(GC::COLLECT_ALL); gc.collect(GC::COLLECT_ALL);
@ -80,7 +94,7 @@ int main() {
//std::cout << "Value of end: " << end.time_since_epoch().count() << std::endl; //std::cout << "Value of end: " << end.time_since_epoch().count() << std::endl;
gc.print_summary(); gc.print_summary();
//gc.dispose(); gc.dispose();
std::cout std::cout
<< "Execution time: " << "Execution time: "