diff --git a/src/GC/include/event.hpp b/src/GC/include/event.hpp new file mode 100644 index 0000000..cee4ded --- /dev/null +++ b/src/GC/include/event.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include +#include + +#include "chunk.hpp" + +using namespace std; + +namespace GC { + + enum GCEventType { + CollectStart, + MarkStart, + ChunkMarked, + ChunkSwept, + ChunkFreed + }; + + using TimeStamp = chrono::_V2::system_clock::time_point; + + class GCEvent { + private: + // make const + GCEventType m_type; + TimeStamp m_timestamp; + Chunk *m_chunk; + public: + GCEvent(GCEventType type) { + m_type = type; + m_timestamp = chrono::system_clock::now(); + } + + GCEvent(GCEventType type, Chunk *chunk) { + m_type = type; + m_timestamp = chrono::system_clock::now(); + m_chunk = chunk; + } + + GCEventType getType(); + TimeStamp getTimeStamp(); + Chunk *getChunk(); + + void printShort(); + void printFull(); + }; +} \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index bfa167d..fc6cc9c 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -35,14 +35,14 @@ namespace GC { std::free((char *)m_heap); } - static inline Heap *the() { // TODO: make private + inline static Heap *the() { // TODO: make private if (m_instance) // if m_instance is not a nullptr return m_instance; m_instance = new Heap(); return m_instance; } - static inline Chunk *getAt(std::vector list, size_t n) { + inline static Chunk *getAt(std::vector list, size_t n) { auto iter = list.begin(); if (!n) return *iter; diff --git a/src/GC/lib/event.cpp b/src/GC/lib/event.cpp new file mode 100644 index 0000000..2936a27 --- /dev/null +++ b/src/GC/lib/event.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "chunk.hpp" +#include "event.hpp" +#include "heap.hpp" + +using namespace std; + +namespace GC { + + GCEventType GCEvent::getType() { + return m_type; + } + + TimeStamp GCEvent::getTimeStamp() { + return m_timestamp; + } + + Chunk *GCEvent::getChunk() { + return m_chunk; + } + + void GCEvent::printShort() { + assert(false && "TODO: unimplemented"); + } + + void GCEvent::printFull() { + assert(false && "TODO: unimplemented"); + } +} \ No newline at end of file diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index bfd6f26..31f4b2e 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -141,12 +141,7 @@ namespace GC { // get current stack auto stack_bottom = reinterpret_cast(__builtin_frame_address(0)); - // fix this block, it's nästy - uintptr_t *stack_top; - if (heap->m_stack_top != nullptr) - stack_top = heap->m_stack_top; - else - stack_top = (uintptr_t *)0; // temporary + uintptr_t *stack_top = heap->m_stack_top != nullptr ? heap->m_stack_top : (uintptr_t *)0; auto work_list = heap->m_allocated_chunks; mark(stack_bottom, stack_top, work_list); diff --git a/src/GC/lib/profiler.cpp b/src/GC/lib/profiler.cpp new file mode 100644 index 0000000..6138a52 --- /dev/null +++ b/src/GC/lib/profiler.cpp @@ -0,0 +1,34 @@ +#include +#include + +#include "chunk.hpp" +#include "event.hpp" +#include "heap.hpp" +#include "profiler.hpp" + +namespace GC { + void Profiler::record(GCEventType type) { + auto event = new GCEvent(type); + auto profiler = Profiler::the(); + profiler->m_events.push_back(event); + } + + void Profiler::record(GCEventType type, Chunk *chunk) { + auto event = new GCEvent(type, chunk); + auto profiler = Profiler::the(); + profiler->m_events.push_back(event); + } + + void Profiler::printTrace(PrintTraceOptions opt) { + auto profiler = Profiler::the(); + auto start = profiler->m_events.begin(); + auto end = profiler->m_events.end(); + while (start != end) { + auto event = *start++; + if (opt == Short) + event->printShort(); + else + event->printFull(); + } + } +} \ No newline at end of file diff --git a/src/GC/tests/h_test.out.dSYM/Contents/Info.plist b/src/GC/tests/h_test.out.dSYM/Contents/Info.plist deleted file mode 100644 index 3db2218..0000000 --- a/src/GC/tests/h_test.out.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.h_test.out - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out b/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out deleted file mode 100644 index d3ad3e7..0000000 Binary files a/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out and /dev/null differ diff --git a/src/GC/tests/struct_test.cpp b/src/GC/tests/struct_test.cpp new file mode 100644 index 0000000..2b2b677 --- /dev/null +++ b/src/GC/tests/struct_test.cpp @@ -0,0 +1,41 @@ +#include + +#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(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(GC::Heap::alloc(sizeof(Node))); + node->value = getValue(); + return node; +} + +void insert(Node *root) { + Node *node = createNode(); + Node *curr = root; + while (curr) +} \ No newline at end of file