]> git.mdlowis.com Git - archive/atc.git/commitdiff
Started outlining the algorithm for collection
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 3 Apr 2015 16:20:23 +0000 (12:20 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 3 Apr 2015 16:20:23 +0000 (12:20 -0400)
modules/atf
source/runtime/gc.c
source/runtime/gc.h
source/runtime/heap.c
source/runtime/heap.h
source/runtime/main.c
tests/test_heap.c

index 04d4571218dbef83a4321209c21b383defb9dc16..0110dca83199322915e157fc1278ee6b58dfb4f8 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 04d4571218dbef83a4321209c21b383defb9dc16
+Subproject commit 0110dca83199322915e157fc1278ee6b58dfb4f8
index da1a32d68b940fb9535a8934f5adaec125b27bac..56f2222fe1d261b52c9725141528114888dd6f61 100644 (file)
@@ -1,23 +1,47 @@
 #include "gc.h"
 #include "heap.h"
+#include <assert.h>
+#include <stdlib.h>
 
 typedef struct {
     uint64_t objmap;
     uint8_t data[];
 } obj_t;
 
-static heap_t* heap = NULL;
+typedef struct root_t {
+    struct root_t* next;
+    void* address;
+    size_t size;
+} root_t;
 
-void gc_init(void* stack_bottom)
+static heap_t* Heap = NULL;
+static uintptr_t* Stack_Bottom = NULL;
+static root_t* Roots = NULL;
+
+void gc_init(uintptr_t* stack_bottom)
+{
+    Stack_Bottom = stack_bottom;
+    Heap = heap_create();
+}
+
+void gc_shutdown(void)
 {
-    (void)stack_bottom;
-    heap = heap_create();
+    heap_destroy(Heap);
+}
+
+void gc_add_root(void* address, size_t size)
+{
+    root_t* root = (root_t*)malloc(sizeof(root_t));
+    root->address = address;
+    root->size = size;
+    root->next = Roots;
+    Roots = root;
 }
 
 void* gc_object(uint64_t objmap, size_t num_slots)
 {
     (void)objmap;
-    return heap_allocate(heap, num_slots+1);
+    return heap_allocate(Heap, num_slots+1);
 }
 
 void* gc_allocate(size_t size)
@@ -25,15 +49,46 @@ void* gc_allocate(size_t size)
     size_t slot_sz   = sizeof(uintptr_t);
     size_t remainder = size % slot_sz;
     size_t num_slots = (size / slot_sz) + ((remainder == 0) ? 0 : (slot_sz - remainder));
-    return heap_allocate(heap, num_slots + 1);
+    return heap_allocate(Heap, num_slots + 1);
 }
 
-void gc_collect(void)
-{
+static void gc_scan_object(void* object) {
+    (void)object;
 }
 
-void gc_shutdown(void)
-{
-    heap_destroy(heap);
+static void gc_scan_region(uintptr_t* start, uintptr_t* stop) {
+    for (; start < stop; start++) {
+        obj_t* obj = (obj_t*)heap_find_and_mark(Heap, start);
+        if (NULL != obj)
+            gc_scan_object(obj);
+    }
 }
 
+static void gc_scan_stack(void) {
+    /* Setup pointers to the stack top and bottom */
+    uintptr_t* stack_bot = Stack_Bottom;
+    uintptr_t* stack_top = (uintptr_t*)&stack_top;
+    /* Make sure we swap them in the event the stack grows downward */
+    if (stack_bot > stack_top) {
+        uintptr_t* temp = stack_top;
+        stack_top = stack_bot;
+        stack_bot = temp;
+    }
+    /* Scan the stack and mark any live objects */
+    gc_scan_region(stack_bot, stack_top);
+}
+
+static void gc_scan_roots(void) {
+    root_t* root = Roots;
+    for (; root != NULL; root = root->next)
+        gc_scan_region(root->address, root->address + (root->size / sizeof(uintptr_t)));
+}
+
+void gc_collect(void)
+{
+    heap_start_collection(Heap);
+    gc_scan_stack();
+    gc_scan_roots();
+    gc_scan_object(NULL);
+    heap_finish_collection(Heap);
+}
index 1e43fefff1d21531e47df2c1818f9d1efd5bcd12..545468e1421779a128cf25629751eb33a476be12 100644 (file)
@@ -8,7 +8,11 @@
 #include <stddef.h>
 #include <stdint.h>
 
-void gc_init(void* stack_bottom);
+void gc_init(uintptr_t* stack_bottom);
+
+void gc_shutdown(void);
+
+void gc_add_root(void* address, size_t size);
 
 void* gc_object(uint64_t objmap, size_t num_slots);
 
@@ -16,6 +20,4 @@ void* gc_allocate(size_t size);
 
 void gc_collect(void);
 
-void gc_shutdown(void);
-
 #endif /* GC_H */
index 4378d7d16e1324f980010df27e33898a892e4223..038d2ad68ce69747ee935bb49396be4b4d89dd27 100644 (file)
@@ -57,3 +57,20 @@ void* heap_allocate(heap_t* heap, uintptr_t num_slots)
     return obj;
 }
 
+void heap_start_collection(heap_t* heap)
+{
+    (void)heap;
+}
+
+void heap_finish_collection(heap_t* heap)
+{
+    (void)heap;
+}
+
+void* heap_find_and_mark(heap_t* heap, uintptr_t* addr)
+{
+    (void)heap;
+    (void)addr;
+    return NULL;
+}
+
index 9c134b4ff7b792cd9ebbdb337e116bf819edffe0..361e40933a0ceb6a56703bc202edf8ec4831eb8f 100644 (file)
 #define HEAP_INDEX_OFFSET (MIN_NUM_SLOTS)
 
 #define NUM_HEAP_STACKS (MAX_NUM_SLOTS)
-/*
-
-/\__/\
-| . . |
-\____/
-
-*/
 
 typedef struct block_t {
     struct block_t* next;
@@ -38,4 +31,10 @@ void heap_destroy(heap_t* heap);
 
 void* heap_allocate(heap_t* heap, uintptr_t num_slots);
 
+void heap_start_collection(heap_t* heap);
+
+void heap_finish_collection(heap_t* heap);
+
+void* heap_find_and_mark(heap_t* heap, uintptr_t* addr);
+
 #endif /* HEAP_H */
index c232333292b70e6b37eba7a1e86a90d3c8b2a985..b16ca1d340eacce3af25c4a63042986f00af3b78 100644 (file)
@@ -2,10 +2,10 @@
 
 int main(int argc, char** argv) {
     extern int ATC_Main(int argc, char** argv);
-    int ret_code;
+    uintptr_t ret_code;
     gc_init(&ret_code);
     ret_code = ATC_Main(argc, argv);
     gc_shutdown();
-    return ret_code;
+    return (int)ret_code;
 }
 
index 347cbc5532536e97a9d32b2fee380be0a6b8b12a..73ce97caf7c4b040f3f9e15ad06484dd2a2d775e 100644 (file)
@@ -1,6 +1,7 @@
 #include "atf.h"
 #include "heap.h"
 
+#include "gc.h"
 TEST_SUITE(Heap) {
     /* Verify: heap_create
      *************************************************************************/
@@ -10,6 +11,7 @@ TEST_SUITE(Heap) {
         CHECK(NULL != heap_allocate(heap, 64));
         CHECK(NULL != heap_allocate(heap, 65));
         heap_destroy(heap);
+        gc_collect();
     }
 
     /* Verify: heap_alloc