From 61300ce313359877368149227b7abd19f6c3656e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=9A=93=E6=99=9F?= <2067144018@qq.com> Date: Sun, 23 Nov 2025 16:24:10 +0800 Subject: [PATCH] [no ci] fix --- include/pocketpy/interpreter/heap.h | 2 +- src/interpreter/heap.c | 13 ++++++------- src/interpreter/vm.c | 2 +- src/modules/pkpy.c | 6 +----- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/include/pocketpy/interpreter/heap.h b/include/pocketpy/interpreter/heap.h index 60fd7c46..9b79824c 100644 --- a/include/pocketpy/interpreter/heap.h +++ b/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 { diff --git a/src/interpreter/heap.c b/src/interpreter/heap.c index 5f2beb36..3ad5e941 100644 --- a/src/interpreter/heap.c +++ b/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); } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 308d1153..ed662c0f 100644 --- a/src/interpreter/vm.c +++ b/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); /*****************************/ diff --git a/src/modules/pkpy.c b/src/modules/pkpy.c index e7a7b015..a8ad1909 100644 --- a/src/modules/pkpy.c +++ b/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; }