Fixed broken tests

This commit is contained in:
Victor Olin 2023-02-28 16:01:39 +01:00
parent 0b0344be69
commit 5e52de10bd
7 changed files with 29 additions and 98 deletions

View file

@ -78,11 +78,17 @@ namespace GC {
* that the address of the topmost stack frame is
* saved as the limit for scanning the stack in collect.
*/
static void init(); // TODO: make static
static void dispose(); // -||-
static void *alloc(size_t size); // -||-
static void init();
static void dispose();
static void *alloc(size_t size);
// DEBUG ONLY
static inline Heap *debug_the() { // TODO: make private
if (m_instance) // if m_instance is not a nullptr
return m_instance;
m_instance = new Heap();
return m_instance;
}
void collect(uint flags); // conditional collection
void check_init(); // print dummy things
void print_contents(); // print dummy things

View file

@ -9,21 +9,24 @@ struct Obj {
};
int main() {
GC::Heap *heap = GC::Heap::the2();
GC::Heap::init();
Obj *obj;
for (int i = 0; i < 4; i++) {
obj = static_cast<Obj *>(heap->alloc(sizeof(Obj)));
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;
}

View file

@ -29,8 +29,8 @@ int main() {
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 1:\t" << GC::Heap::debug_the() << std::endl; // First call which initializes the singleton instance
GC::Heap *heap = GC::Heap::debug_the(); // Second call which should return the initialized instance
std::cout << "Call 2:\t" << heap << std::endl;
std::cout << "===========================" << std::endl;
return heap;

View file

@ -1,6 +1,4 @@
#include "../include/heap.hpp"
GC::Heap *gc = GC::Heap::the();
#include "heap.hpp"
struct Node {
int id;
@ -10,12 +8,12 @@ struct Node {
Node *create_chain(int depth) {
std::vector<Node*> nodes;
if (depth > 0) {
Node *last_node = static_cast<Node *>(gc->alloc(sizeof(Node)));
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 (int i = 0; i < depth; i++) {
Node *node = static_cast<Node *>(gc->alloc(sizeof(Node)));
Node *node = static_cast<Node *>(GC::Heap::alloc(sizeof(Node)));
node->id = depth-i;
node->child = nodes[i-1];
nodes.push_back(node);
@ -30,7 +28,7 @@ Node *create_chain(int depth) {
}
void create_array(size_t size) {
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * size));
int *arr = static_cast<int *>(GC::Heap::alloc(sizeof(int) * size));
}
void detach_pointer(long **ptr) {
@ -54,19 +52,20 @@ void test_some_types() {
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->alloc(sizeof(long)));
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->alloc(sizeof(int)));
char *c = static_cast<char *>(gc->alloc(sizeof(int)));
short *s = static_cast<short *>(gc->alloc(sizeof(short)));
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)));
}
int main() {
gc->init();
GC::Heap::init();
GC::Heap *gc = GC::Heap::debug_the();
gc->check_init();
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
std::cout << "Stack start from main:\t" << stack_start << std::endl;

View file

@ -9,11 +9,11 @@ struct Obj {
};
int main() {
auto heap = GC::Heap::the2();
auto heap = GC::Heap::debug_the();
std::cout << "heap:\t" << heap << std::endl;
auto obj = static_cast<Obj *>(heap->alloc(sizeof(Obj)));
auto obj = static_cast<Obj *>(GC::Heap::alloc(sizeof(Obj)));
std::cout << "obj: \t" << obj << std::endl;