Skip to content

Commit 755a55f

Browse files
committed
modgc: Implement return value for gc.collect(), enable on Unix.
1 parent d4c2bdd commit 755a55f

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

py/gc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,14 @@ STATIC void gc_deal_with_stack_overflow(void) {
231231
}
232232
}
233233

234+
#if MICROPY_PY_GC_COLLECT_RETVAL
235+
uint gc_collected;
236+
#endif
237+
234238
STATIC void gc_sweep(void) {
239+
#if MICROPY_PY_GC_COLLECT_RETVAL
240+
gc_collected = 0;
241+
#endif
235242
// free unmarked heads and their tails
236243
int free_tail = 0;
237244
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) {
254261
}
255262
#endif
256263
free_tail = 1;
264+
#if MICROPY_PY_GC_COLLECT_RETVAL
265+
gc_collected++;
266+
#endif
257267
// fall through to free the head
258268

259269
case AT_TAIL:

py/modgc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@
3737

3838
#if MICROPY_PY_GC && MICROPY_ENABLE_GC
3939

40+
extern uint gc_collected;
41+
4042
STATIC mp_obj_t py_gc_collect(void) {
4143
gc_collect();
44+
#if MICROPY_PY_GC_COLLECT_RETVAL
45+
return MP_OBJ_NEW_SMALL_INT(gc_collected);
46+
#else
4247
return mp_const_none;
48+
#endif
4349
}
4450
MP_DEFINE_CONST_FUN_OBJ_0(gc_collect_obj, py_gc_collect);
4551

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ typedef double mp_float_t;
279279
#define MICROPY_PY_GC (1)
280280
#endif
281281

282+
// Whether to return number of collected objects from gc.collect()
283+
#ifndef MICROPY_PY_GC_COLLECT_RETVAL
284+
#define MICROPY_PY_GC_COLLECT_RETVAL (0)
285+
#endif
286+
282287
// Whether to provide "io" module
283288
#ifndef MICROPY_PY_IO
284289
#define MICROPY_PY_IO (1)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define MICROPY_PY_SYS_STDFILES (1)
4747
#define MICROPY_PY_CMATH (1)
4848
#define MICROPY_PY_IO_FILEIO (1)
49+
#define MICROPY_PY_GC_COLLECT_RETVAL (1)
4950
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
5051
// names in exception messages (may require more RAM).
5152
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)

0 commit comments

Comments
 (0)