From 109c1de015eeee385020233e2f8cc6f921149103 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 31 Oct 2014 21:30:46 +0000 Subject: py: Make gc.enable/disable just control auto-GC; alloc is still allowed. gc.enable/disable are now the same as CPython: they just control whether automatic garbage collection is enabled or not. If disabled, you can still allocate heap memory, and initiate a manual collection. --- py/gc.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'py/gc.c') diff --git a/py/gc.c b/py/gc.c index ce2fa3a48..fca5050e7 100644 --- a/py/gc.c +++ b/py/gc.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "mpconfig.h" #include "misc.h" @@ -68,7 +69,8 @@ STATIC mp_uint_t *gc_pool_end; STATIC int gc_stack_overflow; STATIC mp_uint_t gc_stack[STACK_SIZE]; STATIC mp_uint_t *gc_sp; -STATIC mp_uint_t gc_lock_depth; +STATIC uint16_t gc_lock_depth; +uint16_t gc_auto_collect_enabled; STATIC mp_uint_t gc_last_free_atb_index; // ATB = allocation table byte @@ -163,6 +165,9 @@ void gc_init(void *start, void *end) { // unlock the GC gc_lock_depth = 0; + // allow auto collection + gc_auto_collect_enabled = 1; + DEBUG_printf("GC layout:\n"); DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", gc_alloc_table_start, gc_alloc_table_byte_len, gc_alloc_table_byte_len * BLOCKS_PER_ATB); #if MICROPY_ENABLE_FINALISER @@ -375,7 +380,7 @@ void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) { mp_uint_t end_block; mp_uint_t start_block; mp_uint_t n_free = 0; - int collected = 0; + int collected = !gc_auto_collect_enabled; for (;;) { // look for a run of n_blocks available blocks -- cgit v1.2.3