From c329e09fac67dd88540bbafeb18679301155cd65 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 31 Dec 2024 12:54:43 +0800 Subject: [PATCH] allowing disable gc --- docs/modules/gc.md | 14 +++++++++++++- include/pocketpy/config.h | 8 -------- include/pocketpy/interpreter/heap.h | 2 ++ src/common/algorithm.c | 4 ++-- src/interpreter/heap.c | 3 +++ src/modules/gc.c | 30 +++++++++++++++++++++++++---- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/docs/modules/gc.md b/docs/modules/gc.md index 4e773cbb..6f70c10b 100644 --- a/docs/modules/gc.md +++ b/docs/modules/gc.md @@ -5,4 +5,16 @@ label: gc ### `gc.collect()` -Invoke the garbage collector. \ No newline at end of file +Invoke the garbage collector. + +### `gc.enable()` + +Enable automatic garbage collection. + +### `gc.disable()` + +Disable automatic garbage collection. + +### `gc.isenabled()` + +Return `True` if automatic garbage collection is enabled, `False` otherwise. \ No newline at end of file diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 29c3257d..0b71c69b 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -25,14 +25,6 @@ #define PK_FREE(ptr) free(ptr) #endif -/*************** debug settings ***************/ -// Do not edit the following settings unless you know what you are doing -#define PK_DEBUG_CEVAL_STEP 0 -#define PK_DEBUG_MEMORY_POOL 0 -#define PK_DEBUG_NO_AUTO_GC 0 -#define PK_DEBUG_GC_STATS 0 -#define PK_DEBUG_COMPILER 0 - /*************** internal settings ***************/ // This is the maximum size of the value stack in py_TValue units diff --git a/include/pocketpy/interpreter/heap.h b/include/pocketpy/interpreter/heap.h index 5fd2b59c..719e4955 100644 --- a/include/pocketpy/interpreter/heap.h +++ b/include/pocketpy/interpreter/heap.h @@ -6,6 +6,8 @@ typedef struct ManagedHeap{ int gc_threshold; int gc_counter; + bool gc_enabled; + VM* vm; void (*gc_on_delete)(VM*, PyObject*); diff --git a/src/common/algorithm.c b/src/common/algorithm.c index edbc2b6e..55a7a927 100644 --- a/src/common/algorithm.c +++ b/src/common/algorithm.c @@ -3,7 +3,7 @@ #include #include -static bool merge(char* a, +static bool _stable_sort_merge(char* a, char* a_end, char* b, char* b_end, @@ -44,7 +44,7 @@ bool c11__stable_sort(void* ptr_, for(char* a = ptr; a < ptr + (length - seg) * elem_size; a += 2 * seg * elem_size) { char *b = a + seg * elem_size, *a_end = b, *b_end = b + seg * elem_size; if(b_end > ptr + length * elem_size) b_end = ptr + length * elem_size; - bool ok = merge(a, a_end, b, b_end, tmp, elem_size, f_lt, extra); + bool ok = _stable_sort_merge(a, a_end, b, b_end, tmp, elem_size, f_lt, extra); if(!ok) { PK_FREE(tmp); return false; diff --git a/src/interpreter/heap.c b/src/interpreter/heap.c index 7c0d37d6..7b044b68 100644 --- a/src/interpreter/heap.c +++ b/src/interpreter/heap.c @@ -8,6 +8,8 @@ void ManagedHeap__ctor(ManagedHeap* self, VM* vm) { self->gc_threshold = PK_GC_MIN_THRESHOLD; self->gc_counter = 0; + self->gc_enabled = true; + self->vm = vm; self->gc_on_delete = NULL; @@ -27,6 +29,7 @@ void ManagedHeap__dtor(ManagedHeap* self) { } void ManagedHeap__collect_if_needed(ManagedHeap* self) { + if(!self->gc_enabled) return; if(self->gc_counter < self->gc_threshold) return; self->gc_counter = 0; ManagedHeap__collect(self); diff --git a/src/modules/gc.c b/src/modules/gc.c index 50527a88..0116609b 100644 --- a/src/modules/gc.c +++ b/src/modules/gc.c @@ -1,8 +1,4 @@ #include "pocketpy/pocketpy.h" - -#include "pocketpy/common/utils.h" -#include "pocketpy/objects/object.h" -#include "pocketpy/common/sstream.h" #include "pocketpy/interpreter/vm.h" static bool gc_collect(int argc, py_Ref argv){ @@ -13,8 +9,34 @@ static bool gc_collect(int argc, py_Ref argv){ return true; } +static bool gc_enable(int argc, py_Ref argv){ + PY_CHECK_ARGC(0); + ManagedHeap* heap = &pk_current_vm->heap; + heap->gc_enabled = true; + py_newnone(py_retval()); + return true; +} + +static bool gc_disable(int argc, py_Ref argv){ + PY_CHECK_ARGC(0); + ManagedHeap* heap = &pk_current_vm->heap; + heap->gc_enabled = false; + py_newnone(py_retval()); + return true; +} + +static bool gc_isenabled(int argc, py_Ref argv){ + PY_CHECK_ARGC(0); + ManagedHeap* heap = &pk_current_vm->heap; + py_newbool(py_retval(), heap->gc_enabled); + return true; +} + void pk__add_module_gc() { py_Ref mod = py_newmodule("gc"); py_bindfunc(mod, "collect", gc_collect); + py_bindfunc(mod, "enable", gc_enable); + py_bindfunc(mod, "disable", gc_disable); + py_bindfunc(mod, "isenabled", gc_isenabled); } \ No newline at end of file