From 755a55f5070c442a7e2bacf976a73359d1da68a2 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Thu, 5 Jun 2014 22:48:02 +0300
Subject: [PATCH] modgc: Implement return value for gc.collect(), enable on
 Unix.

---
 py/gc.c             | 10 ++++++++++
 py/modgc.c          |  6 ++++++
 py/mpconfig.h       |  5 +++++
 unix/mpconfigport.h |  1 +
 4 files changed, 22 insertions(+)

diff --git a/py/gc.c b/py/gc.c
index 87c458f82..7fab0409a 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -231,7 +231,14 @@ STATIC void gc_deal_with_stack_overflow(void) {
     }
 }
 
+#if MICROPY_PY_GC_COLLECT_RETVAL
+uint gc_collected;
+#endif
+
 STATIC void gc_sweep(void) {
+    #if MICROPY_PY_GC_COLLECT_RETVAL
+    gc_collected = 0;
+    #endif
     // free unmarked heads and their tails
     int free_tail = 0;
     for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
@@ -254,6 +261,9 @@ STATIC void gc_sweep(void) {
                 }
 #endif
                 free_tail = 1;
+                #if MICROPY_PY_GC_COLLECT_RETVAL
+                gc_collected++;
+                #endif
                 // fall through to free the head
 
             case AT_TAIL:
diff --git a/py/modgc.c b/py/modgc.c
index 03b520b94..03a41358c 100644
--- a/py/modgc.c
+++ b/py/modgc.c
@@ -37,9 +37,15 @@
 
 #if MICROPY_PY_GC && MICROPY_ENABLE_GC
 
+extern uint gc_collected;
+
 STATIC mp_obj_t py_gc_collect(void) {
     gc_collect();
+#if MICROPY_PY_GC_COLLECT_RETVAL
+    return MP_OBJ_NEW_SMALL_INT(gc_collected);
+#else
     return mp_const_none;
+#endif
 }
 MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
 
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 77b76bf13..93e98c25b 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -279,6 +279,11 @@ typedef double mp_float_t;
 #define MICROPY_PY_GC (1)
 #endif
 
+// Whether to return number of collected objects from gc.collect()
+#ifndef MICROPY_PY_GC_COLLECT_RETVAL
+#define MICROPY_PY_GC_COLLECT_RETVAL (0)
+#endif
+
 // Whether to provide "io" module
 #ifndef MICROPY_PY_IO
 #define MICROPY_PY_IO (1)
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 0c12101fd..ace7a419b 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -46,6 +46,7 @@
 #define MICROPY_PY_SYS_STDFILES     (1)
 #define MICROPY_PY_CMATH            (1)
 #define MICROPY_PY_IO_FILEIO        (1)
+#define MICROPY_PY_GC_COLLECT_RETVAL (1)
 // Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
 // names in exception messages (may require more RAM).
 #define MICROPY_ERROR_REPORTING     (MICROPY_ERROR_REPORTING_DETAILED)
-- 
GitLab