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.
This commit is contained in:
Damien George 2018-05-31 23:10:48 +10:00
parent 8fb95d6520
commit 522ea80f06
2 changed files with 10 additions and 0 deletions

View File

@ -362,6 +362,13 @@ void gc_collect_end(void) {
GC_EXIT(); GC_EXIT();
} }
void gc_sweep_all(void) {
GC_ENTER();
MP_STATE_MEM(gc_lock_depth)++;
MP_STATE_MEM(gc_stack_overflow) = 0;
gc_collect_end();
}
void gc_info(gc_info_t *info) { void gc_info(gc_info_t *info) {
GC_ENTER(); GC_ENTER();
info->total = MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start); info->total = MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start);

View File

@ -45,6 +45,9 @@ void gc_collect_start(void);
void gc_collect_root(void **ptrs, size_t len); void gc_collect_root(void **ptrs, size_t len);
void gc_collect_end(void); void gc_collect_end(void);
// Use this function to sweep the whole heap and run all finalisers
void gc_sweep_all(void);
void *gc_alloc(size_t n_bytes, bool has_finaliser); void *gc_alloc(size_t n_bytes, bool has_finaliser);
void gc_free(void *ptr); // does not call finaliser void gc_free(void *ptr); // does not call finaliser
size_t gc_nbytes(const void *ptr); size_t gc_nbytes(const void *ptr);