Skip to content
Browse files

Integrate gc in objects and state, update libtreadmill

  • Loading branch information...
1 parent 3897843 commit 10e4efac6a834b2977ac8f74f600e442c6397a0f @txus committed Feb 25, 2013
Showing with 120 additions and 63 deletions.
  1. +1 −1 deps/libtreadmill
  2. +87 −0 src/terror/gc.c
  3. +12 −0 src/terror/gc.h
  4. +15 −61 src/terror/state.c
  5. +1 −0 src/terror/state.h
  6. +2 −1 src/terror/value.c
  7. +2 −0 src/terror/value.h
2 deps/libtreadmill
@@ -1 +1 @@
-Subproject commit f11325e7913b7310e620aaf4e77048592b1bdd30
+Subproject commit 6c8686229ec688f523a0af26daa90587aedf77e1
View
87 src/terror/gc.c
@@ -0,0 +1,87 @@
+#include <terror/gc.h>
+#include <terror/state.h>
+
+VALUE Object_bp;
+VALUE Number_bp;
+VALUE String_bp;
+VALUE Vector_bp;
+VALUE Map_bp;
+VALUE Closure_bp;
+VALUE TrueObject;
+VALUE FalseObject;
+VALUE NilObject;
+
+void
+GC_release(void *value)
+{
+ Value_destroy((VALUE)value);
+}
+
+void
+GC_scan_pointers(TmHeap *heap, TmObjectHeader *object, TmCallbackFn callback)
+{
+ Value_each((VALUE)object, ^ void (VALUE key, VALUE val) {
+ callback(heap, (TmObjectHeader*)key);
+ callback(heap, (TmObjectHeader*)val);
+ });
+}
+
+#define ROOT(A) if((A)) Tm_DArray_push(rootset, (A))
+
+Tm_DArray*
+GC_rootset(TmStateHeader *state_header)
+{
+ STATE = (State*)state_header;
+
+ Tm_DArray *rootset = Tm_DArray_create(sizeof(VALUE), 20);
+
+ // Runtime values
+ ROOT(Object_bp);
+ ROOT(Number_bp);
+ ROOT(String_bp);
+ ROOT(Vector_bp);
+ ROOT(Map_bp);
+ ROOT(Closure_bp);
+ ROOT(TrueObject);
+ ROOT(FalseObject);
+ ROOT(NilObject);
+
+ // Lobby
+ ROOT(state->lobby);
+
+ {
+ // Stack
+ STACK_FOREACH(state->stack, node) {
+ ROOT(node->value);
+ }
+ }
+
+ {
+ // Frames
+ STACK_FOREACH(state->frames, node) {
+
+ CallFrame *frame = (CallFrame*)node->value;
+ ROOT(frame->self);
+
+ if(frame->parent) {
+ CallFrame *parent = frame->parent;
+ for(int i=0; i < DArray_count(parent->locals); i++) {
+ ROOT(DArray_at(parent->locals, i));
+ }
+ }
+
+ if(frame->fn->scope) {
+ CallFrame *scope = frame->fn->scope;
+ for(int i=0; i < DArray_count(scope->locals); i++) {
+ ROOT(DArray_at(scope->locals, i));
+ }
+ }
+
+ for(int i=0; i < DArray_count(frame->locals); i++) {
+ ROOT(DArray_at(frame->locals, i));
+ }
+ }
+ }
+
+ return rootset;
+}
View
12 src/terror/gc.h
@@ -0,0 +1,12 @@
+#ifndef _tvm_gc_h_
+#define _tvm_gc_h_
+
+#include <terror/value.h>
+#include <treadmill/gc.h>
+#include <treadmill/darray.h>
+
+void GC_release(void *value);
+void GC_scan_pointers(TmHeap *heap, TmObjectHeader *object, TmCallbackFn callback);
+Tm_DArray* GC_rootset(TmStateHeader *state_header);
+
+#endif
View
76 src/terror/state.c
@@ -3,6 +3,7 @@
#include <terror/vm.h>
#include <treadmill/darray.h>
#include <treadmill/gc.h>
+#include <terror/gc.h>
VALUE Object_bp;
VALUE Number_bp;
@@ -14,66 +15,6 @@ VALUE TrueObject;
VALUE FalseObject;
VALUE NilObject;
-#define ROOT(A) if((A)) Tm_DArray_push(rootset, (A))
-
-static inline Tm_DArray*
-State_rootset(TmStateHeader *state_header)
-{
- STATE = (State*)state_header;
-
- Tm_DArray *rootset = Tm_DArray_create(sizeof(VALUE), 20);
-
- // Runtime values
- ROOT(Object_bp);
- ROOT(Number_bp);
- ROOT(String_bp);
- ROOT(Vector_bp);
- ROOT(Map_bp);
- ROOT(Closure_bp);
- ROOT(TrueObject);
- ROOT(FalseObject);
- ROOT(NilObject);
-
- // Lobby
- ROOT(state->lobby);
-
- {
- // Stack
- STACK_FOREACH(state->stack, node) {
- ROOT(node->value);
- }
- }
-
- {
- // Frames
- STACK_FOREACH(state->frames, node) {
-
- CallFrame *frame = (CallFrame*)node->value;
- ROOT(frame->self);
-
- if(frame->parent) {
- CallFrame *parent = frame->parent;
- for(int i=0; i < DArray_count(parent->locals); i++) {
- ROOT(DArray_at(parent->locals, i));
- }
- }
-
- if(frame->fn->scope) {
- CallFrame *scope = frame->fn->scope;
- for(int i=0; i < DArray_count(scope->locals); i++) {
- ROOT(DArray_at(scope->locals, i));
- }
- }
-
- for(int i=0; i < DArray_count(frame->locals); i++) {
- ROOT(DArray_at(frame->locals, i));
- }
- }
- }
-
- return rootset;
-}
-
State*
State_new()
{
@@ -82,7 +23,18 @@ State_new()
state->frames = Stack_create();
state->stack = Stack_create();
state->dbg = Debugger_new();
- state->gc.rootset = State_rootset;
+
+ state->heap = TmHeap_new(
+ (TmStateHeader*)state,
+ 200, // initial size of the heap
+ 200, // growth rate in number of cells
+ 50, // scan every 200 allocations
+ sizeof(val_t),
+ GC_release,
+ GC_scan_pointers
+ );
+ state->gc.rootset = GC_rootset;
+
return state;
}
@@ -96,6 +48,8 @@ State_destroy(STATE)
Debugger_destroy(state->dbg);
+ TmHeap_destroy(state->heap);
+
TrueObject = NULL;
FalseObject = NULL;
NilObject = NULL;
View
1 src/terror/state.h
@@ -15,6 +15,7 @@ struct state_s {
Stack *stack;
VALUE lobby;
int *ret;
+ TmHeap *heap;
Debugger *dbg;
};
typedef struct state_s State;
View
3 src/terror/value.c
@@ -5,6 +5,7 @@
#include <terror/primitives.h>
#include <terror/call_frame.h>
#include <terror/vector.h>
+#include <treadmill/gc.h>
VALUE Object_bp;
VALUE Number_bp;
@@ -18,7 +19,7 @@ VALUE NilObject;
VALUE
Value_new(STATE, ValueType type)
{
- VALUE val = calloc(1, sizeof(val_t));
+ VALUE val = (VALUE)Tm_allocate(state->heap);
val->type = type;
val->table = Hashmap_create(NULL, NULL);
val->fields = DArray_create(sizeof(VALUE), 10);
View
2 src/terror/value.h
@@ -5,11 +5,13 @@
#include <terror/function.h>
#include <terror/call_frame.h>
#include <terror/hashmap.h>
+#include <treadmill/gc.h>
#include <Block.h>
struct state_s;
struct val_s {
+ TmObjectHeader gc;
ValueType type;
union {
double as_num;

0 comments on commit 10e4efa

Please sign in to comment.
Something went wrong with that request. Please try again.