This commit is contained in:
Victor Olin 2023-03-21 17:35:33 +01:00
parent 7105c570d9
commit 87dc0fef2d
6 changed files with 135 additions and 5 deletions

View file

@ -15,7 +15,10 @@
namespace GC
{
/**
* Flags for the collect overlead for conditional
* collection (mark/sweep/free/all).
*/
enum CollectOption {
MARK=0x1,
SWEEP=0x2,
@ -24,9 +27,16 @@ namespace GC
COLLECT_ALL=0x7
};
/**
* The heap class to represent the heap for the
* garbage collection. The heap is a singleton
* instance and can be retrieved by Heap::the()
* inside the heap class. The heap is represented
* by a char array of size 65536 and can enable
* a profiler to track the actions on the heap.
*/
class Heap
{
private:
Heap() : m_heap(static_cast<char *>(malloc(HEAP_SIZE))) {}
@ -35,6 +45,14 @@ namespace GC
std::free((char *)m_heap);
}
/**
* If m_instance is a nullptr (the singleton has not
* been initialized yet) initialize the singleton
* and return the pointer. Otherwise return the
* previously initialized pointer.
*
* @returns The pointer to the heap singleton.
*/
static Heap *the()
{
if (m_instance) // if m_instance is not a nullptr
@ -43,6 +61,16 @@ namespace GC
return m_instance;
}
/**
* Advances an iterator and returns an element
* at position `n`.
*
* @param list The list to retrieve an element from.
*
* @param n The position to retrieve an element at.
*
* @returns The pointer to the chunk at position n in list.
*/
static Chunk *get_at(std::vector<Chunk *> &list, size_t n)
{
auto iter = list.begin();
@ -52,6 +80,13 @@ namespace GC
return *iter;
}
/**
* Returns a bool whether the profiler is enabled
* or not.
*
* @returns True or false if the profiler is enabled
* or disabled respectively.
*/
inline bool profiler_enabled() {
auto heap = Heap::the();
return heap->m_profiler_enable;
@ -60,7 +95,6 @@ namespace GC
char *const m_heap;
size_t m_size {0};
inline static Heap *m_instance {nullptr};
// size_t m_allocated_size {0};
uintptr_t *m_stack_top {nullptr};
bool m_profiler_enable {false};
@ -85,6 +119,7 @@ namespace GC
* that the address of the topmost stack frame is
* saved as the limit for scanning the stack in collect.
*/
static void init();
static void dispose();
static void *alloc(size_t size);