张皓晟 3 месяцев назад
Родитель
Сommit
61300ce313
4 измененных файлов с 9 добавлено и 14 удалено
  1. 1 1
      include/pocketpy/interpreter/heap.h
  2. 6 7
      src/interpreter/heap.c
  3. 1 1
      src/interpreter/vm.c
  4. 1 5
      src/modules/pkpy.c

+ 1 - 1
include/pocketpy/interpreter/heap.h

@@ -13,7 +13,7 @@ typedef struct ManagedHeap {
     int gc_threshold;  // threshold for gc_counter
     int gc_counter;    // objects created since last gc
     bool gc_enabled;
-    py_Ref debug_callback;
+    py_TValue debug_callback;
 } ManagedHeap;
 
 typedef struct {

+ 6 - 7
src/interpreter/heap.c

@@ -17,7 +17,7 @@ void ManagedHeap__ctor(ManagedHeap* self) {
     self->gc_threshold = PK_GC_MIN_THRESHOLD;
     self->gc_counter = 0;
     self->gc_enabled = true;
-    self->debug_callback = NULL;
+    self->debug_callback = *py_None();
 }
 
 void ManagedHeap__dtor(ManagedHeap* self) {
@@ -34,7 +34,6 @@ void ManagedHeap__dtor(ManagedHeap* self) {
 }
 
 static void ManagedHeap__fire_debug_callback(ManagedHeap* self, ManagedHeapSwpetInfo* out_info) {
-    assert(self->debug_callback != NULL);
     assert(out_info != NULL);
 
     c11_sbuf buf;
@@ -84,7 +83,7 @@ static void ManagedHeap__fire_debug_callback(ManagedHeap* self, ManagedHeapSwpet
     pk_sprintf(&buf, "auto_thres.free_ratio:    %f\n", out_info->auto_thres.free_ratio);
     c11_sbuf__write_cstr(&buf, DIVIDER);
 
-    py_push(self->debug_callback);
+    py_push(&self->debug_callback);
     py_pushnil();
     py_StackRef arg = py_pushtmp();
     c11_sbuf__py_submit(&buf, arg);
@@ -101,7 +100,7 @@ void ManagedHeap__collect_if_needed(ManagedHeap* self) {
     self->gc_counter = 0;
 
     ManagedHeapSwpetInfo* out_info = NULL;
-    if(self->debug_callback) out_info = ManagedHeapSwpetInfo__new();
+    if(!py_isnone(&self->debug_callback)) out_info = ManagedHeapSwpetInfo__new();
     
     ManagedHeap__mark(self);
     if(out_info) out_info->mark_end = clock();
@@ -127,7 +126,7 @@ void ManagedHeap__collect_if_needed(ManagedHeap* self) {
     }
     self->gc_threshold = c11__min(c11__max(new_threshold, lower), upper);
 
-    if(self->debug_callback) {
+    if(!py_isnone(&self->debug_callback)) {
         ManagedHeap__fire_debug_callback(self, out_info);
         ManagedHeapSwpetInfo__delete(out_info);
     }
@@ -137,7 +136,7 @@ int ManagedHeap__collect(ManagedHeap* self) {
     self->gc_counter = 0;
 
     ManagedHeapSwpetInfo* out_info = NULL;
-    if(self->debug_callback) out_info = ManagedHeapSwpetInfo__new();
+    if(!py_isnone(&self->debug_callback)) out_info = ManagedHeapSwpetInfo__new();
     
     ManagedHeap__mark(self);
     if(out_info) out_info->mark_end = clock();
@@ -149,7 +148,7 @@ int ManagedHeap__collect(ManagedHeap* self) {
         out_info->auto_thres.after = self->gc_threshold;
     }
 
-    if(self->debug_callback) {
+    if(!py_isnone(&self->debug_callback)) {
         ManagedHeap__fire_debug_callback(self, out_info);
         ManagedHeapSwpetInfo__delete(out_info);
     }

+ 1 - 1
src/interpreter/vm.c

@@ -686,7 +686,7 @@ void ManagedHeap__mark(ManagedHeap* self) {
         pk__mark_value(&vm->reg[i]);
     }
     // mark gc debug callback
-    if(vm->heap.debug_callback) pk__mark_value(vm->heap.debug_callback);
+    pk__mark_value(&vm->heap.debug_callback);
     // mark user func
     if(vm->callbacks.gc_mark) vm->callbacks.gc_mark(pk__mark_value_func, p_stack);
     /*****************************/

+ 1 - 5
src/modules/pkpy.c

@@ -60,11 +60,7 @@ static bool pkpy_memory_usage(int argc, py_Ref argv) {
 static bool pkpy_setup_gc_debug_callback(int argc, py_Ref argv) {
     PY_CHECK_ARGC(1);
     ManagedHeap* heap = &pk_current_vm->heap;
-    if(py_isnone(argv)) {
-        heap->debug_callback = NULL;
-    } else {
-        heap->debug_callback = argv;
-    }
+    heap->debug_callback = *argv;
     py_newnone(py_retval());
     return true;
 }