Skip to content

Commit 522ea80

Browse files
committed
py/gc: Add gc_sweep_all() function to run all remaining finalisers.
This patch adds the gc_sweep_all() function which does a garbage collection without tracing any root pointers, so frees all the memory, and most importantly runs any remaining finalisers. This helps primarily for soft reset: it will close any open files, any open sockets, and help to get the system back to a clean state upon soft reset.
1 parent 8fb95d6 commit 522ea80

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

py/gc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ void gc_collect_end(void) {
362362
GC_EXIT();
363363
}
364364

365+
void gc_sweep_all(void) {
366+
GC_ENTER();
367+
MP_STATE_MEM(gc_lock_depth)++;
368+
MP_STATE_MEM(gc_stack_overflow) = 0;
369+
gc_collect_end();
370+
}
371+
365372
void gc_info(gc_info_t *info) {
366373
GC_ENTER();
367374
info->total = MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start);

py/gc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ void gc_collect_start(void);
4545
void gc_collect_root(void **ptrs, size_t len);
4646
void gc_collect_end(void);
4747

48+
// Use this function to sweep the whole heap and run all finalisers
49+
void gc_sweep_all(void);
50+
4851
void *gc_alloc(size_t n_bytes, bool has_finaliser);
4952
void gc_free(void *ptr); // does not call finaliser
5053
size_t gc_nbytes(const void *ptr);

0 commit comments

Comments
 (0)